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>
38 * SECTION:gsimpleasyncresult
39 * @short_description: simple asynchronous results implementation.
40 * @see_also: #GAsyncResult.
42 * Implements #GAsyncResult for simple cases. Most of the time, this
43 * will be all an application needs, and will be used transparently.
44 * Because of this, #GSimpleAsyncResult is used throughout GIO for
45 * handling asynchronous functions.
47 * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error reporting,
48 * operation cancellation and the final state of an operation, completely
49 * transparent to the application. Results can be returned as a pointer e.g.
50 * for functions that return data that is collected asynchronously,
51 * a boolean value for checking the success or failure of an operation,
52 * or a #gssize for operations which return the number of bytes modified
53 * by the operation; all of the simple return cases are covered.
55 * Most of the time, an application will not need to know of the details
56 * of this API; it is handled transparently, and any necessary operations are handled by
57 * #GAsyncResult's interface. However, if implementing a new GIO module, for writing
58 * language bindings, or for complex applications that need better control of how
59 * asynchronous operations are completed, it is important to understand this functionality.
61 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new(). If the
62 * result needs to be created for a #GError, use g_simple_async_result_new_from_error().
63 * If a #GError is not available (e.g. the asynchronous operation's doesn't take a #GError
64 * argument), but the result still needs to be created for an error condition, use
65 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
66 * if your application or binding requires passing a variable argument list directly),
67 * and the error can then be propegated through the use of g_simple_async_result_propagate_error().
69 * An asynchronous operation can be made to ignore a cancellation event by calling
70 * g_simple_async_result_set_handle_cancellation() with a #GSimpleAsyncResult for the operation
73 * GSimpleAsyncResult can integrate into GLib's Main Event Loop <!-- TODO: Crosslink -->,
74 * or it can use #GThread<!-- -->s if available. g_simple_async_result_complete() will finish an
75 * I/O task directly within the main event loop. g_simple_async_result_complete_in_idle() will integrate
76 * the I/O task into the main event loop as an idle function and g_simple_async_result_run_in_thread()
77 * will run the job in a separate thread.
79 * To set the results of an asynchronous function, g_simple_async_result_set_op_res_gpointer(),
80 * g_simple_async_result_set_op_res_gboolean(), and g_simple_async_result_set_op_res_gssize()
81 * are provided, setting the operation's result to a gpointer, gboolean, or gssize, respectively.
83 * Likewise, to get the result of an asynchronous function, g_simple_async_result_get_op_res_gpointer(),
84 * g_simple_async_result_get_op_res_gboolean(), and g_simple_async_result_get_op_res_gssize() are
85 * provided, getting the operation's result as a gpointer, gboolean, and gssize, respectively.
89 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
91 struct _GSimpleAsyncResult
93 GObject parent_instance;
95 GObject *source_object;
96 GAsyncReadyCallback callback;
100 gboolean handle_cancellation;
110 GDestroyNotify destroy_op_res;
113 struct _GSimpleAsyncResultClass
115 GObjectClass parent_class;
119 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
120 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
121 g_simple_async_result_async_result_iface_init))
124 g_simple_async_result_finalize (GObject *object)
126 GSimpleAsyncResult *simple;
128 simple = G_SIMPLE_ASYNC_RESULT (object);
130 if (simple->source_object)
131 g_object_unref (simple->source_object);
133 if (simple->destroy_op_res)
134 simple->destroy_op_res (simple->op_res.v_pointer);
137 g_error_free (simple->error);
139 if (G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize)
140 (*G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize) (object);
144 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
146 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
148 gobject_class->finalize = g_simple_async_result_finalize;
152 g_simple_async_result_init (GSimpleAsyncResult *simple)
154 simple->handle_cancellation = TRUE;
158 * g_simple_async_result_new:
159 * @source_object: a #GObject.
160 * @callback: a #GAsyncReadyCallback.
161 * @user_data: user data passed to @callback.
164 * Creates a #GSimpleAsyncResult.
166 * Returns: a #GSimpleAsyncResult.
169 g_simple_async_result_new (GObject *source_object,
170 GAsyncReadyCallback callback,
174 GSimpleAsyncResult *simple;
176 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
178 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
179 simple->callback = callback;
180 simple->source_object = g_object_ref (source_object);
181 simple->user_data = user_data;
182 simple->source_tag = source_tag;
188 * g_simple_async_result_new_from_error:
189 * @source_object: a #GObject.
190 * @callback: a #GAsyncReadyCallback.
191 * @user_data: user data passed to @callback.
192 * @error: a #GError location.
194 * Creates a #GSimpleAsyncResult from an error condition.
196 * Returns: a #GSimpleAsyncResult.
199 g_simple_async_result_new_from_error (GObject *source_object,
200 GAsyncReadyCallback callback,
204 GSimpleAsyncResult *simple;
206 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
208 simple = g_simple_async_result_new (source_object,
211 g_simple_async_result_set_from_error (simple, error);
217 * g_simple_async_result_new_error:
218 * @source_object: a #GObject.
219 * @callback: a #GAsyncReadyCallback.
220 * @user_data: user data passed to @callback.
221 * @domain: a #GQuark.
222 * @code: an error code.
223 * @format: a string with format characters.
224 * @Varargs: a list of values to insert into @format.
226 * Creates a new #GSimpleAsyncResult with a set error.
228 * Returns: a #GSimpleAsyncResult.
231 g_simple_async_result_new_error (GObject *source_object,
232 GAsyncReadyCallback callback,
239 GSimpleAsyncResult *simple;
242 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
243 g_return_val_if_fail (domain != 0, NULL);
244 g_return_val_if_fail (format != NULL, NULL);
246 simple = g_simple_async_result_new (source_object,
250 va_start (args, format);
251 g_simple_async_result_set_error_va (simple, domain, code, format, args);
259 g_simple_async_result_get_user_data (GAsyncResult *res)
261 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
265 g_simple_async_result_get_source_object (GAsyncResult *res)
267 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
268 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
273 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
275 iface->get_user_data = g_simple_async_result_get_user_data;
276 iface->get_source_object = g_simple_async_result_get_source_object;
280 * g_simple_async_result_set_handle_cancellation:
281 * @simple: a #GSimpleAsyncResult.
282 * @handle_cancellation: a #gboolean.
284 * Sets whether to handle cancellation within the asynchronous operation.
288 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
289 gboolean handle_cancellation)
291 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
292 simple->handle_cancellation = handle_cancellation;
296 * g_simple_async_result_get_source_tag:
297 * @simple: a #GSimpleAsyncResult.
299 * Gets the source tag for the #GSimpleAsyncResult.
301 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
304 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
306 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
307 return simple->source_tag;
311 * g_simple_async_result_propagate_error:
312 * @simple: a #GSimpleAsyncResult.
313 * @dest: a location to propegate the error to.
315 * Propegates an error from within the simple asynchronous result to
316 * a given destination.
318 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
321 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
324 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
328 g_propagate_error (dest, simple->error);
329 simple->error = NULL;
336 * g_simple_async_result_set_op_res_gpointer:
337 * @simple: a #GSimpleAsyncResult.
338 * @op_res: a pointer result from an asynchronous function.
339 * @destroy_op_res: a #GDestroyNotify function.
341 * Sets the operation result within the asynchronous result to a pointer.
344 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
346 GDestroyNotify destroy_op_res)
348 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
350 simple->op_res.v_pointer = op_res;
351 simple->destroy_op_res = destroy_op_res;
355 * g_simple_async_result_get_op_res_gpointer:
356 * @simple: a #GSimpleAsyncResult.
358 * Gets a pointer result as returned by the asynchronous function.
360 * Returns: a pointer from the result.
363 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
365 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
366 return simple->op_res.v_pointer;
370 * g_simple_async_result_set_op_res_gssize:
371 * @simple: a #GSimpleAsyncResult.
372 * @op_res: a #gssize.
374 * Sets the operation result within the asynchronous result to the given @op_res.
377 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
380 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
381 simple->op_res.v_ssize = op_res;
385 * g_simple_async_result_get_op_res_gssize:
386 * @simple: a #GSimpleAsyncResult.
388 * Gets a gssize from the asynchronous result.
390 * Returns: a gssize returned from the asynchronous function.
393 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
395 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
396 return simple->op_res.v_ssize;
400 * g_simple_async_result_set_op_res_gboolean:
401 * @simple: a #GSimpleAsyncResult.
402 * @op_res: a #gboolean.
404 * Sets the operation result to a boolean within the asynchronous result.
408 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
411 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
412 simple->op_res.v_boolean = !!op_res;
416 * g_simple_async_result_get_op_res_gboolean:
417 * @simple: a #GSimpleAsyncResult.
419 * Gets the operation result boolean from within the asynchronous result.
421 * Returns: %TRUE if the operation's result was %TRUE, %FALSE if the operation's
425 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
427 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
428 return simple->op_res.v_boolean;
432 * g_simple_async_result_set_from_error:
433 * @simple: a #GSimpleAsyncResult.
436 * Sets the result from a #GError.
439 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
442 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
443 g_return_if_fail (error != NULL);
445 simple->error = g_error_copy (error);
446 simple->failed = TRUE;
450 _g_error_new_valist (GQuark domain,
458 message = g_strdup_vprintf (format, args);
460 error = g_error_new_literal (domain, code, message);
467 * g_simple_async_result_set_error_va:
468 * @simple: a #GSimpleAsyncResult.
469 * @domain: a #GQuark (usually #G_IO_ERROR).
470 * @code: an error code.
471 * @format: a formatted error reporting string.
472 * @args: va_list of arguments.
474 * Sets an error within the asynchronous result without a #GError. Unless
475 * writing a binding, see g_simple_async_result_set_error().
478 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
484 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
485 g_return_if_fail (domain != 0);
486 g_return_if_fail (format != NULL);
488 simple->error = _g_error_new_valist (domain, code, format, args);
489 simple->failed = TRUE;
493 * g_simple_async_result_set_error:
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 * @Varargs: a list of variables to fill in @format.
500 * Sets an error within the asynchronous result without a #GError.
504 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
512 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
513 g_return_if_fail (domain != 0);
514 g_return_if_fail (format != NULL);
516 va_start (args, format);
517 g_simple_async_result_set_error_va (simple, domain, code, format, args);
522 * g_simple_async_result_complete:
523 * @simple: a #GSimpleAsyncResult.
525 * Completes an asynchronous I/O job.
529 g_simple_async_result_complete (GSimpleAsyncResult *simple)
531 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
533 if (simple->callback)
534 simple->callback (simple->source_object,
535 G_ASYNC_RESULT (simple),
540 complete_in_idle_cb (gpointer data)
542 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
544 g_simple_async_result_complete (simple);
550 * g_simple_async_result_complete_in_idle:
551 * @simple: a #GSimpleAsyncResult.
553 * Completes an asynchronous function in the main event loop using
558 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
563 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
565 g_object_ref (simple);
567 source = g_idle_source_new ();
568 g_source_set_priority (source, G_PRIORITY_DEFAULT);
569 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
571 id = g_source_attach (source, NULL);
572 g_source_unref (source);
576 GSimpleAsyncResult *simple;
577 GSimpleAsyncThreadFunc func;
581 run_in_thread (GIOJob *job,
585 RunInThreadData *data = _data;
586 GSimpleAsyncResult *simple = data->simple;
588 if (simple->handle_cancellation &&
589 g_cancellable_is_cancelled (c))
591 g_simple_async_result_set_error (simple,
593 G_IO_ERROR_CANCELLED,
594 _("Operation was cancelled"));
599 simple->source_object,
603 g_simple_async_result_complete_in_idle (data->simple);
604 g_object_unref (data->simple);
609 * g_simple_async_result_run_in_thread:
610 * @simple: a #GSimpleAsyncResult.
611 * @func: a #GSimpleAsyncThreadFunc.
612 * @io_priority: the io priority of the request.
613 * @cancellable: optional #GCancellable object, %NULL to ignore.
615 * Runs the asynchronous job in a separated thread.
619 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
620 GSimpleAsyncThreadFunc func,
622 GCancellable *cancellable)
624 RunInThreadData *data;
626 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
627 g_return_if_fail (func != NULL);
629 data = g_new (RunInThreadData, 1);
631 data->simple = g_object_ref (simple);
632 g_schedule_io_job (run_in_thread, data, NULL, io_priority, cancellable);
636 * g_simple_async_report_error_in_idle:
638 * @callback: a #GAsyncReadyCallback.
639 * @user_data: user data passed to @callback.
640 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
641 * @code: a specific error code.
642 * @format: a formatted error reporting string.
643 * @Varargs: a list of variables to fill in @format.
645 * Reports an error in an idle function.
649 g_simple_async_report_error_in_idle (GObject *object,
650 GAsyncReadyCallback callback,
657 GSimpleAsyncResult *simple;
660 g_return_if_fail (G_IS_OBJECT (object));
661 g_return_if_fail (domain != 0);
662 g_return_if_fail (format != NULL);
664 simple = g_simple_async_result_new (object,
668 va_start (args, format);
669 g_simple_async_result_set_error_va (simple, domain, code, format, args);
671 g_simple_async_result_complete_in_idle (simple);
672 g_object_unref (simple);