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 propegated 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 propegated 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 ();
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.
612 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
616 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
618 g_object_ref (simple);
620 source = g_idle_source_new ();
621 g_source_set_priority (source, G_PRIORITY_DEFAULT);
622 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
624 g_source_attach (source, simple->context);
625 g_source_unref (source);
629 GSimpleAsyncResult *simple;
630 GCancellable *cancellable;
631 GSimpleAsyncThreadFunc func;
636 complete_in_idle_cb_for_thread (gpointer _data)
638 RunInThreadData *data = _data;
639 GSimpleAsyncResult *simple;
641 simple = data->simple;
643 if (simple->handle_cancellation &&
644 g_cancellable_is_cancelled (data->cancellable))
645 g_simple_async_result_set_error (simple,
647 G_IO_ERROR_CANCELLED,
648 "%s", _("Operation was cancelled"));
650 g_simple_async_result_complete (simple);
652 if (data->cancellable)
653 g_object_unref (data->cancellable);
654 g_object_unref (data->simple);
661 run_in_thread (GIOSchedulerJob *job,
665 RunInThreadData *data = _data;
666 GSimpleAsyncResult *simple = data->simple;
669 if (simple->handle_cancellation &&
670 g_cancellable_is_cancelled (c))
671 g_simple_async_result_set_error (simple,
673 G_IO_ERROR_CANCELLED,
674 "%s", _("Operation was cancelled"));
677 simple->source_object,
680 source = g_idle_source_new ();
681 g_source_set_priority (source, G_PRIORITY_DEFAULT);
682 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
684 g_source_attach (source, simple->context);
685 g_source_unref (source);
691 * g_simple_async_result_run_in_thread:
692 * @simple: a #GSimpleAsyncResult.
693 * @func: a #GSimpleAsyncThreadFunc.
694 * @io_priority: the io priority of the request.
695 * @cancellable: optional #GCancellable object, %NULL to ignore.
697 * Runs the asynchronous job in a separate thread and then calls
698 * g_simple_async_result_complete_in_idle() on @simple to return
699 * the result to the appropriate main loop.
702 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
703 GSimpleAsyncThreadFunc func,
705 GCancellable *cancellable)
707 RunInThreadData *data;
709 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
710 g_return_if_fail (func != NULL);
712 data = g_new (RunInThreadData, 1);
714 data->simple = g_object_ref (simple);
715 data->cancellable = cancellable;
717 g_object_ref (cancellable);
718 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
722 * g_simple_async_result_is_valid:
723 * @result: the #GAsyncResult passed to the _finish function.
724 * @source: the #GObject passed to the _finish function.
725 * @source_tag: the asynchronous function.
727 * Ensures that the data passed to the _finish function of an async
728 * operation is consistent. Three checks are performed.
730 * First, @result is checked to ensure that it is really a
731 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
732 * matches the source object of @result. Third, @source_tag is
733 * checked to ensure that it is equal to the source_tag argument given
734 * to g_simple_async_result_new() (which, by convention, is a pointer
735 * to the _async function corresponding to the _finish function from
736 * which this function is called).
738 * Returns: #TRUE if all checks passed or #FALSE if any failed.
741 g_simple_async_result_is_valid (GAsyncResult *result,
745 GSimpleAsyncResult *simple;
748 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
750 simple = (GSimpleAsyncResult *)result;
752 cmp_source = g_async_result_get_source_object (result);
753 if (cmp_source != source)
755 g_object_unref (cmp_source);
758 g_object_unref (cmp_source);
760 return source_tag == g_simple_async_result_get_source_tag (simple);
764 * g_simple_async_report_error_in_idle:
765 * @object: a #GObject.
766 * @callback: a #GAsyncReadyCallback.
767 * @user_data: user data passed to @callback.
768 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
769 * @code: a specific error code.
770 * @format: a formatted error reporting string.
771 * @...: a list of variables to fill in @format.
773 * Reports an error in an asynchronous function in an idle function by
774 * directly setting the contents of the #GAsyncResult with the given error
778 g_simple_async_report_error_in_idle (GObject *object,
779 GAsyncReadyCallback callback,
786 GSimpleAsyncResult *simple;
789 g_return_if_fail (G_IS_OBJECT (object));
790 g_return_if_fail (domain != 0);
791 g_return_if_fail (format != NULL);
793 simple = g_simple_async_result_new (object,
797 va_start (args, format);
798 g_simple_async_result_set_error_va (simple, domain, code, format, args);
800 g_simple_async_result_complete_in_idle (simple);
801 g_object_unref (simple);
805 * g_simple_async_report_gerror_in_idle:
806 * @object: a #GObject.
807 * @callback: a #GAsyncReadyCallback.
808 * @user_data: user data passed to @callback.
809 * @error: the #GError to report
811 * Reports an error in an idle function. Similar to
812 * g_simple_async_report_error_in_idle(), but takes a #GError rather
813 * than building a new one.
816 g_simple_async_report_gerror_in_idle (GObject *object,
817 GAsyncReadyCallback callback,
821 GSimpleAsyncResult *simple;
823 g_return_if_fail (G_IS_OBJECT (object));
824 g_return_if_fail (error != NULL);
826 simple = g_simple_async_result_new_from_error (object,
830 g_simple_async_result_complete_in_idle (simple);
831 g_object_unref (simple);
834 #define __G_SIMPLE_ASYNC_RESULT_C__
835 #include "gioaliasdef.c"