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().
565 g_simple_async_result_complete (GSimpleAsyncResult *simple)
567 #ifndef G_DISABLE_CHECKS
568 GSource *current_source;
569 GMainContext *current_context;
572 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
574 #ifndef G_DISABLE_CHECKS
575 current_source = g_main_current_source ();
576 if (current_source && !g_source_is_destroyed (current_source))
578 current_context = g_source_get_context (current_source);
579 if (current_context == g_main_context_default ())
580 current_context = NULL;
581 if (simple->context != current_context)
582 g_warning ("g_simple_async_result_complete() called from wrong context!");
586 if (simple->callback)
587 simple->callback (simple->source_object,
588 G_ASYNC_RESULT (simple),
593 complete_in_idle_cb (gpointer data)
595 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
597 g_simple_async_result_complete (simple);
603 * g_simple_async_result_complete_in_idle:
604 * @simple: a #GSimpleAsyncResult.
606 * Completes an asynchronous function in an idle handler in the <link
607 * linkend="g-main-context-push-thread-default">thread-default main
608 * loop</link> of the thread that @simple was initially created in.
610 * Calling this function takes a reference to @simple for as long as
611 * is needed to complete the call.
614 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
618 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
620 g_object_ref (simple);
622 source = g_idle_source_new ();
623 g_source_set_priority (source, G_PRIORITY_DEFAULT);
624 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
626 g_source_attach (source, simple->context);
627 g_source_unref (source);
631 GSimpleAsyncResult *simple;
632 GCancellable *cancellable;
633 GSimpleAsyncThreadFunc func;
638 complete_in_idle_cb_for_thread (gpointer _data)
640 RunInThreadData *data = _data;
641 GSimpleAsyncResult *simple;
643 simple = data->simple;
645 if (simple->handle_cancellation &&
646 g_cancellable_is_cancelled (data->cancellable))
647 g_simple_async_result_set_error (simple,
649 G_IO_ERROR_CANCELLED,
650 "%s", _("Operation was cancelled"));
652 g_simple_async_result_complete (simple);
654 if (data->cancellable)
655 g_object_unref (data->cancellable);
656 g_object_unref (data->simple);
663 run_in_thread (GIOSchedulerJob *job,
667 RunInThreadData *data = _data;
668 GSimpleAsyncResult *simple = data->simple;
671 if (simple->handle_cancellation &&
672 g_cancellable_is_cancelled (c))
673 g_simple_async_result_set_error (simple,
675 G_IO_ERROR_CANCELLED,
676 "%s", _("Operation was cancelled"));
679 simple->source_object,
682 source = g_idle_source_new ();
683 g_source_set_priority (source, G_PRIORITY_DEFAULT);
684 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
686 g_source_attach (source, simple->context);
687 g_source_unref (source);
693 * g_simple_async_result_run_in_thread:
694 * @simple: a #GSimpleAsyncResult.
695 * @func: a #GSimpleAsyncThreadFunc.
696 * @io_priority: the io priority of the request.
697 * @cancellable: optional #GCancellable object, %NULL to ignore.
699 * Runs the asynchronous job in a separate thread and then calls
700 * g_simple_async_result_complete_in_idle() on @simple to return
701 * the result to the appropriate main loop.
703 * Calling this function takes a reference to @simple for as long as
704 * is needed to run the job and report its completion.
707 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
708 GSimpleAsyncThreadFunc func,
710 GCancellable *cancellable)
712 RunInThreadData *data;
714 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
715 g_return_if_fail (func != NULL);
717 data = g_new (RunInThreadData, 1);
719 data->simple = g_object_ref (simple);
720 data->cancellable = cancellable;
722 g_object_ref (cancellable);
723 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
727 * g_simple_async_result_is_valid:
728 * @result: the #GAsyncResult passed to the _finish function.
729 * @source: the #GObject passed to the _finish function.
730 * @source_tag: the asynchronous function.
732 * Ensures that the data passed to the _finish function of an async
733 * operation is consistent. Three checks are performed.
735 * First, @result is checked to ensure that it is really a
736 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
737 * matches the source object of @result. Third, @source_tag is
738 * checked to ensure that it is equal to the source_tag argument given
739 * to g_simple_async_result_new() (which, by convention, is a pointer
740 * to the _async function corresponding to the _finish function from
741 * which this function is called).
743 * Returns: #TRUE if all checks passed or #FALSE if any failed.
746 g_simple_async_result_is_valid (GAsyncResult *result,
750 GSimpleAsyncResult *simple;
753 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
755 simple = (GSimpleAsyncResult *)result;
757 cmp_source = g_async_result_get_source_object (result);
758 if (cmp_source != source)
760 if (cmp_source != NULL)
761 g_object_unref (cmp_source);
764 if (cmp_source != NULL)
765 g_object_unref (cmp_source);
767 return source_tag == g_simple_async_result_get_source_tag (simple);
771 * g_simple_async_report_error_in_idle:
772 * @object: a #GObject.
773 * @callback: a #GAsyncReadyCallback.
774 * @user_data: user data passed to @callback.
775 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
776 * @code: a specific error code.
777 * @format: a formatted error reporting string.
778 * @...: a list of variables to fill in @format.
780 * Reports an error in an asynchronous function in an idle function by
781 * directly setting the contents of the #GAsyncResult with the given error
785 g_simple_async_report_error_in_idle (GObject *object,
786 GAsyncReadyCallback callback,
793 GSimpleAsyncResult *simple;
796 g_return_if_fail (G_IS_OBJECT (object));
797 g_return_if_fail (domain != 0);
798 g_return_if_fail (format != NULL);
800 simple = g_simple_async_result_new (object,
804 va_start (args, format);
805 g_simple_async_result_set_error_va (simple, domain, code, format, args);
807 g_simple_async_result_complete_in_idle (simple);
808 g_object_unref (simple);
812 * g_simple_async_report_gerror_in_idle:
813 * @object: a #GObject.
814 * @callback: a #GAsyncReadyCallback.
815 * @user_data: user data passed to @callback.
816 * @error: the #GError to report
818 * Reports an error in an idle function. Similar to
819 * g_simple_async_report_error_in_idle(), but takes a #GError rather
820 * than building a new one.
823 g_simple_async_report_gerror_in_idle (GObject *object,
824 GAsyncReadyCallback callback,
828 GSimpleAsyncResult *simple;
830 g_return_if_fail (G_IS_OBJECT (object));
831 g_return_if_fail (error != NULL);
833 simple = g_simple_async_result_new_from_error (object,
837 g_simple_async_result_complete_in_idle (simple);
838 g_object_unref (simple);