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