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>
44 * SECTION:gsimpleasyncresult
45 * @short_description: Simple asynchronous results implementation
47 * @see_also: #GAsyncResult
49 * Implements #GAsyncResult for simple cases. Most of the time, this
50 * will be all an application needs, and will be used transparently.
51 * Because of this, #GSimpleAsyncResult is used throughout GIO for
52 * handling asynchronous functions.
54 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
55 * reporting, operation cancellation and the final state of an operation,
56 * completely transparent to the application. Results can be returned
57 * as a pointer e.g. for functions that return data that is collected
58 * asynchronously, a boolean value for checking the success or failure
59 * of an operation, or a #gssize for operations which return the number
60 * of bytes modified by the operation; all of the simple return cases
63 * Most of the time, an application will not need to know of the details
64 * of this API; it is handled transparently, and any necessary operations
65 * are handled by #GAsyncResult's interface. However, if implementing a
66 * new GIO module, for writing language bindings, or for complex
67 * applications that need better control of how asynchronous operations
68 * are completed, it is important to understand this functionality.
70 * GSimpleAsyncResults are tagged with the calling function to ensure
71 * that asynchronous functions and their finishing functions are used
74 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
75 * If the result needs to be created for a #GError, use
76 * g_simple_async_result_new_from_error(). If a #GError is not available
77 * (e.g. the asynchronous operation's doesn't take a #GError argument),
78 * but the result still needs to be created for an error condition, use
79 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
80 * if your application or binding requires passing a variable argument list
81 * directly), and the error can then be propagated through the use of
82 * g_simple_async_result_propagate_error().
84 * An asynchronous operation can be made to ignore a cancellation event by
85 * calling g_simple_async_result_set_handle_cancellation() with a
86 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
87 * operations that are dangerous to cancel, such as close (which would
88 * cause a leak if cancelled before being run).
90 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
91 * or it can use #GThread<!-- -->s if available.
92 * g_simple_async_result_complete() will finish an I/O task directly
93 * from the point where it is called. g_simple_async_result_complete_in_idle()
94 * will finish it from an idle handler in the <link
95 * linkend="g-main-context-push-thread-default">thread-default main
96 * context</link>. g_simple_async_result_run_in_thread() will run the
97 * job in a separate thread and then deliver the result to the
98 * thread-default main context.
100 * To set the results of an asynchronous function,
101 * g_simple_async_result_set_op_res_gpointer(),
102 * g_simple_async_result_set_op_res_gboolean(), and
103 * g_simple_async_result_set_op_res_gssize()
104 * are provided, setting the operation's result to a gpointer, gboolean, or
105 * gssize, respectively.
107 * Likewise, to get the result of an asynchronous function,
108 * g_simple_async_result_get_op_res_gpointer(),
109 * g_simple_async_result_get_op_res_gboolean(), and
110 * g_simple_async_result_get_op_res_gssize() are
111 * provided, getting the operation's result as a gpointer, gboolean, and
112 * gssize, respectively.
115 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
117 struct _GSimpleAsyncResult
119 GObject parent_instance;
121 GObject *source_object;
122 GAsyncReadyCallback callback;
124 GMainContext *context;
127 gboolean handle_cancellation;
137 GDestroyNotify destroy_op_res;
140 struct _GSimpleAsyncResultClass
142 GObjectClass parent_class;
146 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
147 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
148 g_simple_async_result_async_result_iface_init))
151 clear_op_res (GSimpleAsyncResult *simple)
153 if (simple->destroy_op_res)
154 simple->destroy_op_res (simple->op_res.v_pointer);
155 simple->destroy_op_res = NULL;
156 simple->op_res.v_ssize = 0;
160 g_simple_async_result_finalize (GObject *object)
162 GSimpleAsyncResult *simple;
164 simple = G_SIMPLE_ASYNC_RESULT (object);
166 if (simple->source_object)
167 g_object_unref (simple->source_object);
170 g_main_context_unref (simple->context);
172 clear_op_res (simple);
175 g_error_free (simple->error);
177 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
181 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 gobject_class->finalize = g_simple_async_result_finalize;
189 g_simple_async_result_init (GSimpleAsyncResult *simple)
191 simple->handle_cancellation = TRUE;
193 simple->context = g_main_context_get_thread_default ();
195 g_main_context_ref (simple->context);
199 * g_simple_async_result_new:
200 * @source_object: a #GObject the asynchronous function was called with,
202 * @callback: a #GAsyncReadyCallback.
203 * @user_data: user data passed to @callback.
204 * @source_tag: the asynchronous function.
206 * Creates a #GSimpleAsyncResult.
208 * Returns: a #GSimpleAsyncResult.
211 g_simple_async_result_new (GObject *source_object,
212 GAsyncReadyCallback callback,
216 GSimpleAsyncResult *simple;
218 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
220 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
221 simple->callback = callback;
223 simple->source_object = g_object_ref (source_object);
225 simple->source_object = NULL;
226 simple->user_data = user_data;
227 simple->source_tag = source_tag;
233 * g_simple_async_result_new_from_error:
234 * @source_object: a #GObject, or %NULL.
235 * @callback: a #GAsyncReadyCallback.
236 * @user_data: user data passed to @callback.
237 * @error: a #GError location.
239 * Creates a #GSimpleAsyncResult from an error condition.
241 * Returns: a #GSimpleAsyncResult.
244 g_simple_async_result_new_from_error (GObject *source_object,
245 GAsyncReadyCallback callback,
249 GSimpleAsyncResult *simple;
251 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
253 simple = g_simple_async_result_new (source_object,
256 g_simple_async_result_set_from_error (simple, error);
262 * g_simple_async_result_new_error:
263 * @source_object: a #GObject, or %NULL.
264 * @callback: a #GAsyncReadyCallback.
265 * @user_data: user data passed to @callback.
266 * @domain: a #GQuark.
267 * @code: an error code.
268 * @format: a string with format characters.
269 * @...: a list of values to insert into @format.
271 * Creates a new #GSimpleAsyncResult with a set error.
273 * Returns: a #GSimpleAsyncResult.
276 g_simple_async_result_new_error (GObject *source_object,
277 GAsyncReadyCallback callback,
284 GSimpleAsyncResult *simple;
287 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
288 g_return_val_if_fail (domain != 0, NULL);
289 g_return_val_if_fail (format != NULL, NULL);
291 simple = g_simple_async_result_new (source_object,
295 va_start (args, format);
296 g_simple_async_result_set_error_va (simple, domain, code, format, args);
304 g_simple_async_result_get_user_data (GAsyncResult *res)
306 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
310 g_simple_async_result_get_source_object (GAsyncResult *res)
312 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
313 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
318 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
320 iface->get_user_data = g_simple_async_result_get_user_data;
321 iface->get_source_object = g_simple_async_result_get_source_object;
325 * g_simple_async_result_set_handle_cancellation:
326 * @simple: a #GSimpleAsyncResult.
327 * @handle_cancellation: a #gboolean.
329 * Sets whether to handle cancellation within the asynchronous operation.
333 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
334 gboolean handle_cancellation)
336 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
337 simple->handle_cancellation = handle_cancellation;
341 * g_simple_async_result_get_source_tag:
342 * @simple: a #GSimpleAsyncResult.
344 * Gets the source tag for the #GSimpleAsyncResult.
346 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
349 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
351 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
352 return simple->source_tag;
356 * g_simple_async_result_propagate_error:
357 * @simple: a #GSimpleAsyncResult.
358 * @dest: a location to propegate the error to.
360 * Propagates an error from within the simple asynchronous result to
361 * a given destination.
363 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
366 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
369 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
373 g_propagate_error (dest, simple->error);
374 simple->error = NULL;
382 * g_simple_async_result_set_op_res_gpointer:
383 * @simple: a #GSimpleAsyncResult.
384 * @op_res: a pointer result from an asynchronous function.
385 * @destroy_op_res: a #GDestroyNotify function.
387 * Sets the operation result within the asynchronous result to a pointer.
390 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
392 GDestroyNotify destroy_op_res)
394 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
396 clear_op_res (simple);
397 simple->op_res.v_pointer = op_res;
398 simple->destroy_op_res = destroy_op_res;
402 * g_simple_async_result_get_op_res_gpointer:
403 * @simple: a #GSimpleAsyncResult.
405 * Gets a pointer result as returned by the asynchronous function.
407 * Returns: a pointer from the result.
410 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
412 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
413 return simple->op_res.v_pointer;
417 * g_simple_async_result_set_op_res_gssize:
418 * @simple: a #GSimpleAsyncResult.
419 * @op_res: a #gssize.
421 * Sets the operation result within the asynchronous result to
425 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
428 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
429 clear_op_res (simple);
430 simple->op_res.v_ssize = op_res;
434 * g_simple_async_result_get_op_res_gssize:
435 * @simple: a #GSimpleAsyncResult.
437 * Gets a gssize from the asynchronous result.
439 * Returns: a gssize returned from the asynchronous function.
442 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
444 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
445 return simple->op_res.v_ssize;
449 * g_simple_async_result_set_op_res_gboolean:
450 * @simple: a #GSimpleAsyncResult.
451 * @op_res: a #gboolean.
453 * Sets the operation result to a boolean within the asynchronous result.
456 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
459 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
460 clear_op_res (simple);
461 simple->op_res.v_boolean = !!op_res;
465 * g_simple_async_result_get_op_res_gboolean:
466 * @simple: a #GSimpleAsyncResult.
468 * Gets the operation result boolean from within the asynchronous result.
470 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
471 * if the operation's result was %FALSE.
474 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
476 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
477 return simple->op_res.v_boolean;
481 * g_simple_async_result_set_from_error:
482 * @simple: a #GSimpleAsyncResult.
485 * Sets the result from a #GError.
488 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
491 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
492 g_return_if_fail (error != NULL);
495 g_error_free (simple->error);
496 simple->error = g_error_copy (error);
497 simple->failed = TRUE;
501 * g_simple_async_result_set_error_va:
502 * @simple: a #GSimpleAsyncResult.
503 * @domain: a #GQuark (usually #G_IO_ERROR).
504 * @code: an error code.
505 * @format: a formatted error reporting string.
506 * @args: va_list of arguments.
508 * Sets an error within the asynchronous result without a #GError.
509 * Unless writing a binding, see g_simple_async_result_set_error().
512 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
518 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
519 g_return_if_fail (domain != 0);
520 g_return_if_fail (format != NULL);
523 g_error_free (simple->error);
524 simple->error = g_error_new_valist (domain, code, format, args);
525 simple->failed = TRUE;
529 * g_simple_async_result_set_error:
530 * @simple: a #GSimpleAsyncResult.
531 * @domain: a #GQuark (usually #G_IO_ERROR).
532 * @code: an error code.
533 * @format: a formatted error reporting string.
534 * @...: a list of variables to fill in @format.
536 * Sets an error within the asynchronous result without a #GError.
539 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
547 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
548 g_return_if_fail (domain != 0);
549 g_return_if_fail (format != NULL);
551 va_start (args, format);
552 g_simple_async_result_set_error_va (simple, domain, code, format, args);
557 * g_simple_async_result_complete:
558 * @simple: a #GSimpleAsyncResult.
560 * Completes an asynchronous I/O job immediately. Must be called in
561 * the thread where the asynchronous result was to be delivered, as it
562 * invokes the callback directly. If you are in a different thread use
563 * g_simple_async_result_complete_in_idle().
566 g_simple_async_result_complete (GSimpleAsyncResult *simple)
568 #ifndef G_DISABLE_CHECKS
569 GSource *current_source;
570 GMainContext *current_context;
573 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
575 #ifndef G_DISABLE_CHECKS
576 current_source = g_main_current_source ();
577 if (current_source && !g_source_is_destroyed (current_source))
579 current_context = g_source_get_context (current_source);
580 if (current_context == g_main_context_default ())
581 current_context = NULL;
582 if (simple->context != current_context)
583 g_warning ("g_simple_async_result_complete() called from wrong context!");
587 if (simple->callback)
588 simple->callback (simple->source_object,
589 G_ASYNC_RESULT (simple),
594 complete_in_idle_cb (gpointer data)
596 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
598 g_simple_async_result_complete (simple);
604 * g_simple_async_result_complete_in_idle:
605 * @simple: a #GSimpleAsyncResult.
607 * Completes an asynchronous function in an idle handler in the <link
608 * linkend="g-main-context-push-thread-default">thread-default main
609 * loop</link> of the thread that @simple was initially created in.
611 * Calling this function takes a reference to @simple for as long as
612 * is needed to complete the call.
615 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
619 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
621 g_object_ref (simple);
623 source = g_idle_source_new ();
624 g_source_set_priority (source, G_PRIORITY_DEFAULT);
625 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
627 g_source_attach (source, simple->context);
628 g_source_unref (source);
632 GSimpleAsyncResult *simple;
633 GCancellable *cancellable;
634 GSimpleAsyncThreadFunc func;
639 complete_in_idle_cb_for_thread (gpointer _data)
641 RunInThreadData *data = _data;
642 GSimpleAsyncResult *simple;
644 simple = data->simple;
646 if (simple->handle_cancellation &&
647 g_cancellable_is_cancelled (data->cancellable))
648 g_simple_async_result_set_error (simple,
650 G_IO_ERROR_CANCELLED,
651 "%s", _("Operation was cancelled"));
653 g_simple_async_result_complete (simple);
655 if (data->cancellable)
656 g_object_unref (data->cancellable);
657 g_object_unref (data->simple);
664 run_in_thread (GIOSchedulerJob *job,
668 RunInThreadData *data = _data;
669 GSimpleAsyncResult *simple = data->simple;
672 if (simple->handle_cancellation &&
673 g_cancellable_is_cancelled (c))
674 g_simple_async_result_set_error (simple,
676 G_IO_ERROR_CANCELLED,
677 "%s", _("Operation was cancelled"));
680 simple->source_object,
683 source = g_idle_source_new ();
684 g_source_set_priority (source, G_PRIORITY_DEFAULT);
685 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
687 g_source_attach (source, simple->context);
688 g_source_unref (source);
694 * g_simple_async_result_run_in_thread:
695 * @simple: a #GSimpleAsyncResult.
696 * @func: a #GSimpleAsyncThreadFunc.
697 * @io_priority: the io priority of the request.
698 * @cancellable: optional #GCancellable object, %NULL to ignore.
700 * Runs the asynchronous job in a separate thread and then calls
701 * g_simple_async_result_complete_in_idle() on @simple to return
702 * the result to the appropriate main loop.
704 * Calling this function takes a reference to @simple for as long as
705 * is needed to run the job and report its completion.
708 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
709 GSimpleAsyncThreadFunc func,
711 GCancellable *cancellable)
713 RunInThreadData *data;
715 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
716 g_return_if_fail (func != NULL);
718 data = g_new (RunInThreadData, 1);
720 data->simple = g_object_ref (simple);
721 data->cancellable = cancellable;
723 g_object_ref (cancellable);
724 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
728 * g_simple_async_result_is_valid:
729 * @result: the #GAsyncResult passed to the _finish function.
730 * @source: the #GObject passed to the _finish function.
731 * @source_tag: the asynchronous function.
733 * Ensures that the data passed to the _finish function of an async
734 * operation is consistent. Three checks are performed.
736 * First, @result is checked to ensure that it is really a
737 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
738 * matches the source object of @result. Third, @source_tag is
739 * checked to ensure that it is equal to the source_tag argument given
740 * to g_simple_async_result_new() (which, by convention, is a pointer
741 * to the _async function corresponding to the _finish function from
742 * which this function is called).
744 * Returns: #TRUE if all checks passed or #FALSE if any failed.
747 g_simple_async_result_is_valid (GAsyncResult *result,
751 GSimpleAsyncResult *simple;
754 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
756 simple = (GSimpleAsyncResult *)result;
758 cmp_source = g_async_result_get_source_object (result);
759 if (cmp_source != source)
761 g_object_unref (cmp_source);
764 g_object_unref (cmp_source);
766 return source_tag == g_simple_async_result_get_source_tag (simple);
770 * g_simple_async_report_error_in_idle:
771 * @object: a #GObject.
772 * @callback: a #GAsyncReadyCallback.
773 * @user_data: user data passed to @callback.
774 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
775 * @code: a specific error code.
776 * @format: a formatted error reporting string.
777 * @...: a list of variables to fill in @format.
779 * Reports an error in an asynchronous function in an idle function by
780 * directly setting the contents of the #GAsyncResult with the given error
784 g_simple_async_report_error_in_idle (GObject *object,
785 GAsyncReadyCallback callback,
792 GSimpleAsyncResult *simple;
795 g_return_if_fail (G_IS_OBJECT (object));
796 g_return_if_fail (domain != 0);
797 g_return_if_fail (format != NULL);
799 simple = g_simple_async_result_new (object,
803 va_start (args, format);
804 g_simple_async_result_set_error_va (simple, domain, code, format, args);
806 g_simple_async_result_complete_in_idle (simple);
807 g_object_unref (simple);
811 * g_simple_async_report_gerror_in_idle:
812 * @object: a #GObject.
813 * @callback: a #GAsyncReadyCallback.
814 * @user_data: user data passed to @callback.
815 * @error: the #GError to report
817 * Reports an error in an idle function. Similar to
818 * g_simple_async_report_error_in_idle(), but takes a #GError rather
819 * than building a new one.
822 g_simple_async_report_gerror_in_idle (GObject *object,
823 GAsyncReadyCallback callback,
827 GSimpleAsyncResult *simple;
829 g_return_if_fail (G_IS_OBJECT (object));
830 g_return_if_fail (error != NULL);
832 simple = g_simple_async_result_new_from_error (object,
836 g_simple_async_result_complete_in_idle (simple);
837 g_object_unref (simple);
840 #define __G_SIMPLE_ASYNC_RESULT_C__
841 #include "gioaliasdef.c"