Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gasyncinitable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
27 #include "glibintl.h"
28
29 #include "gioalias.h"
30
31 /**
32  * SECTION:gasyncinitable
33  * @short_description: Asynchronously failable object initialization interface
34  * @include: gio/gio.h
35  * @see_also: #GInitable
36  *
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.
40  *
41  * A class may implement both the #GInitable and #GAsyncInitable interfaces.
42  *
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.
49  */
50
51 static void     g_async_initable_base_init        (gpointer              g_iface);
52 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
53                                                    int                   io_priority,
54                                                    GCancellable         *cancellable,
55                                                    GAsyncReadyCallback   callback,
56                                                    gpointer              user_data);
57 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
58                                                    GAsyncResult         *res,
59                                                    GError              **error);
60
61 GType
62 g_async_initable_get_type (void)
63 {
64   static volatile gsize g_define_type_id__volatile = 0;
65
66   if (g_once_init_enter (&g_define_type_id__volatile))
67     {
68       const GTypeInfo initable_info =
69       {
70         sizeof (GAsyncInitableIface), /* class_size */
71         g_async_initable_base_init,   /* base_init */
72         NULL,           /* base_finalize */
73         NULL,
74         NULL,           /* class_finalize */
75         NULL,           /* class_data */
76         0,
77         0,              /* n_preallocs */
78         NULL
79       };
80       GType g_define_type_id =
81         g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncInitable"),
82                                 &initable_info, 0);
83
84       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
85
86       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
87     }
88
89   return g_define_type_id__volatile;
90 }
91
92 static void
93 g_async_initable_base_init (gpointer g_iface)
94 {
95   GAsyncInitableIface *iface = g_iface;
96
97   iface->init_async = g_async_initable_real_init_async;
98   iface->init_finish = g_async_initable_real_init_finish;
99 }
100
101 /**
102  * g_async_initable_init_async:
103  * @initable: a #GAsyncInitable.
104  * @io_priority: the <link linkend="io-priority">I/O priority</link>
105  *     of the operation.
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
109  *
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.
114  *
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
117  * initialization.
118  *
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.
125  *
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.
129  *
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 it's safe to implement the singleton
134  * pattern in the GObject constructor function.
135  *
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.
141  *
142  * Since: 2.22
143  */
144 void
145 g_async_initable_init_async (GAsyncInitable      *initable,
146                              int                  io_priority,
147                              GCancellable        *cancellable,
148                              GAsyncReadyCallback  callback,
149                              gpointer             user_data)
150 {
151   GAsyncInitableIface *iface;
152
153   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
154
155   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
156
157   (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
158 }
159
160 /**
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
165  * ignore.
166  *
167  * Finishes asynchronous initialization and returns the result.
168  * See g_async_initable_init_async().
169  *
170  * Returns: %TRUE if successful. If an error has occurred, this function
171  * will return %FALSE and set @error appropriately if present.
172  *
173  * Since: 2.22
174  */
175 gboolean
176 g_async_initable_init_finish (GAsyncInitable  *initable,
177                               GAsyncResult    *res,
178                               GError         **error)
179 {
180   GAsyncInitableIface *iface;
181
182   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
183   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
184
185   if (G_IS_SIMPLE_ASYNC_RESULT (res))
186     {
187       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
188       if (g_simple_async_result_propagate_error (simple, error))
189         return FALSE;
190     }
191
192   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
193
194   return (* iface->init_finish) (initable, res, error);
195 }
196
197 static void
198 async_init_thread (GSimpleAsyncResult *res,
199                    GObject            *object,
200                    GCancellable       *cancellable)
201 {
202   GError *error = NULL;
203
204   if (!g_initable_init (G_INITABLE (object), cancellable, &error))
205     {
206       g_simple_async_result_set_from_error (res, error);
207       g_error_free (error);
208     }
209 }
210
211 static void
212 g_async_initable_real_init_async (GAsyncInitable      *initable,
213                                   int                  io_priority,
214                                   GCancellable        *cancellable,
215                                   GAsyncReadyCallback  callback,
216                                   gpointer             user_data)
217 {
218   GSimpleAsyncResult *res;
219
220   g_return_if_fail (G_IS_INITABLE (initable));
221
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);
227 }
228
229 static gboolean
230 g_async_initable_real_init_finish (GAsyncInitable  *initable,
231                                    GAsyncResult    *res,
232                                    GError         **error)
233 {
234   return TRUE; /* Errors handled by base impl */
235 }
236
237 /**
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>
241  *     of the operation.
242  * @cancellable: optional #GCancellable object, %NULL to ignore.
243  * @callback: a #GAsyncReadyCallback to call when the initialization is
244  *     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
247  *     properties
248  * @...:  the value of the first property, followed by other property
249  *    value pairs, and ended by %NULL.
250  *
251  * Helper function for constructing #GAsyncInitiable object. This is
252  * similar to g_object_new() but also initializes the object asynchronously.
253  *
254  * When the initialization is finished, @callback will be called. You can
255  * then call g_async_initable_new_finish() to get the new object and check
256  * for any errors.
257  *
258  * Since: 2.22
259  */
260 void
261 g_async_initable_new_async (GType                object_type,
262                             int                  io_priority,
263                             GCancellable        *cancellable,
264                             GAsyncReadyCallback  callback,
265                             gpointer             user_data,
266                             const gchar         *first_property_name,
267                             ...)
268 {
269   va_list var_args;
270
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);
276   va_end (var_args);
277 }
278
279 /**
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>
285  *     of the operation.
286  * @cancellable: optional #GCancellable object, %NULL to ignore.
287  * @callback: a #GAsyncReadyCallback to call when the initialization is
288  *     finished
289  * @user_data: the data to pass to callback function
290  *
291  * Helper function for constructing #GAsyncInitiable object. This is
292  * similar to g_object_newv() but also initializes the object asynchronously.
293  *
294  * When the initialization is finished, @callback will be called. You can
295  * then call g_async_initable_new_finish() to get the new object and check
296  * for any errors.
297  *
298  * Since: 2.22
299  */
300 void
301 g_async_initable_newv_async (GType                object_type,
302                              guint                n_parameters,
303                              GParameter          *parameters,
304                              int                  io_priority,
305                              GCancellable        *cancellable,
306                              GAsyncReadyCallback  callback,
307                              gpointer             user_data)
308 {
309   GObject *obj;
310
311   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
312
313   obj = g_object_newv (object_type, n_parameters, parameters);
314
315   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
316                                io_priority, cancellable,
317                                callback, user_data);
318 }
319
320 /**
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>
327  *     of the operation.
328  * @cancellable: optional #GCancellable object, %NULL to ignore.
329  * @callback: a #GAsyncReadyCallback to call when the initialization is
330  *     finished
331  * @user_data: the data to pass to callback function
332  *
333  * Helper function for constructing #GAsyncInitiable object. This is
334  * similar to g_object_new_valist() but also initializes the object
335  * asynchronously.
336  *
337  * When the initialization is finished, @callback will be called. You can
338  * then call g_async_initable_new_finish() to get the new object and check
339  * for any errors.
340  *
341  * Since: 2.22
342  */
343 void
344 g_async_initable_new_valist_async (GType                object_type,
345                                    const gchar         *first_property_name,
346                                    va_list              var_args,
347                                    int                  io_priority,
348                                    GCancellable        *cancellable,
349                                    GAsyncReadyCallback  callback,
350                                    gpointer             user_data)
351 {
352   GObject *obj;
353
354   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
355
356   obj = g_object_new_valist (object_type,
357                              first_property_name,
358                              var_args);
359
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 */
364 }
365
366 /**
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
371  *     ignore.
372  *
373  * Finishes the async construction for the various g_async_initable_new calls,
374  * returning the created object or %NULL on error.
375  *
376  * Returns: a newly created #GObject, or %NULL on error. Free with
377  *     g_object_unref().
378  *
379  * Since: 2.22
380  */
381 GObject *
382 g_async_initable_new_finish (GAsyncInitable  *initable,
383                              GAsyncResult    *res,
384                              GError         **error)
385 {
386   if (g_async_initable_init_finish (initable, res, error))
387     return g_object_ref (initable);
388   else
389     return NULL;
390 }
391
392 #define __G_ASYNC_INITABLE_C__
393 #include "gioaliasdef.c"