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
44 * @see_also: #GAsyncResult
46 * Implements #GAsyncResult for simple cases. Most of the time, this
47 * will be all an application needs, and will be used transparently.
48 * Because of this, #GSimpleAsyncResult is used throughout GIO for
49 * handling asynchronous functions.
51 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error reporting,
52 * operation cancellation and the final state of an operation, completely
53 * transparent to the application. Results can be returned as a pointer e.g.
54 * for functions that return data that is collected asynchronously,
55 * a boolean value for checking the success or failure of an operation,
56 * or a #gssize for operations which return the number of bytes modified
57 * by the operation; all of the simple return cases are covered.
59 * Most of the time, an application will not need to know of the details
60 * of this API; it is handled transparently, and any necessary operations are
61 * handled by #GAsyncResult's interface. However, if implementing a new GIO
62 * module, for writing language bindings, or for complex applications that
63 * need better control of how asynchronous operations are completed, it is
64 * important to understand this functionality.
66 * GSimpleAsyncResults are tagged with the calling function to ensure that
67 * asynchronous functions and their finishing functions are used together
70 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
71 * If the result needs to be created for a #GError, use
72 * g_simple_async_result_new_from_error(). If a #GError is not available
73 * (e.g. the asynchronous operation's doesn't take a #GError argument),
74 * but the result still needs to be created for an error condition, use
75 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
76 * if your application or binding requires passing a variable argument list
77 * directly), and the error can then be propegated through the use of
78 * g_simple_async_result_propagate_error().
80 * An asynchronous operation can be made to ignore a cancellation event by
81 * calling g_simple_async_result_set_handle_cancellation() with a
82 * #GSimpleAsyncResult for the operation and %FALSE.
84 * GSimpleAsyncResult can integrate into GLib's Main Event Loop
85 * <!-- TODO: Crosslink -->, or it can use #GThread<!-- -->s if available.
86 * g_simple_async_result_complete() will finish an I/O task directly within
87 * the main event loop. g_simple_async_result_complete_in_idle() will
88 * integrate the I/O task into the main event loop as an idle function and
89 * g_simple_async_result_run_in_thread() will run the job in a separate
92 * To set the results of an asynchronous function,
93 * g_simple_async_result_set_op_res_gpointer(),
94 * g_simple_async_result_set_op_res_gboolean(), and
95 * g_simple_async_result_set_op_res_gssize()
96 * are provided, setting the operation's result to a gpointer, gboolean, or
97 * gssize, respectively.
99 * Likewise, to get the result of an asynchronous function,
100 * g_simple_async_result_get_op_res_gpointer(),
101 * g_simple_async_result_get_op_res_gboolean(), and
102 * g_simple_async_result_get_op_res_gssize() are
103 * provided, getting the operation's result as a gpointer, gboolean, and
104 * gssize, respectively.
107 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
109 struct _GSimpleAsyncResult
111 GObject parent_instance;
113 GObject *source_object;
114 GAsyncReadyCallback callback;
118 gboolean handle_cancellation;
128 GDestroyNotify destroy_op_res;
131 struct _GSimpleAsyncResultClass
133 GObjectClass parent_class;
137 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
138 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
139 g_simple_async_result_async_result_iface_init))
142 g_simple_async_result_finalize (GObject *object)
144 GSimpleAsyncResult *simple;
146 simple = G_SIMPLE_ASYNC_RESULT (object);
148 if (simple->source_object)
149 g_object_unref (simple->source_object);
151 if (simple->destroy_op_res)
152 simple->destroy_op_res (simple->op_res.v_pointer);
155 g_error_free (simple->error);
157 if (G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize)
158 (*G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize) (object);
162 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166 gobject_class->finalize = g_simple_async_result_finalize;
170 g_simple_async_result_init (GSimpleAsyncResult *simple)
172 simple->handle_cancellation = TRUE;
176 * g_simple_async_result_new:
177 * @source_object: a #GObject the asynchronous function was called with.
178 * @callback: a #GAsyncReadyCallback.
179 * @user_data: user data passed to @callback.
180 * @source_tag: the asynchronous function.
182 * Creates a #GSimpleAsyncResult.
184 * Returns: a #GSimpleAsyncResult.
187 g_simple_async_result_new (GObject *source_object,
188 GAsyncReadyCallback callback,
192 GSimpleAsyncResult *simple;
194 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
196 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
197 simple->callback = callback;
198 simple->source_object = g_object_ref (source_object);
199 simple->user_data = user_data;
200 simple->source_tag = source_tag;
206 * g_simple_async_result_new_from_error:
207 * @source_object: a #GObject.
208 * @callback: a #GAsyncReadyCallback.
209 * @user_data: user data passed to @callback.
210 * @error: a #GError location.
212 * Creates a #GSimpleAsyncResult from an error condition.
214 * Returns: a #GSimpleAsyncResult.
217 g_simple_async_result_new_from_error (GObject *source_object,
218 GAsyncReadyCallback callback,
222 GSimpleAsyncResult *simple;
224 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
226 simple = g_simple_async_result_new (source_object,
229 g_simple_async_result_set_from_error (simple, error);
235 * g_simple_async_result_new_error:
236 * @source_object: a #GObject.
237 * @callback: a #GAsyncReadyCallback.
238 * @user_data: user data passed to @callback.
239 * @domain: a #GQuark.
240 * @code: an error code.
241 * @format: a string with format characters.
242 * @...: a list of values to insert into @format.
244 * Creates a new #GSimpleAsyncResult with a set error.
246 * Returns: a #GSimpleAsyncResult.
249 g_simple_async_result_new_error (GObject *source_object,
250 GAsyncReadyCallback callback,
257 GSimpleAsyncResult *simple;
260 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
261 g_return_val_if_fail (domain != 0, NULL);
262 g_return_val_if_fail (format != NULL, NULL);
264 simple = g_simple_async_result_new (source_object,
268 va_start (args, format);
269 g_simple_async_result_set_error_va (simple, domain, code, format, args);
277 g_simple_async_result_get_user_data (GAsyncResult *res)
279 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
283 g_simple_async_result_get_source_object (GAsyncResult *res)
285 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
286 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
291 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
293 iface->get_user_data = g_simple_async_result_get_user_data;
294 iface->get_source_object = g_simple_async_result_get_source_object;
298 * g_simple_async_result_set_handle_cancellation:
299 * @simple: a #GSimpleAsyncResult.
300 * @handle_cancellation: a #gboolean.
302 * Sets whether to handle cancellation within the asynchronous operation.
306 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
307 gboolean handle_cancellation)
309 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
310 simple->handle_cancellation = handle_cancellation;
314 * g_simple_async_result_get_source_tag:
315 * @simple: a #GSimpleAsyncResult.
317 * Gets the source tag for the #GSimpleAsyncResult.
319 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
322 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
324 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
325 return simple->source_tag;
329 * g_simple_async_result_propagate_error:
330 * @simple: a #GSimpleAsyncResult.
331 * @dest: a location to propegate the error to.
333 * Propagates an error from within the simple asynchronous result to
334 * a given destination.
336 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
339 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
342 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
346 g_propagate_error (dest, simple->error);
347 simple->error = NULL;
355 * g_simple_async_result_set_op_res_gpointer:
356 * @simple: a #GSimpleAsyncResult.
357 * @op_res: a pointer result from an asynchronous function.
358 * @destroy_op_res: a #GDestroyNotify function.
360 * Sets the operation result within the asynchronous result to a pointer.
363 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
365 GDestroyNotify destroy_op_res)
367 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
369 simple->op_res.v_pointer = op_res;
370 simple->destroy_op_res = destroy_op_res;
374 * g_simple_async_result_get_op_res_gpointer:
375 * @simple: a #GSimpleAsyncResult.
377 * Gets a pointer result as returned by the asynchronous function.
379 * Returns: a pointer from the result.
382 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
384 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
385 return simple->op_res.v_pointer;
389 * g_simple_async_result_set_op_res_gssize:
390 * @simple: a #GSimpleAsyncResult.
391 * @op_res: a #gssize.
393 * Sets the operation result within the asynchronous result to
397 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
400 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
401 simple->op_res.v_ssize = op_res;
405 * g_simple_async_result_get_op_res_gssize:
406 * @simple: a #GSimpleAsyncResult.
408 * Gets a gssize from the asynchronous result.
410 * Returns: a gssize returned from the asynchronous function.
413 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
415 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
416 return simple->op_res.v_ssize;
420 * g_simple_async_result_set_op_res_gboolean:
421 * @simple: a #GSimpleAsyncResult.
422 * @op_res: a #gboolean.
424 * Sets the operation result to a boolean within the asynchronous result.
427 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
430 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
431 simple->op_res.v_boolean = !!op_res;
435 * g_simple_async_result_get_op_res_gboolean:
436 * @simple: a #GSimpleAsyncResult.
438 * Gets the operation result boolean from within the asynchronous result.
440 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
441 * if the operation's result was %FALSE.
444 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
446 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
447 return simple->op_res.v_boolean;
451 * g_simple_async_result_set_from_error:
452 * @simple: a #GSimpleAsyncResult.
455 * Sets the result from a #GError.
458 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
461 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
462 g_return_if_fail (error != NULL);
464 simple->error = g_error_copy (error);
465 simple->failed = TRUE;
469 _g_error_new_valist (GQuark domain,
477 message = g_strdup_vprintf (format, args);
479 error = g_error_new_literal (domain, code, message);
486 * g_simple_async_result_set_error_va:
487 * @simple: a #GSimpleAsyncResult.
488 * @domain: a #GQuark (usually #G_IO_ERROR).
489 * @code: an error code.
490 * @format: a formatted error reporting string.
491 * @args: va_list of arguments.
493 * Sets an error within the asynchronous result without a #GError.
494 * Unless writing a binding, see g_simple_async_result_set_error().
497 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
503 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
504 g_return_if_fail (domain != 0);
505 g_return_if_fail (format != NULL);
507 simple->error = _g_error_new_valist (domain, code, format, args);
508 simple->failed = TRUE;
512 * g_simple_async_result_set_error:
513 * @simple: a #GSimpleAsyncResult.
514 * @domain: a #GQuark (usually #G_IO_ERROR).
515 * @code: an error code.
516 * @format: a formatted error reporting string.
517 * @...: a list of variables to fill in @format.
519 * Sets an error within the asynchronous result without a #GError.
522 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
530 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
531 g_return_if_fail (domain != 0);
532 g_return_if_fail (format != NULL);
534 va_start (args, format);
535 g_simple_async_result_set_error_va (simple, domain, code, format, args);
540 * g_simple_async_result_complete:
541 * @simple: a #GSimpleAsyncResult.
543 * Completes an asynchronous I/O job.
546 g_simple_async_result_complete (GSimpleAsyncResult *simple)
548 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
550 if (simple->callback)
551 simple->callback (simple->source_object,
552 G_ASYNC_RESULT (simple),
557 complete_in_idle_cb (gpointer data)
559 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
561 g_simple_async_result_complete (simple);
567 * g_simple_async_result_complete_in_idle:
568 * @simple: a #GSimpleAsyncResult.
570 * Completes an asynchronous function in the main event loop using
574 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
579 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
581 g_object_ref (simple);
583 source = g_idle_source_new ();
584 g_source_set_priority (source, G_PRIORITY_DEFAULT);
585 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
587 id = g_source_attach (source, NULL);
588 g_source_unref (source);
592 GSimpleAsyncResult *simple;
593 GSimpleAsyncThreadFunc func;
597 run_in_thread (GIOSchedulerJob *job,
601 RunInThreadData *data = _data;
602 GSimpleAsyncResult *simple = data->simple;
604 if (simple->handle_cancellation &&
605 g_cancellable_is_cancelled (c))
606 g_simple_async_result_set_error (simple,
608 G_IO_ERROR_CANCELLED,
609 _("Operation was cancelled"));
612 simple->source_object,
615 g_simple_async_result_complete_in_idle (data->simple);
616 g_object_unref (data->simple);
621 * g_simple_async_result_run_in_thread:
622 * @simple: a #GSimpleAsyncResult.
623 * @func: a #GSimpleAsyncThreadFunc.
624 * @io_priority: the io priority of the request.
625 * @cancellable: optional #GCancellable object, %NULL to ignore.
627 * Runs the asynchronous job in a separated thread.
630 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
631 GSimpleAsyncThreadFunc func,
633 GCancellable *cancellable)
635 RunInThreadData *data;
637 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
638 g_return_if_fail (func != NULL);
640 data = g_new (RunInThreadData, 1);
642 data->simple = g_object_ref (simple);
643 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
647 * g_simple_async_report_error_in_idle:
648 * @object: a #GObject.
649 * @callback: a #GAsyncReadyCallback.
650 * @user_data: user data passed to @callback.
651 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
652 * @code: a specific error code.
653 * @format: a formatted error reporting string.
654 * @...: a list of variables to fill in @format.
656 * Reports an error in an asynchronous function in an idle function by
657 * directly setting the contents of the #GAsyncResult with the given error
661 g_simple_async_report_error_in_idle (GObject *object,
662 GAsyncReadyCallback callback,
669 GSimpleAsyncResult *simple;
672 g_return_if_fail (G_IS_OBJECT (object));
673 g_return_if_fail (domain != 0);
674 g_return_if_fail (format != NULL);
676 simple = g_simple_async_result_new (object,
680 va_start (args, format);
681 g_simple_async_result_set_error_va (simple, domain, code, format, args);
683 g_simple_async_result_complete_in_idle (simple);
684 g_object_unref (simple);
688 * g_simple_async_report_gerror_in_idle:
689 * @object: a #GObject.
690 * @callback: a #GAsyncReadyCallback.
691 * @user_data: user data passed to @callback.
692 * @error: the #GError to report
694 * Reports an error in an idle function. Similar to
695 * g_simple_async_report_error_in_idle(), but takes a #GError rather
696 * than building a new one.
699 g_simple_async_report_gerror_in_idle (GObject *object,
700 GAsyncReadyCallback callback,
704 GSimpleAsyncResult *simple;
706 g_return_if_fail (G_IS_OBJECT (object));
707 g_return_if_fail (error != NULL);
709 simple = g_simple_async_result_new_from_error (object,
713 g_simple_async_result_complete_in_idle (simple);
714 g_object_unref (simple);
717 #define __G_SIMPLE_ASYNC_RESULT_C__
718 #include "gioaliasdef.c"