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