1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
34 #include "gsimpleasyncresult.h"
35 #include "gasyncresult.h"
36 #include "gcancellable.h"
37 #include "gioscheduler.h"
38 #include <gio/gioerror.h>
43 * SECTION:gsimpleasyncresult
44 * @short_description: Simple asynchronous results implementation
46 * @see_also: #GAsyncResult
48 * Implements #GAsyncResult for simple cases. Most of the time, this
49 * will be all an application needs, and will be used transparently.
50 * Because of this, #GSimpleAsyncResult is used throughout GIO for
51 * handling asynchronous functions.
53 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
54 * reporting, operation cancellation and the final state of an operation,
55 * completely transparent to the application. Results can be returned
56 * as a pointer e.g. for functions that return data that is collected
57 * asynchronously, a boolean value for checking the success or failure
58 * of an operation, or a #gssize for operations which return the number
59 * of bytes modified by the operation; all of the simple return cases
62 * Most of the time, an application will not need to know of the details
63 * of this API; it is handled transparently, and any necessary operations
64 * are handled by #GAsyncResult's interface. However, if implementing a
65 * new GIO module, for writing language bindings, or for complex
66 * applications that need better control of how asynchronous operations
67 * are completed, it is important to understand this functionality.
69 * GSimpleAsyncResults are tagged with the calling function to ensure
70 * that asynchronous functions and their finishing functions are used
73 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
74 * If the result needs to be created for a #GError, use
75 * g_simple_async_result_new_from_error(). If a #GError is not available
76 * (e.g. the asynchronous operation's doesn't take a #GError argument),
77 * but the result still needs to be created for an error condition, use
78 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
79 * if your application or binding requires passing a variable argument list
80 * directly), and the error can then be propagated through the use of
81 * g_simple_async_result_propagate_error().
83 * An asynchronous operation can be made to ignore a cancellation event by
84 * calling g_simple_async_result_set_handle_cancellation() with a
85 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
86 * operations that are dangerous to cancel, such as close (which would
87 * cause a leak if cancelled before being run).
89 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
90 * or it can use #GThread<!-- -->s if available.
91 * g_simple_async_result_complete() will finish an I/O task directly
92 * from the point where it is called. g_simple_async_result_complete_in_idle()
93 * will finish it from an idle handler in the <link
94 * linkend="g-main-context-push-thread-default">thread-default main
95 * context</link>. g_simple_async_result_run_in_thread() will run the
96 * job in a separate thread and then deliver the result to the
97 * thread-default main context.
99 * To set the results of an asynchronous function,
100 * g_simple_async_result_set_op_res_gpointer(),
101 * g_simple_async_result_set_op_res_gboolean(), and
102 * g_simple_async_result_set_op_res_gssize()
103 * are provided, setting the operation's result to a gpointer, gboolean, or
104 * gssize, respectively.
106 * Likewise, to get the result of an asynchronous function,
107 * g_simple_async_result_get_op_res_gpointer(),
108 * g_simple_async_result_get_op_res_gboolean(), and
109 * g_simple_async_result_get_op_res_gssize() are
110 * provided, getting the operation's result as a gpointer, gboolean, and
111 * gssize, respectively.
114 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
116 struct _GSimpleAsyncResult
118 GObject parent_instance;
120 GObject *source_object;
121 GAsyncReadyCallback callback;
123 GMainContext *context;
126 gboolean handle_cancellation;
136 GDestroyNotify destroy_op_res;
139 struct _GSimpleAsyncResultClass
141 GObjectClass parent_class;
145 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
146 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
147 g_simple_async_result_async_result_iface_init))
150 clear_op_res (GSimpleAsyncResult *simple)
152 if (simple->destroy_op_res)
153 simple->destroy_op_res (simple->op_res.v_pointer);
154 simple->destroy_op_res = NULL;
155 simple->op_res.v_ssize = 0;
159 g_simple_async_result_finalize (GObject *object)
161 GSimpleAsyncResult *simple;
163 simple = G_SIMPLE_ASYNC_RESULT (object);
165 if (simple->source_object)
166 g_object_unref (simple->source_object);
169 g_main_context_unref (simple->context);
171 clear_op_res (simple);
174 g_error_free (simple->error);
176 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
180 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->finalize = g_simple_async_result_finalize;
188 g_simple_async_result_init (GSimpleAsyncResult *simple)
190 simple->handle_cancellation = TRUE;
192 simple->context = g_main_context_get_thread_default ();
194 g_main_context_ref (simple->context);
198 * g_simple_async_result_new:
199 * @source_object: a #GObject the asynchronous function was called with,
201 * @callback: a #GAsyncReadyCallback.
202 * @user_data: user data passed to @callback.
203 * @source_tag: the asynchronous function.
205 * Creates a #GSimpleAsyncResult.
207 * Returns: a #GSimpleAsyncResult.
210 g_simple_async_result_new (GObject *source_object,
211 GAsyncReadyCallback callback,
215 GSimpleAsyncResult *simple;
217 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
219 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
220 simple->callback = callback;
222 simple->source_object = g_object_ref (source_object);
224 simple->source_object = NULL;
225 simple->user_data = user_data;
226 simple->source_tag = source_tag;
232 * g_simple_async_result_new_from_error:
233 * @source_object: a #GObject, or %NULL.
234 * @callback: a #GAsyncReadyCallback.
235 * @user_data: user data passed to @callback.
236 * @error: a #GError location.
238 * Creates a #GSimpleAsyncResult from an error condition.
240 * Returns: a #GSimpleAsyncResult.
243 g_simple_async_result_new_from_error (GObject *source_object,
244 GAsyncReadyCallback callback,
248 GSimpleAsyncResult *simple;
250 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
252 simple = g_simple_async_result_new (source_object,
255 g_simple_async_result_set_from_error (simple, error);
261 * g_simple_async_result_new_error:
262 * @source_object: a #GObject, or %NULL.
263 * @callback: a #GAsyncReadyCallback.
264 * @user_data: user data passed to @callback.
265 * @domain: a #GQuark.
266 * @code: an error code.
267 * @format: a string with format characters.
268 * @...: a list of values to insert into @format.
270 * Creates a new #GSimpleAsyncResult with a set error.
272 * Returns: a #GSimpleAsyncResult.
275 g_simple_async_result_new_error (GObject *source_object,
276 GAsyncReadyCallback callback,
283 GSimpleAsyncResult *simple;
286 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
287 g_return_val_if_fail (domain != 0, NULL);
288 g_return_val_if_fail (format != NULL, NULL);
290 simple = g_simple_async_result_new (source_object,
294 va_start (args, format);
295 g_simple_async_result_set_error_va (simple, domain, code, format, args);
303 g_simple_async_result_get_user_data (GAsyncResult *res)
305 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
309 g_simple_async_result_get_source_object (GAsyncResult *res)
311 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
312 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
317 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
319 iface->get_user_data = g_simple_async_result_get_user_data;
320 iface->get_source_object = g_simple_async_result_get_source_object;
324 * g_simple_async_result_set_handle_cancellation:
325 * @simple: a #GSimpleAsyncResult.
326 * @handle_cancellation: a #gboolean.
328 * Sets whether to handle cancellation within the asynchronous operation.
332 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
333 gboolean handle_cancellation)
335 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
336 simple->handle_cancellation = handle_cancellation;
340 * g_simple_async_result_get_source_tag:
341 * @simple: a #GSimpleAsyncResult.
343 * Gets the source tag for the #GSimpleAsyncResult.
345 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
348 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
350 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
351 return simple->source_tag;
355 * g_simple_async_result_propagate_error:
356 * @simple: a #GSimpleAsyncResult.
357 * @dest: a location to propegate the error to.
359 * Propagates an error from within the simple asynchronous result to
360 * a given destination.
362 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
365 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
368 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
372 g_propagate_error (dest, simple->error);
373 simple->error = NULL;
381 * g_simple_async_result_set_op_res_gpointer:
382 * @simple: a #GSimpleAsyncResult.
383 * @op_res: a pointer result from an asynchronous function.
384 * @destroy_op_res: a #GDestroyNotify function.
386 * Sets the operation result within the asynchronous result to a pointer.
389 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
391 GDestroyNotify destroy_op_res)
393 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
395 clear_op_res (simple);
396 simple->op_res.v_pointer = op_res;
397 simple->destroy_op_res = destroy_op_res;
401 * g_simple_async_result_get_op_res_gpointer:
402 * @simple: a #GSimpleAsyncResult.
404 * Gets a pointer result as returned by the asynchronous function.
406 * Returns: a pointer from the result.
409 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
411 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
412 return simple->op_res.v_pointer;
416 * g_simple_async_result_set_op_res_gssize:
417 * @simple: a #GSimpleAsyncResult.
418 * @op_res: a #gssize.
420 * Sets the operation result within the asynchronous result to
424 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
427 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
428 clear_op_res (simple);
429 simple->op_res.v_ssize = op_res;
433 * g_simple_async_result_get_op_res_gssize:
434 * @simple: a #GSimpleAsyncResult.
436 * Gets a gssize from the asynchronous result.
438 * Returns: a gssize returned from the asynchronous function.
441 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
443 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
444 return simple->op_res.v_ssize;
448 * g_simple_async_result_set_op_res_gboolean:
449 * @simple: a #GSimpleAsyncResult.
450 * @op_res: a #gboolean.
452 * Sets the operation result to a boolean within the asynchronous result.
455 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
458 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
459 clear_op_res (simple);
460 simple->op_res.v_boolean = !!op_res;
464 * g_simple_async_result_get_op_res_gboolean:
465 * @simple: a #GSimpleAsyncResult.
467 * Gets the operation result boolean from within the asynchronous result.
469 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
470 * if the operation's result was %FALSE.
473 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
475 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
476 return simple->op_res.v_boolean;
480 * g_simple_async_result_set_from_error:
481 * @simple: a #GSimpleAsyncResult.
484 * Sets the result from a #GError.
487 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
490 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
491 g_return_if_fail (error != NULL);
494 g_error_free (simple->error);
495 simple->error = g_error_copy (error);
496 simple->failed = TRUE;
500 * g_simple_async_result_set_error_va:
501 * @simple: a #GSimpleAsyncResult.
502 * @domain: a #GQuark (usually #G_IO_ERROR).
503 * @code: an error code.
504 * @format: a formatted error reporting string.
505 * @args: va_list of arguments.
507 * Sets an error within the asynchronous result without a #GError.
508 * Unless writing a binding, see g_simple_async_result_set_error().
511 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
517 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
518 g_return_if_fail (domain != 0);
519 g_return_if_fail (format != NULL);
522 g_error_free (simple->error);
523 simple->error = g_error_new_valist (domain, code, format, args);
524 simple->failed = TRUE;
528 * g_simple_async_result_set_error:
529 * @simple: a #GSimpleAsyncResult.
530 * @domain: a #GQuark (usually #G_IO_ERROR).
531 * @code: an error code.
532 * @format: a formatted error reporting string.
533 * @...: a list of variables to fill in @format.
535 * Sets an error within the asynchronous result without a #GError.
538 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
546 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
547 g_return_if_fail (domain != 0);
548 g_return_if_fail (format != NULL);
550 va_start (args, format);
551 g_simple_async_result_set_error_va (simple, domain, code, format, args);
556 * g_simple_async_result_complete:
557 * @simple: a #GSimpleAsyncResult.
559 * Completes an asynchronous I/O job immediately. Must be called in
560 * the thread where the asynchronous result was to be delivered, as it
561 * invokes the callback directly. If you are in a different thread use
562 * g_simple_async_result_complete_in_idle().
564 * Calling this function takes a reference to @simple for as long as
565 * is needed to complete the call.
568 g_simple_async_result_complete (GSimpleAsyncResult *simple)
570 #ifndef G_DISABLE_CHECKS
571 GSource *current_source;
572 GMainContext *current_context;
575 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
577 #ifndef G_DISABLE_CHECKS
578 current_source = g_main_current_source ();
579 if (current_source && !g_source_is_destroyed (current_source))
581 current_context = g_source_get_context (current_source);
582 if (current_context == g_main_context_default ())
583 current_context = NULL;
584 if (simple->context != current_context)
585 g_warning ("g_simple_async_result_complete() called from wrong context!");
589 if (simple->callback)
590 simple->callback (simple->source_object,
591 G_ASYNC_RESULT (simple),
596 complete_in_idle_cb (gpointer data)
598 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
600 g_simple_async_result_complete (simple);
606 * g_simple_async_result_complete_in_idle:
607 * @simple: a #GSimpleAsyncResult.
609 * Completes an asynchronous function in an idle handler in the <link
610 * linkend="g-main-context-push-thread-default">thread-default main
611 * loop</link> of the thread that @simple was initially created in.
613 * Calling this function takes a reference to @simple for as long as
614 * is needed to complete the call.
617 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
621 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
623 g_object_ref (simple);
625 source = g_idle_source_new ();
626 g_source_set_priority (source, G_PRIORITY_DEFAULT);
627 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
629 g_source_attach (source, simple->context);
630 g_source_unref (source);
634 GSimpleAsyncResult *simple;
635 GCancellable *cancellable;
636 GSimpleAsyncThreadFunc func;
641 complete_in_idle_cb_for_thread (gpointer _data)
643 RunInThreadData *data = _data;
644 GSimpleAsyncResult *simple;
646 simple = data->simple;
648 if (simple->handle_cancellation &&
649 g_cancellable_is_cancelled (data->cancellable))
650 g_simple_async_result_set_error (simple,
652 G_IO_ERROR_CANCELLED,
653 "%s", _("Operation was cancelled"));
655 g_simple_async_result_complete (simple);
657 if (data->cancellable)
658 g_object_unref (data->cancellable);
659 g_object_unref (data->simple);
666 run_in_thread (GIOSchedulerJob *job,
670 RunInThreadData *data = _data;
671 GSimpleAsyncResult *simple = data->simple;
674 if (simple->handle_cancellation &&
675 g_cancellable_is_cancelled (c))
676 g_simple_async_result_set_error (simple,
678 G_IO_ERROR_CANCELLED,
679 "%s", _("Operation was cancelled"));
682 simple->source_object,
685 source = g_idle_source_new ();
686 g_source_set_priority (source, G_PRIORITY_DEFAULT);
687 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
689 g_source_attach (source, simple->context);
690 g_source_unref (source);
696 * g_simple_async_result_run_in_thread:
697 * @simple: a #GSimpleAsyncResult.
698 * @func: a #GSimpleAsyncThreadFunc.
699 * @io_priority: the io priority of the request.
700 * @cancellable: optional #GCancellable object, %NULL to ignore.
702 * Runs the asynchronous job in a separate thread and then calls
703 * g_simple_async_result_complete_in_idle() on @simple to return
704 * the result to the appropriate main loop.
706 * Calling this function takes a reference to @simple for as long as
707 * is needed to run the job and report its completion.
710 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
711 GSimpleAsyncThreadFunc func,
713 GCancellable *cancellable)
715 RunInThreadData *data;
717 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
718 g_return_if_fail (func != NULL);
720 data = g_new (RunInThreadData, 1);
722 data->simple = g_object_ref (simple);
723 data->cancellable = cancellable;
725 g_object_ref (cancellable);
726 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
730 * g_simple_async_result_is_valid:
731 * @result: the #GAsyncResult passed to the _finish function.
732 * @source: the #GObject passed to the _finish function.
733 * @source_tag: the asynchronous function.
735 * Ensures that the data passed to the _finish function of an async
736 * operation is consistent. Three checks are performed.
738 * First, @result is checked to ensure that it is really a
739 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
740 * matches the source object of @result. Third, @source_tag is
741 * checked to ensure that it is either %NULL (as it is when the result was
742 * created by g_simple_async_report_error_in_idle() or
743 * g_simple_async_report_gerror_in_idle()) or equal to the
744 * @source_tag argument given to g_simple_async_result_new() (which, by
745 * convention, is a pointer to the _async function corresponding to the
746 * _finish function from which this function is called).
748 * Returns: #TRUE if all checks passed or #FALSE if any failed.
751 g_simple_async_result_is_valid (GAsyncResult *result,
755 GSimpleAsyncResult *simple;
758 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
760 simple = (GSimpleAsyncResult *)result;
762 cmp_source = g_async_result_get_source_object (result);
763 if (cmp_source != source)
765 if (cmp_source != NULL)
766 g_object_unref (cmp_source);
769 if (cmp_source != NULL)
770 g_object_unref (cmp_source);
772 return source_tag == NULL ||
773 source_tag == g_simple_async_result_get_source_tag (simple);
777 * g_simple_async_report_error_in_idle:
778 * @object: a #GObject.
779 * @callback: a #GAsyncReadyCallback.
780 * @user_data: user data passed to @callback.
781 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
782 * @code: a specific error code.
783 * @format: a formatted error reporting string.
784 * @...: a list of variables to fill in @format.
786 * Reports an error in an asynchronous function in an idle function by
787 * directly setting the contents of the #GAsyncResult with the given error
791 g_simple_async_report_error_in_idle (GObject *object,
792 GAsyncReadyCallback callback,
799 GSimpleAsyncResult *simple;
802 g_return_if_fail (G_IS_OBJECT (object));
803 g_return_if_fail (domain != 0);
804 g_return_if_fail (format != NULL);
806 simple = g_simple_async_result_new (object,
810 va_start (args, format);
811 g_simple_async_result_set_error_va (simple, domain, code, format, args);
813 g_simple_async_result_complete_in_idle (simple);
814 g_object_unref (simple);
818 * g_simple_async_report_gerror_in_idle:
819 * @object: a #GObject.
820 * @callback: a #GAsyncReadyCallback.
821 * @user_data: user data passed to @callback.
822 * @error: the #GError to report
824 * Reports an error in an idle function. Similar to
825 * g_simple_async_report_error_in_idle(), but takes a #GError rather
826 * than building a new one.
829 g_simple_async_report_gerror_in_idle (GObject *object,
830 GAsyncReadyCallback callback,
834 GSimpleAsyncResult *simple;
836 g_return_if_fail (G_IS_OBJECT (object));
837 g_return_if_fail (error != NULL);
839 simple = g_simple_async_result_new_from_error (object,
843 g_simple_async_result_complete_in_idle (simple);
844 g_object_unref (simple);