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.1 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.46, #GSimpleAsyncResult is deprecated in favor of
40 * #GTask, which provides a simpler API.
42 * #GSimpleAsyncResult implements #GAsyncResult.
44 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, 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 #GThreads.
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
86 * [thread-default main context][g-main-context-push-thread-default]
87 * . 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:
109 * |[<!-- language="C" -->
111 * baked_cb (Cake *cake,
112 * gpointer user_data)
114 * // In this example, this callback is not given a reference to the cake,
115 * // so the GSimpleAsyncResult has to take a reference to it.
116 * GSimpleAsyncResult *result = user_data;
119 * g_simple_async_result_set_error (result,
121 * BAKER_ERROR_NO_FLOUR,
122 * "Go to the supermarket");
124 * g_simple_async_result_set_op_res_gpointer (result,
125 * g_object_ref (cake),
129 * // In this example, we assume that baked_cb is called as a callback from
130 * // the mainloop, so it's safe to complete the operation synchronously here.
131 * // If, however, _baker_prepare_cake () might call its callback without
132 * // first returning to the mainloop — inadvisable, but some APIs do so —
133 * // we would need to use g_simple_async_result_complete_in_idle().
134 * g_simple_async_result_complete (result);
135 * g_object_unref (result);
139 * baker_bake_cake_async (Baker *self,
141 * GAsyncReadyCallback callback,
142 * gpointer user_data)
144 * GSimpleAsyncResult *simple;
149 * g_simple_async_report_error_in_idle (G_OBJECT (self),
153 * BAKER_ERROR_TOO_SMALL,
154 * "%ucm radius cakes are silly",
159 * simple = g_simple_async_result_new (G_OBJECT (self),
162 * baker_bake_cake_async);
163 * cake = _baker_get_cached_cake (self, radius);
167 * g_simple_async_result_set_op_res_gpointer (simple,
168 * g_object_ref (cake),
170 * g_simple_async_result_complete_in_idle (simple);
171 * g_object_unref (simple);
172 * // Drop the reference returned by _baker_get_cached_cake();
173 * // the GSimpleAsyncResult has taken its own reference.
174 * g_object_unref (cake);
178 * _baker_prepare_cake (self, radius, baked_cb, simple);
182 * baker_bake_cake_finish (Baker *self,
183 * GAsyncResult *result,
186 * GSimpleAsyncResult *simple;
189 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
191 * baker_bake_cake_async),
194 * simple = (GSimpleAsyncResult *) result;
196 * if (g_simple_async_result_propagate_error (simple, error))
199 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
200 * return g_object_ref (cake);
205 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
207 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface
*iface
);
209 struct _GSimpleAsyncResult
211 GObject parent_instance
;
213 GObject
*source_object
;
214 GAsyncReadyCallback callback
;
216 GMainContext
*context
;
219 gboolean handle_cancellation
;
220 GCancellable
*check_cancellable
;
230 GDestroyNotify destroy_op_res
;
233 struct _GSimpleAsyncResultClass
235 GObjectClass parent_class
;
239 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult
, g_simple_async_result
, G_TYPE_OBJECT
,
240 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT
,
241 g_simple_async_result_async_result_iface_init
))
244 clear_op_res (GSimpleAsyncResult
*simple
)
246 if (simple
->destroy_op_res
)
247 simple
->destroy_op_res (simple
->op_res
.v_pointer
);
248 simple
->destroy_op_res
= NULL
;
249 simple
->op_res
.v_ssize
= 0;
253 g_simple_async_result_finalize (GObject
*object
)
255 GSimpleAsyncResult
*simple
;
257 simple
= G_SIMPLE_ASYNC_RESULT (object
);
259 if (simple
->source_object
)
260 g_object_unref (simple
->source_object
);
262 if (simple
->check_cancellable
)
263 g_object_unref (simple
->check_cancellable
);
265 g_main_context_unref (simple
->context
);
267 clear_op_res (simple
);
270 g_error_free (simple
->error
);
272 G_OBJECT_CLASS (g_simple_async_result_parent_class
)->finalize (object
);
276 g_simple_async_result_class_init (GSimpleAsyncResultClass
*klass
)
278 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
280 gobject_class
->finalize
= g_simple_async_result_finalize
;
284 g_simple_async_result_init (GSimpleAsyncResult
*simple
)
286 simple
->handle_cancellation
= TRUE
;
288 simple
->context
= g_main_context_ref_thread_default ();
292 * g_simple_async_result_new:
293 * @source_object: (nullable): a #GObject, or %NULL.
294 * @callback: (scope async): a #GAsyncReadyCallback.
295 * @user_data: (closure): user data passed to @callback.
296 * @source_tag: the asynchronous function.
298 * Creates a #GSimpleAsyncResult.
300 * The common convention is to create the #GSimpleAsyncResult in the
301 * function that starts the asynchronous operation and use that same
302 * function as the @source_tag.
304 * If your operation supports cancellation with #GCancellable (which it
305 * probably should) then you should provide the user's cancellable to
306 * g_simple_async_result_set_check_cancellable() immediately after
307 * this function returns.
309 * Returns: a #GSimpleAsyncResult.
311 * Deprecated: 2.46: Use g_task_new() instead.
314 g_simple_async_result_new (GObject
*source_object
,
315 GAsyncReadyCallback callback
,
319 GSimpleAsyncResult
*simple
;
321 g_return_val_if_fail (!source_object
|| G_IS_OBJECT (source_object
), NULL
);
323 simple
= g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT
, NULL
);
324 simple
->callback
= callback
;
326 simple
->source_object
= g_object_ref (source_object
);
328 simple
->source_object
= NULL
;
329 simple
->user_data
= user_data
;
330 simple
->source_tag
= source_tag
;
336 * g_simple_async_result_new_from_error:
337 * @source_object: (nullable): a #GObject, or %NULL.
338 * @callback: (scope async): a #GAsyncReadyCallback.
339 * @user_data: (closure): user data passed to @callback.
342 * Creates a #GSimpleAsyncResult from an error condition.
344 * Returns: a #GSimpleAsyncResult.
346 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
349 g_simple_async_result_new_from_error (GObject
*source_object
,
350 GAsyncReadyCallback callback
,
354 GSimpleAsyncResult
*simple
;
356 g_return_val_if_fail (!source_object
|| G_IS_OBJECT (source_object
), NULL
);
358 simple
= g_simple_async_result_new (source_object
,
361 g_simple_async_result_set_from_error (simple
, error
);
367 * g_simple_async_result_new_take_error: (skip)
368 * @source_object: (nullable): a #GObject, or %NULL
369 * @callback: (scope async): a #GAsyncReadyCallback
370 * @user_data: (closure): user data passed to @callback
373 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
374 * caller's ownership of @error, so the caller does not need to free it anymore.
376 * Returns: a #GSimpleAsyncResult
380 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
383 g_simple_async_result_new_take_error (GObject
*source_object
,
384 GAsyncReadyCallback callback
,
388 GSimpleAsyncResult
*simple
;
390 g_return_val_if_fail (!source_object
|| G_IS_OBJECT (source_object
), NULL
);
392 simple
= g_simple_async_result_new (source_object
,
395 g_simple_async_result_take_error (simple
, error
);
401 * g_simple_async_result_new_error:
402 * @source_object: (nullable): a #GObject, or %NULL.
403 * @callback: (scope async): a #GAsyncReadyCallback.
404 * @user_data: (closure): user data passed to @callback.
405 * @domain: a #GQuark.
406 * @code: an error code.
407 * @format: a string with format characters.
408 * @...: a list of values to insert into @format.
410 * Creates a new #GSimpleAsyncResult with a set error.
412 * Returns: a #GSimpleAsyncResult.
414 * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
417 g_simple_async_result_new_error (GObject
*source_object
,
418 GAsyncReadyCallback callback
,
425 GSimpleAsyncResult
*simple
;
428 g_return_val_if_fail (!source_object
|| G_IS_OBJECT (source_object
), NULL
);
429 g_return_val_if_fail (domain
!= 0, NULL
);
430 g_return_val_if_fail (format
!= NULL
, NULL
);
432 simple
= g_simple_async_result_new (source_object
,
436 va_start (args
, format
);
437 g_simple_async_result_set_error_va (simple
, domain
, code
, format
, args
);
445 g_simple_async_result_get_user_data (GAsyncResult
*res
)
447 return G_SIMPLE_ASYNC_RESULT (res
)->user_data
;
451 g_simple_async_result_get_source_object (GAsyncResult
*res
)
453 if (G_SIMPLE_ASYNC_RESULT (res
)->source_object
)
454 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res
)->source_object
);
459 g_simple_async_result_is_tagged (GAsyncResult
*res
,
462 return G_SIMPLE_ASYNC_RESULT (res
)->source_tag
== source_tag
;
466 g_simple_async_result_async_result_iface_init (GAsyncResultIface
*iface
)
468 iface
->get_user_data
= g_simple_async_result_get_user_data
;
469 iface
->get_source_object
= g_simple_async_result_get_source_object
;
470 iface
->is_tagged
= g_simple_async_result_is_tagged
;
474 * g_simple_async_result_set_handle_cancellation:
475 * @simple: a #GSimpleAsyncResult.
476 * @handle_cancellation: a #gboolean.
478 * Sets whether to handle cancellation within the asynchronous operation.
480 * This function has nothing to do with
481 * g_simple_async_result_set_check_cancellable(). It only refers to the
482 * #GCancellable passed to g_simple_async_result_run_in_thread().
487 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult
*simple
,
488 gboolean handle_cancellation
)
490 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
491 simple
->handle_cancellation
= handle_cancellation
;
495 * g_simple_async_result_get_source_tag: (skip)
496 * @simple: a #GSimpleAsyncResult.
498 * Gets the source tag for the #GSimpleAsyncResult.
500 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
502 * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
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.
525 * Deprecated: 2.46: Use #GTask instead.
528 g_simple_async_result_propagate_error (GSimpleAsyncResult
*simple
,
531 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
), FALSE
);
533 if (g_cancellable_set_error_if_cancelled (simple
->check_cancellable
, dest
))
538 g_propagate_error (dest
, simple
->error
);
539 simple
->error
= NULL
;
547 * g_simple_async_result_set_op_res_gpointer: (skip)
548 * @simple: a #GSimpleAsyncResult.
549 * @op_res: a pointer result from an asynchronous function.
550 * @destroy_op_res: a #GDestroyNotify function.
552 * Sets the operation result within the asynchronous result to a pointer.
554 * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
557 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult
*simple
,
559 GDestroyNotify destroy_op_res
)
561 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
563 clear_op_res (simple
);
564 simple
->op_res
.v_pointer
= op_res
;
565 simple
->destroy_op_res
= destroy_op_res
;
569 * g_simple_async_result_get_op_res_gpointer: (skip)
570 * @simple: a #GSimpleAsyncResult.
572 * Gets a pointer result as returned by the asynchronous function.
574 * Returns: a pointer from the result.
576 * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
579 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult
*simple
)
581 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
), NULL
);
582 return simple
->op_res
.v_pointer
;
586 * g_simple_async_result_set_op_res_gssize:
587 * @simple: a #GSimpleAsyncResult.
588 * @op_res: a #gssize.
590 * Sets the operation result within the asynchronous result to
593 * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
596 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult
*simple
,
599 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
600 clear_op_res (simple
);
601 simple
->op_res
.v_ssize
= op_res
;
605 * g_simple_async_result_get_op_res_gssize:
606 * @simple: a #GSimpleAsyncResult.
608 * Gets a gssize from the asynchronous result.
610 * Returns: a gssize returned from the asynchronous function.
612 * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
615 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult
*simple
)
617 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
), 0);
618 return simple
->op_res
.v_ssize
;
622 * g_simple_async_result_set_op_res_gboolean:
623 * @simple: a #GSimpleAsyncResult.
624 * @op_res: a #gboolean.
626 * Sets the operation result to a boolean within the asynchronous result.
628 * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
631 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult
*simple
,
634 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
635 clear_op_res (simple
);
636 simple
->op_res
.v_boolean
= !!op_res
;
640 * g_simple_async_result_get_op_res_gboolean:
641 * @simple: a #GSimpleAsyncResult.
643 * Gets the operation result boolean from within the asynchronous result.
645 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
646 * if the operation's result was %FALSE.
648 * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
651 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult
*simple
)
653 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
), FALSE
);
654 return simple
->op_res
.v_boolean
;
658 * g_simple_async_result_set_from_error:
659 * @simple: a #GSimpleAsyncResult.
662 * Sets the result from a #GError.
664 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
667 g_simple_async_result_set_from_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
= g_error_copy (error
);
676 simple
->failed
= TRUE
;
680 * g_simple_async_result_take_error: (skip)
681 * @simple: a #GSimpleAsyncResult
684 * Sets the result from @error, and takes over the caller's ownership
685 * of @error, so the caller does not need to free it any more.
689 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
692 g_simple_async_result_take_error (GSimpleAsyncResult
*simple
,
695 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
696 g_return_if_fail (error
!= NULL
);
699 g_error_free (simple
->error
);
700 simple
->error
= error
;
701 simple
->failed
= TRUE
;
705 * g_simple_async_result_set_error_va: (skip)
706 * @simple: a #GSimpleAsyncResult.
707 * @domain: a #GQuark (usually #G_IO_ERROR).
708 * @code: an error code.
709 * @format: a formatted error reporting string.
710 * @args: va_list of arguments.
712 * Sets an error within the asynchronous result without a #GError.
713 * Unless writing a binding, see g_simple_async_result_set_error().
715 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
718 g_simple_async_result_set_error_va (GSimpleAsyncResult
*simple
,
724 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
725 g_return_if_fail (domain
!= 0);
726 g_return_if_fail (format
!= NULL
);
729 g_error_free (simple
->error
);
730 simple
->error
= g_error_new_valist (domain
, code
, format
, args
);
731 simple
->failed
= TRUE
;
735 * g_simple_async_result_set_error: (skip)
736 * @simple: a #GSimpleAsyncResult.
737 * @domain: a #GQuark (usually #G_IO_ERROR).
738 * @code: an error code.
739 * @format: a formatted error reporting string.
740 * @...: a list of variables to fill in @format.
742 * Sets an error within the asynchronous result without a #GError.
744 * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
747 g_simple_async_result_set_error (GSimpleAsyncResult
*simple
,
755 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
756 g_return_if_fail (domain
!= 0);
757 g_return_if_fail (format
!= NULL
);
759 va_start (args
, format
);
760 g_simple_async_result_set_error_va (simple
, domain
, code
, format
, args
);
765 * g_simple_async_result_complete:
766 * @simple: a #GSimpleAsyncResult.
768 * Completes an asynchronous I/O job immediately. Must be called in
769 * the thread where the asynchronous result was to be delivered, as it
770 * invokes the callback directly. If you are in a different thread use
771 * g_simple_async_result_complete_in_idle().
773 * Calling this function takes a reference to @simple for as long as
774 * is needed to complete the call.
776 * Deprecated: 2.46: Use #GTask instead.
779 g_simple_async_result_complete (GSimpleAsyncResult
*simple
)
781 #ifndef G_DISABLE_CHECKS
782 GSource
*current_source
;
783 GMainContext
*current_context
;
786 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
788 #ifndef G_DISABLE_CHECKS
789 current_source
= g_main_current_source ();
790 if (current_source
&& !g_source_is_destroyed (current_source
))
792 current_context
= g_source_get_context (current_source
);
793 if (simple
->context
!= current_context
)
794 g_warning ("g_simple_async_result_complete() called from wrong context!");
798 if (simple
->callback
)
800 g_main_context_push_thread_default (simple
->context
);
801 simple
->callback (simple
->source_object
,
802 G_ASYNC_RESULT (simple
),
804 g_main_context_pop_thread_default (simple
->context
);
809 complete_in_idle_cb (gpointer data
)
811 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (data
);
813 g_simple_async_result_complete (simple
);
819 * g_simple_async_result_complete_in_idle:
820 * @simple: a #GSimpleAsyncResult.
822 * Completes an asynchronous function in an idle handler in the
823 * [thread-default main context][g-main-context-push-thread-default]
824 * of the thread that @simple was initially created in
825 * (and re-pushes that context around the invocation of the callback).
827 * Calling this function takes a reference to @simple for as long as
828 * is needed to complete the call.
830 * Deprecated: 2.46: Use #GTask instead.
833 g_simple_async_result_complete_in_idle (GSimpleAsyncResult
*simple
)
837 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
839 g_object_ref (simple
);
841 source
= g_idle_source_new ();
842 g_source_set_priority (source
, G_PRIORITY_DEFAULT
);
843 g_source_set_callback (source
, complete_in_idle_cb
, simple
, g_object_unref
);
844 g_source_set_name (source
, "[gio] complete_in_idle_cb");
846 g_source_attach (source
, simple
->context
);
847 g_source_unref (source
);
851 GSimpleAsyncResult
*simple
;
852 GCancellable
*cancellable
;
853 GSimpleAsyncThreadFunc func
;
858 complete_in_idle_cb_for_thread (gpointer _data
)
860 RunInThreadData
*data
= _data
;
861 GSimpleAsyncResult
*simple
;
863 simple
= data
->simple
;
865 if (simple
->handle_cancellation
&&
866 g_cancellable_is_cancelled (data
->cancellable
))
867 g_simple_async_result_set_error (simple
,
869 G_IO_ERROR_CANCELLED
,
870 "%s", _("Operation was cancelled"));
872 g_simple_async_result_complete (simple
);
874 if (data
->cancellable
)
875 g_object_unref (data
->cancellable
);
876 g_object_unref (data
->simple
);
883 run_in_thread (GIOSchedulerJob
*job
,
887 RunInThreadData
*data
= _data
;
888 GSimpleAsyncResult
*simple
= data
->simple
;
891 if (simple
->handle_cancellation
&&
892 g_cancellable_is_cancelled (c
))
893 g_simple_async_result_set_error (simple
,
895 G_IO_ERROR_CANCELLED
,
896 "%s", _("Operation was cancelled"));
899 simple
->source_object
,
902 source
= g_idle_source_new ();
903 g_source_set_priority (source
, G_PRIORITY_DEFAULT
);
904 g_source_set_callback (source
, complete_in_idle_cb_for_thread
, data
, NULL
);
905 g_source_set_name (source
, "[gio] complete_in_idle_cb_for_thread");
907 g_source_attach (source
, simple
->context
);
908 g_source_unref (source
);
914 * g_simple_async_result_run_in_thread: (skip)
915 * @simple: a #GSimpleAsyncResult.
916 * @func: a #GSimpleAsyncThreadFunc.
917 * @io_priority: the io priority of the request.
918 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
920 * Runs the asynchronous job in a separate thread and then calls
921 * g_simple_async_result_complete_in_idle() on @simple to return
922 * the result to the appropriate main loop.
924 * Calling this function takes a reference to @simple for as long as
925 * is needed to run the job and report its completion.
927 * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
930 g_simple_async_result_run_in_thread (GSimpleAsyncResult
*simple
,
931 GSimpleAsyncThreadFunc func
,
933 GCancellable
*cancellable
)
935 RunInThreadData
*data
;
937 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
938 g_return_if_fail (func
!= NULL
);
940 data
= g_new (RunInThreadData
, 1);
942 data
->simple
= g_object_ref (simple
);
943 data
->cancellable
= cancellable
;
945 g_object_ref (cancellable
);
946 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
;
947 g_io_scheduler_push_job (run_in_thread
, data
, NULL
, io_priority
, cancellable
);
948 G_GNUC_END_IGNORE_DEPRECATIONS
;
952 * g_simple_async_result_is_valid:
953 * @result: the #GAsyncResult passed to the _finish function.
954 * @source: (nullable): the #GObject passed to the _finish function.
955 * @source_tag: (nullable): the asynchronous function.
957 * Ensures that the data passed to the _finish function of an async
958 * operation is consistent. Three checks are performed.
960 * First, @result is checked to ensure that it is really a
961 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
962 * matches the source object of @result. Third, @source_tag is
963 * checked to ensure that it is equal to the @source_tag argument given
964 * to g_simple_async_result_new() (which, by convention, is a pointer
965 * to the _async function corresponding to the _finish function from
966 * which this function is called). (Alternatively, if either
967 * @source_tag or @result's source tag is %NULL, then the source tag
970 * Returns: #TRUE if all checks passed or #FALSE if any failed.
974 * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
977 g_simple_async_result_is_valid (GAsyncResult
*result
,
981 GSimpleAsyncResult
*simple
;
983 gpointer result_source_tag
;
985 if (!G_IS_SIMPLE_ASYNC_RESULT (result
))
987 simple
= (GSimpleAsyncResult
*)result
;
989 cmp_source
= g_async_result_get_source_object (result
);
990 if (cmp_source
!= source
)
992 if (cmp_source
!= NULL
)
993 g_object_unref (cmp_source
);
996 if (cmp_source
!= NULL
)
997 g_object_unref (cmp_source
);
999 result_source_tag
= g_simple_async_result_get_source_tag (simple
);
1000 return source_tag
== NULL
|| result_source_tag
== NULL
||
1001 source_tag
== result_source_tag
;
1005 * g_simple_async_report_error_in_idle: (skip)
1006 * @object: (nullable): a #GObject, or %NULL.
1007 * @callback: a #GAsyncReadyCallback.
1008 * @user_data: user data passed to @callback.
1009 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
1010 * @code: a specific error code.
1011 * @format: a formatted error reporting string.
1012 * @...: a list of variables to fill in @format.
1014 * Reports an error in an asynchronous function in an idle function by
1015 * directly setting the contents of the #GAsyncResult with the given error
1018 * Deprecated: 2.46: Use g_task_report_error().
1021 g_simple_async_report_error_in_idle (GObject
*object
,
1022 GAsyncReadyCallback callback
,
1029 GSimpleAsyncResult
*simple
;
1032 g_return_if_fail (!object
|| G_IS_OBJECT (object
));
1033 g_return_if_fail (domain
!= 0);
1034 g_return_if_fail (format
!= NULL
);
1036 simple
= g_simple_async_result_new (object
,
1040 va_start (args
, format
);
1041 g_simple_async_result_set_error_va (simple
, domain
, code
, format
, args
);
1043 g_simple_async_result_complete_in_idle (simple
);
1044 g_object_unref (simple
);
1048 * g_simple_async_report_gerror_in_idle:
1049 * @object: (nullable): a #GObject, or %NULL
1050 * @callback: (scope async): a #GAsyncReadyCallback.
1051 * @user_data: (closure): user data passed to @callback.
1052 * @error: the #GError to report
1054 * Reports an error in an idle function. Similar to
1055 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1056 * than building a new one.
1058 * Deprecated: 2.46: Use g_task_report_error().
1061 g_simple_async_report_gerror_in_idle (GObject
*object
,
1062 GAsyncReadyCallback callback
,
1064 const GError
*error
)
1066 GSimpleAsyncResult
*simple
;
1068 g_return_if_fail (!object
|| G_IS_OBJECT (object
));
1069 g_return_if_fail (error
!= NULL
);
1071 simple
= g_simple_async_result_new_from_error (object
,
1075 g_simple_async_result_complete_in_idle (simple
);
1076 g_object_unref (simple
);
1080 * g_simple_async_report_take_gerror_in_idle: (skip)
1081 * @object: (nullable): a #GObject, or %NULL
1082 * @callback: a #GAsyncReadyCallback.
1083 * @user_data: user data passed to @callback.
1084 * @error: the #GError to report
1086 * Reports an error in an idle function. Similar to
1087 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1088 * ownership of @error, so the caller does not have to free it any more.
1092 * Deprecated: 2.46: Use g_task_report_error().
1095 g_simple_async_report_take_gerror_in_idle (GObject
*object
,
1096 GAsyncReadyCallback callback
,
1100 GSimpleAsyncResult
*simple
;
1102 g_return_if_fail (!object
|| G_IS_OBJECT (object
));
1103 g_return_if_fail (error
!= NULL
);
1105 simple
= g_simple_async_result_new_take_error (object
,
1109 g_simple_async_result_complete_in_idle (simple
);
1110 g_object_unref (simple
);
1114 * g_simple_async_result_set_check_cancellable:
1115 * @simple: a #GSimpleAsyncResult
1116 * @check_cancellable: (nullable): a #GCancellable to check, or %NULL to unset
1118 * Sets a #GCancellable to check before dispatching results.
1120 * This function has one very specific purpose: the provided cancellable
1121 * is checked at the time of g_simple_async_result_propagate_error() If
1122 * it is cancelled, these functions will return an "Operation was
1123 * cancelled" error (%G_IO_ERROR_CANCELLED).
1125 * Implementors of cancellable asynchronous functions should use this in
1126 * order to provide a guarantee to their callers that cancelling an
1127 * async operation will reliably result in an error being returned for
1128 * that operation (even if a positive result for the operation has
1129 * already been sent as an idle to the main context to be dispatched).
1131 * The checking described above is done regardless of any call to the
1132 * unrelated g_simple_async_result_set_handle_cancellation() function.
1136 * Deprecated: 2.46: Use #GTask instead.
1139 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult
*simple
,
1140 GCancellable
*check_cancellable
)
1142 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple
));
1143 g_return_if_fail (check_cancellable
== NULL
|| G_IS_CANCELLABLE (check_cancellable
));
1145 g_clear_object (&simple
->check_cancellable
);
1146 if (check_cancellable
)
1147 simple
->check_cancellable
= g_object_ref (check_cancellable
);
1150 G_GNUC_END_IGNORE_DEPRECATIONS