1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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, #GTask
41 * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
42 * #GTask, which provides a simpler API.
44 * #GSimpleAsyncResult implements #GAsyncResult.
46 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, 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 #GThreads.
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
88 * [thread-default main context][g-main-context-push-thread-default]
89 * where the #GSimpleAsyncResult was created.
90 * g_simple_async_result_run_in_thread() will run the job in a
91 * separate thread and then use
92 * g_simple_async_result_complete_in_idle() to deliver the result.
94 * To set the results of an asynchronous function,
95 * g_simple_async_result_set_op_res_gpointer(),
96 * g_simple_async_result_set_op_res_gboolean(), and
97 * g_simple_async_result_set_op_res_gssize()
98 * are provided, setting the operation's result to a gpointer, gboolean, or
99 * gssize, respectively.
101 * Likewise, to get the result of an asynchronous function,
102 * g_simple_async_result_get_op_res_gpointer(),
103 * g_simple_async_result_get_op_res_gboolean(), and
104 * g_simple_async_result_get_op_res_gssize() are
105 * provided, getting the operation's result as a gpointer, gboolean, and
106 * gssize, respectively.
108 * For the details of the requirements implementations must respect, see
109 * #GAsyncResult. A typical implementation of an asynchronous operation
110 * using GSimpleAsyncResult looks something like this:
112 * |[<!-- language="C" -->
114 * baked_cb (Cake *cake,
115 * gpointer user_data)
117 * // In this example, this callback is not given a reference to the cake,
118 * // so 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().
137 * g_simple_async_result_complete (result);
138 * g_object_unref (result);
142 * baker_bake_cake_async (Baker *self,
144 * GAsyncReadyCallback callback,
145 * gpointer user_data)
147 * GSimpleAsyncResult *simple;
152 * g_simple_async_report_error_in_idle (G_OBJECT (self),
156 * BAKER_ERROR_TOO_SMALL,
157 * "%ucm radius cakes are silly",
162 * simple = g_simple_async_result_new (G_OBJECT (self),
165 * baker_bake_cake_async);
166 * cake = _baker_get_cached_cake (self, radius);
170 * g_simple_async_result_set_op_res_gpointer (simple,
171 * g_object_ref (cake),
173 * g_simple_async_result_complete_in_idle (simple);
174 * g_object_unref (simple);
175 * // Drop the reference returned by _baker_get_cached_cake();
176 * // the 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 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
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: (nullable): a #GObject, or %NULL.
297 * @callback: (scope async): a #GAsyncReadyCallback.
298 * @user_data: 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.
314 * Deprecated: 2.46: Use g_task_new() instead.
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: (nullable): a #GObject, or %NULL.
341 * @callback: (scope async): a #GAsyncReadyCallback.
342 * @user_data: user data passed to @callback.
345 * Creates a #GSimpleAsyncResult from an error condition.
347 * Returns: a #GSimpleAsyncResult.
349 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
352 g_simple_async_result_new_from_error (GObject *source_object,
353 GAsyncReadyCallback callback,
357 GSimpleAsyncResult *simple;
359 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
361 simple = g_simple_async_result_new (source_object,
364 g_simple_async_result_set_from_error (simple, error);
370 * g_simple_async_result_new_take_error: (skip)
371 * @source_object: (nullable): a #GObject, or %NULL
372 * @callback: (scope async): a #GAsyncReadyCallback.
373 * @user_data: user data passed to @callback.
376 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
377 * caller's ownership of @error, so the caller does not need to free it anymore.
379 * Returns: a #GSimpleAsyncResult
383 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
386 g_simple_async_result_new_take_error (GObject *source_object,
387 GAsyncReadyCallback callback,
391 GSimpleAsyncResult *simple;
393 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
395 simple = g_simple_async_result_new (source_object,
398 g_simple_async_result_take_error (simple, error);
404 * g_simple_async_result_new_error:
405 * @source_object: (nullable): a #GObject, or %NULL.
406 * @callback: (scope async): a #GAsyncReadyCallback.
407 * @user_data: user data passed to @callback.
408 * @domain: a #GQuark.
409 * @code: an error code.
410 * @format: a string with format characters.
411 * @...: a list of values to insert into @format.
413 * Creates a new #GSimpleAsyncResult with a set error.
415 * Returns: a #GSimpleAsyncResult.
417 * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
420 g_simple_async_result_new_error (GObject *source_object,
421 GAsyncReadyCallback callback,
428 GSimpleAsyncResult *simple;
431 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
432 g_return_val_if_fail (domain != 0, NULL);
433 g_return_val_if_fail (format != NULL, NULL);
435 simple = g_simple_async_result_new (source_object,
439 va_start (args, format);
440 g_simple_async_result_set_error_va (simple, domain, code, format, args);
448 g_simple_async_result_get_user_data (GAsyncResult *res)
450 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
454 g_simple_async_result_get_source_object (GAsyncResult *res)
456 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
457 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
462 g_simple_async_result_is_tagged (GAsyncResult *res,
465 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
469 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
471 iface->get_user_data = g_simple_async_result_get_user_data;
472 iface->get_source_object = g_simple_async_result_get_source_object;
473 iface->is_tagged = g_simple_async_result_is_tagged;
477 * g_simple_async_result_set_handle_cancellation:
478 * @simple: a #GSimpleAsyncResult.
479 * @handle_cancellation: a #gboolean.
481 * Sets whether to handle cancellation within the asynchronous operation.
483 * This function has nothing to do with
484 * g_simple_async_result_set_check_cancellable(). It only refers to the
485 * #GCancellable passed to g_simple_async_result_run_in_thread().
490 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
491 gboolean handle_cancellation)
493 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
494 simple->handle_cancellation = handle_cancellation;
498 * g_simple_async_result_get_source_tag: (skip)
499 * @simple: a #GSimpleAsyncResult.
501 * Gets the source tag for the #GSimpleAsyncResult.
503 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
505 * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
508 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
510 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
511 return simple->source_tag;
515 * g_simple_async_result_propagate_error:
516 * @simple: a #GSimpleAsyncResult.
517 * @dest: (out): a location to propagate the error to.
519 * Propagates an error from within the simple asynchronous result to
520 * a given destination.
522 * If the #GCancellable given to a prior call to
523 * g_simple_async_result_set_check_cancellable() is cancelled then this
524 * function will return %TRUE with @dest set appropriately.
526 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
528 * Deprecated: 2.46: Use #GTask instead.
531 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
534 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
536 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
541 g_propagate_error (dest, simple->error);
542 simple->error = NULL;
550 * g_simple_async_result_set_op_res_gpointer: (skip)
551 * @simple: a #GSimpleAsyncResult.
552 * @op_res: a pointer result from an asynchronous function.
553 * @destroy_op_res: a #GDestroyNotify function.
555 * Sets the operation result within the asynchronous result to a pointer.
557 * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
560 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
562 GDestroyNotify destroy_op_res)
564 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
566 clear_op_res (simple);
567 simple->op_res.v_pointer = op_res;
568 simple->destroy_op_res = destroy_op_res;
572 * g_simple_async_result_get_op_res_gpointer: (skip)
573 * @simple: a #GSimpleAsyncResult.
575 * Gets a pointer result as returned by the asynchronous function.
577 * Returns: a pointer from the result.
579 * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
582 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
584 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
585 return simple->op_res.v_pointer;
589 * g_simple_async_result_set_op_res_gssize:
590 * @simple: a #GSimpleAsyncResult.
591 * @op_res: a #gssize.
593 * Sets the operation result within the asynchronous result to
596 * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
599 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
602 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
603 clear_op_res (simple);
604 simple->op_res.v_ssize = op_res;
608 * g_simple_async_result_get_op_res_gssize:
609 * @simple: a #GSimpleAsyncResult.
611 * Gets a gssize from the asynchronous result.
613 * Returns: a gssize returned from the asynchronous function.
615 * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
618 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
620 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
621 return simple->op_res.v_ssize;
625 * g_simple_async_result_set_op_res_gboolean:
626 * @simple: a #GSimpleAsyncResult.
627 * @op_res: a #gboolean.
629 * Sets the operation result to a boolean within the asynchronous result.
631 * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
634 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
637 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
638 clear_op_res (simple);
639 simple->op_res.v_boolean = !!op_res;
643 * g_simple_async_result_get_op_res_gboolean:
644 * @simple: a #GSimpleAsyncResult.
646 * Gets the operation result boolean from within the asynchronous result.
648 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
649 * if the operation's result was %FALSE.
651 * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
654 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
656 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
657 return simple->op_res.v_boolean;
661 * g_simple_async_result_set_from_error:
662 * @simple: a #GSimpleAsyncResult.
665 * Sets the result from a #GError.
667 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
670 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
673 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
674 g_return_if_fail (error != NULL);
677 g_error_free (simple->error);
678 simple->error = g_error_copy (error);
679 simple->failed = TRUE;
683 * g_simple_async_result_take_error: (skip)
684 * @simple: a #GSimpleAsyncResult
687 * Sets the result from @error, and takes over the caller's ownership
688 * of @error, so the caller does not need to free it any more.
692 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
695 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
698 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
699 g_return_if_fail (error != NULL);
702 g_error_free (simple->error);
703 simple->error = error;
704 simple->failed = TRUE;
708 * g_simple_async_result_set_error_va: (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 * @args: va_list of arguments.
715 * Sets an error within the asynchronous result without a #GError.
716 * Unless writing a binding, see g_simple_async_result_set_error().
718 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
721 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
727 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
728 g_return_if_fail (domain != 0);
729 g_return_if_fail (format != NULL);
732 g_error_free (simple->error);
733 simple->error = g_error_new_valist (domain, code, format, args);
734 simple->failed = TRUE;
738 * g_simple_async_result_set_error: (skip)
739 * @simple: a #GSimpleAsyncResult.
740 * @domain: a #GQuark (usually %G_IO_ERROR).
741 * @code: an error code.
742 * @format: a formatted error reporting string.
743 * @...: a list of variables to fill in @format.
745 * Sets an error within the asynchronous result without a #GError.
747 * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
750 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
758 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
759 g_return_if_fail (domain != 0);
760 g_return_if_fail (format != NULL);
762 va_start (args, format);
763 g_simple_async_result_set_error_va (simple, domain, code, format, args);
768 * g_simple_async_result_complete:
769 * @simple: a #GSimpleAsyncResult.
771 * Completes an asynchronous I/O job immediately. Must be called in
772 * the thread where the asynchronous result was to be delivered, as it
773 * invokes the callback directly. If you are in a different thread use
774 * g_simple_async_result_complete_in_idle().
776 * Calling this function takes a reference to @simple for as long as
777 * is needed to complete the call.
779 * Deprecated: 2.46: Use #GTask instead.
782 g_simple_async_result_complete (GSimpleAsyncResult *simple)
784 #ifndef G_DISABLE_CHECKS
785 GSource *current_source;
786 GMainContext *current_context;
789 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
791 #ifndef G_DISABLE_CHECKS
792 current_source = g_main_current_source ();
793 if (current_source && !g_source_is_destroyed (current_source))
795 current_context = g_source_get_context (current_source);
796 if (simple->context != current_context)
797 g_warning ("g_simple_async_result_complete() called from wrong context!");
801 if (simple->callback)
803 g_main_context_push_thread_default (simple->context);
804 simple->callback (simple->source_object,
805 G_ASYNC_RESULT (simple),
807 g_main_context_pop_thread_default (simple->context);
812 complete_in_idle_cb (gpointer data)
814 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
816 g_simple_async_result_complete (simple);
822 * g_simple_async_result_complete_in_idle:
823 * @simple: a #GSimpleAsyncResult.
825 * Completes an asynchronous function in an idle handler in the
826 * [thread-default main context][g-main-context-push-thread-default]
827 * of the thread that @simple was initially created in
828 * (and re-pushes that context around the invocation of the callback).
830 * Calling this function takes a reference to @simple for as long as
831 * is needed to complete the call.
833 * Deprecated: 2.46: Use #GTask instead.
836 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
840 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
842 g_object_ref (simple);
844 source = g_idle_source_new ();
845 g_source_set_priority (source, G_PRIORITY_DEFAULT);
846 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
847 g_source_set_static_name (source, "[gio] complete_in_idle_cb");
849 g_source_attach (source, simple->context);
850 g_source_unref (source);
854 GSimpleAsyncResult *simple;
855 GCancellable *cancellable;
856 GSimpleAsyncThreadFunc func;
861 complete_in_idle_cb_for_thread (gpointer _data)
863 RunInThreadData *data = _data;
864 GSimpleAsyncResult *simple;
866 simple = data->simple;
868 if (simple->handle_cancellation &&
869 g_cancellable_is_cancelled (data->cancellable))
870 g_simple_async_result_set_error (simple,
872 G_IO_ERROR_CANCELLED,
873 "%s", _("Operation was cancelled"));
875 g_simple_async_result_complete (simple);
877 if (data->cancellable)
878 g_object_unref (data->cancellable);
879 g_object_unref (data->simple);
886 run_in_thread (GIOSchedulerJob *job,
890 RunInThreadData *data = _data;
891 GSimpleAsyncResult *simple = data->simple;
894 if (simple->handle_cancellation &&
895 g_cancellable_is_cancelled (c))
896 g_simple_async_result_set_error (simple,
898 G_IO_ERROR_CANCELLED,
899 "%s", _("Operation was cancelled"));
902 simple->source_object,
905 source = g_idle_source_new ();
906 g_source_set_priority (source, G_PRIORITY_DEFAULT);
907 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
908 g_source_set_static_name (source, "[gio] complete_in_idle_cb_for_thread");
910 g_source_attach (source, simple->context);
911 g_source_unref (source);
917 * g_simple_async_result_run_in_thread: (skip)
918 * @simple: a #GSimpleAsyncResult.
919 * @func: a #GSimpleAsyncThreadFunc.
920 * @io_priority: the io priority of the request.
921 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
923 * Runs the asynchronous job in a separate thread and then calls
924 * g_simple_async_result_complete_in_idle() on @simple to return
925 * the result to the appropriate main loop.
927 * Calling this function takes a reference to @simple for as long as
928 * is needed to run the job and report its completion.
930 * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
933 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
934 GSimpleAsyncThreadFunc func,
936 GCancellable *cancellable)
938 RunInThreadData *data;
940 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
941 g_return_if_fail (func != NULL);
943 data = g_new (RunInThreadData, 1);
945 data->simple = g_object_ref (simple);
946 data->cancellable = cancellable;
948 g_object_ref (cancellable);
949 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
950 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
951 G_GNUC_END_IGNORE_DEPRECATIONS;
955 * g_simple_async_result_is_valid:
956 * @result: the #GAsyncResult passed to the _finish function.
957 * @source: (nullable): the #GObject passed to the _finish function.
958 * @source_tag: (nullable): the asynchronous function.
960 * Ensures that the data passed to the _finish function of an async
961 * operation is consistent. Three checks are performed.
963 * First, @result is checked to ensure that it is really a
964 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
965 * matches the source object of @result. Third, @source_tag is
966 * checked to ensure that it is equal to the @source_tag argument given
967 * to g_simple_async_result_new() (which, by convention, is a pointer
968 * to the _async function corresponding to the _finish function from
969 * which this function is called). (Alternatively, if either
970 * @source_tag or @result's source tag is %NULL, then the source tag
973 * Returns: #TRUE if all checks passed or #FALSE if any failed.
977 * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
980 g_simple_async_result_is_valid (GAsyncResult *result,
984 GSimpleAsyncResult *simple;
986 gpointer result_source_tag;
988 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
990 simple = (GSimpleAsyncResult *)result;
992 cmp_source = g_async_result_get_source_object (result);
993 if (cmp_source != source)
995 if (cmp_source != NULL)
996 g_object_unref (cmp_source);
999 if (cmp_source != NULL)
1000 g_object_unref (cmp_source);
1002 result_source_tag = g_simple_async_result_get_source_tag (simple);
1003 return source_tag == NULL || result_source_tag == NULL ||
1004 source_tag == result_source_tag;
1008 * g_simple_async_report_error_in_idle: (skip)
1009 * @object: (nullable): a #GObject, or %NULL.
1010 * @callback: a #GAsyncReadyCallback.
1011 * @user_data: user data passed to @callback.
1012 * @domain: a #GQuark containing the error domain (usually %G_IO_ERROR).
1013 * @code: a specific error code.
1014 * @format: a formatted error reporting string.
1015 * @...: a list of variables to fill in @format.
1017 * Reports an error in an asynchronous function in an idle function by
1018 * directly setting the contents of the #GAsyncResult with the given error
1021 * Deprecated: 2.46: Use g_task_report_error().
1024 g_simple_async_report_error_in_idle (GObject *object,
1025 GAsyncReadyCallback callback,
1032 GSimpleAsyncResult *simple;
1035 g_return_if_fail (!object || G_IS_OBJECT (object));
1036 g_return_if_fail (domain != 0);
1037 g_return_if_fail (format != NULL);
1039 simple = g_simple_async_result_new (object,
1043 va_start (args, format);
1044 g_simple_async_result_set_error_va (simple, domain, code, format, args);
1046 g_simple_async_result_complete_in_idle (simple);
1047 g_object_unref (simple);
1051 * g_simple_async_report_gerror_in_idle:
1052 * @object: (nullable): a #GObject, or %NULL
1053 * @callback: (scope async): a #GAsyncReadyCallback.
1054 * @user_data: user data passed to @callback.
1055 * @error: the #GError to report
1057 * Reports an error in an idle function. Similar to
1058 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1059 * than building a new one.
1061 * Deprecated: 2.46: Use g_task_report_error().
1064 g_simple_async_report_gerror_in_idle (GObject *object,
1065 GAsyncReadyCallback callback,
1067 const GError *error)
1069 GSimpleAsyncResult *simple;
1071 g_return_if_fail (!object || G_IS_OBJECT (object));
1072 g_return_if_fail (error != NULL);
1074 simple = g_simple_async_result_new_from_error (object,
1078 g_simple_async_result_complete_in_idle (simple);
1079 g_object_unref (simple);
1083 * g_simple_async_report_take_gerror_in_idle: (skip)
1084 * @object: (nullable): a #GObject, or %NULL
1085 * @callback: a #GAsyncReadyCallback.
1086 * @user_data: user data passed to @callback.
1087 * @error: the #GError to report
1089 * Reports an error in an idle function. Similar to
1090 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1091 * ownership of @error, so the caller does not have to free it any more.
1095 * Deprecated: 2.46: Use g_task_report_error().
1098 g_simple_async_report_take_gerror_in_idle (GObject *object,
1099 GAsyncReadyCallback callback,
1103 GSimpleAsyncResult *simple;
1105 g_return_if_fail (!object || G_IS_OBJECT (object));
1106 g_return_if_fail (error != NULL);
1108 simple = g_simple_async_result_new_take_error (object,
1112 g_simple_async_result_complete_in_idle (simple);
1113 g_object_unref (simple);
1117 * g_simple_async_result_set_check_cancellable:
1118 * @simple: a #GSimpleAsyncResult
1119 * @check_cancellable: (nullable): a #GCancellable to check, or %NULL to unset
1121 * Sets a #GCancellable to check before dispatching results.
1123 * This function has one very specific purpose: the provided cancellable
1124 * is checked at the time of g_simple_async_result_propagate_error() If
1125 * it is cancelled, these functions will return an "Operation was
1126 * cancelled" error (%G_IO_ERROR_CANCELLED).
1128 * Implementors of cancellable asynchronous functions should use this in
1129 * order to provide a guarantee to their callers that cancelling an
1130 * async operation will reliably result in an error being returned for
1131 * that operation (even if a positive result for the operation has
1132 * already been sent as an idle to the main context to be dispatched).
1134 * The checking described above is done regardless of any call to the
1135 * unrelated g_simple_async_result_set_handle_cancellation() function.
1139 * Deprecated: 2.46: Use #GTask instead.
1142 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1143 GCancellable *check_cancellable)
1145 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1146 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1148 g_clear_object (&simple->check_cancellable);
1149 if (check_cancellable)
1150 simple->check_cancellable = g_object_ref (check_cancellable);
1153 G_GNUC_END_IGNORE_DEPRECATIONS