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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
22 #include "gasyncinitable.h"
23 #include "gasyncresult.h"
24 #include "gsimpleasyncresult.h"
30 * SECTION:gasyncinitable
31 * @short_description: Asynchronously failable object initialization interface
33 * @see_also: #GInitable
35 * This is the asynchronous version of #GInitable; it behaves the same
36 * in all ways except that initialization is asynchronous. For more details
37 * see the descriptions on #GInitable.
39 * A class may implement both the #GInitable and #GAsyncInitable interfaces.
41 * Users of objects implementing this are not intended to use the interface
42 * method directly; instead it will be used automatically in various ways.
43 * For C applications you generally just call g_async_initable_new_async()
44 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
45 * g_async_initable_init_async() under the cover, calling back with %NULL and
46 * a set %GError on failure.
48 * A typical implementation might look something like this:
50 * |[<!-- language="C" -->
58 * _foo_ready_cb (Foo *self)
62 * self->priv->state = INITIALIZED;
64 * for (l = self->priv->init_results; l != NULL; l = l->next)
66 * GTask *task = l->data;
68 * if (self->priv->success)
69 * g_task_return_boolean (task, TRUE);
71 * g_task_return_new_error (task, ...);
72 * g_object_unref (task);
75 * g_list_free (self->priv->init_results);
76 * self->priv->init_results = NULL;
80 * foo_init_async (GAsyncInitable *initable,
82 * GCancellable *cancellable,
83 * GAsyncReadyCallback callback,
86 * Foo *self = FOO (initable);
89 * task = g_task_new (initable, cancellable, callback, user_data);
91 * switch (self->priv->state)
93 * case NOT_INITIALIZED:
94 * _foo_get_ready (self);
95 * self->priv->init_results = g_list_append (self->priv->init_results,
97 * self->priv->state = INITIALIZING;
100 * self->priv->init_results = g_list_append (self->priv->init_results,
104 * if (!self->priv->success)
105 * g_task_return_new_error (task, ...);
107 * g_task_return_boolean (task, TRUE);
108 * g_object_unref (task);
114 * foo_init_finish (GAsyncInitable *initable,
115 * GAsyncResult *result,
118 * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
120 * return g_task_propagate_boolean (G_TASK (result), error);
124 * foo_async_initable_iface_init (gpointer g_iface,
127 * GAsyncInitableIface *iface = g_iface;
129 * iface->init_async = foo_init_async;
130 * iface->init_finish = foo_init_finish;
135 static void g_async_initable_real_init_async (GAsyncInitable *initable,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
140 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
145 typedef GAsyncInitableIface GAsyncInitableInterface;
146 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
150 g_async_initable_default_init (GAsyncInitableInterface *iface)
152 iface->init_async = g_async_initable_real_init_async;
153 iface->init_finish = g_async_initable_real_init_finish;
157 * g_async_initable_init_async:
158 * @initable: a #GAsyncInitable.
159 * @io_priority: the [I/O priority][io-priority] of the operation
160 * @cancellable: optional #GCancellable object, %NULL to ignore.
161 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
162 * @user_data: the data to pass to callback function
164 * Starts asynchronous initialization of the object implementing the
165 * interface. This must be done before any real use of the object after
166 * initial construction. If the object also implements #GInitable you can
167 * optionally call g_initable_init() instead.
169 * When the initialization is finished, @callback will be called. You can
170 * then call g_async_initable_init_finish() to get the result of the
173 * Implementations may also support cancellation. If @cancellable is not
174 * %NULL, then initialization can be cancelled by triggering the cancellable
175 * object from another thread. If the operation was cancelled, the error
176 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
177 * the object doesn't support cancellable initialization, the error
178 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
180 * As with #GInitable, if the object is not initialized, or initialization
181 * returns with an error, then all operations on the object except
182 * g_object_ref() and g_object_unref() are considered to be invalid, and
183 * have undefined behaviour. They will often fail with g_critical() or
184 * g_warning(), but this must not be relied on.
186 * Implementations of this method must be idempotent: i.e. multiple calls
187 * to this function with the same argument should return the same results.
188 * Only the first call initializes the object; further calls return the result
189 * of the first call. This is so that it's safe to implement the singleton
190 * pattern in the GObject constructor function.
192 * For classes that also support the #GInitable interface, the default
193 * implementation of this method will run the g_initable_init() function
194 * in a thread, so if you want to support asynchronous initialization via
195 * threads, just implement the #GAsyncInitable interface without overriding
196 * any interface methods.
201 g_async_initable_init_async (GAsyncInitable *initable,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
207 GAsyncInitableIface *iface;
209 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
211 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
213 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
217 * g_async_initable_init_finish:
218 * @initable: a #GAsyncInitable.
219 * @res: a #GAsyncResult.
220 * @error: a #GError location to store the error occurring, or %NULL to
223 * Finishes asynchronous initialization and returns the result.
224 * See g_async_initable_init_async().
226 * Returns: %TRUE if successful. If an error has occurred, this function
227 * will return %FALSE and set @error appropriately if present.
232 g_async_initable_init_finish (GAsyncInitable *initable,
236 GAsyncInitableIface *iface;
238 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
239 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
241 if (g_async_result_legacy_propagate_error (res, error))
244 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
246 return (* iface->init_finish) (initable, res, error);
250 async_init_thread (GTask *task,
251 gpointer source_object,
253 GCancellable *cancellable)
255 GError *error = NULL;
257 if (g_initable_init (G_INITABLE (source_object), cancellable, &error))
258 g_task_return_boolean (task, TRUE);
260 g_task_return_error (task, error);
264 g_async_initable_real_init_async (GAsyncInitable *initable,
266 GCancellable *cancellable,
267 GAsyncReadyCallback callback,
272 g_return_if_fail (G_IS_INITABLE (initable));
274 task = g_task_new (initable, cancellable, callback, user_data);
275 g_task_set_priority (task, io_priority);
276 g_task_run_in_thread (task, async_init_thread);
277 g_object_unref (task);
281 g_async_initable_real_init_finish (GAsyncInitable *initable,
285 /* For backward compatibility we have to process GSimpleAsyncResults
286 * even though g_async_initable_real_init_async doesn't generate
289 if (G_IS_SIMPLE_ASYNC_RESULT (res))
291 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
292 if (g_simple_async_result_propagate_error (simple, error))
298 g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
300 return g_task_propagate_boolean (G_TASK (res), error);
304 * g_async_initable_new_async:
305 * @object_type: a #GType supporting #GAsyncInitable.
306 * @io_priority: the [I/O priority][io-priority] of the operation
307 * @cancellable: optional #GCancellable object, %NULL to ignore.
308 * @callback: a #GAsyncReadyCallback to call when the initialization is
310 * @user_data: the data to pass to callback function
311 * @first_property_name: (allow-none): the name of the first property, or %NULL if no
313 * @...: the value of the first property, followed by other property
314 * value pairs, and ended by %NULL.
316 * Helper function for constructing #GAsyncInitable object. This is
317 * similar to g_object_new() but also initializes the object asynchronously.
319 * When the initialization is finished, @callback will be called. You can
320 * then call g_async_initable_new_finish() to get the new object and check
326 g_async_initable_new_async (GType object_type,
328 GCancellable *cancellable,
329 GAsyncReadyCallback callback,
331 const gchar *first_property_name,
336 va_start (var_args, first_property_name);
337 g_async_initable_new_valist_async (object_type,
338 first_property_name, var_args,
339 io_priority, cancellable,
340 callback, user_data);
345 * g_async_initable_newv_async:
346 * @object_type: a #GType supporting #GAsyncInitable.
347 * @n_parameters: the number of parameters in @parameters
348 * @parameters: the parameters to use to construct the object
349 * @io_priority: the [I/O priority][io-priority] of the operation
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 #GAsyncInitable 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 [I/O priority][io-priority] of the operation
391 * @cancellable: optional #GCancellable object, %NULL to ignore.
392 * @callback: a #GAsyncReadyCallback to call when the initialization is
394 * @user_data: the data to pass to callback function
396 * Helper function for constructing #GAsyncInitable object. This is
397 * similar to g_object_new_valist() but also initializes the object
400 * When the initialization is finished, @callback will be called. You can
401 * then call g_async_initable_new_finish() to get the new object and check
407 g_async_initable_new_valist_async (GType object_type,
408 const gchar *first_property_name,
411 GCancellable *cancellable,
412 GAsyncReadyCallback callback,
417 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
419 obj = g_object_new_valist (object_type,
423 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
424 io_priority, cancellable,
425 callback, user_data);
426 g_object_unref (obj); /* Passed ownership to async call */
430 * g_async_initable_new_finish:
431 * @initable: the #GAsyncInitable from the callback
432 * @res: the #GAsyncResult from the callback
433 * @error: return location for errors, or %NULL to ignore
435 * Finishes the async construction for the various g_async_initable_new
436 * calls, returning the created object or %NULL on error.
438 * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
439 * or %NULL on error. Free with g_object_unref().
444 g_async_initable_new_finish (GAsyncInitable *initable,
448 if (g_async_initable_init_finish (initable, res, error))
449 return g_object_ref (initable);