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"
32 * SECTION:gasyncinitable
33 * @short_description: Asynchronously failable object initialization interface
35 * @see_also: #GInitable
37 * This is the asynchronous version of #GInitable, it behaves the same
38 * in all ways except that initialization is asynchronous. For more details
39 * see the descriptions on #GInitable.
41 * A class may implement both the #GInitable and #GAsyncInitable interfaces.
43 * Users of objects implementing this are not intended to use the interface
44 * method directly, instead it will be used automatically in various ways.
45 * For C applications you generally just call g_async_initable_new() directly,
46 * or indirectly via a foo_thing_new_async() wrapper. This will call
47 * g_async_initable_init() under the cover, calling back with %NULL and
48 * a set %GError on failure.
51 static void g_async_initable_base_init (gpointer g_iface);
52 static void g_async_initable_real_init_async (GAsyncInitable *initable,
54 GCancellable *cancellable,
55 GAsyncReadyCallback callback,
57 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
62 g_async_initable_get_type (void)
64 static volatile gsize g_define_type_id__volatile = 0;
66 if (g_once_init_enter (&g_define_type_id__volatile))
68 const GTypeInfo initable_info =
70 sizeof (GAsyncInitableIface), /* class_size */
71 g_async_initable_base_init, /* base_init */
72 NULL, /* base_finalize */
74 NULL, /* class_finalize */
75 NULL, /* class_data */
80 GType g_define_type_id =
81 g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncInitable"),
84 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
86 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
89 return g_define_type_id__volatile;
93 g_async_initable_base_init (gpointer g_iface)
95 GAsyncInitableIface *iface = g_iface;
97 iface->init_async = g_async_initable_real_init_async;
98 iface->init_finish = g_async_initable_real_init_finish;
102 * g_async_initable_init_async:
103 * @initable: a #GAsyncInitable.
104 * @io_priority: the <link linkend="io-priority">I/O priority</link>
106 * @cancellable: optional #GCancellable object, %NULL to ignore.
107 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
108 * @user_data: the data to pass to callback function
110 * Starts asynchronous initialization of the object implementing the
111 * interface. This must be done before any real use of the object after
112 * initial construction. If the object also implements #GInitable you can
113 * optionally call g_initable_init() instead.
115 * When the initialization is finished, @callback will be called. You can
116 * then call g_async_initable_init_finish() to get the result of the
119 * Implementations may also support cancellation. If @cancellable is not
120 * %NULL, then initialization can be cancelled by triggering the cancellable
121 * object from another thread. If the operation was cancelled, the error
122 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
123 * the object doesn't support cancellable initialization the error
124 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
126 * If this function is not called, or returns with an error then all
127 * operations on the object should fail, generally returning the
128 * error %G_IO_ERROR_NOT_INITIALIZED.
130 * Implementations of this method must be idempotent, i.e. multiple calls
131 * to this function with the same argument should return the same results.
132 * Only the first call initializes the object, further calls return the result
133 * of the first call. This is so that its safe to implement the singleton
134 * pattern in the GObject constructor function.
136 * For classes that also support the #GInitable interface the default
137 * implementation of this method will run the g_initable_init() function
138 * in a thread, so if you want to support asynchronous initialization via
139 * threads, just implement the #GAsyncInitable interface without overriding
140 * any interface methods.
145 g_async_initable_init_async (GAsyncInitable *initable,
147 GCancellable *cancellable,
148 GAsyncReadyCallback callback,
151 GAsyncInitableIface *iface;
153 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
155 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
157 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
161 * g_async_initable_init_finish:
162 * @initable: a #GAsyncInitable.
163 * @res: a #GAsyncResult.
164 * @error: a #GError location to store the error occuring, or %NULL to
167 * Finishes asynchronous initialization and returns the result.
168 * See g_async_initable_init_async().
170 * Returns: %TRUE if successful. If an error has occurred, this function
171 * will return %FALSE and set @error appropriately if present.
176 g_async_initable_init_finish (GAsyncInitable *initable,
180 GAsyncInitableIface *iface;
182 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
183 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
185 if (G_IS_SIMPLE_ASYNC_RESULT (res))
187 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
188 if (g_simple_async_result_propagate_error (simple, error))
192 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
194 return (* iface->init_finish) (initable, res, error);
198 async_init_thread (GSimpleAsyncResult *res,
200 GCancellable *cancellable)
202 GError *error = NULL;
204 if (!g_initable_init (G_INITABLE (res), cancellable, &error))
206 g_simple_async_result_set_from_error (res, error);
207 g_error_free (error);
212 g_async_initable_real_init_async (GAsyncInitable *initable,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
218 GSimpleAsyncResult *res;
220 g_return_if_fail (G_IS_INITABLE (initable));
222 res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
223 g_async_initable_real_init_async);
224 g_simple_async_result_run_in_thread (res, async_init_thread,
225 io_priority, cancellable);
226 g_object_unref (res);
230 g_async_initable_real_init_finish (GAsyncInitable *initable,
234 return TRUE; /* Errors handled by base impl */
238 * g_async_initable_new_async:
239 * @object_type: a #GType supporting #GAsyncInitable.
240 * @io_priority: the <link linkend="io-priority">I/O priority</link>
242 * @cancellable: optional #GCancellable object, %NULL to ignore.
243 * @callback: a #GAsyncReadyCallback to call when the initialization is
245 * @user_data: the data to pass to callback function
246 * @first_property_name: the name of the first property, or %NULL if no
248 * @...: the value if the first property, followed by and other property
249 * value pairs, and ended by %NULL.
251 * Helper function for constructing #GAsyncInitiable object. This is
252 * similar to g_object_new() but also initializes the object asyncronously.
254 * When the initialization is finished, @callback will be called. You can
255 * then call g_async_initable_new_finish() to get new object and check for
261 g_async_initable_new_async (GType object_type,
263 GCancellable *cancellable,
264 GAsyncReadyCallback callback,
266 const gchar *first_property_name,
271 va_start (var_args, first_property_name);
272 g_async_initable_new_valist_async (object_type,
273 first_property_name, var_args,
274 io_priority, cancellable,
275 callback, user_data);
280 * g_async_initable_newv_async:
281 * @object_type: a #GType supporting #GAsyncInitable.
282 * @n_parameters: the number of parameters in @parameters
283 * @parameters: the parameters to use to construct the object
284 * @io_priority: the <link linkend="io-priority">I/O priority</link>
286 * @cancellable: optional #GCancellable object, %NULL to ignore.
287 * @callback: a #GAsyncReadyCallback to call when the initialization is
289 * @user_data: the data to pass to callback function
291 * Helper function for constructing #GAsyncInitiable object. This is
292 * similar to g_object_newv() but also initializes the object asyncronously.
294 * When the initialization is finished, @callback will be called. You can
295 * then call g_async_initable_new_finish() to get new object and check for
301 g_async_initable_newv_async (GType object_type,
303 GParameter *parameters,
305 GCancellable *cancellable,
306 GAsyncReadyCallback callback,
311 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
313 obj = g_object_newv (object_type, n_parameters, parameters);
315 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
316 io_priority, cancellable,
317 callback, user_data);
321 * g_async_initable_new_valist_async:
322 * @object_type: a #GType supporting #GAsyncInitable.
323 * @first_property_name: the name of the first property, followed by
324 * the value, and other property value pairs, and ended by %NULL.
325 * @var_args: The var args list generated from @first_property_name.
326 * @io_priority: the <link linkend="io-priority">I/O priority</link>
328 * @cancellable: optional #GCancellable object, %NULL to ignore.
329 * @callback: a #GAsyncReadyCallback to call when the initialization is
331 * @user_data: the data to pass to callback function
333 * Helper function for constructing #GAsyncInitiable object. This is
334 * similar to g_object_new_valist() but also initializes the object
337 * When the initialization is finished, @callback will be called. You can
338 * then call g_async_initable_new_finish() to get new object and check for
344 g_async_initable_new_valist_async (GType object_type,
345 const gchar *first_property_name,
348 GCancellable *cancellable,
349 GAsyncReadyCallback callback,
354 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
356 obj = g_object_new_valist (object_type,
360 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
361 io_priority, cancellable,
362 callback, user_data);
363 g_object_unref (obj); /* Passed ownership to async call */
367 * g_async_initable_new_finish:
368 * @initable: the #GAsyncInitable from the callback
369 * @res: the #GAsyncResult.from the callback
370 * @error: a #GError location to store the error occuring, or %NULL to
373 * Finishes the async construction for the various g_async_initable_new calls,
374 * returning the created object or %NULL on error.
376 * Returns: a newly created #GObject, or %NULL on error. Free with
382 g_async_initable_new_finish (GAsyncInitable *initable,
386 if (g_async_initable_init_finish (initable, res, error))
387 return g_object_ref (initable);
392 #define __G_ASYNC_INITABLE_C__
393 #include "gioaliasdef.c"