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 #GAsyncReadyCallback<!-- -->s, 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 #GThread<!-- -->s.
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 <link
86 * linkend="g-main-context-push-thread-default">thread-default main
87 * context</link>. 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:
111 * baked_cb (Cake *cake,
112 * gpointer user_data)
114 * /* In this example, this callback is not given a reference to the cake, so
115 * * the GSimpleAsyncResult has to take a reference to it.
117 * GSimpleAsyncResult *result = user_data;
120 * g_simple_async_result_set_error (result,
122 * BAKER_ERROR_NO_FLOUR,
123 * "Go to the supermarket");
125 * g_simple_async_result_set_op_res_gpointer (result,
126 * g_object_ref (cake),
130 * /* In this example, we assume that baked_cb is called as a callback from
131 * * the mainloop, so it's safe to complete the operation synchronously here.
132 * * If, however, _baker_prepare_cake () might call its callback without
133 * * first returning to the mainloop — inadvisable, but some APIs do so —
134 * * we would need to use g_simple_async_result_complete_in_idle().
136 * g_simple_async_result_complete (result);
137 * g_object_unref (result);
141 * baker_bake_cake_async (Baker *self,
143 * GAsyncReadyCallback callback,
144 * gpointer user_data)
146 * GSimpleAsyncResult *simple;
151 * g_simple_async_report_error_in_idle (G_OBJECT (self),
155 * BAKER_ERROR_TOO_SMALL,
156 * "%ucm radius cakes are silly",
161 * simple = g_simple_async_result_new (G_OBJECT (self),
164 * baker_bake_cake_async);
165 * cake = _baker_get_cached_cake (self, radius);
169 * g_simple_async_result_set_op_res_gpointer (simple,
170 * g_object_ref (cake),
172 * g_simple_async_result_complete_in_idle (simple);
173 * g_object_unref (simple);
174 * /* Drop the reference returned by _baker_get_cached_cake(); the
175 * * GSimpleAsyncResult has taken its own reference.
177 * g_object_unref (cake);
181 * _baker_prepare_cake (self, radius, baked_cb, simple);
185 * baker_bake_cake_finish (Baker *self,
186 * GAsyncResult *result,
189 * GSimpleAsyncResult *simple;
192 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
194 * baker_bake_cake_async),
197 * simple = (GSimpleAsyncResult *) result;
199 * if (g_simple_async_result_propagate_error (simple, error))
202 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
203 * return g_object_ref (cake);
208 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
210 struct _GSimpleAsyncResult
212 GObject parent_instance;
214 GObject *source_object;
215 GAsyncReadyCallback callback;
217 GMainContext *context;
220 gboolean handle_cancellation;
221 GCancellable *check_cancellable;
231 GDestroyNotify destroy_op_res;
234 struct _GSimpleAsyncResultClass
236 GObjectClass parent_class;
240 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
241 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
242 g_simple_async_result_async_result_iface_init))
245 clear_op_res (GSimpleAsyncResult *simple)
247 if (simple->destroy_op_res)
248 simple->destroy_op_res (simple->op_res.v_pointer);
249 simple->destroy_op_res = NULL;
250 simple->op_res.v_ssize = 0;
254 g_simple_async_result_finalize (GObject *object)
256 GSimpleAsyncResult *simple;
258 simple = G_SIMPLE_ASYNC_RESULT (object);
260 if (simple->source_object)
261 g_object_unref (simple->source_object);
263 if (simple->check_cancellable)
264 g_object_unref (simple->check_cancellable);
266 g_main_context_unref (simple->context);
268 clear_op_res (simple);
271 g_error_free (simple->error);
273 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
277 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
279 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281 gobject_class->finalize = g_simple_async_result_finalize;
285 g_simple_async_result_init (GSimpleAsyncResult *simple)
287 simple->handle_cancellation = TRUE;
289 simple->context = g_main_context_ref_thread_default ();
293 * g_simple_async_result_new:
294 * @source_object: (allow-none): a #GObject, or %NULL.
295 * @callback: (scope async): a #GAsyncReadyCallback.
296 * @user_data: (closure): user data passed to @callback.
297 * @source_tag: the asynchronous function.
299 * Creates a #GSimpleAsyncResult.
301 * The common convention is to create the #GSimpleAsyncResult in the
302 * function that starts the asynchronous operation and use that same
303 * function as the @source_tag.
305 * If your operation supports cancellation with #GCancellable (which it
306 * probably should) then you should provide the user's cancellable to
307 * g_simple_async_result_set_check_cancellable() immediately after
308 * this function returns.
310 * Returns: a #GSimpleAsyncResult.
313 g_simple_async_result_new (GObject *source_object,
314 GAsyncReadyCallback callback,
318 GSimpleAsyncResult *simple;
320 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
322 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
323 simple->callback = callback;
325 simple->source_object = g_object_ref (source_object);
327 simple->source_object = NULL;
328 simple->user_data = user_data;
329 simple->source_tag = source_tag;
335 * g_simple_async_result_new_from_error:
336 * @source_object: (allow-none): a #GObject, or %NULL.
337 * @callback: (scope async): a #GAsyncReadyCallback.
338 * @user_data: (closure): user data passed to @callback.
341 * Creates a #GSimpleAsyncResult from an error condition.
343 * Returns: a #GSimpleAsyncResult.
346 g_simple_async_result_new_from_error (GObject *source_object,
347 GAsyncReadyCallback callback,
351 GSimpleAsyncResult *simple;
353 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
355 simple = g_simple_async_result_new (source_object,
358 g_simple_async_result_set_from_error (simple, error);
364 * g_simple_async_result_new_take_error: (skip)
365 * @source_object: (allow-none): a #GObject, or %NULL
366 * @callback: (scope async): a #GAsyncReadyCallback
367 * @user_data: (closure): user data passed to @callback
370 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
371 * caller's ownership of @error, so the caller does not need to free it anymore.
373 * Returns: a #GSimpleAsyncResult
378 g_simple_async_result_new_take_error (GObject *source_object,
379 GAsyncReadyCallback callback,
383 GSimpleAsyncResult *simple;
385 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
387 simple = g_simple_async_result_new (source_object,
390 g_simple_async_result_take_error (simple, error);
396 * g_simple_async_result_new_error:
397 * @source_object: (allow-none): a #GObject, or %NULL.
398 * @callback: (scope async): a #GAsyncReadyCallback.
399 * @user_data: (closure): user data passed to @callback.
400 * @domain: a #GQuark.
401 * @code: an error code.
402 * @format: a string with format characters.
403 * @...: a list of values to insert into @format.
405 * Creates a new #GSimpleAsyncResult with a set error.
407 * Returns: a #GSimpleAsyncResult.
410 g_simple_async_result_new_error (GObject *source_object,
411 GAsyncReadyCallback callback,
418 GSimpleAsyncResult *simple;
421 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
422 g_return_val_if_fail (domain != 0, NULL);
423 g_return_val_if_fail (format != NULL, NULL);
425 simple = g_simple_async_result_new (source_object,
429 va_start (args, format);
430 g_simple_async_result_set_error_va (simple, domain, code, format, args);
438 g_simple_async_result_get_user_data (GAsyncResult *res)
440 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
444 g_simple_async_result_get_source_object (GAsyncResult *res)
446 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
447 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
452 g_simple_async_result_is_tagged (GAsyncResult *res,
455 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
459 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
461 iface->get_user_data = g_simple_async_result_get_user_data;
462 iface->get_source_object = g_simple_async_result_get_source_object;
463 iface->is_tagged = g_simple_async_result_is_tagged;
467 * g_simple_async_result_set_handle_cancellation:
468 * @simple: a #GSimpleAsyncResult.
469 * @handle_cancellation: a #gboolean.
471 * Sets whether to handle cancellation within the asynchronous operation.
473 * This function has nothing to do with
474 * g_simple_async_result_set_check_cancellable(). It only refers to the
475 * #GCancellable passed to g_simple_async_result_run_in_thread().
478 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
479 gboolean handle_cancellation)
481 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
482 simple->handle_cancellation = handle_cancellation;
486 * g_simple_async_result_get_source_tag: (skip)
487 * @simple: a #GSimpleAsyncResult.
489 * Gets the source tag for the #GSimpleAsyncResult.
491 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
494 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
496 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
497 return simple->source_tag;
501 * g_simple_async_result_propagate_error:
502 * @simple: a #GSimpleAsyncResult.
503 * @dest: (out): a location to propagate the error to.
505 * Propagates an error from within the simple asynchronous result to
506 * a given destination.
508 * If the #GCancellable given to a prior call to
509 * g_simple_async_result_set_check_cancellable() is cancelled then this
510 * function will return %TRUE with @dest set appropriately.
512 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
515 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
518 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
520 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
525 g_propagate_error (dest, simple->error);
526 simple->error = NULL;
534 * g_simple_async_result_set_op_res_gpointer: (skip)
535 * @simple: a #GSimpleAsyncResult.
536 * @op_res: a pointer result from an asynchronous function.
537 * @destroy_op_res: a #GDestroyNotify function.
539 * Sets the operation result within the asynchronous result to a pointer.
542 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
544 GDestroyNotify destroy_op_res)
546 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
548 clear_op_res (simple);
549 simple->op_res.v_pointer = op_res;
550 simple->destroy_op_res = destroy_op_res;
554 * g_simple_async_result_get_op_res_gpointer: (skip)
555 * @simple: a #GSimpleAsyncResult.
557 * Gets a pointer result as returned by the asynchronous function.
559 * Returns: a pointer from the result.
562 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
564 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
565 return simple->op_res.v_pointer;
569 * g_simple_async_result_set_op_res_gssize:
570 * @simple: a #GSimpleAsyncResult.
571 * @op_res: a #gssize.
573 * Sets the operation result within the asynchronous result to
577 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
580 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
581 clear_op_res (simple);
582 simple->op_res.v_ssize = op_res;
586 * g_simple_async_result_get_op_res_gssize:
587 * @simple: a #GSimpleAsyncResult.
589 * Gets a gssize from the asynchronous result.
591 * Returns: a gssize returned from the asynchronous function.
594 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
596 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
597 return simple->op_res.v_ssize;
601 * g_simple_async_result_set_op_res_gboolean:
602 * @simple: a #GSimpleAsyncResult.
603 * @op_res: a #gboolean.
605 * Sets the operation result to a boolean within the asynchronous result.
608 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
611 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
612 clear_op_res (simple);
613 simple->op_res.v_boolean = !!op_res;
617 * g_simple_async_result_get_op_res_gboolean:
618 * @simple: a #GSimpleAsyncResult.
620 * Gets the operation result boolean from within the asynchronous result.
622 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
623 * if the operation's result was %FALSE.
626 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
628 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
629 return simple->op_res.v_boolean;
633 * g_simple_async_result_set_from_error:
634 * @simple: a #GSimpleAsyncResult.
637 * Sets the result from a #GError.
640 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
643 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
644 g_return_if_fail (error != NULL);
647 g_error_free (simple->error);
648 simple->error = g_error_copy (error);
649 simple->failed = TRUE;
653 * g_simple_async_result_take_error: (skip)
654 * @simple: a #GSimpleAsyncResult
657 * Sets the result from @error, and takes over the caller's ownership
658 * of @error, so the caller does not need to free it any more.
663 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
666 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
667 g_return_if_fail (error != NULL);
670 g_error_free (simple->error);
671 simple->error = error;
672 simple->failed = TRUE;
676 * g_simple_async_result_set_error_va: (skip)
677 * @simple: a #GSimpleAsyncResult.
678 * @domain: a #GQuark (usually #G_IO_ERROR).
679 * @code: an error code.
680 * @format: a formatted error reporting string.
681 * @args: va_list of arguments.
683 * Sets an error within the asynchronous result without a #GError.
684 * Unless writing a binding, see g_simple_async_result_set_error().
687 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
693 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
694 g_return_if_fail (domain != 0);
695 g_return_if_fail (format != NULL);
698 g_error_free (simple->error);
699 simple->error = g_error_new_valist (domain, code, format, args);
700 simple->failed = TRUE;
704 * g_simple_async_result_set_error: (skip)
705 * @simple: a #GSimpleAsyncResult.
706 * @domain: a #GQuark (usually #G_IO_ERROR).
707 * @code: an error code.
708 * @format: a formatted error reporting string.
709 * @...: a list of variables to fill in @format.
711 * Sets an error within the asynchronous result without a #GError.
714 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
722 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
723 g_return_if_fail (domain != 0);
724 g_return_if_fail (format != NULL);
726 va_start (args, format);
727 g_simple_async_result_set_error_va (simple, domain, code, format, args);
732 * g_simple_async_result_complete:
733 * @simple: a #GSimpleAsyncResult.
735 * Completes an asynchronous I/O job immediately. Must be called in
736 * the thread where the asynchronous result was to be delivered, as it
737 * invokes the callback directly. If you are in a different thread use
738 * g_simple_async_result_complete_in_idle().
740 * Calling this function takes a reference to @simple for as long as
741 * is needed to complete the call.
744 g_simple_async_result_complete (GSimpleAsyncResult *simple)
746 #ifndef G_DISABLE_CHECKS
747 GSource *current_source;
748 GMainContext *current_context;
751 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
753 #ifndef G_DISABLE_CHECKS
754 current_source = g_main_current_source ();
755 if (current_source && !g_source_is_destroyed (current_source))
757 current_context = g_source_get_context (current_source);
758 if (simple->context != current_context)
759 g_warning ("g_simple_async_result_complete() called from wrong context!");
763 if (simple->callback)
765 g_main_context_push_thread_default (simple->context);
766 simple->callback (simple->source_object,
767 G_ASYNC_RESULT (simple),
769 g_main_context_pop_thread_default (simple->context);
774 complete_in_idle_cb (gpointer data)
776 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
778 g_simple_async_result_complete (simple);
784 * g_simple_async_result_complete_in_idle:
785 * @simple: a #GSimpleAsyncResult.
787 * Completes an asynchronous function in an idle handler in the <link
788 * linkend="g-main-context-push-thread-default">thread-default main
789 * loop</link> of the thread that @simple was initially created in
790 * (and re-pushes that context around the invocation of the callback).
792 * Calling this function takes a reference to @simple for as long as
793 * is needed to complete the call.
796 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
800 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
802 g_object_ref (simple);
804 source = g_idle_source_new ();
805 g_source_set_priority (source, G_PRIORITY_DEFAULT);
806 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
808 g_source_attach (source, simple->context);
809 g_source_unref (source);
813 GSimpleAsyncResult *simple;
814 GCancellable *cancellable;
815 GSimpleAsyncThreadFunc func;
820 complete_in_idle_cb_for_thread (gpointer _data)
822 RunInThreadData *data = _data;
823 GSimpleAsyncResult *simple;
825 simple = data->simple;
827 if (simple->handle_cancellation &&
828 g_cancellable_is_cancelled (data->cancellable))
829 g_simple_async_result_set_error (simple,
831 G_IO_ERROR_CANCELLED,
832 "%s", _("Operation was cancelled"));
834 g_simple_async_result_complete (simple);
836 if (data->cancellable)
837 g_object_unref (data->cancellable);
838 g_object_unref (data->simple);
845 run_in_thread (GIOSchedulerJob *job,
849 RunInThreadData *data = _data;
850 GSimpleAsyncResult *simple = data->simple;
853 if (simple->handle_cancellation &&
854 g_cancellable_is_cancelled (c))
855 g_simple_async_result_set_error (simple,
857 G_IO_ERROR_CANCELLED,
858 "%s", _("Operation was cancelled"));
861 simple->source_object,
864 source = g_idle_source_new ();
865 g_source_set_priority (source, G_PRIORITY_DEFAULT);
866 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
868 g_source_attach (source, simple->context);
869 g_source_unref (source);
875 * g_simple_async_result_run_in_thread: (skip)
876 * @simple: a #GSimpleAsyncResult.
877 * @func: a #GSimpleAsyncThreadFunc.
878 * @io_priority: the io priority of the request.
879 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
881 * Runs the asynchronous job in a separate thread and then calls
882 * g_simple_async_result_complete_in_idle() on @simple to return
883 * the result to the appropriate main loop.
885 * Calling this function takes a reference to @simple for as long as
886 * is needed to run the job and report its completion.
889 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
890 GSimpleAsyncThreadFunc func,
892 GCancellable *cancellable)
894 RunInThreadData *data;
896 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
897 g_return_if_fail (func != NULL);
899 data = g_new (RunInThreadData, 1);
901 data->simple = g_object_ref (simple);
902 data->cancellable = cancellable;
904 g_object_ref (cancellable);
905 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
906 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
907 G_GNUC_END_IGNORE_DEPRECATIONS;
911 * g_simple_async_result_is_valid:
912 * @result: the #GAsyncResult passed to the _finish function.
913 * @source: the #GObject passed to the _finish function.
914 * @source_tag: the asynchronous function.
916 * Ensures that the data passed to the _finish function of an async
917 * operation is consistent. Three checks are performed.
919 * First, @result is checked to ensure that it is really a
920 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
921 * matches the source object of @result. Third, @source_tag is
922 * checked to ensure that it is either %NULL (as it is when the result was
923 * created by g_simple_async_report_error_in_idle() or
924 * g_simple_async_report_gerror_in_idle()) or equal to the
925 * @source_tag argument given to g_simple_async_result_new() (which, by
926 * convention, is a pointer to the _async function corresponding to the
927 * _finish function from which this function is called).
929 * Returns: #TRUE if all checks passed or #FALSE if any failed.
934 g_simple_async_result_is_valid (GAsyncResult *result,
938 GSimpleAsyncResult *simple;
941 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
943 simple = (GSimpleAsyncResult *)result;
945 cmp_source = g_async_result_get_source_object (result);
946 if (cmp_source != source)
948 if (cmp_source != NULL)
949 g_object_unref (cmp_source);
952 if (cmp_source != NULL)
953 g_object_unref (cmp_source);
955 return source_tag == NULL ||
956 source_tag == g_simple_async_result_get_source_tag (simple);
960 * g_simple_async_report_error_in_idle: (skip)
961 * @object: (allow-none): a #GObject, or %NULL.
962 * @callback: a #GAsyncReadyCallback.
963 * @user_data: user data passed to @callback.
964 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
965 * @code: a specific error code.
966 * @format: a formatted error reporting string.
967 * @...: a list of variables to fill in @format.
969 * Reports an error in an asynchronous function in an idle function by
970 * directly setting the contents of the #GAsyncResult with the given error
974 g_simple_async_report_error_in_idle (GObject *object,
975 GAsyncReadyCallback callback,
982 GSimpleAsyncResult *simple;
985 g_return_if_fail (!object || G_IS_OBJECT (object));
986 g_return_if_fail (domain != 0);
987 g_return_if_fail (format != NULL);
989 simple = g_simple_async_result_new (object,
993 va_start (args, format);
994 g_simple_async_result_set_error_va (simple, domain, code, format, args);
996 g_simple_async_result_complete_in_idle (simple);
997 g_object_unref (simple);
1001 * g_simple_async_report_gerror_in_idle:
1002 * @object: (allow-none): a #GObject, or %NULL
1003 * @callback: (scope async): a #GAsyncReadyCallback.
1004 * @user_data: (closure): user data passed to @callback.
1005 * @error: the #GError to report
1007 * Reports an error in an idle function. Similar to
1008 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1009 * than building a new one.
1012 g_simple_async_report_gerror_in_idle (GObject *object,
1013 GAsyncReadyCallback callback,
1015 const GError *error)
1017 GSimpleAsyncResult *simple;
1019 g_return_if_fail (!object || G_IS_OBJECT (object));
1020 g_return_if_fail (error != NULL);
1022 simple = g_simple_async_result_new_from_error (object,
1026 g_simple_async_result_complete_in_idle (simple);
1027 g_object_unref (simple);
1031 * g_simple_async_report_take_gerror_in_idle: (skip)
1032 * @object: (allow-none): a #GObject, or %NULL
1033 * @callback: a #GAsyncReadyCallback.
1034 * @user_data: user data passed to @callback.
1035 * @error: the #GError to report
1037 * Reports an error in an idle function. Similar to
1038 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1039 * ownership of @error, so the caller does not have to free it any more.
1044 g_simple_async_report_take_gerror_in_idle (GObject *object,
1045 GAsyncReadyCallback callback,
1049 GSimpleAsyncResult *simple;
1051 g_return_if_fail (!object || G_IS_OBJECT (object));
1052 g_return_if_fail (error != NULL);
1054 simple = g_simple_async_result_new_take_error (object,
1058 g_simple_async_result_complete_in_idle (simple);
1059 g_object_unref (simple);
1063 * g_simple_async_result_set_check_cancellable:
1064 * @simple: a #GSimpleAsyncResult
1065 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1067 * Sets a #GCancellable to check before dispatching results.
1069 * This function has one very specific purpose: the provided cancellable
1070 * is checked at the time of g_simple_async_result_propagate_error() If
1071 * it is cancelled, these functions will return an "Operation was
1072 * cancelled" error (%G_IO_ERROR_CANCELLED).
1074 * Implementors of cancellable asynchronous functions should use this in
1075 * order to provide a guarantee to their callers that cancelling an
1076 * async operation will reliably result in an error being returned for
1077 * that operation (even if a positive result for the operation has
1078 * already been sent as an idle to the main context to be dispatched).
1080 * The checking described above is done regardless of any call to the
1081 * unrelated g_simple_async_result_set_handle_cancellation() function.
1086 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1087 GCancellable *check_cancellable)
1089 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1090 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1092 g_clear_object (&simple->check_cancellable);
1093 if (check_cancellable)
1094 simple->check_cancellable = g_object_ref (check_cancellable);