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 within
93 * the main event loop. g_simple_async_result_complete_in_idle() will
94 * integrate the I/O task into the main event loop as an idle function and
95 * g_simple_async_result_run_in_thread() will run the job in a separate
98 * To set the results of an asynchronous function,
99 * g_simple_async_result_set_op_res_gpointer(),
100 * g_simple_async_result_set_op_res_gboolean(), and
101 * g_simple_async_result_set_op_res_gssize()
102 * are provided, setting the operation's result to a gpointer, gboolean, or
103 * gssize, respectively.
105 * Likewise, to get the result of an asynchronous function,
106 * g_simple_async_result_get_op_res_gpointer(),
107 * g_simple_async_result_get_op_res_gboolean(), and
108 * g_simple_async_result_get_op_res_gssize() are
109 * provided, getting the operation's result as a gpointer, gboolean, and
110 * gssize, respectively.
113 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
115 struct _GSimpleAsyncResult
117 GObject parent_instance;
119 GObject *source_object;
120 GAsyncReadyCallback callback;
124 gboolean handle_cancellation;
134 GDestroyNotify destroy_op_res;
137 struct _GSimpleAsyncResultClass
139 GObjectClass parent_class;
143 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
144 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
145 g_simple_async_result_async_result_iface_init))
148 g_simple_async_result_finalize (GObject *object)
150 GSimpleAsyncResult *simple;
152 simple = G_SIMPLE_ASYNC_RESULT (object);
154 if (simple->source_object)
155 g_object_unref (simple->source_object);
157 if (simple->destroy_op_res)
158 simple->destroy_op_res (simple->op_res.v_pointer);
161 g_error_free (simple->error);
163 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
167 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
171 gobject_class->finalize = g_simple_async_result_finalize;
175 g_simple_async_result_init (GSimpleAsyncResult *simple)
177 simple->handle_cancellation = TRUE;
181 * g_simple_async_result_new:
182 * @source_object: a #GObject the asynchronous function was called with.
183 * @callback: a #GAsyncReadyCallback.
184 * @user_data: user data passed to @callback.
185 * @source_tag: the asynchronous function.
187 * Creates a #GSimpleAsyncResult.
189 * Returns: a #GSimpleAsyncResult.
192 g_simple_async_result_new (GObject *source_object,
193 GAsyncReadyCallback callback,
197 GSimpleAsyncResult *simple;
199 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
201 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
202 simple->callback = callback;
203 simple->source_object = g_object_ref (source_object);
204 simple->user_data = user_data;
205 simple->source_tag = source_tag;
211 * g_simple_async_result_new_from_error:
212 * @source_object: a #GObject.
213 * @callback: a #GAsyncReadyCallback.
214 * @user_data: user data passed to @callback.
215 * @error: a #GError location.
217 * Creates a #GSimpleAsyncResult from an error condition.
219 * Returns: a #GSimpleAsyncResult.
222 g_simple_async_result_new_from_error (GObject *source_object,
223 GAsyncReadyCallback callback,
227 GSimpleAsyncResult *simple;
229 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
231 simple = g_simple_async_result_new (source_object,
234 g_simple_async_result_set_from_error (simple, error);
240 * g_simple_async_result_new_error:
241 * @source_object: a #GObject.
242 * @callback: a #GAsyncReadyCallback.
243 * @user_data: user data passed to @callback.
244 * @domain: a #GQuark.
245 * @code: an error code.
246 * @format: a string with format characters.
247 * @...: a list of values to insert into @format.
249 * Creates a new #GSimpleAsyncResult with a set error.
251 * Returns: a #GSimpleAsyncResult.
254 g_simple_async_result_new_error (GObject *source_object,
255 GAsyncReadyCallback callback,
262 GSimpleAsyncResult *simple;
265 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
266 g_return_val_if_fail (domain != 0, NULL);
267 g_return_val_if_fail (format != NULL, NULL);
269 simple = g_simple_async_result_new (source_object,
273 va_start (args, format);
274 g_simple_async_result_set_error_va (simple, domain, code, format, args);
282 g_simple_async_result_get_user_data (GAsyncResult *res)
284 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
288 g_simple_async_result_get_source_object (GAsyncResult *res)
290 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
291 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
296 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
298 iface->get_user_data = g_simple_async_result_get_user_data;
299 iface->get_source_object = g_simple_async_result_get_source_object;
303 * g_simple_async_result_set_handle_cancellation:
304 * @simple: a #GSimpleAsyncResult.
305 * @handle_cancellation: a #gboolean.
307 * Sets whether to handle cancellation within the asynchronous operation.
311 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
312 gboolean handle_cancellation)
314 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
315 simple->handle_cancellation = handle_cancellation;
319 * g_simple_async_result_get_source_tag:
320 * @simple: a #GSimpleAsyncResult.
322 * Gets the source tag for the #GSimpleAsyncResult.
324 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
327 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
329 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
330 return simple->source_tag;
334 * g_simple_async_result_propagate_error:
335 * @simple: a #GSimpleAsyncResult.
336 * @dest: a location to propegate the error to.
338 * Propagates an error from within the simple asynchronous result to
339 * a given destination.
341 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
344 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
347 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
351 g_propagate_error (dest, simple->error);
352 simple->error = NULL;
360 * g_simple_async_result_set_op_res_gpointer:
361 * @simple: a #GSimpleAsyncResult.
362 * @op_res: a pointer result from an asynchronous function.
363 * @destroy_op_res: a #GDestroyNotify function.
365 * Sets the operation result within the asynchronous result to a pointer.
368 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
370 GDestroyNotify destroy_op_res)
372 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
374 simple->op_res.v_pointer = op_res;
375 simple->destroy_op_res = destroy_op_res;
379 * g_simple_async_result_get_op_res_gpointer:
380 * @simple: a #GSimpleAsyncResult.
382 * Gets a pointer result as returned by the asynchronous function.
384 * Returns: a pointer from the result.
387 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
389 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
390 return simple->op_res.v_pointer;
394 * g_simple_async_result_set_op_res_gssize:
395 * @simple: a #GSimpleAsyncResult.
396 * @op_res: a #gssize.
398 * Sets the operation result within the asynchronous result to
402 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
405 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
406 simple->op_res.v_ssize = op_res;
410 * g_simple_async_result_get_op_res_gssize:
411 * @simple: a #GSimpleAsyncResult.
413 * Gets a gssize from the asynchronous result.
415 * Returns: a gssize returned from the asynchronous function.
418 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
420 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
421 return simple->op_res.v_ssize;
425 * g_simple_async_result_set_op_res_gboolean:
426 * @simple: a #GSimpleAsyncResult.
427 * @op_res: a #gboolean.
429 * Sets the operation result to a boolean within the asynchronous result.
432 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
435 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
436 simple->op_res.v_boolean = !!op_res;
440 * g_simple_async_result_get_op_res_gboolean:
441 * @simple: a #GSimpleAsyncResult.
443 * Gets the operation result boolean from within the asynchronous result.
445 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
446 * if the operation's result was %FALSE.
449 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
451 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
452 return simple->op_res.v_boolean;
456 * g_simple_async_result_set_from_error:
457 * @simple: a #GSimpleAsyncResult.
460 * Sets the result from a #GError.
463 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
466 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
467 g_return_if_fail (error != NULL);
470 g_error_free (simple->error);
471 simple->error = g_error_copy (error);
472 simple->failed = TRUE;
476 _g_error_new_valist (GQuark domain,
484 message = g_strdup_vprintf (format, args);
486 error = g_error_new_literal (domain, code, message);
493 * g_simple_async_result_set_error_va:
494 * @simple: a #GSimpleAsyncResult.
495 * @domain: a #GQuark (usually #G_IO_ERROR).
496 * @code: an error code.
497 * @format: a formatted error reporting string.
498 * @args: va_list of arguments.
500 * Sets an error within the asynchronous result without a #GError.
501 * Unless writing a binding, see g_simple_async_result_set_error().
504 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
510 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
511 g_return_if_fail (domain != 0);
512 g_return_if_fail (format != NULL);
515 g_error_free (simple->error);
516 simple->error = _g_error_new_valist (domain, code, format, args);
517 simple->failed = TRUE;
521 * g_simple_async_result_set_error:
522 * @simple: a #GSimpleAsyncResult.
523 * @domain: a #GQuark (usually #G_IO_ERROR).
524 * @code: an error code.
525 * @format: a formatted error reporting string.
526 * @...: a list of variables to fill in @format.
528 * Sets an error within the asynchronous result without a #GError.
531 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
539 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
540 g_return_if_fail (domain != 0);
541 g_return_if_fail (format != NULL);
543 va_start (args, format);
544 g_simple_async_result_set_error_va (simple, domain, code, format, args);
549 * g_simple_async_result_complete:
550 * @simple: a #GSimpleAsyncResult.
552 * Completes an asynchronous I/O job.
553 * Must be called in the main thread, as it invokes the callback that
554 * should be called in the main thread. If you are in a different thread
555 * use g_simple_async_result_complete_in_idle().
558 g_simple_async_result_complete (GSimpleAsyncResult *simple)
560 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
562 if (simple->callback)
563 simple->callback (simple->source_object,
564 G_ASYNC_RESULT (simple),
569 complete_in_idle_cb (gpointer data)
571 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
573 g_simple_async_result_complete (simple);
579 * g_simple_async_result_complete_in_idle:
580 * @simple: a #GSimpleAsyncResult.
582 * Completes an asynchronous function in the main event loop using
586 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
591 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
593 g_object_ref (simple);
595 source = g_idle_source_new ();
596 g_source_set_priority (source, G_PRIORITY_DEFAULT);
597 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
599 id = g_source_attach (source, NULL);
600 g_source_unref (source);
604 GSimpleAsyncResult *simple;
605 GCancellable *cancellable;
606 GSimpleAsyncThreadFunc func;
611 complete_in_idle_cb_for_thread (gpointer _data)
613 RunInThreadData *data = _data;
614 GSimpleAsyncResult *simple;
616 simple = data->simple;
618 if (simple->handle_cancellation &&
619 g_cancellable_is_cancelled (data->cancellable))
620 g_simple_async_result_set_error (simple,
622 G_IO_ERROR_CANCELLED,
623 "%s", _("Operation was cancelled"));
625 g_simple_async_result_complete (simple);
627 if (data->cancellable)
628 g_object_unref (data->cancellable);
629 g_object_unref (data->simple);
636 run_in_thread (GIOSchedulerJob *job,
640 RunInThreadData *data = _data;
641 GSimpleAsyncResult *simple = data->simple;
645 if (simple->handle_cancellation &&
646 g_cancellable_is_cancelled (c))
647 g_simple_async_result_set_error (simple,
649 G_IO_ERROR_CANCELLED,
650 "%s", _("Operation was cancelled"));
653 simple->source_object,
656 source = g_idle_source_new ();
657 g_source_set_priority (source, G_PRIORITY_DEFAULT);
658 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
660 id = g_source_attach (source, NULL);
661 g_source_unref (source);
667 * g_simple_async_result_run_in_thread:
668 * @simple: a #GSimpleAsyncResult.
669 * @func: a #GSimpleAsyncThreadFunc.
670 * @io_priority: the io priority of the request.
671 * @cancellable: optional #GCancellable object, %NULL to ignore.
673 * Runs the asynchronous job in a separated thread.
676 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
677 GSimpleAsyncThreadFunc func,
679 GCancellable *cancellable)
681 RunInThreadData *data;
683 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
684 g_return_if_fail (func != NULL);
686 data = g_new (RunInThreadData, 1);
688 data->simple = g_object_ref (simple);
689 data->cancellable = cancellable;
691 g_object_ref (cancellable);
692 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
696 * g_simple_async_result_is_valid:
697 * @result: the #GAsyncResult passed to the _finish function.
698 * @source: the #GObject passed to the _finish function.
699 * @source_tag: the asynchronous function.
701 * Ensures that the data passed to the _finish function of an async
702 * operation is consistent. Three checks are performed.
704 * First, @result is checked to ensure that it is really a
705 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
706 * matches the source object of @result. Third, @source_tag is
707 * checked to ensure that it is equal to the source_tag argument given
708 * to g_simple_async_result_new() (which, by convention, is a pointer
709 * to the _async function corresponding to the _finish function from
710 * which this function is called).
712 * Returns: #TRUE if all checks passed or #FALSE if any failed.
715 g_simple_async_result_is_valid (GAsyncResult *result,
719 GSimpleAsyncResult *simple;
722 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
724 simple = (GSimpleAsyncResult *)result;
726 cmp_source = g_async_result_get_source_object (result);
727 if (cmp_source != source)
729 g_object_unref (cmp_source);
732 g_object_unref (cmp_source);
734 return source_tag == g_simple_async_result_get_source_tag (simple);
738 * g_simple_async_report_error_in_idle:
739 * @object: a #GObject.
740 * @callback: a #GAsyncReadyCallback.
741 * @user_data: user data passed to @callback.
742 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
743 * @code: a specific error code.
744 * @format: a formatted error reporting string.
745 * @...: a list of variables to fill in @format.
747 * Reports an error in an asynchronous function in an idle function by
748 * directly setting the contents of the #GAsyncResult with the given error
752 g_simple_async_report_error_in_idle (GObject *object,
753 GAsyncReadyCallback callback,
760 GSimpleAsyncResult *simple;
763 g_return_if_fail (G_IS_OBJECT (object));
764 g_return_if_fail (domain != 0);
765 g_return_if_fail (format != NULL);
767 simple = g_simple_async_result_new (object,
771 va_start (args, format);
772 g_simple_async_result_set_error_va (simple, domain, code, format, args);
774 g_simple_async_result_complete_in_idle (simple);
775 g_object_unref (simple);
779 * g_simple_async_report_gerror_in_idle:
780 * @object: a #GObject.
781 * @callback: a #GAsyncReadyCallback.
782 * @user_data: user data passed to @callback.
783 * @error: the #GError to report
785 * Reports an error in an idle function. Similar to
786 * g_simple_async_report_error_in_idle(), but takes a #GError rather
787 * than building a new one.
790 g_simple_async_report_gerror_in_idle (GObject *object,
791 GAsyncReadyCallback callback,
795 GSimpleAsyncResult *simple;
797 g_return_if_fail (G_IS_OBJECT (object));
798 g_return_if_fail (error != NULL);
800 simple = g_simple_async_result_new_from_error (object,
804 g_simple_async_result_complete_in_idle (simple);
805 g_object_unref (simple);
808 #define __G_SIMPLE_ASYNC_RESULT_C__
809 #include "gioaliasdef.c"