1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
34 #include "gsimpleasyncresult.h"
35 #include "gasyncresult.h"
36 #include "gcancellable.h"
37 #include "gioscheduler.h"
38 #include <gio/gioerror.h>
43 * SECTION:gsimpleasyncresult
44 * @short_description: Simple asynchronous results implementation
46 * @see_also: #GAsyncResult
48 * Implements #GAsyncResult for simple cases. Most of the time, this
49 * will be all an application needs, and will be used transparently.
50 * Because of this, #GSimpleAsyncResult is used throughout GIO for
51 * handling asynchronous functions.
53 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
54 * reporting, operation cancellation and the final state of an operation,
55 * completely transparent to the application. Results can be returned
56 * as a pointer e.g. for functions that return data that is collected
57 * asynchronously, a boolean value for checking the success or failure
58 * of an operation, or a #gssize for operations which return the number
59 * of bytes modified by the operation; all of the simple return cases
62 * Most of the time, an application will not need to know of the details
63 * of this API; it is handled transparently, and any necessary operations
64 * are handled by #GAsyncResult's interface. However, if implementing a
65 * new GIO module, for writing language bindings, or for complex
66 * applications that need better control of how asynchronous operations
67 * are completed, it is important to understand this functionality.
69 * GSimpleAsyncResults are tagged with the calling function to ensure
70 * that asynchronous functions and their finishing functions are used
73 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
74 * If the result needs to be created for a #GError, use
75 * g_simple_async_result_new_from_error() or
76 * g_simple_async_result_new_take_error(). If a #GError is not available
77 * (e.g. the asynchronous operation's doesn't take a #GError argument),
78 * but the result still needs to be created for an error condition, use
79 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
80 * if your application or binding requires passing a variable argument list
81 * directly), and the error can then be propagated through the use of
82 * g_simple_async_result_propagate_error().
84 * An asynchronous operation can be made to ignore a cancellation event by
85 * calling g_simple_async_result_set_handle_cancellation() with a
86 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
87 * operations that are dangerous to cancel, such as close (which would
88 * cause a leak if cancelled before being run).
90 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
91 * or it can use #GThread<!-- -->s if available.
92 * g_simple_async_result_complete() will finish an I/O task directly
93 * from the point where it is called. g_simple_async_result_complete_in_idle()
94 * will finish it from an idle handler in the <link
95 * linkend="g-main-context-push-thread-default">thread-default main
96 * context</link>. g_simple_async_result_run_in_thread() will run the
97 * job in a separate thread and then deliver the result to the
98 * thread-default main context.
100 * To set the results of an asynchronous function,
101 * g_simple_async_result_set_op_res_gpointer(),
102 * g_simple_async_result_set_op_res_gboolean(), and
103 * g_simple_async_result_set_op_res_gssize()
104 * are provided, setting the operation's result to a gpointer, gboolean, or
105 * gssize, respectively.
107 * Likewise, to get the result of an asynchronous function,
108 * g_simple_async_result_get_op_res_gpointer(),
109 * g_simple_async_result_get_op_res_gboolean(), and
110 * g_simple_async_result_get_op_res_gssize() are
111 * provided, getting the operation's result as a gpointer, gboolean, and
112 * gssize, respectively.
114 * For the details of the requirements implementations must respect, see
115 * #GAsyncResult. A typical implementation of an asynchronous operation
116 * using GSimpleAsyncResult looks something like this:
120 * baked_cb (Cake *cake,
121 * gpointer user_data)
123 * /* In this example, this callback is not given a reference to the cake, so
124 * * the GSimpleAsyncResult has to take a reference to it.
126 * GSimpleAsyncResult *result = user_data;
129 * g_simple_async_result_set_error (result,
131 * BAKER_ERROR_NO_FLOUR,
132 * "Go to the supermarket");
134 * g_simple_async_result_set_op_res_gpointer (result,
135 * g_object_ref (cake),
139 * /* In this example, we assume that baked_cb is called as a callback from
140 * * the mainloop, so it's safe to complete the operation synchronously here.
141 * * If, however, _baker_prepare_cake () might call its callback without
142 * * first returning to the mainloop — inadvisable, but some APIs do so —
143 * * we would need to use g_simple_async_result_complete_in_idle().
145 * g_simple_async_result_complete (result);
146 * g_object_unref (result);
150 * baker_bake_cake_async (Baker *self,
152 * GAsyncReadyCallback callback,
153 * gpointer user_data)
155 * GSimpleAsyncResult *simple;
160 * g_simple_async_report_error_in_idle (G_OBJECT (self),
164 * BAKER_ERROR_TOO_SMALL,
165 * "%ucm radius cakes are silly",
170 * simple = g_simple_async_result_new (G_OBJECT (self),
173 * baker_bake_cake_async);
174 * cake = _baker_get_cached_cake (self, radius);
178 * g_simple_async_result_set_op_res_gpointer (simple,
179 * g_object_ref (cake),
181 * g_simple_async_result_complete_in_idle (simple);
182 * g_object_unref (simple);
183 * /* Drop the reference returned by _baker_get_cached_cake(); the
184 * * GSimpleAsyncResult has taken its own reference.
186 * g_object_unref (cake);
190 * _baker_prepare_cake (self, radius, baked_cb, simple);
194 * baker_bake_cake_finish (Baker *self,
195 * GAsyncResult *result,
198 * GSimpleAsyncResult *simple;
201 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
203 * baker_bake_cake_async),
206 * simple = (GSimpleAsyncResult *) result;
208 * if (g_simple_async_result_propagate_error (simple, error))
211 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
212 * return g_object_ref (cake);
217 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
219 struct _GSimpleAsyncResult
221 GObject parent_instance;
223 GObject *source_object;
224 GAsyncReadyCallback callback;
226 GMainContext *context;
229 gboolean handle_cancellation;
239 GDestroyNotify destroy_op_res;
242 struct _GSimpleAsyncResultClass
244 GObjectClass parent_class;
248 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
249 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
250 g_simple_async_result_async_result_iface_init))
253 clear_op_res (GSimpleAsyncResult *simple)
255 if (simple->destroy_op_res)
256 simple->destroy_op_res (simple->op_res.v_pointer);
257 simple->destroy_op_res = NULL;
258 simple->op_res.v_ssize = 0;
262 g_simple_async_result_finalize (GObject *object)
264 GSimpleAsyncResult *simple;
266 simple = G_SIMPLE_ASYNC_RESULT (object);
268 if (simple->source_object)
269 g_object_unref (simple->source_object);
272 g_main_context_unref (simple->context);
274 clear_op_res (simple);
277 g_error_free (simple->error);
279 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
283 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
285 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
287 gobject_class->finalize = g_simple_async_result_finalize;
291 g_simple_async_result_init (GSimpleAsyncResult *simple)
293 simple->handle_cancellation = TRUE;
295 simple->context = g_main_context_get_thread_default ();
297 g_main_context_ref (simple->context);
301 * g_simple_async_result_new:
302 * @source_object: (allow-none): a #GObject, or %NULL.
303 * @callback: (scope async): a #GAsyncReadyCallback.
304 * @user_data: (closure): user data passed to @callback.
305 * @source_tag: the asynchronous function.
307 * Creates a #GSimpleAsyncResult.
309 * Returns: a #GSimpleAsyncResult.
312 g_simple_async_result_new (GObject *source_object,
313 GAsyncReadyCallback callback,
317 GSimpleAsyncResult *simple;
319 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
321 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
322 simple->callback = callback;
324 simple->source_object = g_object_ref (source_object);
326 simple->source_object = NULL;
327 simple->user_data = user_data;
328 simple->source_tag = source_tag;
334 * g_simple_async_result_new_from_error:
335 * @source_object: (allow-none): a #GObject, or %NULL.
336 * @callback: (scope async): a #GAsyncReadyCallback.
337 * @user_data: (closure): user data passed to @callback.
340 * Creates a #GSimpleAsyncResult from an error condition.
342 * Returns: a #GSimpleAsyncResult.
345 g_simple_async_result_new_from_error (GObject *source_object,
346 GAsyncReadyCallback callback,
350 GSimpleAsyncResult *simple;
352 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
354 simple = g_simple_async_result_new (source_object,
357 g_simple_async_result_set_from_error (simple, error);
363 * g_simple_async_result_new_take_error: (skip)
364 * @source_object: (allow-none): a #GObject, or %NULL
365 * @callback: (scope async): a #GAsyncReadyCallback
366 * @user_data: (closure): user data passed to @callback
369 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
370 * caller's ownership of @error, so the caller does not need to free it anymore.
372 * Returns: a #GSimpleAsyncResult
377 g_simple_async_result_new_take_error (GObject *source_object,
378 GAsyncReadyCallback callback,
382 GSimpleAsyncResult *simple;
384 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
386 simple = g_simple_async_result_new (source_object,
389 g_simple_async_result_take_error (simple, error);
395 * g_simple_async_result_new_error:
396 * @source_object: (allow-none): a #GObject, or %NULL.
397 * @callback: (scope async): a #GAsyncReadyCallback.
398 * @user_data: (closure): user data passed to @callback.
399 * @domain: a #GQuark.
400 * @code: an error code.
401 * @format: a string with format characters.
402 * @...: a list of values to insert into @format.
404 * Creates a new #GSimpleAsyncResult with a set error.
406 * Returns: a #GSimpleAsyncResult.
409 g_simple_async_result_new_error (GObject *source_object,
410 GAsyncReadyCallback callback,
417 GSimpleAsyncResult *simple;
420 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
421 g_return_val_if_fail (domain != 0, NULL);
422 g_return_val_if_fail (format != NULL, NULL);
424 simple = g_simple_async_result_new (source_object,
428 va_start (args, format);
429 g_simple_async_result_set_error_va (simple, domain, code, format, args);
437 g_simple_async_result_get_user_data (GAsyncResult *res)
439 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
443 g_simple_async_result_get_source_object (GAsyncResult *res)
445 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
446 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
451 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
453 iface->get_user_data = g_simple_async_result_get_user_data;
454 iface->get_source_object = g_simple_async_result_get_source_object;
458 * g_simple_async_result_set_handle_cancellation:
459 * @simple: a #GSimpleAsyncResult.
460 * @handle_cancellation: a #gboolean.
462 * Sets whether to handle cancellation within the asynchronous operation.
466 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
467 gboolean handle_cancellation)
469 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
470 simple->handle_cancellation = handle_cancellation;
474 * g_simple_async_result_get_source_tag: (skip)
475 * @simple: a #GSimpleAsyncResult.
477 * Gets the source tag for the #GSimpleAsyncResult.
479 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
482 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
484 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
485 return simple->source_tag;
489 * g_simple_async_result_propagate_error:
490 * @simple: a #GSimpleAsyncResult.
491 * @dest: (out): a location to propagate the error to.
493 * Propagates an error from within the simple asynchronous result to
494 * a given destination.
496 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
499 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
502 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
506 g_propagate_error (dest, simple->error);
507 simple->error = NULL;
515 * g_simple_async_result_set_op_res_gpointer: (skip)
516 * @simple: a #GSimpleAsyncResult.
517 * @op_res: a pointer result from an asynchronous function.
518 * @destroy_op_res: a #GDestroyNotify function.
520 * Sets the operation result within the asynchronous result to a pointer.
523 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
525 GDestroyNotify destroy_op_res)
527 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
529 clear_op_res (simple);
530 simple->op_res.v_pointer = op_res;
531 simple->destroy_op_res = destroy_op_res;
535 * g_simple_async_result_get_op_res_gpointer: (skip)
536 * @simple: a #GSimpleAsyncResult.
538 * Gets a pointer result as returned by the asynchronous function.
540 * Returns: a pointer from the result.
543 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
545 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
546 return simple->op_res.v_pointer;
550 * g_simple_async_result_set_op_res_gssize:
551 * @simple: a #GSimpleAsyncResult.
552 * @op_res: a #gssize.
554 * Sets the operation result within the asynchronous result to
558 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
561 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
562 clear_op_res (simple);
563 simple->op_res.v_ssize = op_res;
567 * g_simple_async_result_get_op_res_gssize:
568 * @simple: a #GSimpleAsyncResult.
570 * Gets a gssize from the asynchronous result.
572 * Returns: a gssize returned from the asynchronous function.
575 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
577 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
578 return simple->op_res.v_ssize;
582 * g_simple_async_result_set_op_res_gboolean:
583 * @simple: a #GSimpleAsyncResult.
584 * @op_res: a #gboolean.
586 * Sets the operation result to a boolean within the asynchronous result.
589 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
592 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
593 clear_op_res (simple);
594 simple->op_res.v_boolean = !!op_res;
598 * g_simple_async_result_get_op_res_gboolean:
599 * @simple: a #GSimpleAsyncResult.
601 * Gets the operation result boolean from within the asynchronous result.
603 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
604 * if the operation's result was %FALSE.
607 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
609 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
610 return simple->op_res.v_boolean;
614 * g_simple_async_result_set_from_error:
615 * @simple: a #GSimpleAsyncResult.
618 * Sets the result from a #GError.
621 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
624 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
625 g_return_if_fail (error != NULL);
628 g_error_free (simple->error);
629 simple->error = g_error_copy (error);
630 simple->failed = TRUE;
634 * g_simple_async_result_take_error: (skip)
635 * @simple: a #GSimpleAsyncResult
638 * Sets the result from @error, and takes over the caller's ownership
639 * of @error, so the caller does not need to free it any more.
644 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
647 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
648 g_return_if_fail (error != NULL);
651 g_error_free (simple->error);
652 simple->error = error;
653 simple->failed = TRUE;
657 * g_simple_async_result_set_error_va: (skip)
658 * @simple: a #GSimpleAsyncResult.
659 * @domain: a #GQuark (usually #G_IO_ERROR).
660 * @code: an error code.
661 * @format: a formatted error reporting string.
662 * @args: va_list of arguments.
664 * Sets an error within the asynchronous result without a #GError.
665 * Unless writing a binding, see g_simple_async_result_set_error().
668 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
674 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
675 g_return_if_fail (domain != 0);
676 g_return_if_fail (format != NULL);
679 g_error_free (simple->error);
680 simple->error = g_error_new_valist (domain, code, format, args);
681 simple->failed = TRUE;
685 * g_simple_async_result_set_error: (skip)
686 * @simple: a #GSimpleAsyncResult.
687 * @domain: a #GQuark (usually #G_IO_ERROR).
688 * @code: an error code.
689 * @format: a formatted error reporting string.
690 * @...: a list of variables to fill in @format.
692 * Sets an error within the asynchronous result without a #GError.
695 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
703 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
704 g_return_if_fail (domain != 0);
705 g_return_if_fail (format != NULL);
707 va_start (args, format);
708 g_simple_async_result_set_error_va (simple, domain, code, format, args);
713 * g_simple_async_result_complete:
714 * @simple: a #GSimpleAsyncResult.
716 * Completes an asynchronous I/O job immediately. Must be called in
717 * the thread where the asynchronous result was to be delivered, as it
718 * invokes the callback directly. If you are in a different thread use
719 * g_simple_async_result_complete_in_idle().
721 * Calling this function takes a reference to @simple for as long as
722 * is needed to complete the call.
725 g_simple_async_result_complete (GSimpleAsyncResult *simple)
727 #ifndef G_DISABLE_CHECKS
728 GSource *current_source;
729 GMainContext *current_context;
732 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
734 #ifndef G_DISABLE_CHECKS
735 current_source = g_main_current_source ();
736 if (current_source && !g_source_is_destroyed (current_source))
738 current_context = g_source_get_context (current_source);
739 if (current_context == g_main_context_default ())
740 current_context = NULL;
741 if (simple->context != current_context)
742 g_warning ("g_simple_async_result_complete() called from wrong context!");
746 if (simple->callback)
748 g_main_context_push_thread_default (simple->context);
749 simple->callback (simple->source_object,
750 G_ASYNC_RESULT (simple),
752 g_main_context_pop_thread_default (simple->context);
757 complete_in_idle_cb (gpointer data)
759 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
761 g_simple_async_result_complete (simple);
767 * g_simple_async_result_complete_in_idle:
768 * @simple: a #GSimpleAsyncResult.
770 * Completes an asynchronous function in an idle handler in the <link
771 * linkend="g-main-context-push-thread-default">thread-default main
772 * loop</link> of the thread that @simple was initially created in.
774 * Calling this function takes a reference to @simple for as long as
775 * is needed to complete the call.
778 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
782 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
784 g_object_ref (simple);
786 source = g_idle_source_new ();
787 g_source_set_priority (source, G_PRIORITY_DEFAULT);
788 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
790 g_source_attach (source, simple->context);
791 g_source_unref (source);
795 GSimpleAsyncResult *simple;
796 GCancellable *cancellable;
797 GSimpleAsyncThreadFunc func;
802 complete_in_idle_cb_for_thread (gpointer _data)
804 RunInThreadData *data = _data;
805 GSimpleAsyncResult *simple;
807 simple = data->simple;
809 if (simple->handle_cancellation &&
810 g_cancellable_is_cancelled (data->cancellable))
811 g_simple_async_result_set_error (simple,
813 G_IO_ERROR_CANCELLED,
814 "%s", _("Operation was cancelled"));
816 g_simple_async_result_complete (simple);
818 if (data->cancellable)
819 g_object_unref (data->cancellable);
820 g_object_unref (data->simple);
827 run_in_thread (GIOSchedulerJob *job,
831 RunInThreadData *data = _data;
832 GSimpleAsyncResult *simple = data->simple;
835 if (simple->handle_cancellation &&
836 g_cancellable_is_cancelled (c))
837 g_simple_async_result_set_error (simple,
839 G_IO_ERROR_CANCELLED,
840 "%s", _("Operation was cancelled"));
843 simple->source_object,
846 source = g_idle_source_new ();
847 g_source_set_priority (source, G_PRIORITY_DEFAULT);
848 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
850 g_source_attach (source, simple->context);
851 g_source_unref (source);
857 * g_simple_async_result_run_in_thread: (skip)
858 * @simple: a #GSimpleAsyncResult.
859 * @func: a #GSimpleAsyncThreadFunc.
860 * @io_priority: the io priority of the request.
861 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
863 * Runs the asynchronous job in a separate thread and then calls
864 * g_simple_async_result_complete_in_idle() on @simple to return
865 * the result to the appropriate main loop.
867 * Calling this function takes a reference to @simple for as long as
868 * is needed to run the job and report its completion.
871 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
872 GSimpleAsyncThreadFunc func,
874 GCancellable *cancellable)
876 RunInThreadData *data;
878 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
879 g_return_if_fail (func != NULL);
881 data = g_new (RunInThreadData, 1);
883 data->simple = g_object_ref (simple);
884 data->cancellable = cancellable;
886 g_object_ref (cancellable);
887 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
891 * g_simple_async_result_is_valid:
892 * @result: the #GAsyncResult passed to the _finish function.
893 * @source: the #GObject passed to the _finish function.
894 * @source_tag: the asynchronous function.
896 * Ensures that the data passed to the _finish function of an async
897 * operation is consistent. Three checks are performed.
899 * First, @result is checked to ensure that it is really a
900 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
901 * matches the source object of @result. Third, @source_tag is
902 * checked to ensure that it is either %NULL (as it is when the result was
903 * created by g_simple_async_report_error_in_idle() or
904 * g_simple_async_report_gerror_in_idle()) or equal to the
905 * @source_tag argument given to g_simple_async_result_new() (which, by
906 * convention, is a pointer to the _async function corresponding to the
907 * _finish function from which this function is called).
909 * Returns: #TRUE if all checks passed or #FALSE if any failed.
914 g_simple_async_result_is_valid (GAsyncResult *result,
918 GSimpleAsyncResult *simple;
921 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
923 simple = (GSimpleAsyncResult *)result;
925 cmp_source = g_async_result_get_source_object (result);
926 if (cmp_source != source)
928 if (cmp_source != NULL)
929 g_object_unref (cmp_source);
932 if (cmp_source != NULL)
933 g_object_unref (cmp_source);
935 return source_tag == NULL ||
936 source_tag == g_simple_async_result_get_source_tag (simple);
940 * g_simple_async_report_error_in_idle: (skip)
941 * @object: (allow-none): a #GObject, or %NULL.
942 * @callback: a #GAsyncReadyCallback.
943 * @user_data: user data passed to @callback.
944 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
945 * @code: a specific error code.
946 * @format: a formatted error reporting string.
947 * @...: a list of variables to fill in @format.
949 * Reports an error in an asynchronous function in an idle function by
950 * directly setting the contents of the #GAsyncResult with the given error
954 g_simple_async_report_error_in_idle (GObject *object,
955 GAsyncReadyCallback callback,
962 GSimpleAsyncResult *simple;
965 g_return_if_fail (!object || G_IS_OBJECT (object));
966 g_return_if_fail (domain != 0);
967 g_return_if_fail (format != NULL);
969 simple = g_simple_async_result_new (object,
973 va_start (args, format);
974 g_simple_async_result_set_error_va (simple, domain, code, format, args);
976 g_simple_async_result_complete_in_idle (simple);
977 g_object_unref (simple);
981 * g_simple_async_report_gerror_in_idle:
982 * @object: (allow-none): a #GObject, or %NULL
983 * @callback: (scope async): a #GAsyncReadyCallback.
984 * @user_data: (closure): user data passed to @callback.
985 * @error: the #GError to report
987 * Reports an error in an idle function. Similar to
988 * g_simple_async_report_error_in_idle(), but takes a #GError rather
989 * than building a new one.
992 g_simple_async_report_gerror_in_idle (GObject *object,
993 GAsyncReadyCallback callback,
997 GSimpleAsyncResult *simple;
999 g_return_if_fail (!object || G_IS_OBJECT (object));
1000 g_return_if_fail (error != NULL);
1002 simple = g_simple_async_result_new_from_error (object,
1006 g_simple_async_result_complete_in_idle (simple);
1007 g_object_unref (simple);
1011 * g_simple_async_report_take_gerror_in_idle: (skip)
1012 * @object: (allow-none): a #GObject, or %NULL
1013 * @callback: a #GAsyncReadyCallback.
1014 * @user_data: user data passed to @callback.
1015 * @error: the #GError to report
1017 * Reports an error in an idle function. Similar to
1018 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1019 * ownership of @error, so the caller does not have to free it any more.
1024 g_simple_async_report_take_gerror_in_idle (GObject *object,
1025 GAsyncReadyCallback callback,
1029 GSimpleAsyncResult *simple;
1031 g_return_if_fail (!object || G_IS_OBJECT (object));
1032 g_return_if_fail (error != NULL);
1034 simple = g_simple_async_result_new_take_error (object,
1038 g_simple_async_result_complete_in_idle (simple);
1039 g_object_unref (simple);