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.
88 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
89 * or it can use #GThread<!-- -->s if available.
90 * g_simple_async_result_complete() will finish an I/O task directly within
91 * the main event loop. g_simple_async_result_complete_in_idle() will
92 * integrate the I/O task into the main event loop as an idle function and
93 * g_simple_async_result_run_in_thread() will run the job in a separate
96 * To set the results of an asynchronous function,
97 * g_simple_async_result_set_op_res_gpointer(),
98 * g_simple_async_result_set_op_res_gboolean(), and
99 * g_simple_async_result_set_op_res_gssize()
100 * are provided, setting the operation's result to a gpointer, gboolean, or
101 * gssize, respectively.
103 * Likewise, to get the result of an asynchronous function,
104 * g_simple_async_result_get_op_res_gpointer(),
105 * g_simple_async_result_get_op_res_gboolean(), and
106 * g_simple_async_result_get_op_res_gssize() are
107 * provided, getting the operation's result as a gpointer, gboolean, and
108 * gssize, respectively.
111 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
113 struct _GSimpleAsyncResult
115 GObject parent_instance;
117 GObject *source_object;
118 GAsyncReadyCallback callback;
122 gboolean handle_cancellation;
132 GDestroyNotify destroy_op_res;
135 struct _GSimpleAsyncResultClass
137 GObjectClass parent_class;
141 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
142 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
143 g_simple_async_result_async_result_iface_init))
146 g_simple_async_result_finalize (GObject *object)
148 GSimpleAsyncResult *simple;
150 simple = G_SIMPLE_ASYNC_RESULT (object);
152 if (simple->source_object)
153 g_object_unref (simple->source_object);
155 if (simple->destroy_op_res)
156 simple->destroy_op_res (simple->op_res.v_pointer);
159 g_error_free (simple->error);
161 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
165 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 gobject_class->finalize = g_simple_async_result_finalize;
173 g_simple_async_result_init (GSimpleAsyncResult *simple)
175 simple->handle_cancellation = TRUE;
179 * g_simple_async_result_new:
180 * @source_object: a #GObject the asynchronous function was called with.
181 * @callback: a #GAsyncReadyCallback.
182 * @user_data: user data passed to @callback.
183 * @source_tag: the asynchronous function.
185 * Creates a #GSimpleAsyncResult.
187 * Returns: a #GSimpleAsyncResult.
190 g_simple_async_result_new (GObject *source_object,
191 GAsyncReadyCallback callback,
195 GSimpleAsyncResult *simple;
197 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
199 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
200 simple->callback = callback;
201 simple->source_object = g_object_ref (source_object);
202 simple->user_data = user_data;
203 simple->source_tag = source_tag;
209 * g_simple_async_result_new_from_error:
210 * @source_object: a #GObject.
211 * @callback: a #GAsyncReadyCallback.
212 * @user_data: user data passed to @callback.
213 * @error: a #GError location.
215 * Creates a #GSimpleAsyncResult from an error condition.
217 * Returns: a #GSimpleAsyncResult.
220 g_simple_async_result_new_from_error (GObject *source_object,
221 GAsyncReadyCallback callback,
225 GSimpleAsyncResult *simple;
227 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
229 simple = g_simple_async_result_new (source_object,
232 g_simple_async_result_set_from_error (simple, error);
238 * g_simple_async_result_new_error:
239 * @source_object: a #GObject.
240 * @callback: a #GAsyncReadyCallback.
241 * @user_data: user data passed to @callback.
242 * @domain: a #GQuark.
243 * @code: an error code.
244 * @format: a string with format characters.
245 * @...: a list of values to insert into @format.
247 * Creates a new #GSimpleAsyncResult with a set error.
249 * Returns: a #GSimpleAsyncResult.
252 g_simple_async_result_new_error (GObject *source_object,
253 GAsyncReadyCallback callback,
260 GSimpleAsyncResult *simple;
263 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
264 g_return_val_if_fail (domain != 0, NULL);
265 g_return_val_if_fail (format != NULL, NULL);
267 simple = g_simple_async_result_new (source_object,
271 va_start (args, format);
272 g_simple_async_result_set_error_va (simple, domain, code, format, args);
280 g_simple_async_result_get_user_data (GAsyncResult *res)
282 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
286 g_simple_async_result_get_source_object (GAsyncResult *res)
288 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
289 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
294 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
296 iface->get_user_data = g_simple_async_result_get_user_data;
297 iface->get_source_object = g_simple_async_result_get_source_object;
301 * g_simple_async_result_set_handle_cancellation:
302 * @simple: a #GSimpleAsyncResult.
303 * @handle_cancellation: a #gboolean.
305 * Sets whether to handle cancellation within the asynchronous operation.
309 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
310 gboolean handle_cancellation)
312 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
313 simple->handle_cancellation = handle_cancellation;
317 * g_simple_async_result_get_source_tag:
318 * @simple: a #GSimpleAsyncResult.
320 * Gets the source tag for the #GSimpleAsyncResult.
322 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
325 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
327 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
328 return simple->source_tag;
332 * g_simple_async_result_propagate_error:
333 * @simple: a #GSimpleAsyncResult.
334 * @dest: a location to propegate the error to.
336 * Propagates an error from within the simple asynchronous result to
337 * a given destination.
339 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
342 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
345 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
349 g_propagate_error (dest, simple->error);
350 simple->error = NULL;
358 * g_simple_async_result_set_op_res_gpointer:
359 * @simple: a #GSimpleAsyncResult.
360 * @op_res: a pointer result from an asynchronous function.
361 * @destroy_op_res: a #GDestroyNotify function.
363 * Sets the operation result within the asynchronous result to a pointer.
366 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
368 GDestroyNotify destroy_op_res)
370 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
372 simple->op_res.v_pointer = op_res;
373 simple->destroy_op_res = destroy_op_res;
377 * g_simple_async_result_get_op_res_gpointer:
378 * @simple: a #GSimpleAsyncResult.
380 * Gets a pointer result as returned by the asynchronous function.
382 * Returns: a pointer from the result.
385 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
387 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
388 return simple->op_res.v_pointer;
392 * g_simple_async_result_set_op_res_gssize:
393 * @simple: a #GSimpleAsyncResult.
394 * @op_res: a #gssize.
396 * Sets the operation result within the asynchronous result to
400 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
403 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
404 simple->op_res.v_ssize = op_res;
408 * g_simple_async_result_get_op_res_gssize:
409 * @simple: a #GSimpleAsyncResult.
411 * Gets a gssize from the asynchronous result.
413 * Returns: a gssize returned from the asynchronous function.
416 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
418 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
419 return simple->op_res.v_ssize;
423 * g_simple_async_result_set_op_res_gboolean:
424 * @simple: a #GSimpleAsyncResult.
425 * @op_res: a #gboolean.
427 * Sets the operation result to a boolean within the asynchronous result.
430 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
433 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
434 simple->op_res.v_boolean = !!op_res;
438 * g_simple_async_result_get_op_res_gboolean:
439 * @simple: a #GSimpleAsyncResult.
441 * Gets the operation result boolean from within the asynchronous result.
443 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
444 * if the operation's result was %FALSE.
447 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
449 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
450 return simple->op_res.v_boolean;
454 * g_simple_async_result_set_from_error:
455 * @simple: a #GSimpleAsyncResult.
458 * Sets the result from a #GError.
461 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
464 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
465 g_return_if_fail (error != NULL);
467 simple->error = g_error_copy (error);
468 simple->failed = TRUE;
472 _g_error_new_valist (GQuark domain,
480 message = g_strdup_vprintf (format, args);
482 error = g_error_new_literal (domain, code, message);
489 * g_simple_async_result_set_error_va:
490 * @simple: a #GSimpleAsyncResult.
491 * @domain: a #GQuark (usually #G_IO_ERROR).
492 * @code: an error code.
493 * @format: a formatted error reporting string.
494 * @args: va_list of arguments.
496 * Sets an error within the asynchronous result without a #GError.
497 * Unless writing a binding, see g_simple_async_result_set_error().
500 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
506 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
507 g_return_if_fail (domain != 0);
508 g_return_if_fail (format != NULL);
510 simple->error = _g_error_new_valist (domain, code, format, args);
511 simple->failed = TRUE;
515 * g_simple_async_result_set_error:
516 * @simple: a #GSimpleAsyncResult.
517 * @domain: a #GQuark (usually #G_IO_ERROR).
518 * @code: an error code.
519 * @format: a formatted error reporting string.
520 * @...: a list of variables to fill in @format.
522 * Sets an error within the asynchronous result without a #GError.
525 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
533 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
534 g_return_if_fail (domain != 0);
535 g_return_if_fail (format != NULL);
537 va_start (args, format);
538 g_simple_async_result_set_error_va (simple, domain, code, format, args);
543 * g_simple_async_result_complete:
544 * @simple: a #GSimpleAsyncResult.
546 * Completes an asynchronous I/O job.
549 g_simple_async_result_complete (GSimpleAsyncResult *simple)
551 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
553 if (simple->callback)
554 simple->callback (simple->source_object,
555 G_ASYNC_RESULT (simple),
560 complete_in_idle_cb (gpointer data)
562 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
564 g_simple_async_result_complete (simple);
570 * g_simple_async_result_complete_in_idle:
571 * @simple: a #GSimpleAsyncResult.
573 * Completes an asynchronous function in the main event loop using
577 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
582 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
584 g_object_ref (simple);
586 source = g_idle_source_new ();
587 g_source_set_priority (source, G_PRIORITY_DEFAULT);
588 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
590 id = g_source_attach (source, NULL);
591 g_source_unref (source);
595 GSimpleAsyncResult *simple;
596 GSimpleAsyncThreadFunc func;
600 run_in_thread (GIOSchedulerJob *job,
604 RunInThreadData *data = _data;
605 GSimpleAsyncResult *simple = data->simple;
607 if (simple->handle_cancellation &&
608 g_cancellable_is_cancelled (c))
609 g_simple_async_result_set_error (simple,
611 G_IO_ERROR_CANCELLED,
612 _("Operation was cancelled"));
615 simple->source_object,
618 g_simple_async_result_complete_in_idle (data->simple);
619 g_object_unref (data->simple);
626 * g_simple_async_result_run_in_thread:
627 * @simple: a #GSimpleAsyncResult.
628 * @func: a #GSimpleAsyncThreadFunc.
629 * @io_priority: the io priority of the request.
630 * @cancellable: optional #GCancellable object, %NULL to ignore.
632 * Runs the asynchronous job in a separated thread.
635 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
636 GSimpleAsyncThreadFunc func,
638 GCancellable *cancellable)
640 RunInThreadData *data;
642 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
643 g_return_if_fail (func != NULL);
645 data = g_new (RunInThreadData, 1);
647 data->simple = g_object_ref (simple);
648 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
652 * g_simple_async_report_error_in_idle:
653 * @object: a #GObject.
654 * @callback: a #GAsyncReadyCallback.
655 * @user_data: user data passed to @callback.
656 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
657 * @code: a specific error code.
658 * @format: a formatted error reporting string.
659 * @...: a list of variables to fill in @format.
661 * Reports an error in an asynchronous function in an idle function by
662 * directly setting the contents of the #GAsyncResult with the given error
666 g_simple_async_report_error_in_idle (GObject *object,
667 GAsyncReadyCallback callback,
674 GSimpleAsyncResult *simple;
677 g_return_if_fail (G_IS_OBJECT (object));
678 g_return_if_fail (domain != 0);
679 g_return_if_fail (format != NULL);
681 simple = g_simple_async_result_new (object,
685 va_start (args, format);
686 g_simple_async_result_set_error_va (simple, domain, code, format, args);
688 g_simple_async_result_complete_in_idle (simple);
689 g_object_unref (simple);
693 * g_simple_async_report_gerror_in_idle:
694 * @object: a #GObject.
695 * @callback: a #GAsyncReadyCallback.
696 * @user_data: user data passed to @callback.
697 * @error: the #GError to report
699 * Reports an error in an idle function. Similar to
700 * g_simple_async_report_error_in_idle(), but takes a #GError rather
701 * than building a new one.
704 g_simple_async_report_gerror_in_idle (GObject *object,
705 GAsyncReadyCallback callback,
709 GSimpleAsyncResult *simple;
711 g_return_if_fail (G_IS_OBJECT (object));
712 g_return_if_fail (error != NULL);
714 simple = g_simple_async_result_new_from_error (object,
718 g_simple_async_result_complete_in_idle (simple);
719 g_object_unref (simple);
722 #define __G_SIMPLE_ASYNC_RESULT_C__
723 #include "gioaliasdef.c"