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>
27 #include "gsimpleasyncresult.h"
28 #include "gasyncresult.h"
29 #include "gcancellable.h"
30 #include "gioscheduler.h"
31 #include <gio/gioerror.h>
36 * SECTION:gsimpleasyncresult
37 * @short_description: Simple asynchronous results implementation
39 * @see_also: #GAsyncResult
42 * As of GLib 2.36, #GSimpleAsyncResult is deprecated in favor of
43 * #GTask, which provides a simpler API.
46 * #GSimpleAsyncResult implements #GAsyncResult.
48 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
49 * reporting, operation cancellation and the final state of an operation,
50 * completely transparent to the application. Results can be returned
51 * as a pointer e.g. for functions that return data that is collected
52 * asynchronously, a boolean value for checking the success or failure
53 * of an operation, or a #gssize for operations which return the number
54 * of bytes modified by the operation; all of the simple return cases
57 * Most of the time, an application will not need to know of the details
58 * of this API; it is handled transparently, and any necessary operations
59 * are handled by #GAsyncResult's interface. However, if implementing a
60 * new GIO module, for writing language bindings, or for complex
61 * applications that need better control of how asynchronous operations
62 * are completed, it is important to understand this functionality.
64 * GSimpleAsyncResults are tagged with the calling function to ensure
65 * that asynchronous functions and their finishing functions are used
68 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
69 * If the result needs to be created for a #GError, use
70 * g_simple_async_result_new_from_error() or
71 * g_simple_async_result_new_take_error(). If a #GError is not available
72 * (e.g. the asynchronous operation's doesn't take a #GError argument),
73 * but the result still needs to be created for an error condition, use
74 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
75 * if your application or binding requires passing a variable argument list
76 * directly), and the error can then be propagated through the use of
77 * g_simple_async_result_propagate_error().
79 * An asynchronous operation can be made to ignore a cancellation event by
80 * calling g_simple_async_result_set_handle_cancellation() with a
81 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
82 * operations that are dangerous to cancel, such as close (which would
83 * cause a leak if cancelled before being run).
85 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
86 * or it can use #GThread<!-- -->s.
87 * g_simple_async_result_complete() will finish an I/O task directly
88 * from the point where it is called. g_simple_async_result_complete_in_idle()
89 * will finish it from an idle handler in the <link
90 * linkend="g-main-context-push-thread-default">thread-default main
91 * context</link>. g_simple_async_result_run_in_thread() will run the
92 * job in a separate thread and then deliver the result to the
93 * thread-default main context.
95 * To set the results of an asynchronous function,
96 * g_simple_async_result_set_op_res_gpointer(),
97 * g_simple_async_result_set_op_res_gboolean(), and
98 * g_simple_async_result_set_op_res_gssize()
99 * are provided, setting the operation's result to a gpointer, gboolean, or
100 * gssize, respectively.
102 * Likewise, to get the result of an asynchronous function,
103 * g_simple_async_result_get_op_res_gpointer(),
104 * g_simple_async_result_get_op_res_gboolean(), and
105 * g_simple_async_result_get_op_res_gssize() are
106 * provided, getting the operation's result as a gpointer, gboolean, and
107 * gssize, respectively.
109 * For the details of the requirements implementations must respect, see
110 * #GAsyncResult. A typical implementation of an asynchronous operation
111 * using GSimpleAsyncResult looks something like this:
115 * baked_cb (Cake *cake,
116 * gpointer user_data)
118 * /* In this example, this callback is not given a reference to the cake, so
119 * * the GSimpleAsyncResult has to take a reference to it.
121 * GSimpleAsyncResult *result = user_data;
124 * g_simple_async_result_set_error (result,
126 * BAKER_ERROR_NO_FLOUR,
127 * "Go to the supermarket");
129 * g_simple_async_result_set_op_res_gpointer (result,
130 * g_object_ref (cake),
134 * /* In this example, we assume that baked_cb is called as a callback from
135 * * the mainloop, so it's safe to complete the operation synchronously here.
136 * * If, however, _baker_prepare_cake () might call its callback without
137 * * first returning to the mainloop — inadvisable, but some APIs do so —
138 * * we would need to use g_simple_async_result_complete_in_idle().
140 * g_simple_async_result_complete (result);
141 * g_object_unref (result);
145 * baker_bake_cake_async (Baker *self,
147 * GAsyncReadyCallback callback,
148 * gpointer user_data)
150 * GSimpleAsyncResult *simple;
155 * g_simple_async_report_error_in_idle (G_OBJECT (self),
159 * BAKER_ERROR_TOO_SMALL,
160 * "%ucm radius cakes are silly",
165 * simple = g_simple_async_result_new (G_OBJECT (self),
168 * baker_bake_cake_async);
169 * cake = _baker_get_cached_cake (self, radius);
173 * g_simple_async_result_set_op_res_gpointer (simple,
174 * g_object_ref (cake),
176 * g_simple_async_result_complete_in_idle (simple);
177 * g_object_unref (simple);
178 * /* Drop the reference returned by _baker_get_cached_cake(); the
179 * * GSimpleAsyncResult has taken its own reference.
181 * g_object_unref (cake);
185 * _baker_prepare_cake (self, radius, baked_cb, simple);
189 * baker_bake_cake_finish (Baker *self,
190 * GAsyncResult *result,
193 * GSimpleAsyncResult *simple;
196 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
198 * baker_bake_cake_async),
201 * simple = (GSimpleAsyncResult *) result;
203 * if (g_simple_async_result_propagate_error (simple, error))
206 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
207 * return g_object_ref (cake);
212 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
214 struct _GSimpleAsyncResult
216 GObject parent_instance;
218 GObject *source_object;
219 GAsyncReadyCallback callback;
221 GMainContext *context;
224 gboolean handle_cancellation;
225 GCancellable *check_cancellable;
235 GDestroyNotify destroy_op_res;
238 struct _GSimpleAsyncResultClass
240 GObjectClass parent_class;
244 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
245 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
246 g_simple_async_result_async_result_iface_init))
249 clear_op_res (GSimpleAsyncResult *simple)
251 if (simple->destroy_op_res)
252 simple->destroy_op_res (simple->op_res.v_pointer);
253 simple->destroy_op_res = NULL;
254 simple->op_res.v_ssize = 0;
258 g_simple_async_result_finalize (GObject *object)
260 GSimpleAsyncResult *simple;
262 simple = G_SIMPLE_ASYNC_RESULT (object);
264 if (simple->source_object)
265 g_object_unref (simple->source_object);
267 if (simple->check_cancellable)
268 g_object_unref (simple->check_cancellable);
270 g_main_context_unref (simple->context);
272 clear_op_res (simple);
275 g_error_free (simple->error);
277 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
281 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
283 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
285 gobject_class->finalize = g_simple_async_result_finalize;
289 g_simple_async_result_init (GSimpleAsyncResult *simple)
291 simple->handle_cancellation = TRUE;
293 simple->context = g_main_context_ref_thread_default ();
297 * g_simple_async_result_new:
298 * @source_object: (allow-none): a #GObject, or %NULL.
299 * @callback: (scope async): a #GAsyncReadyCallback.
300 * @user_data: (closure): user data passed to @callback.
301 * @source_tag: the asynchronous function.
303 * Creates a #GSimpleAsyncResult.
305 * The common convention is to create the #GSimpleAsyncResult in the
306 * function that starts the asynchronous operation and use that same
307 * function as the @source_tag.
309 * If your operation supports cancellation with #GCancellable (which it
310 * probably should) then you should provide the user's cancellable to
311 * g_simple_async_result_set_check_cancellable() immediately after
312 * this function returns.
314 * Returns: a #GSimpleAsyncResult.
317 g_simple_async_result_new (GObject *source_object,
318 GAsyncReadyCallback callback,
322 GSimpleAsyncResult *simple;
324 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
326 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
327 simple->callback = callback;
329 simple->source_object = g_object_ref (source_object);
331 simple->source_object = NULL;
332 simple->user_data = user_data;
333 simple->source_tag = source_tag;
339 * g_simple_async_result_new_from_error:
340 * @source_object: (allow-none): a #GObject, or %NULL.
341 * @callback: (scope async): a #GAsyncReadyCallback.
342 * @user_data: (closure): user data passed to @callback.
345 * Creates a #GSimpleAsyncResult from an error condition.
347 * Returns: a #GSimpleAsyncResult.
350 g_simple_async_result_new_from_error (GObject *source_object,
351 GAsyncReadyCallback callback,
355 GSimpleAsyncResult *simple;
357 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
359 simple = g_simple_async_result_new (source_object,
362 g_simple_async_result_set_from_error (simple, error);
368 * g_simple_async_result_new_take_error: (skip)
369 * @source_object: (allow-none): a #GObject, or %NULL
370 * @callback: (scope async): a #GAsyncReadyCallback
371 * @user_data: (closure): user data passed to @callback
374 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
375 * caller's ownership of @error, so the caller does not need to free it anymore.
377 * Returns: a #GSimpleAsyncResult
382 g_simple_async_result_new_take_error (GObject *source_object,
383 GAsyncReadyCallback callback,
387 GSimpleAsyncResult *simple;
389 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
391 simple = g_simple_async_result_new (source_object,
394 g_simple_async_result_take_error (simple, error);
400 * g_simple_async_result_new_error:
401 * @source_object: (allow-none): a #GObject, or %NULL.
402 * @callback: (scope async): a #GAsyncReadyCallback.
403 * @user_data: (closure): user data passed to @callback.
404 * @domain: a #GQuark.
405 * @code: an error code.
406 * @format: a string with format characters.
407 * @...: a list of values to insert into @format.
409 * Creates a new #GSimpleAsyncResult with a set error.
411 * Returns: a #GSimpleAsyncResult.
414 g_simple_async_result_new_error (GObject *source_object,
415 GAsyncReadyCallback callback,
422 GSimpleAsyncResult *simple;
425 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
426 g_return_val_if_fail (domain != 0, NULL);
427 g_return_val_if_fail (format != NULL, NULL);
429 simple = g_simple_async_result_new (source_object,
433 va_start (args, format);
434 g_simple_async_result_set_error_va (simple, domain, code, format, args);
442 g_simple_async_result_get_user_data (GAsyncResult *res)
444 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
448 g_simple_async_result_get_source_object (GAsyncResult *res)
450 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
451 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
456 g_simple_async_result_is_tagged (GAsyncResult *res,
459 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
463 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
465 iface->get_user_data = g_simple_async_result_get_user_data;
466 iface->get_source_object = g_simple_async_result_get_source_object;
467 iface->is_tagged = g_simple_async_result_is_tagged;
471 * g_simple_async_result_set_handle_cancellation:
472 * @simple: a #GSimpleAsyncResult.
473 * @handle_cancellation: a #gboolean.
475 * Sets whether to handle cancellation within the asynchronous operation.
477 * This function has nothing to do with
478 * g_simple_async_result_set_check_cancellable(). It only refers to the
479 * #GCancellable passed to g_simple_async_result_run_in_thread().
482 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
483 gboolean handle_cancellation)
485 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
486 simple->handle_cancellation = handle_cancellation;
490 * g_simple_async_result_get_source_tag: (skip)
491 * @simple: a #GSimpleAsyncResult.
493 * Gets the source tag for the #GSimpleAsyncResult.
495 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
498 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
500 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
501 return simple->source_tag;
505 * g_simple_async_result_propagate_error:
506 * @simple: a #GSimpleAsyncResult.
507 * @dest: (out): a location to propagate the error to.
509 * Propagates an error from within the simple asynchronous result to
510 * a given destination.
512 * If the #GCancellable given to a prior call to
513 * g_simple_async_result_set_check_cancellable() is cancelled then this
514 * function will return %TRUE with @dest set appropriately.
516 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
519 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
522 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
524 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
529 g_propagate_error (dest, simple->error);
530 simple->error = NULL;
538 * g_simple_async_result_set_op_res_gpointer: (skip)
539 * @simple: a #GSimpleAsyncResult.
540 * @op_res: a pointer result from an asynchronous function.
541 * @destroy_op_res: a #GDestroyNotify function.
543 * Sets the operation result within the asynchronous result to a pointer.
546 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
548 GDestroyNotify destroy_op_res)
550 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
552 clear_op_res (simple);
553 simple->op_res.v_pointer = op_res;
554 simple->destroy_op_res = destroy_op_res;
558 * g_simple_async_result_get_op_res_gpointer: (skip)
559 * @simple: a #GSimpleAsyncResult.
561 * Gets a pointer result as returned by the asynchronous function.
563 * Returns: a pointer from the result.
566 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
568 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
569 return simple->op_res.v_pointer;
573 * g_simple_async_result_set_op_res_gssize:
574 * @simple: a #GSimpleAsyncResult.
575 * @op_res: a #gssize.
577 * Sets the operation result within the asynchronous result to
581 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
584 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
585 clear_op_res (simple);
586 simple->op_res.v_ssize = op_res;
590 * g_simple_async_result_get_op_res_gssize:
591 * @simple: a #GSimpleAsyncResult.
593 * Gets a gssize from the asynchronous result.
595 * Returns: a gssize returned from the asynchronous function.
598 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
600 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
601 return simple->op_res.v_ssize;
605 * g_simple_async_result_set_op_res_gboolean:
606 * @simple: a #GSimpleAsyncResult.
607 * @op_res: a #gboolean.
609 * Sets the operation result to a boolean within the asynchronous result.
612 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
615 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
616 clear_op_res (simple);
617 simple->op_res.v_boolean = !!op_res;
621 * g_simple_async_result_get_op_res_gboolean:
622 * @simple: a #GSimpleAsyncResult.
624 * Gets the operation result boolean from within the asynchronous result.
626 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
627 * if the operation's result was %FALSE.
630 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
632 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
633 return simple->op_res.v_boolean;
637 * g_simple_async_result_set_from_error:
638 * @simple: a #GSimpleAsyncResult.
641 * Sets the result from a #GError.
644 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
647 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
648 g_return_if_fail (error != NULL);
651 g_error_free (simple->error);
652 simple->error = g_error_copy (error);
653 simple->failed = TRUE;
657 * g_simple_async_result_take_error: (skip)
658 * @simple: a #GSimpleAsyncResult
661 * Sets the result from @error, and takes over the caller's ownership
662 * of @error, so the caller does not need to free it any more.
667 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
670 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
671 g_return_if_fail (error != NULL);
674 g_error_free (simple->error);
675 simple->error = error;
676 simple->failed = TRUE;
680 * g_simple_async_result_set_error_va: (skip)
681 * @simple: a #GSimpleAsyncResult.
682 * @domain: a #GQuark (usually #G_IO_ERROR).
683 * @code: an error code.
684 * @format: a formatted error reporting string.
685 * @args: va_list of arguments.
687 * Sets an error within the asynchronous result without a #GError.
688 * Unless writing a binding, see g_simple_async_result_set_error().
691 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
697 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
698 g_return_if_fail (domain != 0);
699 g_return_if_fail (format != NULL);
702 g_error_free (simple->error);
703 simple->error = g_error_new_valist (domain, code, format, args);
704 simple->failed = TRUE;
708 * g_simple_async_result_set_error: (skip)
709 * @simple: a #GSimpleAsyncResult.
710 * @domain: a #GQuark (usually #G_IO_ERROR).
711 * @code: an error code.
712 * @format: a formatted error reporting string.
713 * @...: a list of variables to fill in @format.
715 * Sets an error within the asynchronous result without a #GError.
718 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
726 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
727 g_return_if_fail (domain != 0);
728 g_return_if_fail (format != NULL);
730 va_start (args, format);
731 g_simple_async_result_set_error_va (simple, domain, code, format, args);
736 * g_simple_async_result_complete:
737 * @simple: a #GSimpleAsyncResult.
739 * Completes an asynchronous I/O job immediately. Must be called in
740 * the thread where the asynchronous result was to be delivered, as it
741 * invokes the callback directly. If you are in a different thread use
742 * g_simple_async_result_complete_in_idle().
744 * Calling this function takes a reference to @simple for as long as
745 * is needed to complete the call.
748 g_simple_async_result_complete (GSimpleAsyncResult *simple)
750 #ifndef G_DISABLE_CHECKS
751 GSource *current_source;
752 GMainContext *current_context;
755 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
757 #ifndef G_DISABLE_CHECKS
758 current_source = g_main_current_source ();
759 if (current_source && !g_source_is_destroyed (current_source))
761 current_context = g_source_get_context (current_source);
762 if (simple->context != current_context)
763 g_warning ("g_simple_async_result_complete() called from wrong context!");
767 if (simple->callback)
769 g_main_context_push_thread_default (simple->context);
770 simple->callback (simple->source_object,
771 G_ASYNC_RESULT (simple),
773 g_main_context_pop_thread_default (simple->context);
778 complete_in_idle_cb (gpointer data)
780 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
782 g_simple_async_result_complete (simple);
788 * g_simple_async_result_complete_in_idle:
789 * @simple: a #GSimpleAsyncResult.
791 * Completes an asynchronous function in an idle handler in the <link
792 * linkend="g-main-context-push-thread-default">thread-default main
793 * loop</link> of the thread that @simple was initially created in
794 * (and re-pushes that context around the invocation of the callback).
796 * Calling this function takes a reference to @simple for as long as
797 * is needed to complete the call.
800 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
804 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
806 g_object_ref (simple);
808 source = g_idle_source_new ();
809 g_source_set_priority (source, G_PRIORITY_DEFAULT);
810 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
812 g_source_attach (source, simple->context);
813 g_source_unref (source);
817 GSimpleAsyncResult *simple;
818 GCancellable *cancellable;
819 GSimpleAsyncThreadFunc func;
824 complete_in_idle_cb_for_thread (gpointer _data)
826 RunInThreadData *data = _data;
827 GSimpleAsyncResult *simple;
829 simple = data->simple;
831 if (simple->handle_cancellation &&
832 g_cancellable_is_cancelled (data->cancellable))
833 g_simple_async_result_set_error (simple,
835 G_IO_ERROR_CANCELLED,
836 "%s", _("Operation was cancelled"));
838 g_simple_async_result_complete (simple);
840 if (data->cancellable)
841 g_object_unref (data->cancellable);
842 g_object_unref (data->simple);
849 run_in_thread (GIOSchedulerJob *job,
853 RunInThreadData *data = _data;
854 GSimpleAsyncResult *simple = data->simple;
857 if (simple->handle_cancellation &&
858 g_cancellable_is_cancelled (c))
859 g_simple_async_result_set_error (simple,
861 G_IO_ERROR_CANCELLED,
862 "%s", _("Operation was cancelled"));
865 simple->source_object,
868 source = g_idle_source_new ();
869 g_source_set_priority (source, G_PRIORITY_DEFAULT);
870 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
872 g_source_attach (source, simple->context);
873 g_source_unref (source);
879 * g_simple_async_result_run_in_thread: (skip)
880 * @simple: a #GSimpleAsyncResult.
881 * @func: a #GSimpleAsyncThreadFunc.
882 * @io_priority: the io priority of the request.
883 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
885 * Runs the asynchronous job in a separate thread and then calls
886 * g_simple_async_result_complete_in_idle() on @simple to return
887 * the result to the appropriate main loop.
889 * Calling this function takes a reference to @simple for as long as
890 * is needed to run the job and report its completion.
893 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
894 GSimpleAsyncThreadFunc func,
896 GCancellable *cancellable)
898 RunInThreadData *data;
900 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
901 g_return_if_fail (func != NULL);
903 data = g_new (RunInThreadData, 1);
905 data->simple = g_object_ref (simple);
906 data->cancellable = cancellable;
908 g_object_ref (cancellable);
909 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
910 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
911 G_GNUC_END_IGNORE_DEPRECATIONS;
915 * g_simple_async_result_is_valid:
916 * @result: the #GAsyncResult passed to the _finish function.
917 * @source: the #GObject passed to the _finish function.
918 * @source_tag: the asynchronous function.
920 * Ensures that the data passed to the _finish function of an async
921 * operation is consistent. Three checks are performed.
923 * First, @result is checked to ensure that it is really a
924 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
925 * matches the source object of @result. Third, @source_tag is
926 * checked to ensure that it is either %NULL (as it is when the result was
927 * created by g_simple_async_report_error_in_idle() or
928 * g_simple_async_report_gerror_in_idle()) or equal to the
929 * @source_tag argument given to g_simple_async_result_new() (which, by
930 * convention, is a pointer to the _async function corresponding to the
931 * _finish function from which this function is called).
933 * Returns: #TRUE if all checks passed or #FALSE if any failed.
938 g_simple_async_result_is_valid (GAsyncResult *result,
942 GSimpleAsyncResult *simple;
945 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
947 simple = (GSimpleAsyncResult *)result;
949 cmp_source = g_async_result_get_source_object (result);
950 if (cmp_source != source)
952 if (cmp_source != NULL)
953 g_object_unref (cmp_source);
956 if (cmp_source != NULL)
957 g_object_unref (cmp_source);
959 return source_tag == NULL ||
960 source_tag == g_simple_async_result_get_source_tag (simple);
964 * g_simple_async_report_error_in_idle: (skip)
965 * @object: (allow-none): a #GObject, or %NULL.
966 * @callback: a #GAsyncReadyCallback.
967 * @user_data: user data passed to @callback.
968 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
969 * @code: a specific error code.
970 * @format: a formatted error reporting string.
971 * @...: a list of variables to fill in @format.
973 * Reports an error in an asynchronous function in an idle function by
974 * directly setting the contents of the #GAsyncResult with the given error
978 g_simple_async_report_error_in_idle (GObject *object,
979 GAsyncReadyCallback callback,
986 GSimpleAsyncResult *simple;
989 g_return_if_fail (!object || G_IS_OBJECT (object));
990 g_return_if_fail (domain != 0);
991 g_return_if_fail (format != NULL);
993 simple = g_simple_async_result_new (object,
997 va_start (args, format);
998 g_simple_async_result_set_error_va (simple, domain, code, format, args);
1000 g_simple_async_result_complete_in_idle (simple);
1001 g_object_unref (simple);
1005 * g_simple_async_report_gerror_in_idle:
1006 * @object: (allow-none): a #GObject, or %NULL
1007 * @callback: (scope async): a #GAsyncReadyCallback.
1008 * @user_data: (closure): user data passed to @callback.
1009 * @error: the #GError to report
1011 * Reports an error in an idle function. Similar to
1012 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1013 * than building a new one.
1016 g_simple_async_report_gerror_in_idle (GObject *object,
1017 GAsyncReadyCallback callback,
1019 const GError *error)
1021 GSimpleAsyncResult *simple;
1023 g_return_if_fail (!object || G_IS_OBJECT (object));
1024 g_return_if_fail (error != NULL);
1026 simple = g_simple_async_result_new_from_error (object,
1030 g_simple_async_result_complete_in_idle (simple);
1031 g_object_unref (simple);
1035 * g_simple_async_report_take_gerror_in_idle: (skip)
1036 * @object: (allow-none): a #GObject, or %NULL
1037 * @callback: a #GAsyncReadyCallback.
1038 * @user_data: user data passed to @callback.
1039 * @error: the #GError to report
1041 * Reports an error in an idle function. Similar to
1042 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1043 * ownership of @error, so the caller does not have to free it any more.
1048 g_simple_async_report_take_gerror_in_idle (GObject *object,
1049 GAsyncReadyCallback callback,
1053 GSimpleAsyncResult *simple;
1055 g_return_if_fail (!object || G_IS_OBJECT (object));
1056 g_return_if_fail (error != NULL);
1058 simple = g_simple_async_result_new_take_error (object,
1062 g_simple_async_result_complete_in_idle (simple);
1063 g_object_unref (simple);
1067 * g_simple_async_result_set_check_cancellable:
1068 * @simple: a #GSimpleAsyncResult
1069 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1071 * Sets a #GCancellable to check before dispatching results.
1073 * This function has one very specific purpose: the provided cancellable
1074 * is checked at the time of g_simple_async_result_propagate_error() If
1075 * it is cancelled, these functions will return an "Operation was
1076 * cancelled" error (%G_IO_ERROR_CANCELLED).
1078 * Implementors of cancellable asynchronous functions should use this in
1079 * order to provide a guarantee to their callers that cancelling an
1080 * async operation will reliably result in an error being returned for
1081 * that operation (even if a positive result for the operation has
1082 * already been sent as an idle to the main context to be dispatched).
1084 * The checking described above is done regardless of any call to the
1085 * unrelated g_simple_async_result_set_handle_cancellation() function.
1090 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1091 GCancellable *check_cancellable)
1093 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1094 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1096 g_clear_object (&simple->check_cancellable);
1097 if (check_cancellable)
1098 simple->check_cancellable = g_object_ref (check_cancellable);