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
40 * As of GLib 2.36, #GSimpleAsyncResult is deprecated in favor of
41 * #GTask, which provides a simpler API.
44 * #GSimpleAsyncResult implements #GAsyncResult.
46 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
47 * reporting, operation cancellation and the final state of an operation,
48 * completely transparent to the application. Results can be returned
49 * as a pointer e.g. for functions that return data that is collected
50 * asynchronously, a boolean value for checking the success or failure
51 * of an operation, or a #gssize for operations which return the number
52 * of bytes modified by the operation; all of the simple return cases
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.
62 * GSimpleAsyncResults are tagged with the calling function to ensure
63 * that asynchronous functions and their finishing functions are used
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().
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).
83 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
84 * or it can use #GThread<!-- -->s.
85 * g_simple_async_result_complete() will finish an I/O task directly
86 * from the point where it is called. g_simple_async_result_complete_in_idle()
87 * will finish it from an idle handler in the <link
88 * linkend="g-main-context-push-thread-default">thread-default main
89 * context</link>. g_simple_async_result_run_in_thread() will run the
90 * job in a separate thread and then deliver the result to the
91 * thread-default main context.
93 * To set the results of an asynchronous function,
94 * g_simple_async_result_set_op_res_gpointer(),
95 * g_simple_async_result_set_op_res_gboolean(), and
96 * g_simple_async_result_set_op_res_gssize()
97 * are provided, setting the operation's result to a gpointer, gboolean, or
98 * gssize, respectively.
100 * Likewise, to get the result of an asynchronous function,
101 * g_simple_async_result_get_op_res_gpointer(),
102 * g_simple_async_result_get_op_res_gboolean(), and
103 * g_simple_async_result_get_op_res_gssize() are
104 * provided, getting the operation's result as a gpointer, gboolean, and
105 * gssize, respectively.
107 * For the details of the requirements implementations must respect, see
108 * #GAsyncResult. A typical implementation of an asynchronous operation
109 * using GSimpleAsyncResult looks something like this:
113 * baked_cb (Cake *cake,
114 * gpointer user_data)
116 * /* In this example, this callback is not given a reference to the cake, so
117 * * the GSimpleAsyncResult has to take a reference to it.
119 * GSimpleAsyncResult *result = user_data;
122 * g_simple_async_result_set_error (result,
124 * BAKER_ERROR_NO_FLOUR,
125 * "Go to the supermarket");
127 * g_simple_async_result_set_op_res_gpointer (result,
128 * g_object_ref (cake),
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().
138 * g_simple_async_result_complete (result);
139 * g_object_unref (result);
143 * baker_bake_cake_async (Baker *self,
145 * GAsyncReadyCallback callback,
146 * gpointer user_data)
148 * GSimpleAsyncResult *simple;
153 * g_simple_async_report_error_in_idle (G_OBJECT (self),
157 * BAKER_ERROR_TOO_SMALL,
158 * "%ucm radius cakes are silly",
163 * simple = g_simple_async_result_new (G_OBJECT (self),
166 * baker_bake_cake_async);
167 * cake = _baker_get_cached_cake (self, radius);
171 * g_simple_async_result_set_op_res_gpointer (simple,
172 * g_object_ref (cake),
174 * g_simple_async_result_complete_in_idle (simple);
175 * g_object_unref (simple);
176 * /* Drop the reference returned by _baker_get_cached_cake(); the
177 * * GSimpleAsyncResult has taken its own reference.
179 * g_object_unref (cake);
183 * _baker_prepare_cake (self, radius, baked_cb, simple);
187 * baker_bake_cake_finish (Baker *self,
188 * GAsyncResult *result,
191 * GSimpleAsyncResult *simple;
194 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
196 * baker_bake_cake_async),
199 * simple = (GSimpleAsyncResult *) result;
201 * if (g_simple_async_result_propagate_error (simple, error))
204 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
205 * return g_object_ref (cake);
210 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
212 struct _GSimpleAsyncResult
214 GObject parent_instance;
216 GObject *source_object;
217 GAsyncReadyCallback callback;
219 GMainContext *context;
222 gboolean handle_cancellation;
223 GCancellable *check_cancellable;
233 GDestroyNotify destroy_op_res;
236 struct _GSimpleAsyncResultClass
238 GObjectClass parent_class;
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))
247 clear_op_res (GSimpleAsyncResult *simple)
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;
256 g_simple_async_result_finalize (GObject *object)
258 GSimpleAsyncResult *simple;
260 simple = G_SIMPLE_ASYNC_RESULT (object);
262 if (simple->source_object)
263 g_object_unref (simple->source_object);
265 if (simple->check_cancellable)
266 g_object_unref (simple->check_cancellable);
268 g_main_context_unref (simple->context);
270 clear_op_res (simple);
273 g_error_free (simple->error);
275 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
279 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
281 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
283 gobject_class->finalize = g_simple_async_result_finalize;
287 g_simple_async_result_init (GSimpleAsyncResult *simple)
289 simple->handle_cancellation = TRUE;
291 simple->context = g_main_context_ref_thread_default ();
295 * g_simple_async_result_new:
296 * @source_object: (allow-none): a #GObject, or %NULL.
297 * @callback: (scope async): a #GAsyncReadyCallback.
298 * @user_data: (closure): user data passed to @callback.
299 * @source_tag: the asynchronous function.
301 * Creates a #GSimpleAsyncResult.
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.
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.
312 * Returns: a #GSimpleAsyncResult.
315 g_simple_async_result_new (GObject *source_object,
316 GAsyncReadyCallback callback,
320 GSimpleAsyncResult *simple;
322 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
324 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
325 simple->callback = callback;
327 simple->source_object = g_object_ref (source_object);
329 simple->source_object = NULL;
330 simple->user_data = user_data;
331 simple->source_tag = source_tag;
337 * g_simple_async_result_new_from_error:
338 * @source_object: (allow-none): a #GObject, or %NULL.
339 * @callback: (scope async): a #GAsyncReadyCallback.
340 * @user_data: (closure): user data passed to @callback.
343 * Creates a #GSimpleAsyncResult from an error condition.
345 * Returns: a #GSimpleAsyncResult.
348 g_simple_async_result_new_from_error (GObject *source_object,
349 GAsyncReadyCallback callback,
353 GSimpleAsyncResult *simple;
355 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
357 simple = g_simple_async_result_new (source_object,
360 g_simple_async_result_set_from_error (simple, error);
366 * g_simple_async_result_new_take_error: (skip)
367 * @source_object: (allow-none): a #GObject, or %NULL
368 * @callback: (scope async): a #GAsyncReadyCallback
369 * @user_data: (closure): user data passed to @callback
372 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
373 * caller's ownership of @error, so the caller does not need to free it anymore.
375 * Returns: a #GSimpleAsyncResult
380 g_simple_async_result_new_take_error (GObject *source_object,
381 GAsyncReadyCallback callback,
385 GSimpleAsyncResult *simple;
387 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
389 simple = g_simple_async_result_new (source_object,
392 g_simple_async_result_take_error (simple, error);
398 * g_simple_async_result_new_error:
399 * @source_object: (allow-none): a #GObject, or %NULL.
400 * @callback: (scope async): a #GAsyncReadyCallback.
401 * @user_data: (closure): user data passed to @callback.
402 * @domain: a #GQuark.
403 * @code: an error code.
404 * @format: a string with format characters.
405 * @...: a list of values to insert into @format.
407 * Creates a new #GSimpleAsyncResult with a set error.
409 * Returns: a #GSimpleAsyncResult.
412 g_simple_async_result_new_error (GObject *source_object,
413 GAsyncReadyCallback callback,
420 GSimpleAsyncResult *simple;
423 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
424 g_return_val_if_fail (domain != 0, NULL);
425 g_return_val_if_fail (format != NULL, NULL);
427 simple = g_simple_async_result_new (source_object,
431 va_start (args, format);
432 g_simple_async_result_set_error_va (simple, domain, code, format, args);
440 g_simple_async_result_get_user_data (GAsyncResult *res)
442 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
446 g_simple_async_result_get_source_object (GAsyncResult *res)
448 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
449 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
454 g_simple_async_result_is_tagged (GAsyncResult *res,
457 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
461 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
463 iface->get_user_data = g_simple_async_result_get_user_data;
464 iface->get_source_object = g_simple_async_result_get_source_object;
465 iface->is_tagged = g_simple_async_result_is_tagged;
469 * g_simple_async_result_set_handle_cancellation:
470 * @simple: a #GSimpleAsyncResult.
471 * @handle_cancellation: a #gboolean.
473 * Sets whether to handle cancellation within the asynchronous operation.
475 * This function has nothing to do with
476 * g_simple_async_result_set_check_cancellable(). It only refers to the
477 * #GCancellable passed to g_simple_async_result_run_in_thread().
480 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
481 gboolean handle_cancellation)
483 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
484 simple->handle_cancellation = handle_cancellation;
488 * g_simple_async_result_get_source_tag: (skip)
489 * @simple: a #GSimpleAsyncResult.
491 * Gets the source tag for the #GSimpleAsyncResult.
493 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
496 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
498 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
499 return simple->source_tag;
503 * g_simple_async_result_propagate_error:
504 * @simple: a #GSimpleAsyncResult.
505 * @dest: (out): a location to propagate the error to.
507 * Propagates an error from within the simple asynchronous result to
508 * a given destination.
510 * If the #GCancellable given to a prior call to
511 * g_simple_async_result_set_check_cancellable() is cancelled then this
512 * function will return %TRUE with @dest set appropriately.
514 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
517 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
520 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
522 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
527 g_propagate_error (dest, simple->error);
528 simple->error = NULL;
536 * g_simple_async_result_set_op_res_gpointer: (skip)
537 * @simple: a #GSimpleAsyncResult.
538 * @op_res: a pointer result from an asynchronous function.
539 * @destroy_op_res: a #GDestroyNotify function.
541 * Sets the operation result within the asynchronous result to a pointer.
544 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
546 GDestroyNotify destroy_op_res)
548 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
550 clear_op_res (simple);
551 simple->op_res.v_pointer = op_res;
552 simple->destroy_op_res = destroy_op_res;
556 * g_simple_async_result_get_op_res_gpointer: (skip)
557 * @simple: a #GSimpleAsyncResult.
559 * Gets a pointer result as returned by the asynchronous function.
561 * Returns: a pointer from the result.
564 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
566 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
567 return simple->op_res.v_pointer;
571 * g_simple_async_result_set_op_res_gssize:
572 * @simple: a #GSimpleAsyncResult.
573 * @op_res: a #gssize.
575 * Sets the operation result within the asynchronous result to
579 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
582 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
583 clear_op_res (simple);
584 simple->op_res.v_ssize = op_res;
588 * g_simple_async_result_get_op_res_gssize:
589 * @simple: a #GSimpleAsyncResult.
591 * Gets a gssize from the asynchronous result.
593 * Returns: a gssize returned from the asynchronous function.
596 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
598 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
599 return simple->op_res.v_ssize;
603 * g_simple_async_result_set_op_res_gboolean:
604 * @simple: a #GSimpleAsyncResult.
605 * @op_res: a #gboolean.
607 * Sets the operation result to a boolean within the asynchronous result.
610 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
613 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
614 clear_op_res (simple);
615 simple->op_res.v_boolean = !!op_res;
619 * g_simple_async_result_get_op_res_gboolean:
620 * @simple: a #GSimpleAsyncResult.
622 * Gets the operation result boolean from within the asynchronous result.
624 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
625 * if the operation's result was %FALSE.
628 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
630 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
631 return simple->op_res.v_boolean;
635 * g_simple_async_result_set_from_error:
636 * @simple: a #GSimpleAsyncResult.
639 * Sets the result from a #GError.
642 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
645 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
646 g_return_if_fail (error != NULL);
649 g_error_free (simple->error);
650 simple->error = g_error_copy (error);
651 simple->failed = TRUE;
655 * g_simple_async_result_take_error: (skip)
656 * @simple: a #GSimpleAsyncResult
659 * Sets the result from @error, and takes over the caller's ownership
660 * of @error, so the caller does not need to free it any more.
665 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
668 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
669 g_return_if_fail (error != NULL);
672 g_error_free (simple->error);
673 simple->error = error;
674 simple->failed = TRUE;
678 * g_simple_async_result_set_error_va: (skip)
679 * @simple: a #GSimpleAsyncResult.
680 * @domain: a #GQuark (usually #G_IO_ERROR).
681 * @code: an error code.
682 * @format: a formatted error reporting string.
683 * @args: va_list of arguments.
685 * Sets an error within the asynchronous result without a #GError.
686 * Unless writing a binding, see g_simple_async_result_set_error().
689 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
695 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
696 g_return_if_fail (domain != 0);
697 g_return_if_fail (format != NULL);
700 g_error_free (simple->error);
701 simple->error = g_error_new_valist (domain, code, format, args);
702 simple->failed = TRUE;
706 * g_simple_async_result_set_error: (skip)
707 * @simple: a #GSimpleAsyncResult.
708 * @domain: a #GQuark (usually #G_IO_ERROR).
709 * @code: an error code.
710 * @format: a formatted error reporting string.
711 * @...: a list of variables to fill in @format.
713 * Sets an error within the asynchronous result without a #GError.
716 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
724 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
725 g_return_if_fail (domain != 0);
726 g_return_if_fail (format != NULL);
728 va_start (args, format);
729 g_simple_async_result_set_error_va (simple, domain, code, format, args);
734 * g_simple_async_result_complete:
735 * @simple: a #GSimpleAsyncResult.
737 * Completes an asynchronous I/O job immediately. Must be called in
738 * the thread where the asynchronous result was to be delivered, as it
739 * invokes the callback directly. If you are in a different thread use
740 * g_simple_async_result_complete_in_idle().
742 * Calling this function takes a reference to @simple for as long as
743 * is needed to complete the call.
746 g_simple_async_result_complete (GSimpleAsyncResult *simple)
748 #ifndef G_DISABLE_CHECKS
749 GSource *current_source;
750 GMainContext *current_context;
753 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
755 #ifndef G_DISABLE_CHECKS
756 current_source = g_main_current_source ();
757 if (current_source && !g_source_is_destroyed (current_source))
759 current_context = g_source_get_context (current_source);
760 if (simple->context != current_context)
761 g_warning ("g_simple_async_result_complete() called from wrong context!");
765 if (simple->callback)
767 g_main_context_push_thread_default (simple->context);
768 simple->callback (simple->source_object,
769 G_ASYNC_RESULT (simple),
771 g_main_context_pop_thread_default (simple->context);
776 complete_in_idle_cb (gpointer data)
778 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
780 g_simple_async_result_complete (simple);
786 * g_simple_async_result_complete_in_idle:
787 * @simple: a #GSimpleAsyncResult.
789 * Completes an asynchronous function in an idle handler in the <link
790 * linkend="g-main-context-push-thread-default">thread-default main
791 * loop</link> of the thread that @simple was initially created in
792 * (and re-pushes that context around the invocation of the callback).
794 * Calling this function takes a reference to @simple for as long as
795 * is needed to complete the call.
798 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
802 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
804 g_object_ref (simple);
806 source = g_idle_source_new ();
807 g_source_set_priority (source, G_PRIORITY_DEFAULT);
808 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
810 g_source_attach (source, simple->context);
811 g_source_unref (source);
815 GSimpleAsyncResult *simple;
816 GCancellable *cancellable;
817 GSimpleAsyncThreadFunc func;
822 complete_in_idle_cb_for_thread (gpointer _data)
824 RunInThreadData *data = _data;
825 GSimpleAsyncResult *simple;
827 simple = data->simple;
829 if (simple->handle_cancellation &&
830 g_cancellable_is_cancelled (data->cancellable))
831 g_simple_async_result_set_error (simple,
833 G_IO_ERROR_CANCELLED,
834 "%s", _("Operation was cancelled"));
836 g_simple_async_result_complete (simple);
838 if (data->cancellable)
839 g_object_unref (data->cancellable);
840 g_object_unref (data->simple);
847 run_in_thread (GIOSchedulerJob *job,
851 RunInThreadData *data = _data;
852 GSimpleAsyncResult *simple = data->simple;
855 if (simple->handle_cancellation &&
856 g_cancellable_is_cancelled (c))
857 g_simple_async_result_set_error (simple,
859 G_IO_ERROR_CANCELLED,
860 "%s", _("Operation was cancelled"));
863 simple->source_object,
866 source = g_idle_source_new ();
867 g_source_set_priority (source, G_PRIORITY_DEFAULT);
868 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
870 g_source_attach (source, simple->context);
871 g_source_unref (source);
877 * g_simple_async_result_run_in_thread: (skip)
878 * @simple: a #GSimpleAsyncResult.
879 * @func: a #GSimpleAsyncThreadFunc.
880 * @io_priority: the io priority of the request.
881 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
883 * Runs the asynchronous job in a separate thread and then calls
884 * g_simple_async_result_complete_in_idle() on @simple to return
885 * the result to the appropriate main loop.
887 * Calling this function takes a reference to @simple for as long as
888 * is needed to run the job and report its completion.
891 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
892 GSimpleAsyncThreadFunc func,
894 GCancellable *cancellable)
896 RunInThreadData *data;
898 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
899 g_return_if_fail (func != NULL);
901 data = g_new (RunInThreadData, 1);
903 data->simple = g_object_ref (simple);
904 data->cancellable = cancellable;
906 g_object_ref (cancellable);
907 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
908 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
909 G_GNUC_END_IGNORE_DEPRECATIONS;
913 * g_simple_async_result_is_valid:
914 * @result: the #GAsyncResult passed to the _finish function.
915 * @source: the #GObject passed to the _finish function.
916 * @source_tag: the asynchronous function.
918 * Ensures that the data passed to the _finish function of an async
919 * operation is consistent. Three checks are performed.
921 * First, @result is checked to ensure that it is really a
922 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
923 * matches the source object of @result. Third, @source_tag is
924 * checked to ensure that it is either %NULL (as it is when the result was
925 * created by g_simple_async_report_error_in_idle() or
926 * g_simple_async_report_gerror_in_idle()) or equal to the
927 * @source_tag argument given to g_simple_async_result_new() (which, by
928 * convention, is a pointer to the _async function corresponding to the
929 * _finish function from which this function is called).
931 * Returns: #TRUE if all checks passed or #FALSE if any failed.
936 g_simple_async_result_is_valid (GAsyncResult *result,
940 GSimpleAsyncResult *simple;
943 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
945 simple = (GSimpleAsyncResult *)result;
947 cmp_source = g_async_result_get_source_object (result);
948 if (cmp_source != source)
950 if (cmp_source != NULL)
951 g_object_unref (cmp_source);
954 if (cmp_source != NULL)
955 g_object_unref (cmp_source);
957 return source_tag == NULL ||
958 source_tag == g_simple_async_result_get_source_tag (simple);
962 * g_simple_async_report_error_in_idle: (skip)
963 * @object: (allow-none): a #GObject, or %NULL.
964 * @callback: a #GAsyncReadyCallback.
965 * @user_data: user data passed to @callback.
966 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
967 * @code: a specific error code.
968 * @format: a formatted error reporting string.
969 * @...: a list of variables to fill in @format.
971 * Reports an error in an asynchronous function in an idle function by
972 * directly setting the contents of the #GAsyncResult with the given error
976 g_simple_async_report_error_in_idle (GObject *object,
977 GAsyncReadyCallback callback,
984 GSimpleAsyncResult *simple;
987 g_return_if_fail (!object || G_IS_OBJECT (object));
988 g_return_if_fail (domain != 0);
989 g_return_if_fail (format != NULL);
991 simple = g_simple_async_result_new (object,
995 va_start (args, format);
996 g_simple_async_result_set_error_va (simple, domain, code, format, args);
998 g_simple_async_result_complete_in_idle (simple);
999 g_object_unref (simple);
1003 * g_simple_async_report_gerror_in_idle:
1004 * @object: (allow-none): a #GObject, or %NULL
1005 * @callback: (scope async): a #GAsyncReadyCallback.
1006 * @user_data: (closure): user data passed to @callback.
1007 * @error: the #GError to report
1009 * Reports an error in an idle function. Similar to
1010 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1011 * than building a new one.
1014 g_simple_async_report_gerror_in_idle (GObject *object,
1015 GAsyncReadyCallback callback,
1017 const GError *error)
1019 GSimpleAsyncResult *simple;
1021 g_return_if_fail (!object || G_IS_OBJECT (object));
1022 g_return_if_fail (error != NULL);
1024 simple = g_simple_async_result_new_from_error (object,
1028 g_simple_async_result_complete_in_idle (simple);
1029 g_object_unref (simple);
1033 * g_simple_async_report_take_gerror_in_idle: (skip)
1034 * @object: (allow-none): a #GObject, or %NULL
1035 * @callback: a #GAsyncReadyCallback.
1036 * @user_data: user data passed to @callback.
1037 * @error: the #GError to report
1039 * Reports an error in an idle function. Similar to
1040 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1041 * ownership of @error, so the caller does not have to free it any more.
1046 g_simple_async_report_take_gerror_in_idle (GObject *object,
1047 GAsyncReadyCallback callback,
1051 GSimpleAsyncResult *simple;
1053 g_return_if_fail (!object || G_IS_OBJECT (object));
1054 g_return_if_fail (error != NULL);
1056 simple = g_simple_async_result_new_take_error (object,
1060 g_simple_async_result_complete_in_idle (simple);
1061 g_object_unref (simple);
1065 * g_simple_async_result_set_check_cancellable:
1066 * @simple: a #GSimpleAsyncResult
1067 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1069 * Sets a #GCancellable to check before dispatching results.
1071 * This function has one very specific purpose: the provided cancellable
1072 * is checked at the time of g_simple_async_result_propagate_error() If
1073 * it is cancelled, these functions will return an "Operation was
1074 * cancelled" error (%G_IO_ERROR_CANCELLED).
1076 * Implementors of cancellable asynchronous functions should use this in
1077 * order to provide a guarantee to their callers that cancelling an
1078 * async operation will reliably result in an error being returned for
1079 * that operation (even if a positive result for the operation has
1080 * already been sent as an idle to the main context to be dispatched).
1082 * The checking described above is done regardless of any call to the
1083 * unrelated g_simple_async_result_set_handle_cancellation() function.
1088 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1089 GCancellable *check_cancellable)
1091 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1092 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1094 g_clear_object (&simple->check_cancellable);
1095 if (check_cancellable)
1096 simple->check_cancellable = g_object_ref (check_cancellable);