hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / gio / gsimpleasyncresult.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "gsimpleasyncresult.h"
26 #include "gasyncresult.h"
27 #include "gcancellable.h"
28 #include "gioscheduler.h"
29 #include <gio/gioerror.h>
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gsimpleasyncresult
35  * @short_description: Simple asynchronous results implementation
36  * @include: gio/gio.h
37  * @see_also: #GAsyncResult, #GTask
38  *
39  * As of GLib 2.36, #GSimpleAsyncResult is deprecated in favor of
40  * #GTask, which provides a simpler API.
41  *
42  * #GSimpleAsyncResult implements #GAsyncResult.
43  *
44  * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
45  * reporting, operation cancellation and the final state of an operation,
46  * completely transparent to the application. Results can be returned
47  * as a pointer e.g. for functions that return data that is collected
48  * asynchronously, a boolean value for checking the success or failure
49  * of an operation, or a #gssize for operations which return the number
50  * of bytes modified by the operation; all of the simple return cases
51  * are covered.
52  *
53  * Most of the time, an application will not need to know of the details
54  * of this API; it is handled transparently, and any necessary operations
55  * are handled by #GAsyncResult's interface. However, if implementing a
56  * new GIO module, for writing language bindings, or for complex
57  * applications that need better control of how asynchronous operations
58  * are completed, it is important to understand this functionality.
59  *
60  * GSimpleAsyncResults are tagged with the calling function to ensure
61  * that asynchronous functions and their finishing functions are used
62  * together correctly.
63  *
64  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
65  * If the result needs to be created for a #GError, use
66  * g_simple_async_result_new_from_error() or
67  * g_simple_async_result_new_take_error(). If a #GError is not available
68  * (e.g. the asynchronous operation's doesn't take a #GError argument),
69  * but the result still needs to be created for an error condition, use
70  * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
71  * if your application or binding requires passing a variable argument list
72  * directly), and the error can then be propagated through the use of
73  * g_simple_async_result_propagate_error().
74  *
75  * An asynchronous operation can be made to ignore a cancellation event by
76  * calling g_simple_async_result_set_handle_cancellation() with a
77  * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
78  * operations that are dangerous to cancel, such as close (which would
79  * cause a leak if cancelled before being run).
80  *
81  * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
82  * or it can use #GThreads.
83  * g_simple_async_result_complete() will finish an I/O task directly
84  * from the point where it is called. g_simple_async_result_complete_in_idle()
85  * will finish it from an idle handler in the 
86  * [thread-default main context][g-main-context-push-thread-default]
87  * . g_simple_async_result_run_in_thread() will run the
88  * job in a separate thread and then deliver the result to the
89  * thread-default main context.
90  *
91  * To set the results of an asynchronous function,
92  * g_simple_async_result_set_op_res_gpointer(),
93  * g_simple_async_result_set_op_res_gboolean(), and
94  * g_simple_async_result_set_op_res_gssize()
95  * are provided, setting the operation's result to a gpointer, gboolean, or
96  * gssize, respectively.
97  *
98  * Likewise, to get the result of an asynchronous function,
99  * g_simple_async_result_get_op_res_gpointer(),
100  * g_simple_async_result_get_op_res_gboolean(), and
101  * g_simple_async_result_get_op_res_gssize() are
102  * provided, getting the operation's result as a gpointer, gboolean, and
103  * gssize, respectively.
104  *
105  * For the details of the requirements implementations must respect, see
106  * #GAsyncResult.  A typical implementation of an asynchronous operation
107  * using GSimpleAsyncResult looks something like this:
108  *
109  * |[<!-- language="C" -->
110  * static void
111  * baked_cb (Cake    *cake,
112  *           gpointer user_data)
113  * {
114  *   // In this example, this callback is not given a reference to the cake,
115  *   // so the GSimpleAsyncResult has to take a reference to it.
116  *   GSimpleAsyncResult *result = user_data;
117  *
118  *   if (cake == NULL)
119  *     g_simple_async_result_set_error (result,
120  *                                      BAKER_ERRORS,
121  *                                      BAKER_ERROR_NO_FLOUR,
122  *                                      "Go to the supermarket");
123  *   else
124  *     g_simple_async_result_set_op_res_gpointer (result,
125  *                                                g_object_ref (cake),
126  *                                                g_object_unref);
127  *
128  *
129  *   // In this example, we assume that baked_cb is called as a callback from
130  *   // the mainloop, so it's safe to complete the operation synchronously here.
131  *   // If, however, _baker_prepare_cake () might call its callback without
132  *   // first returning to the mainloop — inadvisable, but some APIs do so —
133  *   // we would need to use g_simple_async_result_complete_in_idle().
134  *   g_simple_async_result_complete (result);
135  *   g_object_unref (result);
136  * }
137  *
138  * void
139  * baker_bake_cake_async (Baker              *self,
140  *                        guint               radius,
141  *                        GAsyncReadyCallback callback,
142  *                        gpointer            user_data)
143  * {
144  *   GSimpleAsyncResult *simple;
145  *   Cake               *cake;
146  *
147  *   if (radius < 3)
148  *     {
149  *       g_simple_async_report_error_in_idle (G_OBJECT (self),
150  *                                            callback,
151  *                                            user_data,
152  *                                            BAKER_ERRORS,
153  *                                            BAKER_ERROR_TOO_SMALL,
154  *                                            "%ucm radius cakes are silly",
155  *                                            radius);
156  *       return;
157  *     }
158  *
159  *   simple = g_simple_async_result_new (G_OBJECT (self),
160  *                                       callback,
161  *                                       user_data,
162  *                                       baker_bake_cake_async);
163  *   cake = _baker_get_cached_cake (self, radius);
164  *
165  *   if (cake != NULL)
166  *     {
167  *       g_simple_async_result_set_op_res_gpointer (simple,
168  *                                                  g_object_ref (cake),
169  *                                                  g_object_unref);
170  *       g_simple_async_result_complete_in_idle (simple);
171  *       g_object_unref (simple);
172  *       // Drop the reference returned by _baker_get_cached_cake();
173  *       // the GSimpleAsyncResult has taken its own reference.
174  *       g_object_unref (cake);
175  *       return;
176  *     }
177  *
178  *   _baker_prepare_cake (self, radius, baked_cb, simple);
179  * }
180  *
181  * Cake *
182  * baker_bake_cake_finish (Baker        *self,
183  *                         GAsyncResult *result,
184  *                         GError      **error)
185  * {
186  *   GSimpleAsyncResult *simple;
187  *   Cake               *cake;
188  *
189  *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
190  *                                                         G_OBJECT (self),
191  *                                                         baker_bake_cake_async),
192  *                         NULL);
193  *
194  *   simple = (GSimpleAsyncResult *) result;
195  *
196  *   if (g_simple_async_result_propagate_error (simple, error))
197  *     return NULL;
198  *
199  *   cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
200  *   return g_object_ref (cake);
201  * }
202  * ]|
203  */
204
205 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
206
207 struct _GSimpleAsyncResult
208 {
209   GObject parent_instance;
210
211   GObject *source_object;
212   GAsyncReadyCallback callback;
213   gpointer user_data;
214   GMainContext *context;
215   GError *error;
216   gboolean failed;
217   gboolean handle_cancellation;
218   GCancellable *check_cancellable;
219
220   gpointer source_tag;
221
222   union {
223     gpointer v_pointer;
224     gboolean v_boolean;
225     gssize   v_ssize;
226   } op_res;
227
228   GDestroyNotify destroy_op_res;
229 };
230
231 struct _GSimpleAsyncResultClass
232 {
233   GObjectClass parent_class;
234 };
235
236
237 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
238                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
239                                                 g_simple_async_result_async_result_iface_init))
240
241 static void
242 clear_op_res (GSimpleAsyncResult *simple)
243 {
244   if (simple->destroy_op_res)
245     simple->destroy_op_res (simple->op_res.v_pointer);
246   simple->destroy_op_res = NULL;
247   simple->op_res.v_ssize = 0;
248 }
249
250 static void
251 g_simple_async_result_finalize (GObject *object)
252 {
253   GSimpleAsyncResult *simple;
254
255   simple = G_SIMPLE_ASYNC_RESULT (object);
256
257   if (simple->source_object)
258     g_object_unref (simple->source_object);
259
260   if (simple->check_cancellable)
261     g_object_unref (simple->check_cancellable);
262
263   g_main_context_unref (simple->context);
264
265   clear_op_res (simple);
266
267   if (simple->error)
268     g_error_free (simple->error);
269
270   G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
271 }
272
273 static void
274 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
275 {
276   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
277  
278   gobject_class->finalize = g_simple_async_result_finalize;
279 }
280
281 static void
282 g_simple_async_result_init (GSimpleAsyncResult *simple)
283 {
284   simple->handle_cancellation = TRUE;
285
286   simple->context = g_main_context_ref_thread_default ();
287 }
288
289 /**
290  * g_simple_async_result_new:
291  * @source_object: (allow-none): a #GObject, or %NULL.
292  * @callback: (scope async): a #GAsyncReadyCallback.
293  * @user_data: (closure): user data passed to @callback.
294  * @source_tag: the asynchronous function.
295  *
296  * Creates a #GSimpleAsyncResult.
297  *
298  * The common convention is to create the #GSimpleAsyncResult in the
299  * function that starts the asynchronous operation and use that same
300  * function as the @source_tag.
301  *
302  * If your operation supports cancellation with #GCancellable (which it
303  * probably should) then you should provide the user's cancellable to
304  * g_simple_async_result_set_check_cancellable() immediately after
305  * this function returns.
306  *
307  * Returns: a #GSimpleAsyncResult.
308  **/
309 GSimpleAsyncResult *
310 g_simple_async_result_new (GObject             *source_object,
311                            GAsyncReadyCallback  callback,
312                            gpointer             user_data,
313                            gpointer             source_tag)
314 {
315   GSimpleAsyncResult *simple;
316
317   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
318
319   simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
320   simple->callback = callback;
321   if (source_object)
322     simple->source_object = g_object_ref (source_object);
323   else
324     simple->source_object = NULL;
325   simple->user_data = user_data;
326   simple->source_tag = source_tag;
327  
328   return simple;
329 }
330
331 /**
332  * g_simple_async_result_new_from_error:
333  * @source_object: (allow-none): a #GObject, or %NULL.
334  * @callback: (scope async): a #GAsyncReadyCallback.
335  * @user_data: (closure): user data passed to @callback.
336  * @error: a #GError
337  *
338  * Creates a #GSimpleAsyncResult from an error condition.
339  *
340  * Returns: a #GSimpleAsyncResult.
341  **/
342 GSimpleAsyncResult *
343 g_simple_async_result_new_from_error (GObject             *source_object,
344                                       GAsyncReadyCallback  callback,
345                                       gpointer             user_data,
346                                       const GError        *error)
347 {
348   GSimpleAsyncResult *simple;
349
350   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
351
352   simple = g_simple_async_result_new (source_object,
353                                       callback,
354                                       user_data, NULL);
355   g_simple_async_result_set_from_error (simple, error);
356
357   return simple;
358 }
359
360 /**
361  * g_simple_async_result_new_take_error: (skip)
362  * @source_object: (allow-none): a #GObject, or %NULL
363  * @callback: (scope async): a #GAsyncReadyCallback
364  * @user_data: (closure): user data passed to @callback
365  * @error: a #GError
366  *
367  * Creates a #GSimpleAsyncResult from an error condition, and takes over the
368  * caller's ownership of @error, so the caller does not need to free it anymore.
369  *
370  * Returns: a #GSimpleAsyncResult
371  *
372  * Since: 2.28
373  **/
374 GSimpleAsyncResult *
375 g_simple_async_result_new_take_error (GObject             *source_object,
376                                       GAsyncReadyCallback  callback,
377                                       gpointer             user_data,
378                                       GError              *error)
379 {
380   GSimpleAsyncResult *simple;
381
382   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
383
384   simple = g_simple_async_result_new (source_object,
385                                       callback,
386                                       user_data, NULL);
387   g_simple_async_result_take_error (simple, error);
388
389   return simple;
390 }
391
392 /**
393  * g_simple_async_result_new_error:
394  * @source_object: (allow-none): a #GObject, or %NULL.
395  * @callback: (scope async): a #GAsyncReadyCallback.
396  * @user_data: (closure): user data passed to @callback.
397  * @domain: a #GQuark.
398  * @code: an error code.
399  * @format: a string with format characters.
400  * @...: a list of values to insert into @format.
401  *
402  * Creates a new #GSimpleAsyncResult with a set error.
403  *
404  * Returns: a #GSimpleAsyncResult.
405  **/
406 GSimpleAsyncResult *
407 g_simple_async_result_new_error (GObject             *source_object,
408                                  GAsyncReadyCallback  callback,
409                                  gpointer             user_data,
410                                  GQuark               domain,
411                                  gint                 code,
412                                  const char          *format,
413                                  ...)
414 {
415   GSimpleAsyncResult *simple;
416   va_list args;
417  
418   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
419   g_return_val_if_fail (domain != 0, NULL);
420   g_return_val_if_fail (format != NULL, NULL);
421
422   simple = g_simple_async_result_new (source_object,
423                                       callback,
424                                       user_data, NULL);
425
426   va_start (args, format);
427   g_simple_async_result_set_error_va (simple, domain, code, format, args);
428   va_end (args);
429  
430   return simple;
431 }
432
433
434 static gpointer
435 g_simple_async_result_get_user_data (GAsyncResult *res)
436 {
437   return G_SIMPLE_ASYNC_RESULT (res)->user_data;
438 }
439
440 static GObject *
441 g_simple_async_result_get_source_object (GAsyncResult *res)
442 {
443   if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
444     return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
445   return NULL;
446 }
447
448 static gboolean
449 g_simple_async_result_is_tagged (GAsyncResult *res,
450                                  gpointer      source_tag)
451 {
452   return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
453 }
454
455 static void
456 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
457 {
458   iface->get_user_data = g_simple_async_result_get_user_data;
459   iface->get_source_object = g_simple_async_result_get_source_object;
460   iface->is_tagged = g_simple_async_result_is_tagged;
461 }
462
463 /**
464  * g_simple_async_result_set_handle_cancellation:
465  * @simple: a #GSimpleAsyncResult.
466  * @handle_cancellation: a #gboolean.
467  *
468  * Sets whether to handle cancellation within the asynchronous operation.
469  *
470  * This function has nothing to do with
471  * g_simple_async_result_set_check_cancellable().  It only refers to the
472  * #GCancellable passed to g_simple_async_result_run_in_thread().
473  **/
474 void
475 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
476                                                gboolean            handle_cancellation)
477 {
478   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
479   simple->handle_cancellation = handle_cancellation;
480 }
481
482 /**
483  * g_simple_async_result_get_source_tag: (skip)
484  * @simple: a #GSimpleAsyncResult.
485  *
486  * Gets the source tag for the #GSimpleAsyncResult.
487  *
488  * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
489  **/
490 gpointer
491 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
492 {
493   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
494   return simple->source_tag;
495 }
496
497 /**
498  * g_simple_async_result_propagate_error:
499  * @simple: a #GSimpleAsyncResult.
500  * @dest: (out): a location to propagate the error to.
501  *
502  * Propagates an error from within the simple asynchronous result to
503  * a given destination.
504  *
505  * If the #GCancellable given to a prior call to
506  * g_simple_async_result_set_check_cancellable() is cancelled then this
507  * function will return %TRUE with @dest set appropriately.
508  *
509  * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
510  **/
511 gboolean
512 g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
513                                        GError             **dest)
514 {
515   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
516
517   if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
518     return TRUE;
519
520   if (simple->failed)
521     {
522       g_propagate_error (dest, simple->error);
523       simple->error = NULL;
524       return TRUE;
525     }
526
527   return FALSE;
528 }
529
530 /**
531  * g_simple_async_result_set_op_res_gpointer: (skip)
532  * @simple: a #GSimpleAsyncResult.
533  * @op_res: a pointer result from an asynchronous function.
534  * @destroy_op_res: a #GDestroyNotify function.
535  *
536  * Sets the operation result within the asynchronous result to a pointer.
537  **/
538 void
539 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
540                                            gpointer            op_res,
541                                            GDestroyNotify      destroy_op_res)
542 {
543   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
544
545   clear_op_res (simple);
546   simple->op_res.v_pointer = op_res;
547   simple->destroy_op_res = destroy_op_res;
548 }
549
550 /**
551  * g_simple_async_result_get_op_res_gpointer: (skip)
552  * @simple: a #GSimpleAsyncResult.
553  *
554  * Gets a pointer result as returned by the asynchronous function.
555  *
556  * Returns: a pointer from the result.
557  **/
558 gpointer
559 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
560 {
561   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
562   return simple->op_res.v_pointer;
563 }
564
565 /**
566  * g_simple_async_result_set_op_res_gssize:
567  * @simple: a #GSimpleAsyncResult.
568  * @op_res: a #gssize.
569  *
570  * Sets the operation result within the asynchronous result to
571  * the given @op_res.
572  **/
573 void
574 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
575                                          gssize              op_res)
576 {
577   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
578   clear_op_res (simple);
579   simple->op_res.v_ssize = op_res;
580 }
581
582 /**
583  * g_simple_async_result_get_op_res_gssize:
584  * @simple: a #GSimpleAsyncResult.
585  *
586  * Gets a gssize from the asynchronous result.
587  *
588  * Returns: a gssize returned from the asynchronous function.
589  **/
590 gssize
591 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
592 {
593   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
594   return simple->op_res.v_ssize;
595 }
596
597 /**
598  * g_simple_async_result_set_op_res_gboolean:
599  * @simple: a #GSimpleAsyncResult.
600  * @op_res: a #gboolean.
601  *
602  * Sets the operation result to a boolean within the asynchronous result.
603  **/
604 void
605 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
606                                            gboolean            op_res)
607 {
608   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
609   clear_op_res (simple);
610   simple->op_res.v_boolean = !!op_res;
611 }
612
613 /**
614  * g_simple_async_result_get_op_res_gboolean:
615  * @simple: a #GSimpleAsyncResult.
616  *
617  * Gets the operation result boolean from within the asynchronous result.
618  *
619  * Returns: %TRUE if the operation's result was %TRUE, %FALSE
620  *     if the operation's result was %FALSE.
621  **/
622 gboolean
623 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
624 {
625   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
626   return simple->op_res.v_boolean;
627 }
628
629 /**
630  * g_simple_async_result_set_from_error:
631  * @simple: a #GSimpleAsyncResult.
632  * @error: #GError.
633  *
634  * Sets the result from a #GError.
635  **/
636 void
637 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
638                                       const GError       *error)
639 {
640   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
641   g_return_if_fail (error != NULL);
642
643   if (simple->error)
644     g_error_free (simple->error);
645   simple->error = g_error_copy (error);
646   simple->failed = TRUE;
647 }
648
649 /**
650  * g_simple_async_result_take_error: (skip)
651  * @simple: a #GSimpleAsyncResult
652  * @error: a #GError
653  *
654  * Sets the result from @error, and takes over the caller's ownership
655  * of @error, so the caller does not need to free it any more.
656  *
657  * Since: 2.28
658  **/
659 void
660 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
661                                   GError             *error)
662 {
663   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
664   g_return_if_fail (error != NULL);
665
666   if (simple->error)
667     g_error_free (simple->error);
668   simple->error = error;
669   simple->failed = TRUE;
670 }
671
672 /**
673  * g_simple_async_result_set_error_va: (skip)
674  * @simple: a #GSimpleAsyncResult.
675  * @domain: a #GQuark (usually #G_IO_ERROR).
676  * @code: an error code.
677  * @format: a formatted error reporting string.
678  * @args: va_list of arguments.
679  *
680  * Sets an error within the asynchronous result without a #GError.
681  * Unless writing a binding, see g_simple_async_result_set_error().
682  **/
683 void
684 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
685                                     GQuark              domain,
686                                     gint                code,
687                                     const char         *format,
688                                     va_list             args)
689 {
690   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
691   g_return_if_fail (domain != 0);
692   g_return_if_fail (format != NULL);
693
694   if (simple->error)
695     g_error_free (simple->error);
696   simple->error = g_error_new_valist (domain, code, format, args);
697   simple->failed = TRUE;
698 }
699
700 /**
701  * g_simple_async_result_set_error: (skip)
702  * @simple: a #GSimpleAsyncResult.
703  * @domain: a #GQuark (usually #G_IO_ERROR).
704  * @code: an error code.
705  * @format: a formatted error reporting string.
706  * @...: a list of variables to fill in @format.
707  *
708  * Sets an error within the asynchronous result without a #GError.
709  **/
710 void
711 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
712                                  GQuark              domain,
713                                  gint                code,
714                                  const char         *format,
715                                  ...)
716 {
717   va_list args;
718
719   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
720   g_return_if_fail (domain != 0);
721   g_return_if_fail (format != NULL);
722
723   va_start (args, format);
724   g_simple_async_result_set_error_va (simple, domain, code, format, args);
725   va_end (args);
726 }
727
728 /**
729  * g_simple_async_result_complete:
730  * @simple: a #GSimpleAsyncResult.
731  *
732  * Completes an asynchronous I/O job immediately. Must be called in
733  * the thread where the asynchronous result was to be delivered, as it
734  * invokes the callback directly. If you are in a different thread use
735  * g_simple_async_result_complete_in_idle().
736  *
737  * Calling this function takes a reference to @simple for as long as
738  * is needed to complete the call.
739  **/
740 void
741 g_simple_async_result_complete (GSimpleAsyncResult *simple)
742 {
743 #ifndef G_DISABLE_CHECKS
744   GSource *current_source;
745   GMainContext *current_context;
746 #endif
747
748   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
749
750 #ifndef G_DISABLE_CHECKS
751   current_source = g_main_current_source ();
752   if (current_source && !g_source_is_destroyed (current_source))
753     {
754       current_context = g_source_get_context (current_source);
755       if (simple->context != current_context)
756         g_warning ("g_simple_async_result_complete() called from wrong context!");
757     }
758 #endif
759
760   if (simple->callback)
761     {
762       g_main_context_push_thread_default (simple->context);
763       simple->callback (simple->source_object,
764                         G_ASYNC_RESULT (simple),
765                         simple->user_data);
766       g_main_context_pop_thread_default (simple->context);
767     }
768 }
769
770 static gboolean
771 complete_in_idle_cb (gpointer data)
772 {
773   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
774
775   g_simple_async_result_complete (simple);
776
777   return FALSE;
778 }
779
780 /**
781  * g_simple_async_result_complete_in_idle:
782  * @simple: a #GSimpleAsyncResult.
783  *
784  * Completes an asynchronous function in an idle handler in the
785  * [thread-default main context][g-main-context-push-thread-default]
786  * of the thread that @simple was initially created in
787  * (and re-pushes that context around the invocation of the callback).
788  *
789  * Calling this function takes a reference to @simple for as long as
790  * is needed to complete the call.
791  */
792 void
793 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
794 {
795   GSource *source;
796
797   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
798
799   g_object_ref (simple);
800  
801   source = g_idle_source_new ();
802   g_source_set_priority (source, G_PRIORITY_DEFAULT);
803   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
804   g_source_set_name (source, "[gio] complete_in_idle_cb");
805
806   g_source_attach (source, simple->context);
807   g_source_unref (source);
808 }
809
810 typedef struct {
811   GSimpleAsyncResult *simple;
812   GCancellable *cancellable;
813   GSimpleAsyncThreadFunc func;
814 } RunInThreadData;
815
816
817 static gboolean
818 complete_in_idle_cb_for_thread (gpointer _data)
819 {
820   RunInThreadData *data = _data;
821   GSimpleAsyncResult *simple;
822
823   simple = data->simple;
824  
825   if (simple->handle_cancellation &&
826       g_cancellable_is_cancelled (data->cancellable))
827     g_simple_async_result_set_error (simple,
828                                      G_IO_ERROR,
829                                      G_IO_ERROR_CANCELLED,
830                                      "%s", _("Operation was cancelled"));
831  
832   g_simple_async_result_complete (simple);
833
834   if (data->cancellable)
835     g_object_unref (data->cancellable);
836   g_object_unref (data->simple);
837   g_free (data);
838  
839   return FALSE;
840 }
841
842 static gboolean
843 run_in_thread (GIOSchedulerJob *job,
844                GCancellable    *c,
845                gpointer         _data)
846 {
847   RunInThreadData *data = _data;
848   GSimpleAsyncResult *simple = data->simple;
849   GSource *source;
850  
851   if (simple->handle_cancellation &&
852       g_cancellable_is_cancelled (c))
853     g_simple_async_result_set_error (simple,
854                                      G_IO_ERROR,
855                                      G_IO_ERROR_CANCELLED,
856                                      "%s", _("Operation was cancelled"));
857   else
858     data->func (simple,
859                 simple->source_object,
860                 c);
861
862   source = g_idle_source_new ();
863   g_source_set_priority (source, G_PRIORITY_DEFAULT);
864   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
865   g_source_set_name (source, "[gio] complete_in_idle_cb_for_thread");
866
867   g_source_attach (source, simple->context);
868   g_source_unref (source);
869
870   return FALSE;
871 }
872
873 /**
874  * g_simple_async_result_run_in_thread: (skip)
875  * @simple: a #GSimpleAsyncResult.
876  * @func: a #GSimpleAsyncThreadFunc.
877  * @io_priority: the io priority of the request.
878  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
879  *
880  * Runs the asynchronous job in a separate thread and then calls
881  * g_simple_async_result_complete_in_idle() on @simple to return
882  * the result to the appropriate main loop.
883  *
884  * Calling this function takes a reference to @simple for as long as
885  * is needed to run the job and report its completion.
886  */
887 void
888 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
889                                      GSimpleAsyncThreadFunc  func,
890                                      int                     io_priority,
891                                      GCancellable           *cancellable)
892 {
893   RunInThreadData *data;
894
895   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
896   g_return_if_fail (func != NULL);
897
898   data = g_new (RunInThreadData, 1);
899   data->func = func;
900   data->simple = g_object_ref (simple);
901   data->cancellable = cancellable;
902   if (cancellable)
903     g_object_ref (cancellable);
904   G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
905   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
906   G_GNUC_END_IGNORE_DEPRECATIONS;
907 }
908
909 /**
910  * g_simple_async_result_is_valid:
911  * @result: the #GAsyncResult passed to the _finish function.
912  * @source: (allow-none): the #GObject passed to the _finish function.
913  * @source_tag: (allow-none): the asynchronous function.
914  *
915  * Ensures that the data passed to the _finish function of an async
916  * operation is consistent.  Three checks are performed.
917  *
918  * First, @result is checked to ensure that it is really a
919  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
920  * matches the source object of @result.  Third, @source_tag is
921  * checked to ensure that it is equal to the @source_tag argument given
922  * to g_simple_async_result_new() (which, by convention, is a pointer
923  * to the _async function corresponding to the _finish function from
924  * which this function is called).  (Alternatively, if either
925  * @source_tag or @result's source tag is %NULL, then the source tag
926  * check is skipped.)
927  *
928  * Returns: #TRUE if all checks passed or #FALSE if any failed.
929  *
930  * Since: 2.20
931  **/
932 gboolean
933 g_simple_async_result_is_valid (GAsyncResult *result,
934                                 GObject      *source,
935                                 gpointer      source_tag)
936 {
937   GSimpleAsyncResult *simple;
938   GObject *cmp_source;
939   gpointer result_source_tag;
940
941   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
942     return FALSE;
943   simple = (GSimpleAsyncResult *)result;
944
945   cmp_source = g_async_result_get_source_object (result);
946   if (cmp_source != source)
947     {
948       if (cmp_source != NULL)
949         g_object_unref (cmp_source);
950       return FALSE;
951     }
952   if (cmp_source != NULL)
953     g_object_unref (cmp_source);
954
955   result_source_tag = g_simple_async_result_get_source_tag (simple);
956   return source_tag == NULL || result_source_tag == NULL ||
957          source_tag == result_source_tag;
958 }
959
960 /**
961  * g_simple_async_report_error_in_idle: (skip)
962  * @object: (allow-none): a #GObject, or %NULL.
963  * @callback: a #GAsyncReadyCallback.
964  * @user_data: user data passed to @callback.
965  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
966  * @code: a specific error code.
967  * @format: a formatted error reporting string.
968  * @...: a list of variables to fill in @format.
969  *
970  * Reports an error in an asynchronous function in an idle function by
971  * directly setting the contents of the #GAsyncResult with the given error
972  * information.
973  **/
974 void
975 g_simple_async_report_error_in_idle (GObject             *object,
976                                      GAsyncReadyCallback  callback,
977                                      gpointer             user_data,
978                                      GQuark               domain,
979                                      gint                 code,
980                                      const char          *format,
981                                      ...)
982 {
983   GSimpleAsyncResult *simple;
984   va_list args;
985  
986   g_return_if_fail (!object || G_IS_OBJECT (object));
987   g_return_if_fail (domain != 0);
988   g_return_if_fail (format != NULL);
989
990   simple = g_simple_async_result_new (object,
991                                       callback,
992                                       user_data, NULL);
993
994   va_start (args, format);
995   g_simple_async_result_set_error_va (simple, domain, code, format, args);
996   va_end (args);
997   g_simple_async_result_complete_in_idle (simple);
998   g_object_unref (simple);
999 }
1000
1001 /**
1002  * g_simple_async_report_gerror_in_idle:
1003  * @object: (allow-none): a #GObject, or %NULL
1004  * @callback: (scope async): a #GAsyncReadyCallback.
1005  * @user_data: (closure): user data passed to @callback.
1006  * @error: the #GError to report
1007  *
1008  * Reports an error in an idle function. Similar to
1009  * g_simple_async_report_error_in_idle(), but takes a #GError rather
1010  * than building a new one.
1011  **/
1012 void
1013 g_simple_async_report_gerror_in_idle (GObject *object,
1014                                       GAsyncReadyCallback callback,
1015                                       gpointer user_data,
1016                                       const GError *error)
1017 {
1018   GSimpleAsyncResult *simple;
1019  
1020   g_return_if_fail (!object || G_IS_OBJECT (object));
1021   g_return_if_fail (error != NULL);
1022
1023   simple = g_simple_async_result_new_from_error (object,
1024                                                  callback,
1025                                                  user_data,
1026                                                  error);
1027   g_simple_async_result_complete_in_idle (simple);
1028   g_object_unref (simple);
1029 }
1030
1031 /**
1032  * g_simple_async_report_take_gerror_in_idle: (skip)
1033  * @object: (allow-none): a #GObject, or %NULL
1034  * @callback: a #GAsyncReadyCallback.
1035  * @user_data: user data passed to @callback.
1036  * @error: the #GError to report
1037  *
1038  * Reports an error in an idle function. Similar to
1039  * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1040  * ownership of @error, so the caller does not have to free it any more.
1041  *
1042  * Since: 2.28
1043  **/
1044 void
1045 g_simple_async_report_take_gerror_in_idle (GObject *object,
1046                                            GAsyncReadyCallback callback,
1047                                            gpointer user_data,
1048                                            GError *error)
1049 {
1050   GSimpleAsyncResult *simple;
1051
1052   g_return_if_fail (!object || G_IS_OBJECT (object));
1053   g_return_if_fail (error != NULL);
1054
1055   simple = g_simple_async_result_new_take_error (object,
1056                                                  callback,
1057                                                  user_data,
1058                                                  error);
1059   g_simple_async_result_complete_in_idle (simple);
1060   g_object_unref (simple);
1061 }
1062
1063 /**
1064  * g_simple_async_result_set_check_cancellable:
1065  * @simple: a #GSimpleAsyncResult
1066  * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1067  *
1068  * Sets a #GCancellable to check before dispatching results.
1069  *
1070  * This function has one very specific purpose: the provided cancellable
1071  * is checked at the time of g_simple_async_result_propagate_error() If
1072  * it is cancelled, these functions will return an "Operation was
1073  * cancelled" error (%G_IO_ERROR_CANCELLED).
1074  *
1075  * Implementors of cancellable asynchronous functions should use this in
1076  * order to provide a guarantee to their callers that cancelling an
1077  * async operation will reliably result in an error being returned for
1078  * that operation (even if a positive result for the operation has
1079  * already been sent as an idle to the main context to be dispatched).
1080  *
1081  * The checking described above is done regardless of any call to the
1082  * unrelated g_simple_async_result_set_handle_cancellation() function.
1083  *
1084  * Since: 2.32
1085  **/
1086 void
1087 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1088                                              GCancellable *check_cancellable)
1089 {
1090   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1091   g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1092
1093   g_clear_object (&simple->check_cancellable);
1094   if (check_cancellable)
1095     simple->check_cancellable = g_object_ref (check_cancellable);
1096 }