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 "gioscheduler.h"
36 #include <gio/gioerror.h>
42 * SECTION:gsimpleasyncresult
43 * @short_description: Simple asynchronous results implementation
45 * @see_also: #GAsyncResult
47 * Implements #GAsyncResult for simple cases. Most of the time, this
48 * will be all an application needs, and will be used transparently.
49 * Because of this, #GSimpleAsyncResult is used throughout GIO for
50 * handling asynchronous functions.
52 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
53 * reporting, operation cancellation and the final state of an operation,
54 * completely transparent to the application. Results can be returned
55 * as a pointer e.g. for functions that return data that is collected
56 * asynchronously, a boolean value for checking the success or failure
57 * of an operation, or a #gssize for operations which return the number
58 * of bytes modified by the operation; all of the simple return cases
61 * Most of the time, an application will not need to know of the details
62 * of this API; it is handled transparently, and any necessary operations
63 * are handled by #GAsyncResult's interface. However, if implementing a
64 * new GIO module, for writing language bindings, or for complex
65 * applications that need better control of how asynchronous operations
66 * are completed, it is important to understand this functionality.
68 * GSimpleAsyncResults are tagged with the calling function to ensure
69 * that asynchronous functions and their finishing functions are used
72 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
73 * If the result needs to be created for a #GError, use
74 * g_simple_async_result_new_from_error(). If a #GError is not available
75 * (e.g. the asynchronous operation's doesn't take a #GError argument),
76 * but the result still needs to be created for an error condition, use
77 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
78 * if your application or binding requires passing a variable argument list
79 * directly), and the error can then be propegated through the use of
80 * g_simple_async_result_propagate_error().
82 * An asynchronous operation can be made to ignore a cancellation event by
83 * calling g_simple_async_result_set_handle_cancellation() with a
84 * #GSimpleAsyncResult for the operation and %FALSE.
86 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
87 * or it can use #GThread<!-- -->s if available.
88 * g_simple_async_result_complete() will finish an I/O task directly within
89 * the main event loop. g_simple_async_result_complete_in_idle() will
90 * integrate the I/O task into the main event loop as an idle function and
91 * g_simple_async_result_run_in_thread() will run the job in a separate
94 * To set the results of an asynchronous function,
95 * g_simple_async_result_set_op_res_gpointer(),
96 * g_simple_async_result_set_op_res_gboolean(), and
97 * g_simple_async_result_set_op_res_gssize()
98 * are provided, setting the operation's result to a gpointer, gboolean, or
99 * gssize, respectively.
101 * Likewise, to get the result of an asynchronous function,
102 * g_simple_async_result_get_op_res_gpointer(),
103 * g_simple_async_result_get_op_res_gboolean(), and
104 * g_simple_async_result_get_op_res_gssize() are
105 * provided, getting the operation's result as a gpointer, gboolean, and
106 * gssize, respectively.
109 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
111 struct _GSimpleAsyncResult
113 GObject parent_instance;
115 GObject *source_object;
116 GAsyncReadyCallback callback;
120 gboolean handle_cancellation;
130 GDestroyNotify destroy_op_res;
133 struct _GSimpleAsyncResultClass
135 GObjectClass parent_class;
139 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
140 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
141 g_simple_async_result_async_result_iface_init))
144 g_simple_async_result_finalize (GObject *object)
146 GSimpleAsyncResult *simple;
148 simple = G_SIMPLE_ASYNC_RESULT (object);
150 if (simple->source_object)
151 g_object_unref (simple->source_object);
153 if (simple->destroy_op_res)
154 simple->destroy_op_res (simple->op_res.v_pointer);
157 g_error_free (simple->error);
159 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
163 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167 gobject_class->finalize = g_simple_async_result_finalize;
171 g_simple_async_result_init (GSimpleAsyncResult *simple)
173 simple->handle_cancellation = TRUE;
177 * g_simple_async_result_new:
178 * @source_object: a #GObject the asynchronous function was called with.
179 * @callback: a #GAsyncReadyCallback.
180 * @user_data: user data passed to @callback.
181 * @source_tag: the asynchronous function.
183 * Creates a #GSimpleAsyncResult.
185 * Returns: a #GSimpleAsyncResult.
188 g_simple_async_result_new (GObject *source_object,
189 GAsyncReadyCallback callback,
193 GSimpleAsyncResult *simple;
195 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
197 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
198 simple->callback = callback;
199 simple->source_object = g_object_ref (source_object);
200 simple->user_data = user_data;
201 simple->source_tag = source_tag;
207 * g_simple_async_result_new_from_error:
208 * @source_object: a #GObject.
209 * @callback: a #GAsyncReadyCallback.
210 * @user_data: user data passed to @callback.
211 * @error: a #GError location.
213 * Creates a #GSimpleAsyncResult from an error condition.
215 * Returns: a #GSimpleAsyncResult.
218 g_simple_async_result_new_from_error (GObject *source_object,
219 GAsyncReadyCallback callback,
223 GSimpleAsyncResult *simple;
225 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
227 simple = g_simple_async_result_new (source_object,
230 g_simple_async_result_set_from_error (simple, error);
236 * g_simple_async_result_new_error:
237 * @source_object: a #GObject.
238 * @callback: a #GAsyncReadyCallback.
239 * @user_data: user data passed to @callback.
240 * @domain: a #GQuark.
241 * @code: an error code.
242 * @format: a string with format characters.
243 * @...: a list of values to insert into @format.
245 * Creates a new #GSimpleAsyncResult with a set error.
247 * Returns: a #GSimpleAsyncResult.
250 g_simple_async_result_new_error (GObject *source_object,
251 GAsyncReadyCallback callback,
258 GSimpleAsyncResult *simple;
261 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
262 g_return_val_if_fail (domain != 0, NULL);
263 g_return_val_if_fail (format != NULL, NULL);
265 simple = g_simple_async_result_new (source_object,
269 va_start (args, format);
270 g_simple_async_result_set_error_va (simple, domain, code, format, args);
278 g_simple_async_result_get_user_data (GAsyncResult *res)
280 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
284 g_simple_async_result_get_source_object (GAsyncResult *res)
286 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
287 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
292 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
294 iface->get_user_data = g_simple_async_result_get_user_data;
295 iface->get_source_object = g_simple_async_result_get_source_object;
299 * g_simple_async_result_set_handle_cancellation:
300 * @simple: a #GSimpleAsyncResult.
301 * @handle_cancellation: a #gboolean.
303 * Sets whether to handle cancellation within the asynchronous operation.
307 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
308 gboolean handle_cancellation)
310 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
311 simple->handle_cancellation = handle_cancellation;
315 * g_simple_async_result_get_source_tag:
316 * @simple: a #GSimpleAsyncResult.
318 * Gets the source tag for the #GSimpleAsyncResult.
320 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
323 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
325 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
326 return simple->source_tag;
330 * g_simple_async_result_propagate_error:
331 * @simple: a #GSimpleAsyncResult.
332 * @dest: a location to propegate the error to.
334 * Propagates an error from within the simple asynchronous result to
335 * a given destination.
337 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
340 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
343 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
347 g_propagate_error (dest, simple->error);
348 simple->error = NULL;
356 * g_simple_async_result_set_op_res_gpointer:
357 * @simple: a #GSimpleAsyncResult.
358 * @op_res: a pointer result from an asynchronous function.
359 * @destroy_op_res: a #GDestroyNotify function.
361 * Sets the operation result within the asynchronous result to a pointer.
364 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
366 GDestroyNotify destroy_op_res)
368 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
370 simple->op_res.v_pointer = op_res;
371 simple->destroy_op_res = destroy_op_res;
375 * g_simple_async_result_get_op_res_gpointer:
376 * @simple: a #GSimpleAsyncResult.
378 * Gets a pointer result as returned by the asynchronous function.
380 * Returns: a pointer from the result.
383 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
385 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
386 return simple->op_res.v_pointer;
390 * g_simple_async_result_set_op_res_gssize:
391 * @simple: a #GSimpleAsyncResult.
392 * @op_res: a #gssize.
394 * Sets the operation result within the asynchronous result to
398 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
401 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
402 simple->op_res.v_ssize = op_res;
406 * g_simple_async_result_get_op_res_gssize:
407 * @simple: a #GSimpleAsyncResult.
409 * Gets a gssize from the asynchronous result.
411 * Returns: a gssize returned from the asynchronous function.
414 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
416 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
417 return simple->op_res.v_ssize;
421 * g_simple_async_result_set_op_res_gboolean:
422 * @simple: a #GSimpleAsyncResult.
423 * @op_res: a #gboolean.
425 * Sets the operation result to a boolean within the asynchronous result.
428 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
431 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
432 simple->op_res.v_boolean = !!op_res;
436 * g_simple_async_result_get_op_res_gboolean:
437 * @simple: a #GSimpleAsyncResult.
439 * Gets the operation result boolean from within the asynchronous result.
441 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
442 * if the operation's result was %FALSE.
445 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
447 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
448 return simple->op_res.v_boolean;
452 * g_simple_async_result_set_from_error:
453 * @simple: a #GSimpleAsyncResult.
456 * Sets the result from a #GError.
459 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
462 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
463 g_return_if_fail (error != NULL);
465 simple->error = g_error_copy (error);
466 simple->failed = TRUE;
470 _g_error_new_valist (GQuark domain,
478 message = g_strdup_vprintf (format, args);
480 error = g_error_new_literal (domain, code, message);
487 * g_simple_async_result_set_error_va:
488 * @simple: a #GSimpleAsyncResult.
489 * @domain: a #GQuark (usually #G_IO_ERROR).
490 * @code: an error code.
491 * @format: a formatted error reporting string.
492 * @args: va_list of arguments.
494 * Sets an error within the asynchronous result without a #GError.
495 * Unless writing a binding, see g_simple_async_result_set_error().
498 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
504 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
505 g_return_if_fail (domain != 0);
506 g_return_if_fail (format != NULL);
508 simple->error = _g_error_new_valist (domain, code, format, args);
509 simple->failed = TRUE;
513 * g_simple_async_result_set_error:
514 * @simple: a #GSimpleAsyncResult.
515 * @domain: a #GQuark (usually #G_IO_ERROR).
516 * @code: an error code.
517 * @format: a formatted error reporting string.
518 * @...: a list of variables to fill in @format.
520 * Sets an error within the asynchronous result without a #GError.
523 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
531 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
532 g_return_if_fail (domain != 0);
533 g_return_if_fail (format != NULL);
535 va_start (args, format);
536 g_simple_async_result_set_error_va (simple, domain, code, format, args);
541 * g_simple_async_result_complete:
542 * @simple: a #GSimpleAsyncResult.
544 * Completes an asynchronous I/O job.
547 g_simple_async_result_complete (GSimpleAsyncResult *simple)
549 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
551 if (simple->callback)
552 simple->callback (simple->source_object,
553 G_ASYNC_RESULT (simple),
558 complete_in_idle_cb (gpointer data)
560 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
562 g_simple_async_result_complete (simple);
568 * g_simple_async_result_complete_in_idle:
569 * @simple: a #GSimpleAsyncResult.
571 * Completes an asynchronous function in the main event loop using
575 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
580 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
582 g_object_ref (simple);
584 source = g_idle_source_new ();
585 g_source_set_priority (source, G_PRIORITY_DEFAULT);
586 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
588 id = g_source_attach (source, NULL);
589 g_source_unref (source);
593 GSimpleAsyncResult *simple;
594 GSimpleAsyncThreadFunc func;
598 run_in_thread (GIOSchedulerJob *job,
602 RunInThreadData *data = _data;
603 GSimpleAsyncResult *simple = data->simple;
605 if (simple->handle_cancellation &&
606 g_cancellable_is_cancelled (c))
607 g_simple_async_result_set_error (simple,
609 G_IO_ERROR_CANCELLED,
610 _("Operation was cancelled"));
613 simple->source_object,
616 g_simple_async_result_complete_in_idle (data->simple);
617 g_object_unref (data->simple);
624 * g_simple_async_result_run_in_thread:
625 * @simple: a #GSimpleAsyncResult.
626 * @func: a #GSimpleAsyncThreadFunc.
627 * @io_priority: the io priority of the request.
628 * @cancellable: optional #GCancellable object, %NULL to ignore.
630 * Runs the asynchronous job in a separated thread.
633 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
634 GSimpleAsyncThreadFunc func,
636 GCancellable *cancellable)
638 RunInThreadData *data;
640 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
641 g_return_if_fail (func != NULL);
643 data = g_new (RunInThreadData, 1);
645 data->simple = g_object_ref (simple);
646 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
650 * g_simple_async_report_error_in_idle:
651 * @object: a #GObject.
652 * @callback: a #GAsyncReadyCallback.
653 * @user_data: user data passed to @callback.
654 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
655 * @code: a specific error code.
656 * @format: a formatted error reporting string.
657 * @...: a list of variables to fill in @format.
659 * Reports an error in an asynchronous function in an idle function by
660 * directly setting the contents of the #GAsyncResult with the given error
664 g_simple_async_report_error_in_idle (GObject *object,
665 GAsyncReadyCallback callback,
672 GSimpleAsyncResult *simple;
675 g_return_if_fail (G_IS_OBJECT (object));
676 g_return_if_fail (domain != 0);
677 g_return_if_fail (format != NULL);
679 simple = g_simple_async_result_new (object,
683 va_start (args, format);
684 g_simple_async_result_set_error_va (simple, domain, code, format, args);
686 g_simple_async_result_complete_in_idle (simple);
687 g_object_unref (simple);
691 * g_simple_async_report_gerror_in_idle:
692 * @object: a #GObject.
693 * @callback: a #GAsyncReadyCallback.
694 * @user_data: user data passed to @callback.
695 * @error: the #GError to report
697 * Reports an error in an idle function. Similar to
698 * g_simple_async_report_error_in_idle(), but takes a #GError rather
699 * than building a new one.
702 g_simple_async_report_gerror_in_idle (GObject *object,
703 GAsyncReadyCallback callback,
707 GSimpleAsyncResult *simple;
709 g_return_if_fail (G_IS_OBJECT (object));
710 g_return_if_fail (error != NULL);
712 simple = g_simple_async_result_new_from_error (object,
716 g_simple_async_result_complete_in_idle (simple);
717 g_object_unref (simple);
720 #define __G_SIMPLE_ASYNC_RESULT_C__
721 #include "gioaliasdef.c"