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_async()
46 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
47 * g_async_initable_init_async() under the cover, calling back with %NULL and
48 * a set %GError on failure.
51 static void g_async_initable_real_init_async (GAsyncInitable *initable,
53 GCancellable *cancellable,
54 GAsyncReadyCallback callback,
56 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
61 typedef GAsyncInitableIface GAsyncInitableInterface;
62 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
66 g_async_initable_default_init (GAsyncInitableInterface *iface)
68 iface->init_async = g_async_initable_real_init_async;
69 iface->init_finish = g_async_initable_real_init_finish;
73 * g_async_initable_init_async:
74 * @initable: a #GAsyncInitable.
75 * @io_priority: the <link linkend="io-priority">I/O priority</link>
77 * @cancellable: optional #GCancellable object, %NULL to ignore.
78 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
79 * @user_data: the data to pass to callback function
81 * Starts asynchronous initialization of the object implementing the
82 * interface. This must be done before any real use of the object after
83 * initial construction. If the object also implements #GInitable you can
84 * optionally call g_initable_init() instead.
86 * When the initialization is finished, @callback will be called. You can
87 * then call g_async_initable_init_finish() to get the result of the
90 * Implementations may also support cancellation. If @cancellable is not
91 * %NULL, then initialization can be cancelled by triggering the cancellable
92 * object from another thread. If the operation was cancelled, the error
93 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
94 * the object doesn't support cancellable initialization, the error
95 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
97 * If this function is not called, or returns with an error, then all
98 * operations on the object should fail, generally returning the
99 * error %G_IO_ERROR_NOT_INITIALIZED.
101 * Implementations of this method must be idempotent: i.e. multiple calls
102 * to this function with the same argument should return the same results.
103 * Only the first call initializes the object; further calls return the result
104 * of the first call. This is so that it's safe to implement the singleton
105 * pattern in the GObject constructor function.
107 * For classes that also support the #GInitable interface, the default
108 * implementation of this method will run the g_initable_init() function
109 * in a thread, so if you want to support asynchronous initialization via
110 * threads, just implement the #GAsyncInitable interface without overriding
111 * any interface methods.
116 g_async_initable_init_async (GAsyncInitable *initable,
118 GCancellable *cancellable,
119 GAsyncReadyCallback callback,
122 GAsyncInitableIface *iface;
124 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
126 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
128 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
132 * g_async_initable_init_finish:
133 * @initable: a #GAsyncInitable.
134 * @res: a #GAsyncResult.
135 * @error: a #GError location to store the error occuring, or %NULL to
138 * Finishes asynchronous initialization and returns the result.
139 * See g_async_initable_init_async().
141 * Returns: %TRUE if successful. If an error has occurred, this function
142 * will return %FALSE and set @error appropriately if present.
147 g_async_initable_init_finish (GAsyncInitable *initable,
151 GAsyncInitableIface *iface;
153 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
154 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
156 if (G_IS_SIMPLE_ASYNC_RESULT (res))
158 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
159 if (g_simple_async_result_propagate_error (simple, error))
163 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
165 return (* iface->init_finish) (initable, res, error);
169 async_init_thread (GSimpleAsyncResult *res,
171 GCancellable *cancellable)
173 GError *error = NULL;
175 if (!g_initable_init (G_INITABLE (object), cancellable, &error))
177 g_simple_async_result_set_from_error (res, error);
178 g_error_free (error);
183 g_async_initable_real_init_async (GAsyncInitable *initable,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
189 GSimpleAsyncResult *res;
191 g_return_if_fail (G_IS_INITABLE (initable));
193 res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
194 g_async_initable_real_init_async);
195 g_simple_async_result_run_in_thread (res, async_init_thread,
196 io_priority, cancellable);
197 g_object_unref (res);
201 g_async_initable_real_init_finish (GAsyncInitable *initable,
205 return TRUE; /* Errors handled by base impl */
209 * g_async_initable_new_async:
210 * @object_type: a #GType supporting #GAsyncInitable.
211 * @io_priority: the <link linkend="io-priority">I/O priority</link>
213 * @cancellable: optional #GCancellable object, %NULL to ignore.
214 * @callback: a #GAsyncReadyCallback to call when the initialization is
216 * @user_data: the data to pass to callback function
217 * @first_property_name: the name of the first property, or %NULL if no
219 * @...: the value of the first property, followed by other property
220 * value pairs, and ended by %NULL.
222 * Helper function for constructing #GAsyncInitiable object. This is
223 * similar to g_object_new() but also initializes the object asynchronously.
225 * When the initialization is finished, @callback will be called. You can
226 * then call g_async_initable_new_finish() to get the new object and check
232 g_async_initable_new_async (GType object_type,
234 GCancellable *cancellable,
235 GAsyncReadyCallback callback,
237 const gchar *first_property_name,
242 va_start (var_args, first_property_name);
243 g_async_initable_new_valist_async (object_type,
244 first_property_name, var_args,
245 io_priority, cancellable,
246 callback, user_data);
251 * g_async_initable_newv_async:
252 * @object_type: a #GType supporting #GAsyncInitable.
253 * @n_parameters: the number of parameters in @parameters
254 * @parameters: the parameters to use to construct the object
255 * @io_priority: the <link linkend="io-priority">I/O priority</link>
257 * @cancellable: optional #GCancellable object, %NULL to ignore.
258 * @callback: a #GAsyncReadyCallback to call when the initialization is
260 * @user_data: the data to pass to callback function
262 * Helper function for constructing #GAsyncInitiable object. This is
263 * similar to g_object_newv() but also initializes the object asynchronously.
265 * When the initialization is finished, @callback will be called. You can
266 * then call g_async_initable_new_finish() to get the new object and check
272 g_async_initable_newv_async (GType object_type,
274 GParameter *parameters,
276 GCancellable *cancellable,
277 GAsyncReadyCallback callback,
282 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
284 obj = g_object_newv (object_type, n_parameters, parameters);
286 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
287 io_priority, cancellable,
288 callback, user_data);
292 * g_async_initable_new_valist_async:
293 * @object_type: a #GType supporting #GAsyncInitable.
294 * @first_property_name: the name of the first property, followed by
295 * the value, and other property value pairs, and ended by %NULL.
296 * @var_args: The var args list generated from @first_property_name.
297 * @io_priority: the <link linkend="io-priority">I/O priority</link>
299 * @cancellable: optional #GCancellable object, %NULL to ignore.
300 * @callback: a #GAsyncReadyCallback to call when the initialization is
302 * @user_data: the data to pass to callback function
304 * Helper function for constructing #GAsyncInitiable object. This is
305 * similar to g_object_new_valist() but also initializes the object
308 * When the initialization is finished, @callback will be called. You can
309 * then call g_async_initable_new_finish() to get the new object and check
315 g_async_initable_new_valist_async (GType object_type,
316 const gchar *first_property_name,
319 GCancellable *cancellable,
320 GAsyncReadyCallback callback,
325 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
327 obj = g_object_new_valist (object_type,
331 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
332 io_priority, cancellable,
333 callback, user_data);
334 g_object_unref (obj); /* Passed ownership to async call */
338 * g_async_initable_new_finish:
339 * @initable: the #GAsyncInitable from the callback
340 * @res: the #GAsyncResult.from the callback
341 * @error: a #GError location to store the error occuring, or %NULL to
344 * Finishes the async construction for the various g_async_initable_new calls,
345 * returning the created object or %NULL on error.
347 * Returns: a newly created #GObject, or %NULL on error. Free with
353 g_async_initable_new_finish (GAsyncInitable *initable,
357 if (g_async_initable_init_finish (initable, res, error))
358 return g_object_ref (initable);
363 #define __G_ASYNC_INITABLE_C__
364 #include "gioaliasdef.c"