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
44 * Users of objects implementing this are not intended to use
45 * the interface method directly, instead it will be used automatically
46 * in various ways. For C applications you generally just call
47 * g_async_initable_new() directly, or indirectly via a foo_thing_new_async() wrapper.
48 * This will call g_async_initable_init() under the cover, calling back with %NULL and
49 * a set %GError on failure.
52 static void g_async_initable_base_init (gpointer g_iface);
53 static void g_async_initable_real_init_async (GAsyncInitable *initable,
55 GCancellable *cancellable,
56 GAsyncReadyCallback callback,
58 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
63 g_async_initable_get_type (void)
65 static volatile gsize g_define_type_id__volatile = 0;
67 if (g_once_init_enter (&g_define_type_id__volatile))
69 const GTypeInfo initable_info =
71 sizeof (GAsyncInitableIface), /* class_size */
72 g_async_initable_base_init, /* base_init */
73 NULL, /* base_finalize */
75 NULL, /* class_finalize */
76 NULL, /* class_data */
81 GType g_define_type_id =
82 g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncInitable"),
85 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
87 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
90 return g_define_type_id__volatile;
94 g_async_initable_base_init (gpointer g_iface)
96 GAsyncInitableIface *iface = g_iface;
98 iface->init_async = g_async_initable_real_init_async;
99 iface->init_finish = g_async_initable_real_init_finish;
103 * g_async_initable_init_async:
104 * @initable: a #GAsyncInitable.
105 * @io_priority: the <link linkend="io-priority">I/O priority</link>
107 * @cancellable: optional #GCancellable object, %NULL to ignore.
108 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
109 * @user_data: the data to pass to callback function
111 * Starts asynchronous initialization of the object implementing the interface.
112 * This must be done before any real use of the object after initial construction.
113 * If the object also implements #GInitable you can optionally call g_initable_init()
116 * When the initialization is finished, @callback will be called. You can then call
117 * g_async_initable_init_finish() to get the result of the initialization.
119 * Implementations may also support cancellation. If @cancellable is not %NULL,
120 * then initialization can be cancelled by triggering the cancellable object
121 * 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 return (* 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
171 * has occurred, this function will return %FALSE and set @error
172 * appropriately if present.
177 g_async_initable_init_finish (GAsyncInitable *initable,
181 GAsyncInitableIface *iface;
183 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
184 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
186 if (G_IS_SIMPLE_ASYNC_RESULT (res))
188 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
189 if (g_simple_async_result_propagate_error (simple, error))
193 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
195 return (* iface->init_finish) (initable, res, error);
199 async_init_thread (GSimpleAsyncResult *res,
201 GCancellable *cancellable)
203 GError *error = NULL;
205 if (!g_initable_init (G_INITABLE (res), cancellable, &error))
207 g_simple_async_result_set_from_error (res, error);
208 g_error_free (error);
213 g_async_initable_real_init_async (GAsyncInitable *initable,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
219 GSimpleAsyncResult *res;
221 g_return_if_fail (G_IS_INITABLE (initable));
223 res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
224 g_async_initable_real_init_async);
225 g_simple_async_result_run_in_thread (res, async_init_thread,
226 io_priority, cancellable);
227 g_object_unref (res);
231 g_async_initable_real_init_finish (GAsyncInitable *initable,
235 return TRUE; /* Errors handled by base impl */
239 * g_async_initable_new_async:
240 * @object_type: a #GType supporting #GAsyncInitable.
241 * @io_priority: the <link linkend="io-priority">I/O priority</link>
243 * @cancellable: optional #GCancellable object, %NULL to ignore.
244 * @callback: a #GAsyncReadyCallback to call when the initialization is finished
245 * @user_data: the data to pass to callback function
246 * @first_property_name: the name of the first property, or %NULL if no properties
247 * @...: the value if the first property, followed by and other property
248 * value pairs, and ended by %NULL.
250 * Helper function for constructing #GAsyncInitiable object. This is
251 * similar to g_object_new() but also initializes the object asyncronously.
253 * When the initialization is finished, @callback will be called. You can then call
254 * g_async_initable_new_finish() to get new object and check for any errors.
259 g_async_initable_new_async (GType object_type,
261 GCancellable *cancellable,
262 GAsyncReadyCallback callback,
264 const gchar *first_property_name,
269 va_start (var_args, first_property_name);
270 g_async_initable_new_valist_async (object_type,
271 first_property_name, var_args,
272 io_priority, cancellable,
273 callback, user_data);
278 * g_async_initable_newv_async:
279 * @object_type: a #GType supporting #GAsyncInitable.
280 * @n_parameters: the number of parameters in @parameters
281 * @parameters: the parameters to use to construct the object
282 * @io_priority: the <link linkend="io-priority">I/O priority</link>
284 * @cancellable: optional #GCancellable object, %NULL to ignore.
285 * @callback: a #GAsyncReadyCallback to call when the initialization is finished
286 * @user_data: the data to pass to callback function
288 * Helper function for constructing #GAsyncInitiable object. This is
289 * similar to g_object_newv() but also initializes the object asyncronously.
291 * When the initialization is finished, @callback will be called. You can then call
292 * g_async_initable_new_finish() to get new object and check for any errors.
297 g_async_initable_newv_async (GType object_type,
299 GParameter *parameters,
301 GCancellable *cancellable,
302 GAsyncReadyCallback callback,
307 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
309 obj = g_object_newv (object_type, n_parameters, parameters);
311 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
312 io_priority, cancellable,
313 callback, user_data);
317 * g_async_initable_new_valist_async:
318 * @object_type: a #GType supporting #GAsyncInitable.
319 * @first_property_name: the name of the first property, followed by
320 * the value, and other property value pairs, and ended by %NULL.
321 * @var_args: The var args list generated from @first_property_name.
322 * @io_priority: the <link linkend="io-priority">I/O priority</link>
324 * @cancellable: optional #GCancellable object, %NULL to ignore.
325 * @callback: a #GAsyncReadyCallback to call when the initialization is finished
326 * @user_data: the data to pass to callback function
328 * Helper function for constructing #GAsyncInitiable object. This is
329 * similar to g_object_new_valist() but also initializes the object asyncronously.
331 * When the initialization is finished, @callback will be called. You can then call
332 * g_async_initable_new_finish() to get new object and check for any errors.
337 g_async_initable_new_valist_async (GType object_type,
338 const gchar *first_property_name,
341 GCancellable *cancellable,
342 GAsyncReadyCallback callback,
347 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
349 obj = g_object_new_valist (object_type,
353 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
354 io_priority, cancellable,
355 callback, user_data);
356 g_object_unref (obj); /* Passed ownership to async call */
359 * g_async_initable_new_finish:
360 * @initable: the #GAsyncInitable from the callback
361 * @res: the #GAsyncResult.from the callback
362 * @error: a #GError location to store the error occuring, or %NULL to
365 * Finishes the async construction for the various g_async_initable_new calls,
366 * returning the created object or %NULL on error.
368 * Returns: a newly created #GObject, or %NULL on error. Free with g_object_unref().
373 g_async_initable_new_finish (GAsyncInitable *initable,
377 if (g_async_initable_init_finish (initable, res, error))
378 return g_object_ref (initable);
383 #define __G_ASYNC_INITABLE_C__
384 #include "gioaliasdef.c"