1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
31 * SECTION:gasyncinitable
32 * @short_description: Asynchronously failable object initialization interface
34 * @see_also: #GInitable
36 * This is the asynchronous version of #GInitable; it behaves the same
37 * in all ways except that initialization is asynchronous. For more details
38 * see the descriptions on #GInitable.
40 * A class may implement both the #GInitable and #GAsyncInitable interfaces.
42 * Users of objects implementing this are not intended to use the interface
43 * method directly; instead it will be used automatically in various ways.
44 * For C applications you generally just call g_async_initable_new_async()
45 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
46 * g_async_initable_init_async() under the cover, calling back with %NULL and
47 * a set %GError on failure.
49 * A typical implementation might look something like this:
59 * _foo_ready_cb (Foo *self)
63 * self->priv->state = INITIALIZED;
65 * for (l = self->priv->init_results; l != NULL; l = l->next)
67 * GSimpleAsyncResult *simple = l->data;
69 * if (!self->priv->success)
70 * g_simple_async_result_set_error (simple, ...);
72 * g_simple_async_result_complete (simple);
73 * g_object_unref (simple);
76 * g_list_free (self->priv->init_results);
77 * self->priv->init_results = NULL;
81 * foo_init_async (GAsyncInitable *initable,
83 * GCancellable *cancellable,
84 * GAsyncReadyCallback callback,
87 * Foo *self = FOO (initable);
88 * GSimpleAsyncResult *simple;
90 * simple = g_simple_async_result_new (G_OBJECT (initable)
95 * switch (self->priv->state)
97 * case NOT_INITIALIZED:
98 * _foo_get_ready (self);
99 * self->priv->init_results = g_list_append (self->priv->init_results,
101 * self->priv->state = INITIALIZING;
104 * self->priv->init_results = g_list_append (self->priv->init_results,
108 * if (!self->priv->success)
109 * g_simple_async_result_set_error (simple, ...);
111 * g_simple_async_result_complete_in_idle (simple);
112 * g_object_unref (simple);
118 * foo_init_finish (GAsyncInitable *initable,
119 * GAsyncResult *result,
122 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
123 * G_OBJECT (initable), foo_init_async), FALSE);
125 * if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
133 * foo_async_initable_iface_init (gpointer g_iface,
136 * GAsyncInitableIface *iface = g_iface;
138 * iface->init_async = foo_init_async;
139 * iface->init_finish = foo_init_finish;
144 static void g_async_initable_real_init_async (GAsyncInitable
*initable
,
146 GCancellable
*cancellable
,
147 GAsyncReadyCallback callback
,
149 static gboolean
g_async_initable_real_init_finish (GAsyncInitable
*initable
,
154 typedef GAsyncInitableIface GAsyncInitableInterface
;
155 G_DEFINE_INTERFACE (GAsyncInitable
, g_async_initable
, G_TYPE_OBJECT
)
159 g_async_initable_default_init (GAsyncInitableInterface
*iface
)
161 iface
->init_async
= g_async_initable_real_init_async
;
162 iface
->init_finish
= g_async_initable_real_init_finish
;
166 * g_async_initable_init_async:
167 * @initable: a #GAsyncInitable.
168 * @io_priority: the <link linkend="io-priority">I/O priority</link>
170 * @cancellable: optional #GCancellable object, %NULL to ignore.
171 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
172 * @user_data: the data to pass to callback function
174 * Starts asynchronous initialization of the object implementing the
175 * interface. This must be done before any real use of the object after
176 * initial construction. If the object also implements #GInitable you can
177 * optionally call g_initable_init() instead.
179 * When the initialization is finished, @callback will be called. You can
180 * then call g_async_initable_init_finish() to get the result of the
183 * Implementations may also support cancellation. If @cancellable is not
184 * %NULL, then initialization can be cancelled by triggering the cancellable
185 * object from another thread. If the operation was cancelled, the error
186 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
187 * the object doesn't support cancellable initialization, the error
188 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
190 * If this function is not called, or returns with an error, then all
191 * operations on the object should fail, generally returning the
192 * error %G_IO_ERROR_NOT_INITIALIZED.
194 * Implementations of this method must be idempotent: i.e. multiple calls
195 * to this function with the same argument should return the same results.
196 * Only the first call initializes the object; further calls return the result
197 * of the first call. This is so that it's safe to implement the singleton
198 * pattern in the GObject constructor function.
200 * For classes that also support the #GInitable interface, the default
201 * implementation of this method will run the g_initable_init() function
202 * in a thread, so if you want to support asynchronous initialization via
203 * threads, just implement the #GAsyncInitable interface without overriding
204 * any interface methods.
209 g_async_initable_init_async (GAsyncInitable
*initable
,
211 GCancellable
*cancellable
,
212 GAsyncReadyCallback callback
,
215 GAsyncInitableIface
*iface
;
217 g_return_if_fail (G_IS_ASYNC_INITABLE (initable
));
219 iface
= G_ASYNC_INITABLE_GET_IFACE (initable
);
221 (* iface
->init_async
) (initable
, io_priority
, cancellable
, callback
, user_data
);
225 * g_async_initable_init_finish:
226 * @initable: a #GAsyncInitable.
227 * @res: a #GAsyncResult.
228 * @error: a #GError location to store the error occuring, or %NULL to
231 * Finishes asynchronous initialization and returns the result.
232 * See g_async_initable_init_async().
234 * Returns: %TRUE if successful. If an error has occurred, this function
235 * will return %FALSE and set @error appropriately if present.
240 g_async_initable_init_finish (GAsyncInitable
*initable
,
244 GAsyncInitableIface
*iface
;
246 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable
), FALSE
);
247 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), FALSE
);
249 if (G_IS_SIMPLE_ASYNC_RESULT (res
))
251 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (res
);
252 if (g_simple_async_result_propagate_error (simple
, error
))
256 iface
= G_ASYNC_INITABLE_GET_IFACE (initable
);
258 return (* iface
->init_finish
) (initable
, res
, error
);
262 async_init_thread (GSimpleAsyncResult
*res
,
264 GCancellable
*cancellable
)
266 GError
*error
= NULL
;
268 if (!g_initable_init (G_INITABLE (object
), cancellable
, &error
))
270 g_simple_async_result_set_from_error (res
, error
);
271 g_error_free (error
);
276 g_async_initable_real_init_async (GAsyncInitable
*initable
,
278 GCancellable
*cancellable
,
279 GAsyncReadyCallback callback
,
282 GSimpleAsyncResult
*res
;
284 g_return_if_fail (G_IS_INITABLE (initable
));
286 res
= g_simple_async_result_new (G_OBJECT (initable
), callback
, user_data
,
287 g_async_initable_real_init_async
);
288 g_simple_async_result_run_in_thread (res
, async_init_thread
,
289 io_priority
, cancellable
);
290 g_object_unref (res
);
294 g_async_initable_real_init_finish (GAsyncInitable
*initable
,
298 return TRUE
; /* Errors handled by base impl */
302 * g_async_initable_new_async:
303 * @object_type: a #GType supporting #GAsyncInitable.
304 * @io_priority: the <link linkend="io-priority">I/O priority</link>
306 * @cancellable: optional #GCancellable object, %NULL to ignore.
307 * @callback: a #GAsyncReadyCallback to call when the initialization is
309 * @user_data: the data to pass to callback function
310 * @first_property_name: the name of the first property, or %NULL if no
312 * @...: the value of the first property, followed by other property
313 * value pairs, and ended by %NULL.
315 * Helper function for constructing #GAsyncInitiable object. This is
316 * similar to g_object_new() but also initializes the object asynchronously.
318 * When the initialization is finished, @callback will be called. You can
319 * then call g_async_initable_new_finish() to get the new object and check
325 g_async_initable_new_async (GType object_type
,
327 GCancellable
*cancellable
,
328 GAsyncReadyCallback callback
,
330 const gchar
*first_property_name
,
335 va_start (var_args
, first_property_name
);
336 g_async_initable_new_valist_async (object_type
,
337 first_property_name
, var_args
,
338 io_priority
, cancellable
,
339 callback
, user_data
);
344 * g_async_initable_newv_async:
345 * @object_type: a #GType supporting #GAsyncInitable.
346 * @n_parameters: the number of parameters in @parameters
347 * @parameters: the parameters to use to construct the object
348 * @io_priority: the <link linkend="io-priority">I/O priority</link>
350 * @cancellable: optional #GCancellable object, %NULL to ignore.
351 * @callback: a #GAsyncReadyCallback to call when the initialization is
353 * @user_data: the data to pass to callback function
355 * Helper function for constructing #GAsyncInitiable object. This is
356 * similar to g_object_newv() but also initializes the object asynchronously.
358 * When the initialization is finished, @callback will be called. You can
359 * then call g_async_initable_new_finish() to get the new object and check
365 g_async_initable_newv_async (GType object_type
,
367 GParameter
*parameters
,
369 GCancellable
*cancellable
,
370 GAsyncReadyCallback callback
,
375 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type
));
377 obj
= g_object_newv (object_type
, n_parameters
, parameters
);
379 g_async_initable_init_async (G_ASYNC_INITABLE (obj
),
380 io_priority
, cancellable
,
381 callback
, user_data
);
385 * g_async_initable_new_valist_async:
386 * @object_type: a #GType supporting #GAsyncInitable.
387 * @first_property_name: the name of the first property, followed by
388 * the value, and other property value pairs, and ended by %NULL.
389 * @var_args: The var args list generated from @first_property_name.
390 * @io_priority: the <link linkend="io-priority">I/O priority</link>
392 * @cancellable: optional #GCancellable object, %NULL to ignore.
393 * @callback: a #GAsyncReadyCallback to call when the initialization is
395 * @user_data: the data to pass to callback function
397 * Helper function for constructing #GAsyncInitiable object. This is
398 * similar to g_object_new_valist() but also initializes the object
401 * When the initialization is finished, @callback will be called. You can
402 * then call g_async_initable_new_finish() to get the new object and check
408 g_async_initable_new_valist_async (GType object_type
,
409 const gchar
*first_property_name
,
412 GCancellable
*cancellable
,
413 GAsyncReadyCallback callback
,
418 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type
));
420 obj
= g_object_new_valist (object_type
,
424 g_async_initable_init_async (G_ASYNC_INITABLE (obj
),
425 io_priority
, cancellable
,
426 callback
, user_data
);
427 g_object_unref (obj
); /* Passed ownership to async call */
431 * g_async_initable_new_finish:
432 * @initable: the #GAsyncInitable from the callback
433 * @res: the #GAsyncResult.from the callback
434 * @error: a #GError location to store the error occuring, or %NULL to
437 * Finishes the async construction for the various g_async_initable_new calls,
438 * returning the created object or %NULL on error.
440 * Returns: (transfer full): a newly created #GObject, or %NULL on error. Free with
446 g_async_initable_new_finish (GAsyncInitable
*initable
,
450 if (g_async_initable_init_finish (initable
, res
, error
))
451 return g_object_ref (initable
);