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
49 * As of GLib 2.36, #GSimpleAsyncResult is deprecated in favor of
50 * #GTask, which provides a simpler API.
53 * #GSimpleAsyncResult implements #GAsyncResult.
55 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
56 * reporting, operation cancellation and the final state of an operation,
57 * completely transparent to the application. Results can be returned
58 * as a pointer e.g. for functions that return data that is collected
59 * asynchronously, a boolean value for checking the success or failure
60 * of an operation, or a #gssize for operations which return the number
61 * of bytes modified by the operation; all of the simple return cases
64 * Most of the time, an application will not need to know of the details
65 * of this API; it is handled transparently, and any necessary operations
66 * are handled by #GAsyncResult's interface. However, if implementing a
67 * new GIO module, for writing language bindings, or for complex
68 * applications that need better control of how asynchronous operations
69 * are completed, it is important to understand this functionality.
71 * GSimpleAsyncResults are tagged with the calling function to ensure
72 * that asynchronous functions and their finishing functions are used
75 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
76 * If the result needs to be created for a #GError, use
77 * g_simple_async_result_new_from_error() or
78 * g_simple_async_result_new_take_error(). If a #GError is not available
79 * (e.g. the asynchronous operation's doesn't take a #GError argument),
80 * but the result still needs to be created for an error condition, use
81 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
82 * if your application or binding requires passing a variable argument list
83 * directly), and the error can then be propagated through the use of
84 * g_simple_async_result_propagate_error().
86 * An asynchronous operation can be made to ignore a cancellation event by
87 * calling g_simple_async_result_set_handle_cancellation() with a
88 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
89 * operations that are dangerous to cancel, such as close (which would
90 * cause a leak if cancelled before being run).
92 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
93 * or it can use #GThread<!-- -->s.
94 * g_simple_async_result_complete() will finish an I/O task directly
95 * from the point where it is called. g_simple_async_result_complete_in_idle()
96 * will finish it from an idle handler in the <link
97 * linkend="g-main-context-push-thread-default">thread-default main
98 * context</link>. g_simple_async_result_run_in_thread() will run the
99 * job in a separate thread and then deliver the result to the
100 * thread-default main context.
102 * To set the results of an asynchronous function,
103 * g_simple_async_result_set_op_res_gpointer(),
104 * g_simple_async_result_set_op_res_gboolean(), and
105 * g_simple_async_result_set_op_res_gssize()
106 * are provided, setting the operation's result to a gpointer, gboolean, or
107 * gssize, respectively.
109 * Likewise, to get the result of an asynchronous function,
110 * g_simple_async_result_get_op_res_gpointer(),
111 * g_simple_async_result_get_op_res_gboolean(), and
112 * g_simple_async_result_get_op_res_gssize() are
113 * provided, getting the operation's result as a gpointer, gboolean, and
114 * gssize, respectively.
116 * For the details of the requirements implementations must respect, see
117 * #GAsyncResult. A typical implementation of an asynchronous operation
118 * using GSimpleAsyncResult looks something like this:
122 * baked_cb (Cake *cake,
123 * gpointer user_data)
125 * /* In this example, this callback is not given a reference to the cake, so
126 * * the GSimpleAsyncResult has to take a reference to it.
128 * GSimpleAsyncResult *result = user_data;
131 * g_simple_async_result_set_error (result,
133 * BAKER_ERROR_NO_FLOUR,
134 * "Go to the supermarket");
136 * g_simple_async_result_set_op_res_gpointer (result,
137 * g_object_ref (cake),
141 * /* In this example, we assume that baked_cb is called as a callback from
142 * * the mainloop, so it's safe to complete the operation synchronously here.
143 * * If, however, _baker_prepare_cake () might call its callback without
144 * * first returning to the mainloop — inadvisable, but some APIs do so —
145 * * we would need to use g_simple_async_result_complete_in_idle().
147 * g_simple_async_result_complete (result);
148 * g_object_unref (result);
152 * baker_bake_cake_async (Baker *self,
154 * GAsyncReadyCallback callback,
155 * gpointer user_data)
157 * GSimpleAsyncResult *simple;
162 * g_simple_async_report_error_in_idle (G_OBJECT (self),
166 * BAKER_ERROR_TOO_SMALL,
167 * "%ucm radius cakes are silly",
172 * simple = g_simple_async_result_new (G_OBJECT (self),
175 * baker_bake_cake_async);
176 * cake = _baker_get_cached_cake (self, radius);
180 * g_simple_async_result_set_op_res_gpointer (simple,
181 * g_object_ref (cake),
183 * g_simple_async_result_complete_in_idle (simple);
184 * g_object_unref (simple);
185 * /* Drop the reference returned by _baker_get_cached_cake(); the
186 * * GSimpleAsyncResult has taken its own reference.
188 * g_object_unref (cake);
192 * _baker_prepare_cake (self, radius, baked_cb, simple);
196 * baker_bake_cake_finish (Baker *self,
197 * GAsyncResult *result,
200 * GSimpleAsyncResult *simple;
203 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
205 * baker_bake_cake_async),
208 * simple = (GSimpleAsyncResult *) result;
210 * if (g_simple_async_result_propagate_error (simple, error))
213 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
214 * return g_object_ref (cake);
219 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
221 struct _GSimpleAsyncResult
223 GObject parent_instance;
225 GObject *source_object;
226 GAsyncReadyCallback callback;
228 GMainContext *context;
231 gboolean handle_cancellation;
232 GCancellable *check_cancellable;
242 GDestroyNotify destroy_op_res;
245 struct _GSimpleAsyncResultClass
247 GObjectClass parent_class;
251 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
252 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
253 g_simple_async_result_async_result_iface_init))
256 clear_op_res (GSimpleAsyncResult *simple)
258 if (simple->destroy_op_res)
259 simple->destroy_op_res (simple->op_res.v_pointer);
260 simple->destroy_op_res = NULL;
261 simple->op_res.v_ssize = 0;
265 g_simple_async_result_finalize (GObject *object)
267 GSimpleAsyncResult *simple;
269 simple = G_SIMPLE_ASYNC_RESULT (object);
271 if (simple->source_object)
272 g_object_unref (simple->source_object);
274 if (simple->check_cancellable)
275 g_object_unref (simple->check_cancellable);
277 g_main_context_unref (simple->context);
279 clear_op_res (simple);
282 g_error_free (simple->error);
284 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
288 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
290 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
292 gobject_class->finalize = g_simple_async_result_finalize;
296 g_simple_async_result_init (GSimpleAsyncResult *simple)
298 simple->handle_cancellation = TRUE;
300 simple->context = g_main_context_ref_thread_default ();
304 * g_simple_async_result_new:
305 * @source_object: (allow-none): a #GObject, or %NULL.
306 * @callback: (scope async): a #GAsyncReadyCallback.
307 * @user_data: (closure): user data passed to @callback.
308 * @source_tag: the asynchronous function.
310 * Creates a #GSimpleAsyncResult.
312 * The common convention is to create the #GSimpleAsyncResult in the
313 * function that starts the asynchronous operation and use that same
314 * function as the @source_tag.
316 * If your operation supports cancellation with #GCancellable (which it
317 * probably should) then you should provide the user's cancellable to
318 * g_simple_async_result_set_check_cancellable() immediately after
319 * this function returns.
321 * Returns: a #GSimpleAsyncResult.
324 g_simple_async_result_new (GObject *source_object,
325 GAsyncReadyCallback callback,
329 GSimpleAsyncResult *simple;
331 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
333 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
334 simple->callback = callback;
336 simple->source_object = g_object_ref (source_object);
338 simple->source_object = NULL;
339 simple->user_data = user_data;
340 simple->source_tag = source_tag;
346 * g_simple_async_result_new_from_error:
347 * @source_object: (allow-none): a #GObject, or %NULL.
348 * @callback: (scope async): a #GAsyncReadyCallback.
349 * @user_data: (closure): user data passed to @callback.
352 * Creates a #GSimpleAsyncResult from an error condition.
354 * Returns: a #GSimpleAsyncResult.
357 g_simple_async_result_new_from_error (GObject *source_object,
358 GAsyncReadyCallback callback,
362 GSimpleAsyncResult *simple;
364 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
366 simple = g_simple_async_result_new (source_object,
369 g_simple_async_result_set_from_error (simple, error);
375 * g_simple_async_result_new_take_error: (skip)
376 * @source_object: (allow-none): a #GObject, or %NULL
377 * @callback: (scope async): a #GAsyncReadyCallback
378 * @user_data: (closure): user data passed to @callback
381 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
382 * caller's ownership of @error, so the caller does not need to free it anymore.
384 * Returns: a #GSimpleAsyncResult
389 g_simple_async_result_new_take_error (GObject *source_object,
390 GAsyncReadyCallback callback,
394 GSimpleAsyncResult *simple;
396 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
398 simple = g_simple_async_result_new (source_object,
401 g_simple_async_result_take_error (simple, error);
407 * g_simple_async_result_new_error:
408 * @source_object: (allow-none): a #GObject, or %NULL.
409 * @callback: (scope async): a #GAsyncReadyCallback.
410 * @user_data: (closure): user data passed to @callback.
411 * @domain: a #GQuark.
412 * @code: an error code.
413 * @format: a string with format characters.
414 * @...: a list of values to insert into @format.
416 * Creates a new #GSimpleAsyncResult with a set error.
418 * Returns: a #GSimpleAsyncResult.
421 g_simple_async_result_new_error (GObject *source_object,
422 GAsyncReadyCallback callback,
429 GSimpleAsyncResult *simple;
432 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
433 g_return_val_if_fail (domain != 0, NULL);
434 g_return_val_if_fail (format != NULL, NULL);
436 simple = g_simple_async_result_new (source_object,
440 va_start (args, format);
441 g_simple_async_result_set_error_va (simple, domain, code, format, args);
449 g_simple_async_result_get_user_data (GAsyncResult *res)
451 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
455 g_simple_async_result_get_source_object (GAsyncResult *res)
457 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
458 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
463 g_simple_async_result_is_tagged (GAsyncResult *res,
466 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
470 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
472 iface->get_user_data = g_simple_async_result_get_user_data;
473 iface->get_source_object = g_simple_async_result_get_source_object;
474 iface->is_tagged = g_simple_async_result_is_tagged;
478 * g_simple_async_result_set_handle_cancellation:
479 * @simple: a #GSimpleAsyncResult.
480 * @handle_cancellation: a #gboolean.
482 * Sets whether to handle cancellation within the asynchronous operation.
484 * This function has nothing to do with
485 * g_simple_async_result_set_check_cancellable(). It only refers to the
486 * #GCancellable passed to g_simple_async_result_run_in_thread().
489 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
490 gboolean handle_cancellation)
492 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
493 simple->handle_cancellation = handle_cancellation;
497 * g_simple_async_result_get_source_tag: (skip)
498 * @simple: a #GSimpleAsyncResult.
500 * Gets the source tag for the #GSimpleAsyncResult.
502 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
505 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
507 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
508 return simple->source_tag;
512 * g_simple_async_result_propagate_error:
513 * @simple: a #GSimpleAsyncResult.
514 * @dest: (out): a location to propagate the error to.
516 * Propagates an error from within the simple asynchronous result to
517 * a given destination.
519 * If the #GCancellable given to a prior call to
520 * g_simple_async_result_set_check_cancellable() is cancelled then this
521 * function will return %TRUE with @dest set appropriately.
523 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
526 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
529 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
531 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
536 g_propagate_error (dest, simple->error);
537 simple->error = NULL;
545 * g_simple_async_result_set_op_res_gpointer: (skip)
546 * @simple: a #GSimpleAsyncResult.
547 * @op_res: a pointer result from an asynchronous function.
548 * @destroy_op_res: a #GDestroyNotify function.
550 * Sets the operation result within the asynchronous result to a pointer.
553 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
555 GDestroyNotify destroy_op_res)
557 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
559 clear_op_res (simple);
560 simple->op_res.v_pointer = op_res;
561 simple->destroy_op_res = destroy_op_res;
565 * g_simple_async_result_get_op_res_gpointer: (skip)
566 * @simple: a #GSimpleAsyncResult.
568 * Gets a pointer result as returned by the asynchronous function.
570 * Returns: a pointer from the result.
573 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
575 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
576 return simple->op_res.v_pointer;
580 * g_simple_async_result_set_op_res_gssize:
581 * @simple: a #GSimpleAsyncResult.
582 * @op_res: a #gssize.
584 * Sets the operation result within the asynchronous result to
588 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
591 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
592 clear_op_res (simple);
593 simple->op_res.v_ssize = op_res;
597 * g_simple_async_result_get_op_res_gssize:
598 * @simple: a #GSimpleAsyncResult.
600 * Gets a gssize from the asynchronous result.
602 * Returns: a gssize returned from the asynchronous function.
605 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
607 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
608 return simple->op_res.v_ssize;
612 * g_simple_async_result_set_op_res_gboolean:
613 * @simple: a #GSimpleAsyncResult.
614 * @op_res: a #gboolean.
616 * Sets the operation result to a boolean within the asynchronous result.
619 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
622 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
623 clear_op_res (simple);
624 simple->op_res.v_boolean = !!op_res;
628 * g_simple_async_result_get_op_res_gboolean:
629 * @simple: a #GSimpleAsyncResult.
631 * Gets the operation result boolean from within the asynchronous result.
633 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
634 * if the operation's result was %FALSE.
637 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
639 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
640 return simple->op_res.v_boolean;
644 * g_simple_async_result_set_from_error:
645 * @simple: a #GSimpleAsyncResult.
648 * Sets the result from a #GError.
651 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
654 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
655 g_return_if_fail (error != NULL);
658 g_error_free (simple->error);
659 simple->error = g_error_copy (error);
660 simple->failed = TRUE;
664 * g_simple_async_result_take_error: (skip)
665 * @simple: a #GSimpleAsyncResult
668 * Sets the result from @error, and takes over the caller's ownership
669 * of @error, so the caller does not need to free it any more.
674 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
677 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
678 g_return_if_fail (error != NULL);
681 g_error_free (simple->error);
682 simple->error = error;
683 simple->failed = TRUE;
687 * g_simple_async_result_set_error_va: (skip)
688 * @simple: a #GSimpleAsyncResult.
689 * @domain: a #GQuark (usually #G_IO_ERROR).
690 * @code: an error code.
691 * @format: a formatted error reporting string.
692 * @args: va_list of arguments.
694 * Sets an error within the asynchronous result without a #GError.
695 * Unless writing a binding, see g_simple_async_result_set_error().
698 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
704 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
705 g_return_if_fail (domain != 0);
706 g_return_if_fail (format != NULL);
709 g_error_free (simple->error);
710 simple->error = g_error_new_valist (domain, code, format, args);
711 simple->failed = TRUE;
715 * g_simple_async_result_set_error: (skip)
716 * @simple: a #GSimpleAsyncResult.
717 * @domain: a #GQuark (usually #G_IO_ERROR).
718 * @code: an error code.
719 * @format: a formatted error reporting string.
720 * @...: a list of variables to fill in @format.
722 * Sets an error within the asynchronous result without a #GError.
725 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
733 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
734 g_return_if_fail (domain != 0);
735 g_return_if_fail (format != NULL);
737 va_start (args, format);
738 g_simple_async_result_set_error_va (simple, domain, code, format, args);
743 * g_simple_async_result_complete:
744 * @simple: a #GSimpleAsyncResult.
746 * Completes an asynchronous I/O job immediately. Must be called in
747 * the thread where the asynchronous result was to be delivered, as it
748 * invokes the callback directly. If you are in a different thread use
749 * g_simple_async_result_complete_in_idle().
751 * Calling this function takes a reference to @simple for as long as
752 * is needed to complete the call.
755 g_simple_async_result_complete (GSimpleAsyncResult *simple)
757 #ifndef G_DISABLE_CHECKS
758 GSource *current_source;
759 GMainContext *current_context;
762 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
764 #ifndef G_DISABLE_CHECKS
765 current_source = g_main_current_source ();
766 if (current_source && !g_source_is_destroyed (current_source))
768 current_context = g_source_get_context (current_source);
769 if (simple->context != current_context)
770 g_warning ("g_simple_async_result_complete() called from wrong context!");
774 if (simple->callback)
776 g_main_context_push_thread_default (simple->context);
777 simple->callback (simple->source_object,
778 G_ASYNC_RESULT (simple),
780 g_main_context_pop_thread_default (simple->context);
785 complete_in_idle_cb (gpointer data)
787 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
789 g_simple_async_result_complete (simple);
795 * g_simple_async_result_complete_in_idle:
796 * @simple: a #GSimpleAsyncResult.
798 * Completes an asynchronous function in an idle handler in the <link
799 * linkend="g-main-context-push-thread-default">thread-default main
800 * loop</link> of the thread that @simple was initially created in
801 * (and re-pushes that context around the invocation of the callback).
803 * Calling this function takes a reference to @simple for as long as
804 * is needed to complete the call.
807 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
811 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
813 g_object_ref (simple);
815 source = g_idle_source_new ();
816 g_source_set_priority (source, G_PRIORITY_DEFAULT);
817 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
819 g_source_attach (source, simple->context);
820 g_source_unref (source);
824 GSimpleAsyncResult *simple;
825 GCancellable *cancellable;
826 GSimpleAsyncThreadFunc func;
831 complete_in_idle_cb_for_thread (gpointer _data)
833 RunInThreadData *data = _data;
834 GSimpleAsyncResult *simple;
836 simple = data->simple;
838 if (simple->handle_cancellation &&
839 g_cancellable_is_cancelled (data->cancellable))
840 g_simple_async_result_set_error (simple,
842 G_IO_ERROR_CANCELLED,
843 "%s", _("Operation was cancelled"));
845 g_simple_async_result_complete (simple);
847 if (data->cancellable)
848 g_object_unref (data->cancellable);
849 g_object_unref (data->simple);
856 run_in_thread (GIOSchedulerJob *job,
860 RunInThreadData *data = _data;
861 GSimpleAsyncResult *simple = data->simple;
864 if (simple->handle_cancellation &&
865 g_cancellable_is_cancelled (c))
866 g_simple_async_result_set_error (simple,
868 G_IO_ERROR_CANCELLED,
869 "%s", _("Operation was cancelled"));
872 simple->source_object,
875 source = g_idle_source_new ();
876 g_source_set_priority (source, G_PRIORITY_DEFAULT);
877 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
879 g_source_attach (source, simple->context);
880 g_source_unref (source);
886 * g_simple_async_result_run_in_thread: (skip)
887 * @simple: a #GSimpleAsyncResult.
888 * @func: a #GSimpleAsyncThreadFunc.
889 * @io_priority: the io priority of the request.
890 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
892 * Runs the asynchronous job in a separate thread and then calls
893 * g_simple_async_result_complete_in_idle() on @simple to return
894 * the result to the appropriate main loop.
896 * Calling this function takes a reference to @simple for as long as
897 * is needed to run the job and report its completion.
900 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
901 GSimpleAsyncThreadFunc func,
903 GCancellable *cancellable)
905 RunInThreadData *data;
907 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
908 g_return_if_fail (func != NULL);
910 data = g_new (RunInThreadData, 1);
912 data->simple = g_object_ref (simple);
913 data->cancellable = cancellable;
915 g_object_ref (cancellable);
916 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
917 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
918 G_GNUC_END_IGNORE_DEPRECATIONS;
922 * g_simple_async_result_is_valid:
923 * @result: the #GAsyncResult passed to the _finish function.
924 * @source: the #GObject passed to the _finish function.
925 * @source_tag: the asynchronous function.
927 * Ensures that the data passed to the _finish function of an async
928 * operation is consistent. Three checks are performed.
930 * First, @result is checked to ensure that it is really a
931 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
932 * matches the source object of @result. Third, @source_tag is
933 * checked to ensure that it is either %NULL (as it is when the result was
934 * created by g_simple_async_report_error_in_idle() or
935 * g_simple_async_report_gerror_in_idle()) or equal to the
936 * @source_tag argument given to g_simple_async_result_new() (which, by
937 * convention, is a pointer to the _async function corresponding to the
938 * _finish function from which this function is called).
940 * Returns: #TRUE if all checks passed or #FALSE if any failed.
945 g_simple_async_result_is_valid (GAsyncResult *result,
949 GSimpleAsyncResult *simple;
952 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
954 simple = (GSimpleAsyncResult *)result;
956 cmp_source = g_async_result_get_source_object (result);
957 if (cmp_source != source)
959 if (cmp_source != NULL)
960 g_object_unref (cmp_source);
963 if (cmp_source != NULL)
964 g_object_unref (cmp_source);
966 return source_tag == NULL ||
967 source_tag == g_simple_async_result_get_source_tag (simple);
971 * g_simple_async_report_error_in_idle: (skip)
972 * @object: (allow-none): a #GObject, or %NULL.
973 * @callback: a #GAsyncReadyCallback.
974 * @user_data: user data passed to @callback.
975 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
976 * @code: a specific error code.
977 * @format: a formatted error reporting string.
978 * @...: a list of variables to fill in @format.
980 * Reports an error in an asynchronous function in an idle function by
981 * directly setting the contents of the #GAsyncResult with the given error
985 g_simple_async_report_error_in_idle (GObject *object,
986 GAsyncReadyCallback callback,
993 GSimpleAsyncResult *simple;
996 g_return_if_fail (!object || G_IS_OBJECT (object));
997 g_return_if_fail (domain != 0);
998 g_return_if_fail (format != NULL);
1000 simple = g_simple_async_result_new (object,
1004 va_start (args, format);
1005 g_simple_async_result_set_error_va (simple, domain, code, format, args);
1007 g_simple_async_result_complete_in_idle (simple);
1008 g_object_unref (simple);
1012 * g_simple_async_report_gerror_in_idle:
1013 * @object: (allow-none): a #GObject, or %NULL
1014 * @callback: (scope async): a #GAsyncReadyCallback.
1015 * @user_data: (closure): user data passed to @callback.
1016 * @error: the #GError to report
1018 * Reports an error in an idle function. Similar to
1019 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1020 * than building a new one.
1023 g_simple_async_report_gerror_in_idle (GObject *object,
1024 GAsyncReadyCallback callback,
1026 const GError *error)
1028 GSimpleAsyncResult *simple;
1030 g_return_if_fail (!object || G_IS_OBJECT (object));
1031 g_return_if_fail (error != NULL);
1033 simple = g_simple_async_result_new_from_error (object,
1037 g_simple_async_result_complete_in_idle (simple);
1038 g_object_unref (simple);
1042 * g_simple_async_report_take_gerror_in_idle: (skip)
1043 * @object: (allow-none): a #GObject, or %NULL
1044 * @callback: a #GAsyncReadyCallback.
1045 * @user_data: user data passed to @callback.
1046 * @error: the #GError to report
1048 * Reports an error in an idle function. Similar to
1049 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1050 * ownership of @error, so the caller does not have to free it any more.
1055 g_simple_async_report_take_gerror_in_idle (GObject *object,
1056 GAsyncReadyCallback callback,
1060 GSimpleAsyncResult *simple;
1062 g_return_if_fail (!object || G_IS_OBJECT (object));
1063 g_return_if_fail (error != NULL);
1065 simple = g_simple_async_result_new_take_error (object,
1069 g_simple_async_result_complete_in_idle (simple);
1070 g_object_unref (simple);
1074 * g_simple_async_result_set_check_cancellable:
1075 * @simple: a #GSimpleAsyncResult
1076 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1078 * Sets a #GCancellable to check before dispatching results.
1080 * This function has one very specific purpose: the provided cancellable
1081 * is checked at the time of g_simple_async_result_propagate_error() If
1082 * it is cancelled, these functions will return an "Operation was
1083 * cancelled" error (%G_IO_ERROR_CANCELLED).
1085 * Implementors of cancellable asynchronous functions should use this in
1086 * order to provide a guarantee to their callers that cancelling an
1087 * async operation will reliably result in an error being returned for
1088 * that operation (even if a positive result for the operation has
1089 * already been sent as an idle to the main context to be dispatched).
1091 * The checking described above is done regardless of any call to the
1092 * unrelated g_simple_async_result_set_handle_cancellation() function.
1097 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1098 GCancellable *check_cancellable)
1100 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1101 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1103 g_clear_object (&simple->check_cancellable);
1104 if (check_cancellable)
1105 simple->check_cancellable = g_object_ref (check_cancellable);