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.1 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);
90 * g_task_set_name (task, G_STRFUNC);
92 * switch (self->priv->state)
94 * case NOT_INITIALIZED:
95 * _foo_get_ready (self);
96 * self->priv->init_results = g_list_append (self->priv->init_results,
98 * self->priv->state = INITIALIZING;
101 * self->priv->init_results = g_list_append (self->priv->init_results,
105 * if (!self->priv->success)
106 * g_task_return_new_error (task, ...);
108 * g_task_return_boolean (task, TRUE);
109 * g_object_unref (task);
115 * foo_init_finish (GAsyncInitable *initable,
116 * GAsyncResult *result,
119 * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
121 * return g_task_propagate_boolean (G_TASK (result), error);
125 * foo_async_initable_iface_init (gpointer g_iface,
128 * GAsyncInitableIface *iface = g_iface;
130 * iface->init_async = foo_init_async;
131 * iface->init_finish = foo_init_finish;
136 static void g_async_initable_real_init_async (GAsyncInitable *initable,
138 GCancellable *cancellable,
139 GAsyncReadyCallback callback,
141 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
146 typedef GAsyncInitableIface GAsyncInitableInterface;
147 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
151 g_async_initable_default_init (GAsyncInitableInterface *iface)
153 iface->init_async = g_async_initable_real_init_async;
154 iface->init_finish = g_async_initable_real_init_finish;
158 * g_async_initable_init_async:
159 * @initable: a #GAsyncInitable.
160 * @io_priority: the [I/O priority][io-priority] of the operation
161 * @cancellable: optional #GCancellable object, %NULL to ignore.
162 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
163 * @user_data: the data to pass to callback function
165 * Starts asynchronous initialization of the object implementing the
166 * interface. This must be done before any real use of the object after
167 * initial construction. If the object also implements #GInitable you can
168 * optionally call g_initable_init() instead.
170 * This method is intended for language bindings. If writing in C,
171 * g_async_initable_new_async() should typically be used instead.
173 * When the initialization is finished, @callback will be called. You can
174 * then call g_async_initable_init_finish() to get the result of the
177 * Implementations may also support cancellation. If @cancellable is not
178 * %NULL, then initialization can be cancelled by triggering the cancellable
179 * object from another thread. If the operation was cancelled, the error
180 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
181 * the object doesn't support cancellable initialization, the error
182 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
184 * As with #GInitable, if the object is not initialized, or initialization
185 * returns with an error, then all operations on the object except
186 * g_object_ref() and g_object_unref() are considered to be invalid, and
187 * have undefined behaviour. They will often fail with g_critical() or
188 * g_warning(), but this must not be relied on.
190 * Callers should not assume that a class which implements #GAsyncInitable can
191 * be initialized multiple times; for more information, see g_initable_init().
192 * If a class explicitly supports being initialized multiple times,
193 * implementation requires yielding all subsequent calls to init_async() on the
194 * results of the first call.
196 * For classes that also support the #GInitable interface, the default
197 * implementation of this method will run the g_initable_init() function
198 * in a thread, so if you want to support asynchronous initialization via
199 * threads, just implement the #GAsyncInitable interface without overriding
200 * any interface methods.
205 g_async_initable_init_async (GAsyncInitable *initable,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
211 GAsyncInitableIface *iface;
213 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
215 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
217 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
221 * g_async_initable_init_finish:
222 * @initable: a #GAsyncInitable.
223 * @res: a #GAsyncResult.
224 * @error: a #GError location to store the error occurring, or %NULL to
227 * Finishes asynchronous initialization and returns the result.
228 * See g_async_initable_init_async().
230 * Returns: %TRUE if successful. If an error has occurred, this function
231 * will return %FALSE and set @error appropriately if present.
236 g_async_initable_init_finish (GAsyncInitable *initable,
240 GAsyncInitableIface *iface;
242 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
243 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
245 if (g_async_result_legacy_propagate_error (res, error))
248 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
250 return (* iface->init_finish) (initable, res, error);
254 async_init_thread (GTask *task,
255 gpointer source_object,
257 GCancellable *cancellable)
259 GError *error = NULL;
261 if (g_initable_init (G_INITABLE (source_object), cancellable, &error))
262 g_task_return_boolean (task, TRUE);
264 g_task_return_error (task, error);
268 g_async_initable_real_init_async (GAsyncInitable *initable,
270 GCancellable *cancellable,
271 GAsyncReadyCallback callback,
276 g_return_if_fail (G_IS_INITABLE (initable));
278 task = g_task_new (initable, cancellable, callback, user_data);
279 g_task_set_source_tag (task, g_async_initable_real_init_async);
280 g_task_set_priority (task, io_priority);
281 g_task_run_in_thread (task, async_init_thread);
282 g_object_unref (task);
286 g_async_initable_real_init_finish (GAsyncInitable *initable,
290 /* For backward compatibility we have to process GSimpleAsyncResults
291 * even though g_async_initable_real_init_async doesn't generate
294 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
295 if (G_IS_SIMPLE_ASYNC_RESULT (res))
297 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
298 if (g_simple_async_result_propagate_error (simple, error))
303 G_GNUC_END_IGNORE_DEPRECATIONS
305 g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
307 return g_task_propagate_boolean (G_TASK (res), error);
311 * g_async_initable_new_async:
312 * @object_type: a #GType supporting #GAsyncInitable.
313 * @io_priority: the [I/O priority][io-priority] of the operation
314 * @cancellable: optional #GCancellable object, %NULL to ignore.
315 * @callback: a #GAsyncReadyCallback to call when the initialization is
317 * @user_data: the data to pass to callback function
318 * @first_property_name: (nullable): the name of the first property, or %NULL if no
320 * @...: the value of the first property, followed by other property
321 * value pairs, and ended by %NULL.
323 * Helper function for constructing #GAsyncInitable object. This is
324 * similar to g_object_new() but also initializes the object asynchronously.
326 * When the initialization is finished, @callback will be called. You can
327 * then call g_async_initable_new_finish() to get the new object and check
333 g_async_initable_new_async (GType object_type,
335 GCancellable *cancellable,
336 GAsyncReadyCallback callback,
338 const gchar *first_property_name,
343 va_start (var_args, first_property_name);
344 g_async_initable_new_valist_async (object_type,
345 first_property_name, var_args,
346 io_priority, cancellable,
347 callback, user_data);
352 * g_async_initable_newv_async:
353 * @object_type: a #GType supporting #GAsyncInitable.
354 * @n_parameters: the number of parameters in @parameters
355 * @parameters: the parameters to use to construct the object
356 * @io_priority: the [I/O priority][io-priority] of the operation
357 * @cancellable: optional #GCancellable object, %NULL to ignore.
358 * @callback: a #GAsyncReadyCallback to call when the initialization is
360 * @user_data: the data to pass to callback function
362 * Helper function for constructing #GAsyncInitable object. This is
363 * similar to g_object_newv() but also initializes the object asynchronously.
365 * When the initialization is finished, @callback will be called. You can
366 * then call g_async_initable_new_finish() to get the new object and check
370 * Deprecated: 2.54: Use g_object_new_with_properties() and
371 * g_async_initable_init_async() instead. See #GParameter for more information.
373 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
375 g_async_initable_newv_async (GType object_type,
377 GParameter *parameters,
379 GCancellable *cancellable,
380 GAsyncReadyCallback callback,
385 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
387 obj = g_object_newv (object_type, n_parameters, parameters);
389 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
390 io_priority, cancellable,
391 callback, user_data);
392 g_object_unref (obj); /* Passed ownership to async call */
394 G_GNUC_END_IGNORE_DEPRECATIONS
397 * g_async_initable_new_valist_async:
398 * @object_type: a #GType supporting #GAsyncInitable.
399 * @first_property_name: the name of the first property, followed by
400 * the value, and other property value pairs, and ended by %NULL.
401 * @var_args: The var args list generated from @first_property_name.
402 * @io_priority: the [I/O priority][io-priority] of the operation
403 * @cancellable: optional #GCancellable object, %NULL to ignore.
404 * @callback: a #GAsyncReadyCallback to call when the initialization is
406 * @user_data: the data to pass to callback function
408 * Helper function for constructing #GAsyncInitable object. This is
409 * similar to g_object_new_valist() but also initializes the object
412 * When the initialization is finished, @callback will be called. You can
413 * then call g_async_initable_new_finish() to get the new object and check
419 g_async_initable_new_valist_async (GType object_type,
420 const gchar *first_property_name,
423 GCancellable *cancellable,
424 GAsyncReadyCallback callback,
429 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
431 obj = g_object_new_valist (object_type,
435 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
436 io_priority, cancellable,
437 callback, user_data);
438 g_object_unref (obj); /* Passed ownership to async call */
442 * g_async_initable_new_finish:
443 * @initable: the #GAsyncInitable from the callback
444 * @res: the #GAsyncResult from the callback
445 * @error: return location for errors, or %NULL to ignore
447 * Finishes the async construction for the various g_async_initable_new
448 * calls, returning the created object or %NULL on error.
450 * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
451 * or %NULL on error. Free with g_object_unref().
456 g_async_initable_new_finish (GAsyncInitable *initable,
460 if (g_async_initable_init_finish (initable, res, error))
461 return g_object_ref (G_OBJECT (initable));