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