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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gsimpleasyncresult.h"
26 #include "gasyncresult.h"
27 #include "gcancellable.h"
28 #include "gioscheduler.h"
29 #include <gio/gioerror.h>
34 * SECTION:gsimpleasyncresult
35 * @short_description: Simple asynchronous results implementation
37 * @see_also: #GAsyncResult, #GTask
39 * As of GLib 2.36, #GSimpleAsyncResult is deprecated in favor of
40 * #GTask, which provides a simpler API.
42 * #GSimpleAsyncResult implements #GAsyncResult.
44 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
45 * reporting, operation cancellation and the final state of an operation,
46 * completely transparent to the application. Results can be returned
47 * as a pointer e.g. for functions that return data that is collected
48 * asynchronously, a boolean value for checking the success or failure
49 * of an operation, or a #gssize for operations which return the number
50 * of bytes modified by the operation; all of the simple return cases
53 * Most of the time, an application will not need to know of the details
54 * of this API; it is handled transparently, and any necessary operations
55 * are handled by #GAsyncResult's interface. However, if implementing a
56 * new GIO module, for writing language bindings, or for complex
57 * applications that need better control of how asynchronous operations
58 * are completed, it is important to understand this functionality.
60 * GSimpleAsyncResults are tagged with the calling function to ensure
61 * that asynchronous functions and their finishing functions are used
64 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
65 * If the result needs to be created for a #GError, use
66 * g_simple_async_result_new_from_error() or
67 * g_simple_async_result_new_take_error(). If a #GError is not available
68 * (e.g. the asynchronous operation's doesn't take a #GError argument),
69 * but the result still needs to be created for an error condition, use
70 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
71 * if your application or binding requires passing a variable argument list
72 * directly), and the error can then be propagated through the use of
73 * g_simple_async_result_propagate_error().
75 * An asynchronous operation can be made to ignore a cancellation event by
76 * calling g_simple_async_result_set_handle_cancellation() with a
77 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
78 * operations that are dangerous to cancel, such as close (which would
79 * cause a leak if cancelled before being run).
81 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
82 * or it can use #GThreads.
83 * g_simple_async_result_complete() will finish an I/O task directly
84 * from the point where it is called. g_simple_async_result_complete_in_idle()
85 * will finish it from an idle handler in the
86 * [thread-default main context][g-main-context-push-thread-default]
87 * . g_simple_async_result_run_in_thread() will run the
88 * job in a separate thread and then deliver the result to the
89 * thread-default main context.
91 * To set the results of an asynchronous function,
92 * g_simple_async_result_set_op_res_gpointer(),
93 * g_simple_async_result_set_op_res_gboolean(), and
94 * g_simple_async_result_set_op_res_gssize()
95 * are provided, setting the operation's result to a gpointer, gboolean, or
96 * gssize, respectively.
98 * Likewise, to get the result of an asynchronous function,
99 * g_simple_async_result_get_op_res_gpointer(),
100 * g_simple_async_result_get_op_res_gboolean(), and
101 * g_simple_async_result_get_op_res_gssize() are
102 * provided, getting the operation's result as a gpointer, gboolean, and
103 * gssize, respectively.
105 * For the details of the requirements implementations must respect, see
106 * #GAsyncResult. A typical implementation of an asynchronous operation
107 * using GSimpleAsyncResult looks something like this:
109 * |[<!-- language="C" -->
111 * baked_cb (Cake *cake,
112 * gpointer user_data)
114 * // In this example, this callback is not given a reference to the cake,
115 * // so the GSimpleAsyncResult has to take a reference to it.
116 * GSimpleAsyncResult *result = user_data;
119 * g_simple_async_result_set_error (result,
121 * BAKER_ERROR_NO_FLOUR,
122 * "Go to the supermarket");
124 * g_simple_async_result_set_op_res_gpointer (result,
125 * g_object_ref (cake),
129 * // In this example, we assume that baked_cb is called as a callback from
130 * // the mainloop, so it's safe to complete the operation synchronously here.
131 * // If, however, _baker_prepare_cake () might call its callback without
132 * // first returning to the mainloop — inadvisable, but some APIs do so —
133 * // we would need to use g_simple_async_result_complete_in_idle().
134 * g_simple_async_result_complete (result);
135 * g_object_unref (result);
139 * baker_bake_cake_async (Baker *self,
141 * GAsyncReadyCallback callback,
142 * gpointer user_data)
144 * GSimpleAsyncResult *simple;
149 * g_simple_async_report_error_in_idle (G_OBJECT (self),
153 * BAKER_ERROR_TOO_SMALL,
154 * "%ucm radius cakes are silly",
159 * simple = g_simple_async_result_new (G_OBJECT (self),
162 * baker_bake_cake_async);
163 * cake = _baker_get_cached_cake (self, radius);
167 * g_simple_async_result_set_op_res_gpointer (simple,
168 * g_object_ref (cake),
170 * g_simple_async_result_complete_in_idle (simple);
171 * g_object_unref (simple);
172 * // Drop the reference returned by _baker_get_cached_cake();
173 * // the GSimpleAsyncResult has taken its own reference.
174 * g_object_unref (cake);
178 * _baker_prepare_cake (self, radius, baked_cb, simple);
182 * baker_bake_cake_finish (Baker *self,
183 * GAsyncResult *result,
186 * GSimpleAsyncResult *simple;
189 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
191 * baker_bake_cake_async),
194 * simple = (GSimpleAsyncResult *) result;
196 * if (g_simple_async_result_propagate_error (simple, error))
199 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
200 * return g_object_ref (cake);
205 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
207 struct _GSimpleAsyncResult
209 GObject parent_instance;
211 GObject *source_object;
212 GAsyncReadyCallback callback;
214 GMainContext *context;
217 gboolean handle_cancellation;
218 GCancellable *check_cancellable;
228 GDestroyNotify destroy_op_res;
231 struct _GSimpleAsyncResultClass
233 GObjectClass parent_class;
237 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
238 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
239 g_simple_async_result_async_result_iface_init))
242 clear_op_res (GSimpleAsyncResult *simple)
244 if (simple->destroy_op_res)
245 simple->destroy_op_res (simple->op_res.v_pointer);
246 simple->destroy_op_res = NULL;
247 simple->op_res.v_ssize = 0;
251 g_simple_async_result_finalize (GObject *object)
253 GSimpleAsyncResult *simple;
255 simple = G_SIMPLE_ASYNC_RESULT (object);
257 if (simple->source_object)
258 g_object_unref (simple->source_object);
260 if (simple->check_cancellable)
261 g_object_unref (simple->check_cancellable);
263 g_main_context_unref (simple->context);
265 clear_op_res (simple);
268 g_error_free (simple->error);
270 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
274 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
276 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
278 gobject_class->finalize = g_simple_async_result_finalize;
282 g_simple_async_result_init (GSimpleAsyncResult *simple)
284 simple->handle_cancellation = TRUE;
286 simple->context = g_main_context_ref_thread_default ();
290 * g_simple_async_result_new:
291 * @source_object: (allow-none): a #GObject, or %NULL.
292 * @callback: (scope async): a #GAsyncReadyCallback.
293 * @user_data: (closure): user data passed to @callback.
294 * @source_tag: the asynchronous function.
296 * Creates a #GSimpleAsyncResult.
298 * The common convention is to create the #GSimpleAsyncResult in the
299 * function that starts the asynchronous operation and use that same
300 * function as the @source_tag.
302 * If your operation supports cancellation with #GCancellable (which it
303 * probably should) then you should provide the user's cancellable to
304 * g_simple_async_result_set_check_cancellable() immediately after
305 * this function returns.
307 * Returns: a #GSimpleAsyncResult.
310 g_simple_async_result_new (GObject *source_object,
311 GAsyncReadyCallback callback,
315 GSimpleAsyncResult *simple;
317 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
319 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
320 simple->callback = callback;
322 simple->source_object = g_object_ref (source_object);
324 simple->source_object = NULL;
325 simple->user_data = user_data;
326 simple->source_tag = source_tag;
332 * g_simple_async_result_new_from_error:
333 * @source_object: (allow-none): a #GObject, or %NULL.
334 * @callback: (scope async): a #GAsyncReadyCallback.
335 * @user_data: (closure): user data passed to @callback.
338 * Creates a #GSimpleAsyncResult from an error condition.
340 * Returns: a #GSimpleAsyncResult.
343 g_simple_async_result_new_from_error (GObject *source_object,
344 GAsyncReadyCallback callback,
348 GSimpleAsyncResult *simple;
350 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
352 simple = g_simple_async_result_new (source_object,
355 g_simple_async_result_set_from_error (simple, error);
361 * g_simple_async_result_new_take_error: (skip)
362 * @source_object: (allow-none): a #GObject, or %NULL
363 * @callback: (scope async): a #GAsyncReadyCallback
364 * @user_data: (closure): user data passed to @callback
367 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
368 * caller's ownership of @error, so the caller does not need to free it anymore.
370 * Returns: a #GSimpleAsyncResult
375 g_simple_async_result_new_take_error (GObject *source_object,
376 GAsyncReadyCallback callback,
380 GSimpleAsyncResult *simple;
382 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
384 simple = g_simple_async_result_new (source_object,
387 g_simple_async_result_take_error (simple, error);
393 * g_simple_async_result_new_error:
394 * @source_object: (allow-none): a #GObject, or %NULL.
395 * @callback: (scope async): a #GAsyncReadyCallback.
396 * @user_data: (closure): user data passed to @callback.
397 * @domain: a #GQuark.
398 * @code: an error code.
399 * @format: a string with format characters.
400 * @...: a list of values to insert into @format.
402 * Creates a new #GSimpleAsyncResult with a set error.
404 * Returns: a #GSimpleAsyncResult.
407 g_simple_async_result_new_error (GObject *source_object,
408 GAsyncReadyCallback callback,
415 GSimpleAsyncResult *simple;
418 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
419 g_return_val_if_fail (domain != 0, NULL);
420 g_return_val_if_fail (format != NULL, NULL);
422 simple = g_simple_async_result_new (source_object,
426 va_start (args, format);
427 g_simple_async_result_set_error_va (simple, domain, code, format, args);
435 g_simple_async_result_get_user_data (GAsyncResult *res)
437 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
441 g_simple_async_result_get_source_object (GAsyncResult *res)
443 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
444 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
449 g_simple_async_result_is_tagged (GAsyncResult *res,
452 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
456 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
458 iface->get_user_data = g_simple_async_result_get_user_data;
459 iface->get_source_object = g_simple_async_result_get_source_object;
460 iface->is_tagged = g_simple_async_result_is_tagged;
464 * g_simple_async_result_set_handle_cancellation:
465 * @simple: a #GSimpleAsyncResult.
466 * @handle_cancellation: a #gboolean.
468 * Sets whether to handle cancellation within the asynchronous operation.
470 * This function has nothing to do with
471 * g_simple_async_result_set_check_cancellable(). It only refers to the
472 * #GCancellable passed to g_simple_async_result_run_in_thread().
475 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
476 gboolean handle_cancellation)
478 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
479 simple->handle_cancellation = handle_cancellation;
483 * g_simple_async_result_get_source_tag: (skip)
484 * @simple: a #GSimpleAsyncResult.
486 * Gets the source tag for the #GSimpleAsyncResult.
488 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
491 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
493 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
494 return simple->source_tag;
498 * g_simple_async_result_propagate_error:
499 * @simple: a #GSimpleAsyncResult.
500 * @dest: (out): a location to propagate the error to.
502 * Propagates an error from within the simple asynchronous result to
503 * a given destination.
505 * If the #GCancellable given to a prior call to
506 * g_simple_async_result_set_check_cancellable() is cancelled then this
507 * function will return %TRUE with @dest set appropriately.
509 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
512 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
515 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
517 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
522 g_propagate_error (dest, simple->error);
523 simple->error = NULL;
531 * g_simple_async_result_set_op_res_gpointer: (skip)
532 * @simple: a #GSimpleAsyncResult.
533 * @op_res: a pointer result from an asynchronous function.
534 * @destroy_op_res: a #GDestroyNotify function.
536 * Sets the operation result within the asynchronous result to a pointer.
539 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
541 GDestroyNotify destroy_op_res)
543 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
545 clear_op_res (simple);
546 simple->op_res.v_pointer = op_res;
547 simple->destroy_op_res = destroy_op_res;
551 * g_simple_async_result_get_op_res_gpointer: (skip)
552 * @simple: a #GSimpleAsyncResult.
554 * Gets a pointer result as returned by the asynchronous function.
556 * Returns: a pointer from the result.
559 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
561 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
562 return simple->op_res.v_pointer;
566 * g_simple_async_result_set_op_res_gssize:
567 * @simple: a #GSimpleAsyncResult.
568 * @op_res: a #gssize.
570 * Sets the operation result within the asynchronous result to
574 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
577 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
578 clear_op_res (simple);
579 simple->op_res.v_ssize = op_res;
583 * g_simple_async_result_get_op_res_gssize:
584 * @simple: a #GSimpleAsyncResult.
586 * Gets a gssize from the asynchronous result.
588 * Returns: a gssize returned from the asynchronous function.
591 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
593 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
594 return simple->op_res.v_ssize;
598 * g_simple_async_result_set_op_res_gboolean:
599 * @simple: a #GSimpleAsyncResult.
600 * @op_res: a #gboolean.
602 * Sets the operation result to a boolean within the asynchronous result.
605 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
608 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
609 clear_op_res (simple);
610 simple->op_res.v_boolean = !!op_res;
614 * g_simple_async_result_get_op_res_gboolean:
615 * @simple: a #GSimpleAsyncResult.
617 * Gets the operation result boolean from within the asynchronous result.
619 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
620 * if the operation's result was %FALSE.
623 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
625 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
626 return simple->op_res.v_boolean;
630 * g_simple_async_result_set_from_error:
631 * @simple: a #GSimpleAsyncResult.
634 * Sets the result from a #GError.
637 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
640 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
641 g_return_if_fail (error != NULL);
644 g_error_free (simple->error);
645 simple->error = g_error_copy (error);
646 simple->failed = TRUE;
650 * g_simple_async_result_take_error: (skip)
651 * @simple: a #GSimpleAsyncResult
654 * Sets the result from @error, and takes over the caller's ownership
655 * of @error, so the caller does not need to free it any more.
660 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
663 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
664 g_return_if_fail (error != NULL);
667 g_error_free (simple->error);
668 simple->error = error;
669 simple->failed = TRUE;
673 * g_simple_async_result_set_error_va: (skip)
674 * @simple: a #GSimpleAsyncResult.
675 * @domain: a #GQuark (usually #G_IO_ERROR).
676 * @code: an error code.
677 * @format: a formatted error reporting string.
678 * @args: va_list of arguments.
680 * Sets an error within the asynchronous result without a #GError.
681 * Unless writing a binding, see g_simple_async_result_set_error().
684 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
690 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
691 g_return_if_fail (domain != 0);
692 g_return_if_fail (format != NULL);
695 g_error_free (simple->error);
696 simple->error = g_error_new_valist (domain, code, format, args);
697 simple->failed = TRUE;
701 * g_simple_async_result_set_error: (skip)
702 * @simple: a #GSimpleAsyncResult.
703 * @domain: a #GQuark (usually #G_IO_ERROR).
704 * @code: an error code.
705 * @format: a formatted error reporting string.
706 * @...: a list of variables to fill in @format.
708 * Sets an error within the asynchronous result without a #GError.
711 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
719 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
720 g_return_if_fail (domain != 0);
721 g_return_if_fail (format != NULL);
723 va_start (args, format);
724 g_simple_async_result_set_error_va (simple, domain, code, format, args);
729 * g_simple_async_result_complete:
730 * @simple: a #GSimpleAsyncResult.
732 * Completes an asynchronous I/O job immediately. Must be called in
733 * the thread where the asynchronous result was to be delivered, as it
734 * invokes the callback directly. If you are in a different thread use
735 * g_simple_async_result_complete_in_idle().
737 * Calling this function takes a reference to @simple for as long as
738 * is needed to complete the call.
741 g_simple_async_result_complete (GSimpleAsyncResult *simple)
743 #ifndef G_DISABLE_CHECKS
744 GSource *current_source;
745 GMainContext *current_context;
748 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
750 #ifndef G_DISABLE_CHECKS
751 current_source = g_main_current_source ();
752 if (current_source && !g_source_is_destroyed (current_source))
754 current_context = g_source_get_context (current_source);
755 if (simple->context != current_context)
756 g_warning ("g_simple_async_result_complete() called from wrong context!");
760 if (simple->callback)
762 g_main_context_push_thread_default (simple->context);
763 simple->callback (simple->source_object,
764 G_ASYNC_RESULT (simple),
766 g_main_context_pop_thread_default (simple->context);
771 complete_in_idle_cb (gpointer data)
773 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
775 g_simple_async_result_complete (simple);
781 * g_simple_async_result_complete_in_idle:
782 * @simple: a #GSimpleAsyncResult.
784 * Completes an asynchronous function in an idle handler in the
785 * [thread-default main context][g-main-context-push-thread-default]
786 * of the thread that @simple was initially created in
787 * (and re-pushes that context around the invocation of the callback).
789 * Calling this function takes a reference to @simple for as long as
790 * is needed to complete the call.
793 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
797 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
799 g_object_ref (simple);
801 source = g_idle_source_new ();
802 g_source_set_priority (source, G_PRIORITY_DEFAULT);
803 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
805 g_source_attach (source, simple->context);
806 g_source_unref (source);
810 GSimpleAsyncResult *simple;
811 GCancellable *cancellable;
812 GSimpleAsyncThreadFunc func;
817 complete_in_idle_cb_for_thread (gpointer _data)
819 RunInThreadData *data = _data;
820 GSimpleAsyncResult *simple;
822 simple = data->simple;
824 if (simple->handle_cancellation &&
825 g_cancellable_is_cancelled (data->cancellable))
826 g_simple_async_result_set_error (simple,
828 G_IO_ERROR_CANCELLED,
829 "%s", _("Operation was cancelled"));
831 g_simple_async_result_complete (simple);
833 if (data->cancellable)
834 g_object_unref (data->cancellable);
835 g_object_unref (data->simple);
842 run_in_thread (GIOSchedulerJob *job,
846 RunInThreadData *data = _data;
847 GSimpleAsyncResult *simple = data->simple;
850 if (simple->handle_cancellation &&
851 g_cancellable_is_cancelled (c))
852 g_simple_async_result_set_error (simple,
854 G_IO_ERROR_CANCELLED,
855 "%s", _("Operation was cancelled"));
858 simple->source_object,
861 source = g_idle_source_new ();
862 g_source_set_priority (source, G_PRIORITY_DEFAULT);
863 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
865 g_source_attach (source, simple->context);
866 g_source_unref (source);
872 * g_simple_async_result_run_in_thread: (skip)
873 * @simple: a #GSimpleAsyncResult.
874 * @func: a #GSimpleAsyncThreadFunc.
875 * @io_priority: the io priority of the request.
876 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
878 * Runs the asynchronous job in a separate thread and then calls
879 * g_simple_async_result_complete_in_idle() on @simple to return
880 * the result to the appropriate main loop.
882 * Calling this function takes a reference to @simple for as long as
883 * is needed to run the job and report its completion.
886 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
887 GSimpleAsyncThreadFunc func,
889 GCancellable *cancellable)
891 RunInThreadData *data;
893 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
894 g_return_if_fail (func != NULL);
896 data = g_new (RunInThreadData, 1);
898 data->simple = g_object_ref (simple);
899 data->cancellable = cancellable;
901 g_object_ref (cancellable);
902 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
903 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
904 G_GNUC_END_IGNORE_DEPRECATIONS;
908 * g_simple_async_result_is_valid:
909 * @result: the #GAsyncResult passed to the _finish function.
910 * @source: the #GObject passed to the _finish function.
911 * @source_tag: the asynchronous function.
913 * Ensures that the data passed to the _finish function of an async
914 * operation is consistent. Three checks are performed.
916 * First, @result is checked to ensure that it is really a
917 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
918 * matches the source object of @result. Third, @source_tag is
919 * checked to ensure that it is either %NULL (as it is when the result was
920 * created by g_simple_async_report_error_in_idle() or
921 * g_simple_async_report_gerror_in_idle()) or equal to the
922 * @source_tag argument given to g_simple_async_result_new() (which, by
923 * convention, is a pointer to the _async function corresponding to the
924 * _finish function from which this function is called).
926 * Returns: #TRUE if all checks passed or #FALSE if any failed.
931 g_simple_async_result_is_valid (GAsyncResult *result,
935 GSimpleAsyncResult *simple;
938 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
940 simple = (GSimpleAsyncResult *)result;
942 cmp_source = g_async_result_get_source_object (result);
943 if (cmp_source != source)
945 if (cmp_source != NULL)
946 g_object_unref (cmp_source);
949 if (cmp_source != NULL)
950 g_object_unref (cmp_source);
952 return source_tag == NULL ||
953 source_tag == g_simple_async_result_get_source_tag (simple);
957 * g_simple_async_report_error_in_idle: (skip)
958 * @object: (allow-none): a #GObject, or %NULL.
959 * @callback: a #GAsyncReadyCallback.
960 * @user_data: user data passed to @callback.
961 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
962 * @code: a specific error code.
963 * @format: a formatted error reporting string.
964 * @...: a list of variables to fill in @format.
966 * Reports an error in an asynchronous function in an idle function by
967 * directly setting the contents of the #GAsyncResult with the given error
971 g_simple_async_report_error_in_idle (GObject *object,
972 GAsyncReadyCallback callback,
979 GSimpleAsyncResult *simple;
982 g_return_if_fail (!object || G_IS_OBJECT (object));
983 g_return_if_fail (domain != 0);
984 g_return_if_fail (format != NULL);
986 simple = g_simple_async_result_new (object,
990 va_start (args, format);
991 g_simple_async_result_set_error_va (simple, domain, code, format, args);
993 g_simple_async_result_complete_in_idle (simple);
994 g_object_unref (simple);
998 * g_simple_async_report_gerror_in_idle:
999 * @object: (allow-none): a #GObject, or %NULL
1000 * @callback: (scope async): a #GAsyncReadyCallback.
1001 * @user_data: (closure): user data passed to @callback.
1002 * @error: the #GError to report
1004 * Reports an error in an idle function. Similar to
1005 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1006 * than building a new one.
1009 g_simple_async_report_gerror_in_idle (GObject *object,
1010 GAsyncReadyCallback callback,
1012 const GError *error)
1014 GSimpleAsyncResult *simple;
1016 g_return_if_fail (!object || G_IS_OBJECT (object));
1017 g_return_if_fail (error != NULL);
1019 simple = g_simple_async_result_new_from_error (object,
1023 g_simple_async_result_complete_in_idle (simple);
1024 g_object_unref (simple);
1028 * g_simple_async_report_take_gerror_in_idle: (skip)
1029 * @object: (allow-none): a #GObject, or %NULL
1030 * @callback: a #GAsyncReadyCallback.
1031 * @user_data: user data passed to @callback.
1032 * @error: the #GError to report
1034 * Reports an error in an idle function. Similar to
1035 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1036 * ownership of @error, so the caller does not have to free it any more.
1041 g_simple_async_report_take_gerror_in_idle (GObject *object,
1042 GAsyncReadyCallback callback,
1046 GSimpleAsyncResult *simple;
1048 g_return_if_fail (!object || G_IS_OBJECT (object));
1049 g_return_if_fail (error != NULL);
1051 simple = g_simple_async_result_new_take_error (object,
1055 g_simple_async_result_complete_in_idle (simple);
1056 g_object_unref (simple);
1060 * g_simple_async_result_set_check_cancellable:
1061 * @simple: a #GSimpleAsyncResult
1062 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1064 * Sets a #GCancellable to check before dispatching results.
1066 * This function has one very specific purpose: the provided cancellable
1067 * is checked at the time of g_simple_async_result_propagate_error() If
1068 * it is cancelled, these functions will return an "Operation was
1069 * cancelled" error (%G_IO_ERROR_CANCELLED).
1071 * Implementors of cancellable asynchronous functions should use this in
1072 * order to provide a guarantee to their callers that cancelling an
1073 * async operation will reliably result in an error being returned for
1074 * that operation (even if a positive result for the operation has
1075 * already been sent as an idle to the main context to be dispatched).
1077 * The checking described above is done regardless of any call to the
1078 * unrelated g_simple_async_result_set_handle_cancellation() function.
1083 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1084 GCancellable *check_cancellable)
1086 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1087 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1089 g_clear_object (&simple->check_cancellable);
1090 if (check_cancellable)
1091 simple->check_cancellable = g_object_ref (check_cancellable);