1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
29 #include "gdbusprivate.h"
33 * SECTION:gasyncinitable
34 * @short_description: Asynchronously failable object initialization interface
36 * @see_also: #GInitable
38 * This is the asynchronous version of #GInitable; it behaves the same
39 * in all ways except that initialization is asynchronous. For more details
40 * see the descriptions on #GInitable.
42 * A class may implement both the #GInitable and #GAsyncInitable interfaces.
44 * Users of objects implementing this are not intended to use the interface
45 * method directly; instead it will be used automatically in various ways.
46 * For C applications you generally just call g_async_initable_new_async()
47 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
48 * g_async_initable_init_async() under the cover, calling back with %NULL and
49 * a set %GError on failure.
51 * A typical implementation might look something like this:
53 * |[<!-- language="C" -->
61 * _foo_ready_cb (Foo *self)
65 * self->priv->state = INITIALIZED;
67 * for (l = self->priv->init_results; l != NULL; l = l->next)
69 * GTask *task = l->data;
71 * if (self->priv->success)
72 * g_task_return_boolean (task, TRUE);
74 * g_task_return_new_error (task, ...);
75 * g_object_unref (task);
78 * g_list_free (self->priv->init_results);
79 * self->priv->init_results = NULL;
83 * foo_init_async (GAsyncInitable *initable,
85 * GCancellable *cancellable,
86 * GAsyncReadyCallback callback,
89 * Foo *self = FOO (initable);
92 * task = g_task_new (initable, cancellable, callback, user_data);
93 * g_task_set_name (task, G_STRFUNC);
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_task_return_new_error (task, ...);
111 * g_task_return_boolean (task, TRUE);
112 * g_object_unref (task);
118 * foo_init_finish (GAsyncInitable *initable,
119 * GAsyncResult *result,
122 * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
124 * return g_task_propagate_boolean (G_TASK (result), error);
128 * foo_async_initable_iface_init (gpointer g_iface,
131 * GAsyncInitableIface *iface = g_iface;
133 * iface->init_async = foo_init_async;
134 * iface->init_finish = foo_init_finish;
139 static void g_async_initable_real_init_async (GAsyncInitable *initable,
141 GCancellable *cancellable,
142 GAsyncReadyCallback callback,
144 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
149 typedef GAsyncInitableIface GAsyncInitableInterface;
150 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
154 g_async_initable_default_init (GAsyncInitableInterface *iface)
156 iface->init_async = g_async_initable_real_init_async;
157 iface->init_finish = g_async_initable_real_init_finish;
161 * g_async_initable_init_async:
162 * @initable: a #GAsyncInitable.
163 * @io_priority: the [I/O priority][io-priority] of the operation
164 * @cancellable: optional #GCancellable object, %NULL to ignore.
165 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
166 * @user_data: the data to pass to callback function
168 * Starts asynchronous initialization of the object implementing the
169 * interface. This must be done before any real use of the object after
170 * initial construction. If the object also implements #GInitable you can
171 * optionally call g_initable_init() instead.
173 * This method is intended for language bindings. If writing in C,
174 * g_async_initable_new_async() should typically be used instead.
176 * When the initialization is finished, @callback will be called. You can
177 * then call g_async_initable_init_finish() to get the result of the
180 * Implementations may also support cancellation. If @cancellable is not
181 * %NULL, then initialization can be cancelled by triggering the cancellable
182 * object from another thread. If the operation was cancelled, the error
183 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
184 * the object doesn't support cancellable initialization, the error
185 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
187 * As with #GInitable, if the object is not initialized, or initialization
188 * returns with an error, then all operations on the object except
189 * g_object_ref() and g_object_unref() are considered to be invalid, and
190 * have undefined behaviour. They will often fail with g_critical() or
191 * g_warning(), but this must not be relied on.
193 * Callers should not assume that a class which implements #GAsyncInitable can
194 * be initialized multiple times; for more information, see g_initable_init().
195 * If a class explicitly supports being initialized multiple times,
196 * implementation requires yielding all subsequent calls to init_async() on the
197 * results of the first call.
199 * For classes that also support the #GInitable interface, the default
200 * implementation of this method will run the g_initable_init() function
201 * in a thread, so if you want to support asynchronous initialization via
202 * threads, just implement the #GAsyncInitable interface without overriding
203 * any interface methods.
208 g_async_initable_init_async (GAsyncInitable *initable,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
214 GAsyncInitableIface *iface;
216 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
218 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
220 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
224 * g_async_initable_init_finish:
225 * @initable: a #GAsyncInitable.
226 * @res: a #GAsyncResult.
227 * @error: a #GError location to store the error occurring, or %NULL to
230 * Finishes asynchronous initialization and returns the result.
231 * See g_async_initable_init_async().
233 * Returns: %TRUE if successful. If an error has occurred, this function
234 * will return %FALSE and set @error appropriately if present.
239 g_async_initable_init_finish (GAsyncInitable *initable,
243 GAsyncInitableIface *iface;
245 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
246 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
248 if (g_async_result_legacy_propagate_error (res, error))
251 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
253 return (* iface->init_finish) (initable, res, error);
257 async_init_thread (GTask *task,
258 gpointer source_object,
260 GCancellable *cancellable)
262 GError *error = NULL;
264 if (g_initable_init (G_INITABLE (source_object), cancellable, &error))
265 g_task_return_boolean (task, TRUE);
267 g_task_return_error (task, error);
271 g_async_initable_real_init_async (GAsyncInitable *initable,
273 GCancellable *cancellable,
274 GAsyncReadyCallback callback,
279 g_return_if_fail (G_IS_INITABLE (initable));
281 task = g_task_new (initable, cancellable, callback, user_data);
282 g_task_set_source_tag (task, g_async_initable_real_init_async);
283 g_task_set_priority (task, io_priority);
284 g_task_run_in_thread (task, async_init_thread);
285 g_object_unref (task);
289 g_async_initable_real_init_finish (GAsyncInitable *initable,
293 /* For backward compatibility we have to process GSimpleAsyncResults
294 * even though g_async_initable_real_init_async doesn't generate
297 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
298 if (G_IS_SIMPLE_ASYNC_RESULT (res))
300 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
301 if (g_simple_async_result_propagate_error (simple, error))
306 G_GNUC_END_IGNORE_DEPRECATIONS
308 g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
310 return g_task_propagate_boolean (G_TASK (res), error);
314 * g_async_initable_new_async:
315 * @object_type: a #GType supporting #GAsyncInitable.
316 * @io_priority: the [I/O priority][io-priority] of the operation
317 * @cancellable: optional #GCancellable object, %NULL to ignore.
318 * @callback: a #GAsyncReadyCallback to call when the initialization is
320 * @user_data: the data to pass to callback function
321 * @first_property_name: (nullable): the name of the first property, or %NULL if no
323 * @...: the value of the first property, followed by other property
324 * value pairs, and ended by %NULL.
326 * Helper function for constructing #GAsyncInitable object. This is
327 * similar to g_object_new() but also initializes the object asynchronously.
329 * When the initialization is finished, @callback will be called. You can
330 * then call g_async_initable_new_finish() to get the new object and check
336 g_async_initable_new_async (GType object_type,
338 GCancellable *cancellable,
339 GAsyncReadyCallback callback,
341 const gchar *first_property_name,
346 va_start (var_args, first_property_name);
347 g_async_initable_new_valist_async (object_type,
348 first_property_name, var_args,
349 io_priority, cancellable,
350 callback, user_data);
355 * g_async_initable_newv_async:
356 * @object_type: a #GType supporting #GAsyncInitable.
357 * @n_parameters: the number of parameters in @parameters
358 * @parameters: the parameters to use to construct the object
359 * @io_priority: the [I/O priority][io-priority] of the operation
360 * @cancellable: optional #GCancellable object, %NULL to ignore.
361 * @callback: a #GAsyncReadyCallback to call when the initialization is
363 * @user_data: the data to pass to callback function
365 * Helper function for constructing #GAsyncInitable object. This is
366 * similar to g_object_newv() but also initializes the object asynchronously.
368 * When the initialization is finished, @callback will be called. You can
369 * then call g_async_initable_new_finish() to get the new object and check
373 * Deprecated: 2.54: Use g_object_new_with_properties() and
374 * g_async_initable_init_async() instead. See #GParameter for more information.
376 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
378 g_async_initable_newv_async (GType object_type,
380 GParameter *parameters,
382 GCancellable *cancellable,
383 GAsyncReadyCallback callback,
388 _g_dbus_initialize();
390 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
392 obj = g_object_newv (object_type, n_parameters, parameters);
394 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
395 io_priority, cancellable,
396 callback, user_data);
397 g_object_unref (obj); /* Passed ownership to async call */
399 G_GNUC_END_IGNORE_DEPRECATIONS
402 * g_async_initable_new_valist_async:
403 * @object_type: a #GType supporting #GAsyncInitable.
404 * @first_property_name: the name of the first property, followed by
405 * the value, and other property value pairs, and ended by %NULL.
406 * @var_args: The var args list generated from @first_property_name.
407 * @io_priority: the [I/O priority][io-priority] of the operation
408 * @cancellable: optional #GCancellable object, %NULL to ignore.
409 * @callback: a #GAsyncReadyCallback to call when the initialization is
411 * @user_data: the data to pass to callback function
413 * Helper function for constructing #GAsyncInitable object. This is
414 * similar to g_object_new_valist() but also initializes the object
417 * When the initialization is finished, @callback will be called. You can
418 * then call g_async_initable_new_finish() to get the new object and check
424 g_async_initable_new_valist_async (GType object_type,
425 const gchar *first_property_name,
428 GCancellable *cancellable,
429 GAsyncReadyCallback callback,
434 _g_dbus_initialize();
436 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
438 obj = g_object_new_valist (object_type,
442 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
443 io_priority, cancellable,
444 callback, user_data);
445 g_object_unref (obj); /* Passed ownership to async call */
449 * g_async_initable_new_finish:
450 * @initable: the #GAsyncInitable from the callback
451 * @res: the #GAsyncResult from the callback
452 * @error: return location for errors, or %NULL to ignore
454 * Finishes the async construction for the various g_async_initable_new
455 * calls, returning the created object or %NULL on error.
457 * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
458 * or %NULL on error. Free with g_object_unref().
463 g_async_initable_new_finish (GAsyncInitable *initable,
467 if (g_async_initable_init_finish (initable, res, error))
468 return g_object_ref (G_OBJECT (initable));