gkdbus: Fix underflow and unreachable code bug
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "gsimpleasyncresult.h"
28 #include "gasyncresult.h"
29 #include "gcancellable.h"
30 #include "gioscheduler.h"
31 #include <gio/gioerror.h>
32 #include "glibintl.h"
33
34
35 /**
36  * SECTION:gsimpleasyncresult
37  * @short_description: Simple asynchronous results implementation
38  * @include: gio/gio.h
39  * @see_also: #GAsyncResult, #GTask
40  *
41  * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
42  * #GTask, which provides a simpler API.
43  *
44  * #GSimpleAsyncResult implements #GAsyncResult.
45  *
46  * GSimpleAsyncResult handles #GAsyncReadyCallbacks, 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 #GThreads.
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 
88  * [thread-default main context][g-main-context-push-thread-default]
89  * where the #GSimpleAsyncResult was created.
90  * g_simple_async_result_run_in_thread() will run the job in a
91  * separate thread and then use
92  * g_simple_async_result_complete_in_idle() to deliver the result.
93  *
94  * To set the results of an asynchronous function,
95  * g_simple_async_result_set_op_res_gpointer(),
96  * g_simple_async_result_set_op_res_gboolean(), and
97  * g_simple_async_result_set_op_res_gssize()
98  * are provided, setting the operation's result to a gpointer, gboolean, or
99  * gssize, respectively.
100  *
101  * Likewise, to get the result of an asynchronous function,
102  * g_simple_async_result_get_op_res_gpointer(),
103  * g_simple_async_result_get_op_res_gboolean(), and
104  * g_simple_async_result_get_op_res_gssize() are
105  * provided, getting the operation's result as a gpointer, gboolean, and
106  * gssize, respectively.
107  *
108  * For the details of the requirements implementations must respect, see
109  * #GAsyncResult.  A typical implementation of an asynchronous operation
110  * using GSimpleAsyncResult looks something like this:
111  *
112  * |[<!-- language="C" -->
113  * static void
114  * baked_cb (Cake    *cake,
115  *           gpointer user_data)
116  * {
117  *   // In this example, this callback is not given a reference to the cake,
118  *   // so the GSimpleAsyncResult has to take a reference to it.
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  *   // In this example, we assume that baked_cb is called as a callback from
133  *   // the mainloop, so it's safe to complete the operation synchronously here.
134  *   // If, however, _baker_prepare_cake () might call its callback without
135  *   // first returning to the mainloop — inadvisable, but some APIs do so —
136  *   // we would need to use g_simple_async_result_complete_in_idle().
137  *   g_simple_async_result_complete (result);
138  *   g_object_unref (result);
139  * }
140  *
141  * void
142  * baker_bake_cake_async (Baker              *self,
143  *                        guint               radius,
144  *                        GAsyncReadyCallback callback,
145  *                        gpointer            user_data)
146  * {
147  *   GSimpleAsyncResult *simple;
148  *   Cake               *cake;
149  *
150  *   if (radius < 3)
151  *     {
152  *       g_simple_async_report_error_in_idle (G_OBJECT (self),
153  *                                            callback,
154  *                                            user_data,
155  *                                            BAKER_ERRORS,
156  *                                            BAKER_ERROR_TOO_SMALL,
157  *                                            "%ucm radius cakes are silly",
158  *                                            radius);
159  *       return;
160  *     }
161  *
162  *   simple = g_simple_async_result_new (G_OBJECT (self),
163  *                                       callback,
164  *                                       user_data,
165  *                                       baker_bake_cake_async);
166  *   cake = _baker_get_cached_cake (self, radius);
167  *
168  *   if (cake != NULL)
169  *     {
170  *       g_simple_async_result_set_op_res_gpointer (simple,
171  *                                                  g_object_ref (cake),
172  *                                                  g_object_unref);
173  *       g_simple_async_result_complete_in_idle (simple);
174  *       g_object_unref (simple);
175  *       // Drop the reference returned by _baker_get_cached_cake();
176  *       // the GSimpleAsyncResult has taken its own reference.
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 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
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: (nullable): a #GObject, or %NULL.
297  * @callback: (scope async): a #GAsyncReadyCallback.
298  * @user_data: 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  * Deprecated: 2.46: Use g_task_new() instead.
315  **/
316 GSimpleAsyncResult *
317 g_simple_async_result_new (GObject             *source_object,
318                            GAsyncReadyCallback  callback,
319                            gpointer             user_data,
320                            gpointer             source_tag)
321 {
322   GSimpleAsyncResult *simple;
323
324   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
325
326   simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
327   simple->callback = callback;
328   if (source_object)
329     simple->source_object = g_object_ref (source_object);
330   else
331     simple->source_object = NULL;
332   simple->user_data = user_data;
333   simple->source_tag = source_tag;
334  
335   return simple;
336 }
337
338 /**
339  * g_simple_async_result_new_from_error:
340  * @source_object: (nullable): a #GObject, or %NULL.
341  * @callback: (scope async): a #GAsyncReadyCallback.
342  * @user_data: user data passed to @callback.
343  * @error: a #GError
344  *
345  * Creates a #GSimpleAsyncResult from an error condition.
346  *
347  * Returns: a #GSimpleAsyncResult.
348  *
349  * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
350  **/
351 GSimpleAsyncResult *
352 g_simple_async_result_new_from_error (GObject             *source_object,
353                                       GAsyncReadyCallback  callback,
354                                       gpointer             user_data,
355                                       const GError        *error)
356 {
357   GSimpleAsyncResult *simple;
358
359   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
360
361   simple = g_simple_async_result_new (source_object,
362                                       callback,
363                                       user_data, NULL);
364   g_simple_async_result_set_from_error (simple, error);
365
366   return simple;
367 }
368
369 /**
370  * g_simple_async_result_new_take_error: (skip)
371  * @source_object: (nullable): a #GObject, or %NULL
372  * @callback: (scope async): a #GAsyncReadyCallback.
373  * @user_data: user data passed to @callback.
374  * @error: a #GError
375  *
376  * Creates a #GSimpleAsyncResult from an error condition, and takes over the
377  * caller's ownership of @error, so the caller does not need to free it anymore.
378  *
379  * Returns: a #GSimpleAsyncResult
380  *
381  * Since: 2.28
382  *
383  * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
384  **/
385 GSimpleAsyncResult *
386 g_simple_async_result_new_take_error (GObject             *source_object,
387                                       GAsyncReadyCallback  callback,
388                                       gpointer             user_data,
389                                       GError              *error)
390 {
391   GSimpleAsyncResult *simple;
392
393   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
394
395   simple = g_simple_async_result_new (source_object,
396                                       callback,
397                                       user_data, NULL);
398   g_simple_async_result_take_error (simple, error);
399
400   return simple;
401 }
402
403 /**
404  * g_simple_async_result_new_error:
405  * @source_object: (nullable): a #GObject, or %NULL.
406  * @callback: (scope async): a #GAsyncReadyCallback.
407  * @user_data: user data passed to @callback.
408  * @domain: a #GQuark.
409  * @code: an error code.
410  * @format: a string with format characters.
411  * @...: a list of values to insert into @format.
412  *
413  * Creates a new #GSimpleAsyncResult with a set error.
414  *
415  * Returns: a #GSimpleAsyncResult.
416  *
417  * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
418  **/
419 GSimpleAsyncResult *
420 g_simple_async_result_new_error (GObject             *source_object,
421                                  GAsyncReadyCallback  callback,
422                                  gpointer             user_data,
423                                  GQuark               domain,
424                                  gint                 code,
425                                  const char          *format,
426                                  ...)
427 {
428   GSimpleAsyncResult *simple;
429   va_list args;
430  
431   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
432   g_return_val_if_fail (domain != 0, NULL);
433   g_return_val_if_fail (format != NULL, NULL);
434
435   simple = g_simple_async_result_new (source_object,
436                                       callback,
437                                       user_data, NULL);
438
439   va_start (args, format);
440   g_simple_async_result_set_error_va (simple, domain, code, format, args);
441   va_end (args);
442  
443   return simple;
444 }
445
446
447 static gpointer
448 g_simple_async_result_get_user_data (GAsyncResult *res)
449 {
450   return G_SIMPLE_ASYNC_RESULT (res)->user_data;
451 }
452
453 static GObject *
454 g_simple_async_result_get_source_object (GAsyncResult *res)
455 {
456   if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
457     return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
458   return NULL;
459 }
460
461 static gboolean
462 g_simple_async_result_is_tagged (GAsyncResult *res,
463                                  gpointer      source_tag)
464 {
465   return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
466 }
467
468 static void
469 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
470 {
471   iface->get_user_data = g_simple_async_result_get_user_data;
472   iface->get_source_object = g_simple_async_result_get_source_object;
473   iface->is_tagged = g_simple_async_result_is_tagged;
474 }
475
476 /**
477  * g_simple_async_result_set_handle_cancellation:
478  * @simple: a #GSimpleAsyncResult.
479  * @handle_cancellation: a #gboolean.
480  *
481  * Sets whether to handle cancellation within the asynchronous operation.
482  *
483  * This function has nothing to do with
484  * g_simple_async_result_set_check_cancellable().  It only refers to the
485  * #GCancellable passed to g_simple_async_result_run_in_thread().
486  *
487  * Deprecated: 2.46
488  **/
489 void
490 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
491                                                gboolean            handle_cancellation)
492 {
493   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
494   simple->handle_cancellation = handle_cancellation;
495 }
496
497 /**
498  * g_simple_async_result_get_source_tag: (skip)
499  * @simple: a #GSimpleAsyncResult.
500  *
501  * Gets the source tag for the #GSimpleAsyncResult.
502  *
503  * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
504  *
505  * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
506  **/
507 gpointer
508 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
509 {
510   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
511   return simple->source_tag;
512 }
513
514 /**
515  * g_simple_async_result_propagate_error:
516  * @simple: a #GSimpleAsyncResult.
517  * @dest: (out): a location to propagate the error to.
518  *
519  * Propagates an error from within the simple asynchronous result to
520  * a given destination.
521  *
522  * If the #GCancellable given to a prior call to
523  * g_simple_async_result_set_check_cancellable() is cancelled then this
524  * function will return %TRUE with @dest set appropriately.
525  *
526  * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
527  *
528  * Deprecated: 2.46: Use #GTask instead.
529  **/
530 gboolean
531 g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
532                                        GError             **dest)
533 {
534   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
535
536   if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
537     return TRUE;
538
539   if (simple->failed)
540     {
541       g_propagate_error (dest, simple->error);
542       simple->error = NULL;
543       return TRUE;
544     }
545
546   return FALSE;
547 }
548
549 /**
550  * g_simple_async_result_set_op_res_gpointer: (skip)
551  * @simple: a #GSimpleAsyncResult.
552  * @op_res: a pointer result from an asynchronous function.
553  * @destroy_op_res: a #GDestroyNotify function.
554  *
555  * Sets the operation result within the asynchronous result to a pointer.
556  *
557  * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
558  **/
559 void
560 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
561                                            gpointer            op_res,
562                                            GDestroyNotify      destroy_op_res)
563 {
564   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
565
566   clear_op_res (simple);
567   simple->op_res.v_pointer = op_res;
568   simple->destroy_op_res = destroy_op_res;
569 }
570
571 /**
572  * g_simple_async_result_get_op_res_gpointer: (skip)
573  * @simple: a #GSimpleAsyncResult.
574  *
575  * Gets a pointer result as returned by the asynchronous function.
576  *
577  * Returns: a pointer from the result.
578  *
579  * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
580  **/
581 gpointer
582 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
583 {
584   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
585   return simple->op_res.v_pointer;
586 }
587
588 /**
589  * g_simple_async_result_set_op_res_gssize:
590  * @simple: a #GSimpleAsyncResult.
591  * @op_res: a #gssize.
592  *
593  * Sets the operation result within the asynchronous result to
594  * the given @op_res.
595  *
596  * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
597  **/
598 void
599 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
600                                          gssize              op_res)
601 {
602   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
603   clear_op_res (simple);
604   simple->op_res.v_ssize = op_res;
605 }
606
607 /**
608  * g_simple_async_result_get_op_res_gssize:
609  * @simple: a #GSimpleAsyncResult.
610  *
611  * Gets a gssize from the asynchronous result.
612  *
613  * Returns: a gssize returned from the asynchronous function.
614  *
615  * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
616  **/
617 gssize
618 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
619 {
620   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
621   return simple->op_res.v_ssize;
622 }
623
624 /**
625  * g_simple_async_result_set_op_res_gboolean:
626  * @simple: a #GSimpleAsyncResult.
627  * @op_res: a #gboolean.
628  *
629  * Sets the operation result to a boolean within the asynchronous result.
630  *
631  * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
632  **/
633 void
634 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
635                                            gboolean            op_res)
636 {
637   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
638   clear_op_res (simple);
639   simple->op_res.v_boolean = !!op_res;
640 }
641
642 /**
643  * g_simple_async_result_get_op_res_gboolean:
644  * @simple: a #GSimpleAsyncResult.
645  *
646  * Gets the operation result boolean from within the asynchronous result.
647  *
648  * Returns: %TRUE if the operation's result was %TRUE, %FALSE
649  *     if the operation's result was %FALSE.
650  *
651  * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
652  **/
653 gboolean
654 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
655 {
656   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
657   return simple->op_res.v_boolean;
658 }
659
660 /**
661  * g_simple_async_result_set_from_error:
662  * @simple: a #GSimpleAsyncResult.
663  * @error: #GError.
664  *
665  * Sets the result from a #GError.
666  *
667  * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
668  **/
669 void
670 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
671                                       const GError       *error)
672 {
673   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
674   g_return_if_fail (error != NULL);
675
676   if (simple->error)
677     g_error_free (simple->error);
678   simple->error = g_error_copy (error);
679   simple->failed = TRUE;
680 }
681
682 /**
683  * g_simple_async_result_take_error: (skip)
684  * @simple: a #GSimpleAsyncResult
685  * @error: a #GError
686  *
687  * Sets the result from @error, and takes over the caller's ownership
688  * of @error, so the caller does not need to free it any more.
689  *
690  * Since: 2.28
691  *
692  * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
693  **/
694 void
695 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
696                                   GError             *error)
697 {
698   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
699   g_return_if_fail (error != NULL);
700
701   if (simple->error)
702     g_error_free (simple->error);
703   simple->error = error;
704   simple->failed = TRUE;
705 }
706
707 /**
708  * g_simple_async_result_set_error_va: (skip)
709  * @simple: a #GSimpleAsyncResult.
710  * @domain: a #GQuark (usually %G_IO_ERROR).
711  * @code: an error code.
712  * @format: a formatted error reporting string.
713  * @args: va_list of arguments.
714  *
715  * Sets an error within the asynchronous result without a #GError.
716  * Unless writing a binding, see g_simple_async_result_set_error().
717  *
718  * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
719  **/
720 void
721 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
722                                     GQuark              domain,
723                                     gint                code,
724                                     const char         *format,
725                                     va_list             args)
726 {
727   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
728   g_return_if_fail (domain != 0);
729   g_return_if_fail (format != NULL);
730
731   if (simple->error)
732     g_error_free (simple->error);
733   simple->error = g_error_new_valist (domain, code, format, args);
734   simple->failed = TRUE;
735 }
736
737 /**
738  * g_simple_async_result_set_error: (skip)
739  * @simple: a #GSimpleAsyncResult.
740  * @domain: a #GQuark (usually %G_IO_ERROR).
741  * @code: an error code.
742  * @format: a formatted error reporting string.
743  * @...: a list of variables to fill in @format.
744  *
745  * Sets an error within the asynchronous result without a #GError.
746  *
747  * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
748  **/
749 void
750 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
751                                  GQuark              domain,
752                                  gint                code,
753                                  const char         *format,
754                                  ...)
755 {
756   va_list args;
757
758   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
759   g_return_if_fail (domain != 0);
760   g_return_if_fail (format != NULL);
761
762   va_start (args, format);
763   g_simple_async_result_set_error_va (simple, domain, code, format, args);
764   va_end (args);
765 }
766
767 /**
768  * g_simple_async_result_complete:
769  * @simple: a #GSimpleAsyncResult.
770  *
771  * Completes an asynchronous I/O job immediately. Must be called in
772  * the thread where the asynchronous result was to be delivered, as it
773  * invokes the callback directly. If you are in a different thread use
774  * g_simple_async_result_complete_in_idle().
775  *
776  * Calling this function takes a reference to @simple for as long as
777  * is needed to complete the call.
778  *
779  * Deprecated: 2.46: Use #GTask instead.
780  **/
781 void
782 g_simple_async_result_complete (GSimpleAsyncResult *simple)
783 {
784 #ifndef G_DISABLE_CHECKS
785   GSource *current_source;
786   GMainContext *current_context;
787 #endif
788
789   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
790
791 #ifndef G_DISABLE_CHECKS
792   current_source = g_main_current_source ();
793   if (current_source && !g_source_is_destroyed (current_source))
794     {
795       current_context = g_source_get_context (current_source);
796       if (simple->context != current_context)
797         g_warning ("g_simple_async_result_complete() called from wrong context!");
798     }
799 #endif
800
801   if (simple->callback)
802     {
803       g_main_context_push_thread_default (simple->context);
804       simple->callback (simple->source_object,
805                         G_ASYNC_RESULT (simple),
806                         simple->user_data);
807       g_main_context_pop_thread_default (simple->context);
808     }
809 }
810
811 static gboolean
812 complete_in_idle_cb (gpointer data)
813 {
814   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
815
816   g_simple_async_result_complete (simple);
817
818   return FALSE;
819 }
820
821 /**
822  * g_simple_async_result_complete_in_idle:
823  * @simple: a #GSimpleAsyncResult.
824  *
825  * Completes an asynchronous function in an idle handler in the
826  * [thread-default main context][g-main-context-push-thread-default]
827  * of the thread that @simple was initially created in
828  * (and re-pushes that context around the invocation of the callback).
829  *
830  * Calling this function takes a reference to @simple for as long as
831  * is needed to complete the call.
832  *
833  * Deprecated: 2.46: Use #GTask instead.
834  */
835 void
836 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
837 {
838   GSource *source;
839
840   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
841
842   g_object_ref (simple);
843  
844   source = g_idle_source_new ();
845   g_source_set_priority (source, G_PRIORITY_DEFAULT);
846   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
847   g_source_set_static_name (source, "[gio] complete_in_idle_cb");
848
849   g_source_attach (source, simple->context);
850   g_source_unref (source);
851 }
852
853 typedef struct {
854   GSimpleAsyncResult *simple;
855   GCancellable *cancellable;
856   GSimpleAsyncThreadFunc func;
857 } RunInThreadData;
858
859
860 static gboolean
861 complete_in_idle_cb_for_thread (gpointer _data)
862 {
863   RunInThreadData *data = _data;
864   GSimpleAsyncResult *simple;
865
866   simple = data->simple;
867  
868   if (simple->handle_cancellation &&
869       g_cancellable_is_cancelled (data->cancellable))
870     g_simple_async_result_set_error (simple,
871                                      G_IO_ERROR,
872                                      G_IO_ERROR_CANCELLED,
873                                      "%s", _("Operation was cancelled"));
874  
875   g_simple_async_result_complete (simple);
876
877   if (data->cancellable)
878     g_object_unref (data->cancellable);
879   g_object_unref (data->simple);
880   g_free (data);
881  
882   return FALSE;
883 }
884
885 static gboolean
886 run_in_thread (GIOSchedulerJob *job,
887                GCancellable    *c,
888                gpointer         _data)
889 {
890   RunInThreadData *data = _data;
891   GSimpleAsyncResult *simple = data->simple;
892   GSource *source;
893  
894   if (simple->handle_cancellation &&
895       g_cancellable_is_cancelled (c))
896     g_simple_async_result_set_error (simple,
897                                      G_IO_ERROR,
898                                      G_IO_ERROR_CANCELLED,
899                                      "%s", _("Operation was cancelled"));
900   else
901     data->func (simple,
902                 simple->source_object,
903                 c);
904
905   source = g_idle_source_new ();
906   g_source_set_priority (source, G_PRIORITY_DEFAULT);
907   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
908   g_source_set_static_name (source, "[gio] complete_in_idle_cb_for_thread");
909
910   g_source_attach (source, simple->context);
911   g_source_unref (source);
912
913   return FALSE;
914 }
915
916 /**
917  * g_simple_async_result_run_in_thread: (skip)
918  * @simple: a #GSimpleAsyncResult.
919  * @func: a #GSimpleAsyncThreadFunc.
920  * @io_priority: the io priority of the request.
921  * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
922  *
923  * Runs the asynchronous job in a separate thread and then calls
924  * g_simple_async_result_complete_in_idle() on @simple to return
925  * the result to the appropriate main loop.
926  *
927  * Calling this function takes a reference to @simple for as long as
928  * is needed to run the job and report its completion.
929  *
930  * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
931  */
932 void
933 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
934                                      GSimpleAsyncThreadFunc  func,
935                                      int                     io_priority,
936                                      GCancellable           *cancellable)
937 {
938   RunInThreadData *data;
939
940   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
941   g_return_if_fail (func != NULL);
942
943   data = g_new (RunInThreadData, 1);
944   data->func = func;
945   data->simple = g_object_ref (simple);
946   data->cancellable = cancellable;
947   if (cancellable)
948     g_object_ref (cancellable);
949   G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
950   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
951   G_GNUC_END_IGNORE_DEPRECATIONS;
952 }
953
954 /**
955  * g_simple_async_result_is_valid:
956  * @result: the #GAsyncResult passed to the _finish function.
957  * @source: (nullable): the #GObject passed to the _finish function.
958  * @source_tag: (nullable): the asynchronous function.
959  *
960  * Ensures that the data passed to the _finish function of an async
961  * operation is consistent.  Three checks are performed.
962  *
963  * First, @result is checked to ensure that it is really a
964  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
965  * matches the source object of @result.  Third, @source_tag is
966  * checked to ensure that it is equal to the @source_tag argument given
967  * to g_simple_async_result_new() (which, by convention, is a pointer
968  * to the _async function corresponding to the _finish function from
969  * which this function is called).  (Alternatively, if either
970  * @source_tag or @result's source tag is %NULL, then the source tag
971  * check is skipped.)
972  *
973  * Returns: #TRUE if all checks passed or #FALSE if any failed.
974  *
975  * Since: 2.20
976  *
977  * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
978  **/
979 gboolean
980 g_simple_async_result_is_valid (GAsyncResult *result,
981                                 GObject      *source,
982                                 gpointer      source_tag)
983 {
984   GSimpleAsyncResult *simple;
985   GObject *cmp_source;
986   gpointer result_source_tag;
987
988   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
989     return FALSE;
990   simple = (GSimpleAsyncResult *)result;
991
992   cmp_source = g_async_result_get_source_object (result);
993   if (cmp_source != source)
994     {
995       if (cmp_source != NULL)
996         g_object_unref (cmp_source);
997       return FALSE;
998     }
999   if (cmp_source != NULL)
1000     g_object_unref (cmp_source);
1001
1002   result_source_tag = g_simple_async_result_get_source_tag (simple);
1003   return source_tag == NULL || result_source_tag == NULL ||
1004          source_tag == result_source_tag;
1005 }
1006
1007 /**
1008  * g_simple_async_report_error_in_idle: (skip)
1009  * @object: (nullable): a #GObject, or %NULL.
1010  * @callback: a #GAsyncReadyCallback.
1011  * @user_data: user data passed to @callback.
1012  * @domain: a #GQuark containing the error domain (usually %G_IO_ERROR).
1013  * @code: a specific error code.
1014  * @format: a formatted error reporting string.
1015  * @...: a list of variables to fill in @format.
1016  *
1017  * Reports an error in an asynchronous function in an idle function by
1018  * directly setting the contents of the #GAsyncResult with the given error
1019  * information.
1020  *
1021  * Deprecated: 2.46: Use g_task_report_error().
1022  **/
1023 void
1024 g_simple_async_report_error_in_idle (GObject             *object,
1025                                      GAsyncReadyCallback  callback,
1026                                      gpointer             user_data,
1027                                      GQuark               domain,
1028                                      gint                 code,
1029                                      const char          *format,
1030                                      ...)
1031 {
1032   GSimpleAsyncResult *simple;
1033   va_list args;
1034  
1035   g_return_if_fail (!object || G_IS_OBJECT (object));
1036   g_return_if_fail (domain != 0);
1037   g_return_if_fail (format != NULL);
1038
1039   simple = g_simple_async_result_new (object,
1040                                       callback,
1041                                       user_data, NULL);
1042
1043   va_start (args, format);
1044   g_simple_async_result_set_error_va (simple, domain, code, format, args);
1045   va_end (args);
1046   g_simple_async_result_complete_in_idle (simple);
1047   g_object_unref (simple);
1048 }
1049
1050 /**
1051  * g_simple_async_report_gerror_in_idle:
1052  * @object: (nullable): a #GObject, or %NULL
1053  * @callback: (scope async): a #GAsyncReadyCallback.
1054  * @user_data: user data passed to @callback.
1055  * @error: the #GError to report
1056  *
1057  * Reports an error in an idle function. Similar to
1058  * g_simple_async_report_error_in_idle(), but takes a #GError rather
1059  * than building a new one.
1060  *
1061  * Deprecated: 2.46: Use g_task_report_error().
1062  **/
1063 void
1064 g_simple_async_report_gerror_in_idle (GObject *object,
1065                                       GAsyncReadyCallback callback,
1066                                       gpointer user_data,
1067                                       const GError *error)
1068 {
1069   GSimpleAsyncResult *simple;
1070  
1071   g_return_if_fail (!object || G_IS_OBJECT (object));
1072   g_return_if_fail (error != NULL);
1073
1074   simple = g_simple_async_result_new_from_error (object,
1075                                                  callback,
1076                                                  user_data,
1077                                                  error);
1078   g_simple_async_result_complete_in_idle (simple);
1079   g_object_unref (simple);
1080 }
1081
1082 /**
1083  * g_simple_async_report_take_gerror_in_idle: (skip)
1084  * @object: (nullable): a #GObject, or %NULL
1085  * @callback: a #GAsyncReadyCallback.
1086  * @user_data: user data passed to @callback.
1087  * @error: the #GError to report
1088  *
1089  * Reports an error in an idle function. Similar to
1090  * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1091  * ownership of @error, so the caller does not have to free it any more.
1092  *
1093  * Since: 2.28
1094  *
1095  * Deprecated: 2.46: Use g_task_report_error().
1096  **/
1097 void
1098 g_simple_async_report_take_gerror_in_idle (GObject *object,
1099                                            GAsyncReadyCallback callback,
1100                                            gpointer user_data,
1101                                            GError *error)
1102 {
1103   GSimpleAsyncResult *simple;
1104
1105   g_return_if_fail (!object || G_IS_OBJECT (object));
1106   g_return_if_fail (error != NULL);
1107
1108   simple = g_simple_async_result_new_take_error (object,
1109                                                  callback,
1110                                                  user_data,
1111                                                  error);
1112   g_simple_async_result_complete_in_idle (simple);
1113   g_object_unref (simple);
1114 }
1115
1116 /**
1117  * g_simple_async_result_set_check_cancellable:
1118  * @simple: a #GSimpleAsyncResult
1119  * @check_cancellable: (nullable): a #GCancellable to check, or %NULL to unset
1120  *
1121  * Sets a #GCancellable to check before dispatching results.
1122  *
1123  * This function has one very specific purpose: the provided cancellable
1124  * is checked at the time of g_simple_async_result_propagate_error() If
1125  * it is cancelled, these functions will return an "Operation was
1126  * cancelled" error (%G_IO_ERROR_CANCELLED).
1127  *
1128  * Implementors of cancellable asynchronous functions should use this in
1129  * order to provide a guarantee to their callers that cancelling an
1130  * async operation will reliably result in an error being returned for
1131  * that operation (even if a positive result for the operation has
1132  * already been sent as an idle to the main context to be dispatched).
1133  *
1134  * The checking described above is done regardless of any call to the
1135  * unrelated g_simple_async_result_set_handle_cancellation() function.
1136  *
1137  * Since: 2.32
1138  *
1139  * Deprecated: 2.46: Use #GTask instead.
1140  **/
1141 void
1142 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1143                                              GCancellable *check_cancellable)
1144 {
1145   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1146   g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1147
1148   g_clear_object (&simple->check_cancellable);
1149   if (check_cancellable)
1150     simple->check_cancellable = g_object_ref (check_cancellable);
1151 }
1152
1153 G_GNUC_END_IGNORE_DEPRECATIONS