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>
32 #include "gsimpleasyncresult.h"
33 #include "gioscheduler.h"
34 #include <gio/gioerror.h>
40 * SECTION:gsimpleasyncresult
41 * @short_description: Simple asynchronous results implementation
42 * @see_also: #GAsyncResult
44 * Implements #GAsyncResult for simple cases. Most of the time, this
45 * will be all an application needs, and will be used transparently.
46 * Because of this, #GSimpleAsyncResult is used throughout GIO for
47 * handling asynchronous functions.
49 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error reporting,
50 * operation cancellation and the final state of an operation, completely
51 * transparent to the application. Results can be returned as a pointer e.g.
52 * for functions that return data that is collected asynchronously,
53 * a boolean value for checking the success or failure of an operation,
54 * or a #gssize for operations which return the number of bytes modified
55 * by the operation; all of the simple return cases are covered.
57 * Most of the time, an application will not need to know of the details
58 * of this API; it is handled transparently, and any necessary operations are
59 * handled by #GAsyncResult's interface. However, if implementing a new GIO
60 * module, for writing language bindings, or for complex applications that
61 * need better control of how asynchronous operations are completed, it is
62 * important to understand this functionality.
64 * GSimpleAsyncResults are tagged with the calling function to ensure that
65 * asynchronous functions and their finishing functions are used together
68 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
69 * If the result needs to be created for a #GError, use
70 * g_simple_async_result_new_from_error(). If a #GError is not available
71 * (e.g. the asynchronous operation's doesn't take a #GError argument),
72 * but the result still needs to be created for an error condition, use
73 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
74 * if your application or binding requires passing a variable argument list
75 * directly), and the error can then be propegated through the use of
76 * g_simple_async_result_propagate_error().
78 * An asynchronous operation can be made to ignore a cancellation event by
79 * calling g_simple_async_result_set_handle_cancellation() with a
80 * #GSimpleAsyncResult for the operation and %FALSE.
82 * GSimpleAsyncResult can integrate into GLib's Main Event Loop
83 * <!-- TODO: Crosslink -->, or it can use #GThread<!-- -->s if available.
84 * g_simple_async_result_complete() will finish an I/O task directly within
85 * the main event loop. g_simple_async_result_complete_in_idle() will
86 * integrate the I/O task into the main event loop as an idle function and
87 * g_simple_async_result_run_in_thread() will run the job in a separate
90 * To set the results of an asynchronous function,
91 * g_simple_async_result_set_op_res_gpointer(),
92 * g_simple_async_result_set_op_res_gboolean(), and
93 * g_simple_async_result_set_op_res_gssize()
94 * are provided, setting the operation's result to a gpointer, gboolean, or
95 * gssize, respectively.
97 * Likewise, to get the result of an asynchronous function,
98 * g_simple_async_result_get_op_res_gpointer(),
99 * g_simple_async_result_get_op_res_gboolean(), and
100 * g_simple_async_result_get_op_res_gssize() are
101 * provided, getting the operation's result as a gpointer, gboolean, and
102 * gssize, respectively.
105 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
107 struct _GSimpleAsyncResult
109 GObject parent_instance;
111 GObject *source_object;
112 GAsyncReadyCallback callback;
116 gboolean handle_cancellation;
126 GDestroyNotify destroy_op_res;
129 struct _GSimpleAsyncResultClass
131 GObjectClass parent_class;
135 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
136 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
137 g_simple_async_result_async_result_iface_init))
140 g_simple_async_result_finalize (GObject *object)
142 GSimpleAsyncResult *simple;
144 simple = G_SIMPLE_ASYNC_RESULT (object);
146 if (simple->source_object)
147 g_object_unref (simple->source_object);
149 if (simple->destroy_op_res)
150 simple->destroy_op_res (simple->op_res.v_pointer);
153 g_error_free (simple->error);
155 if (G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize)
156 (*G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize) (object);
160 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164 gobject_class->finalize = g_simple_async_result_finalize;
168 g_simple_async_result_init (GSimpleAsyncResult *simple)
170 simple->handle_cancellation = TRUE;
174 * g_simple_async_result_new:
175 * @source_object: a #GObject the asynchronous function was called with.
176 * @callback: a #GAsyncReadyCallback.
177 * @user_data: user data passed to @callback.
178 * @source_tag: the asynchronous function.
180 * Creates a #GSimpleAsyncResult.
182 * Returns: a #GSimpleAsyncResult.
185 g_simple_async_result_new (GObject *source_object,
186 GAsyncReadyCallback callback,
190 GSimpleAsyncResult *simple;
192 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
194 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
195 simple->callback = callback;
196 simple->source_object = g_object_ref (source_object);
197 simple->user_data = user_data;
198 simple->source_tag = source_tag;
204 * g_simple_async_result_new_from_error:
205 * @source_object: a #GObject.
206 * @callback: a #GAsyncReadyCallback.
207 * @user_data: user data passed to @callback.
208 * @error: a #GError location.
210 * Creates a #GSimpleAsyncResult from an error condition.
212 * Returns: a #GSimpleAsyncResult.
215 g_simple_async_result_new_from_error (GObject *source_object,
216 GAsyncReadyCallback callback,
220 GSimpleAsyncResult *simple;
222 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
224 simple = g_simple_async_result_new (source_object,
227 g_simple_async_result_set_from_error (simple, error);
233 * g_simple_async_result_new_error:
234 * @source_object: a #GObject.
235 * @callback: a #GAsyncReadyCallback.
236 * @user_data: user data passed to @callback.
237 * @domain: a #GQuark.
238 * @code: an error code.
239 * @format: a string with format characters.
240 * @...: a list of values to insert into @format.
242 * Creates a new #GSimpleAsyncResult with a set error.
244 * Returns: a #GSimpleAsyncResult.
247 g_simple_async_result_new_error (GObject *source_object,
248 GAsyncReadyCallback callback,
255 GSimpleAsyncResult *simple;
258 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
259 g_return_val_if_fail (domain != 0, NULL);
260 g_return_val_if_fail (format != NULL, NULL);
262 simple = g_simple_async_result_new (source_object,
266 va_start (args, format);
267 g_simple_async_result_set_error_va (simple, domain, code, format, args);
275 g_simple_async_result_get_user_data (GAsyncResult *res)
277 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
281 g_simple_async_result_get_source_object (GAsyncResult *res)
283 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
284 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
289 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
291 iface->get_user_data = g_simple_async_result_get_user_data;
292 iface->get_source_object = g_simple_async_result_get_source_object;
296 * g_simple_async_result_set_handle_cancellation:
297 * @simple: a #GSimpleAsyncResult.
298 * @handle_cancellation: a #gboolean.
300 * Sets whether to handle cancellation within the asynchronous operation.
304 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
305 gboolean handle_cancellation)
307 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
308 simple->handle_cancellation = handle_cancellation;
312 * g_simple_async_result_get_source_tag:
313 * @simple: a #GSimpleAsyncResult.
315 * Gets the source tag for the #GSimpleAsyncResult.
317 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
320 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
322 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
323 return simple->source_tag;
327 * g_simple_async_result_propagate_error:
328 * @simple: a #GSimpleAsyncResult.
329 * @dest: a location to propegate the error to.
331 * Propagates an error from within the simple asynchronous result to
332 * a given destination.
334 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
337 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
340 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
344 g_propagate_error (dest, simple->error);
345 simple->error = NULL;
353 * g_simple_async_result_set_op_res_gpointer:
354 * @simple: a #GSimpleAsyncResult.
355 * @op_res: a pointer result from an asynchronous function.
356 * @destroy_op_res: a #GDestroyNotify function.
358 * Sets the operation result within the asynchronous result to a pointer.
361 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
363 GDestroyNotify destroy_op_res)
365 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
367 simple->op_res.v_pointer = op_res;
368 simple->destroy_op_res = destroy_op_res;
372 * g_simple_async_result_get_op_res_gpointer:
373 * @simple: a #GSimpleAsyncResult.
375 * Gets a pointer result as returned by the asynchronous function.
377 * Returns: a pointer from the result.
380 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
382 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
383 return simple->op_res.v_pointer;
387 * g_simple_async_result_set_op_res_gssize:
388 * @simple: a #GSimpleAsyncResult.
389 * @op_res: a #gssize.
391 * Sets the operation result within the asynchronous result to
395 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
398 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
399 simple->op_res.v_ssize = op_res;
403 * g_simple_async_result_get_op_res_gssize:
404 * @simple: a #GSimpleAsyncResult.
406 * Gets a gssize from the asynchronous result.
408 * Returns: a gssize returned from the asynchronous function.
411 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
413 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
414 return simple->op_res.v_ssize;
418 * g_simple_async_result_set_op_res_gboolean:
419 * @simple: a #GSimpleAsyncResult.
420 * @op_res: a #gboolean.
422 * Sets the operation result to a boolean within the asynchronous result.
425 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
428 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
429 simple->op_res.v_boolean = !!op_res;
433 * g_simple_async_result_get_op_res_gboolean:
434 * @simple: a #GSimpleAsyncResult.
436 * Gets the operation result boolean from within the asynchronous result.
438 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
439 * if the operation's result was %FALSE.
442 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
444 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
445 return simple->op_res.v_boolean;
449 * g_simple_async_result_set_from_error:
450 * @simple: a #GSimpleAsyncResult.
453 * Sets the result from a #GError.
456 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
459 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
460 g_return_if_fail (error != NULL);
462 simple->error = g_error_copy (error);
463 simple->failed = TRUE;
467 _g_error_new_valist (GQuark domain,
475 message = g_strdup_vprintf (format, args);
477 error = g_error_new_literal (domain, code, message);
484 * g_simple_async_result_set_error_va:
485 * @simple: a #GSimpleAsyncResult.
486 * @domain: a #GQuark (usually #G_IO_ERROR).
487 * @code: an error code.
488 * @format: a formatted error reporting string.
489 * @args: va_list of arguments.
491 * Sets an error within the asynchronous result without a #GError.
492 * Unless writing a binding, see g_simple_async_result_set_error().
495 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
501 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
502 g_return_if_fail (domain != 0);
503 g_return_if_fail (format != NULL);
505 simple->error = _g_error_new_valist (domain, code, format, args);
506 simple->failed = TRUE;
510 * g_simple_async_result_set_error:
511 * @simple: a #GSimpleAsyncResult.
512 * @domain: a #GQuark (usually #G_IO_ERROR).
513 * @code: an error code.
514 * @format: a formatted error reporting string.
515 * @...: a list of variables to fill in @format.
517 * Sets an error within the asynchronous result without a #GError.
520 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
528 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
529 g_return_if_fail (domain != 0);
530 g_return_if_fail (format != NULL);
532 va_start (args, format);
533 g_simple_async_result_set_error_va (simple, domain, code, format, args);
538 * g_simple_async_result_complete:
539 * @simple: a #GSimpleAsyncResult.
541 * Completes an asynchronous I/O job.
544 g_simple_async_result_complete (GSimpleAsyncResult *simple)
546 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
548 if (simple->callback)
549 simple->callback (simple->source_object,
550 G_ASYNC_RESULT (simple),
555 complete_in_idle_cb (gpointer data)
557 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
559 g_simple_async_result_complete (simple);
565 * g_simple_async_result_complete_in_idle:
566 * @simple: a #GSimpleAsyncResult.
568 * Completes an asynchronous function in the main event loop using
572 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
577 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
579 g_object_ref (simple);
581 source = g_idle_source_new ();
582 g_source_set_priority (source, G_PRIORITY_DEFAULT);
583 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
585 id = g_source_attach (source, NULL);
586 g_source_unref (source);
590 GSimpleAsyncResult *simple;
591 GSimpleAsyncThreadFunc func;
595 run_in_thread (GIOJob *job,
599 RunInThreadData *data = _data;
600 GSimpleAsyncResult *simple = data->simple;
602 if (simple->handle_cancellation &&
603 g_cancellable_is_cancelled (c))
604 g_simple_async_result_set_error (simple,
606 G_IO_ERROR_CANCELLED,
607 _("Operation was cancelled"));
610 simple->source_object,
613 g_simple_async_result_complete_in_idle (data->simple);
614 g_object_unref (data->simple);
619 * g_simple_async_result_run_in_thread:
620 * @simple: a #GSimpleAsyncResult.
621 * @func: a #GSimpleAsyncThreadFunc.
622 * @io_priority: the io priority of the request.
623 * @cancellable: optional #GCancellable object, %NULL to ignore.
625 * Runs the asynchronous job in a separated thread.
628 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
629 GSimpleAsyncThreadFunc func,
631 GCancellable *cancellable)
633 RunInThreadData *data;
635 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
636 g_return_if_fail (func != NULL);
638 data = g_new (RunInThreadData, 1);
640 data->simple = g_object_ref (simple);
641 g_schedule_io_job (run_in_thread, data, NULL, io_priority, cancellable);
645 * g_simple_async_report_error_in_idle:
646 * @object: a #GObject.
647 * @callback: a #GAsyncReadyCallback.
648 * @user_data: user data passed to @callback.
649 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
650 * @code: a specific error code.
651 * @format: a formatted error reporting string.
652 * @...: a list of variables to fill in @format.
654 * Reports an error in an idle function.
657 g_simple_async_report_error_in_idle (GObject *object,
658 GAsyncReadyCallback callback,
665 GSimpleAsyncResult *simple;
668 g_return_if_fail (G_IS_OBJECT (object));
669 g_return_if_fail (domain != 0);
670 g_return_if_fail (format != NULL);
672 simple = g_simple_async_result_new (object,
676 va_start (args, format);
677 g_simple_async_result_set_error_va (simple, domain, code, format, args);
679 g_simple_async_result_complete_in_idle (simple);
680 g_object_unref (simple);
683 #define __G_SIMPLE_ASYNC_RESULT_C__
684 #include "gioaliasdef.c"