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
57 * handled by #GAsyncResult's interface. However, if implementing a new GIO
58 * module, for writing language bindings, or for complex applications that
59 * need better control of how asynchronous operations are completed, it is
60 * important to understand this functionality.
62 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new(). If
63 * the result needs to be created for a #GError, use
64 * g_simple_async_result_new_from_error(). If a #GError is not available (e.g.
65 * the asynchronous operation's doesn't take a #GError argument), but the result
66 * still needs to be created for an error condition, use
67 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
68 * if your application or binding requires passing a variable argument list
69 * directly), and the error can then be propegated through the use of
70 * g_simple_async_result_propagate_error().
72 * An asynchronous operation can be made to ignore a cancellation event by calling
73 * g_simple_async_result_set_handle_cancellation() with a #GSimpleAsyncResult
74 * for the operation and %FALSE.
76 * GSimpleAsyncResult can integrate into GLib's Main Event Loop <!-- TODO: Crosslink -->,
77 * or it can use #GThread<!-- -->s if available. g_simple_async_result_complete()
78 * will finish an I/O task directly within the main event loop.
79 * g_simple_async_result_complete_in_idle() will integrate the I/O task into the
80 * main event loop as an idle function and g_simple_async_result_run_in_thread()
81 * will run the job in a separate thread.
83 * To set the results of an asynchronous function,
84 * g_simple_async_result_set_op_res_gpointer(),
85 * g_simple_async_result_set_op_res_gboolean(), and
86 * g_simple_async_result_set_op_res_gssize()
87 * are provided, setting the operation's result to a gpointer, gboolean, or
88 * gssize, respectively.
90 * Likewise, to get the result of an asynchronous function,
91 * g_simple_async_result_get_op_res_gpointer(),
92 * g_simple_async_result_get_op_res_gboolean(), and
93 * g_simple_async_result_get_op_res_gssize() are
94 * provided, getting the operation's result as a gpointer, gboolean, and
95 * gssize, respectively.
98 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
100 struct _GSimpleAsyncResult
102 GObject parent_instance;
104 GObject *source_object;
105 GAsyncReadyCallback callback;
109 gboolean handle_cancellation;
119 GDestroyNotify destroy_op_res;
122 struct _GSimpleAsyncResultClass
124 GObjectClass parent_class;
128 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
129 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
130 g_simple_async_result_async_result_iface_init))
133 g_simple_async_result_finalize (GObject *object)
135 GSimpleAsyncResult *simple;
137 simple = G_SIMPLE_ASYNC_RESULT (object);
139 if (simple->source_object)
140 g_object_unref (simple->source_object);
142 if (simple->destroy_op_res)
143 simple->destroy_op_res (simple->op_res.v_pointer);
146 g_error_free (simple->error);
148 if (G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize)
149 (*G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize) (object);
153 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
155 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
157 gobject_class->finalize = g_simple_async_result_finalize;
161 g_simple_async_result_init (GSimpleAsyncResult *simple)
163 simple->handle_cancellation = TRUE;
167 * g_simple_async_result_new:
168 * @source_object: a #GObject.
169 * @callback: a #GAsyncReadyCallback.
170 * @user_data: user data passed to @callback.
173 * Creates a #GSimpleAsyncResult.
175 * Returns: a #GSimpleAsyncResult.
178 g_simple_async_result_new (GObject *source_object,
179 GAsyncReadyCallback callback,
183 GSimpleAsyncResult *simple;
185 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
187 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
188 simple->callback = callback;
189 simple->source_object = g_object_ref (source_object);
190 simple->user_data = user_data;
191 simple->source_tag = source_tag;
197 * g_simple_async_result_new_from_error:
198 * @source_object: a #GObject.
199 * @callback: a #GAsyncReadyCallback.
200 * @user_data: user data passed to @callback.
201 * @error: a #GError location.
203 * Creates a #GSimpleAsyncResult from an error condition.
205 * Returns: a #GSimpleAsyncResult.
208 g_simple_async_result_new_from_error (GObject *source_object,
209 GAsyncReadyCallback callback,
213 GSimpleAsyncResult *simple;
215 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
217 simple = g_simple_async_result_new (source_object,
220 g_simple_async_result_set_from_error (simple, error);
226 * g_simple_async_result_new_error:
227 * @source_object: a #GObject.
228 * @callback: a #GAsyncReadyCallback.
229 * @user_data: user data passed to @callback.
230 * @domain: a #GQuark.
231 * @code: an error code.
232 * @format: a string with format characters.
233 * @Varargs: a list of values to insert into @format.
235 * Creates a new #GSimpleAsyncResult with a set error.
237 * Returns: a #GSimpleAsyncResult.
240 g_simple_async_result_new_error (GObject *source_object,
241 GAsyncReadyCallback callback,
248 GSimpleAsyncResult *simple;
251 g_return_val_if_fail (G_IS_OBJECT (source_object), NULL);
252 g_return_val_if_fail (domain != 0, NULL);
253 g_return_val_if_fail (format != NULL, NULL);
255 simple = g_simple_async_result_new (source_object,
259 va_start (args, format);
260 g_simple_async_result_set_error_va (simple, domain, code, format, args);
268 g_simple_async_result_get_user_data (GAsyncResult *res)
270 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
274 g_simple_async_result_get_source_object (GAsyncResult *res)
276 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
277 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
282 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
284 iface->get_user_data = g_simple_async_result_get_user_data;
285 iface->get_source_object = g_simple_async_result_get_source_object;
289 * g_simple_async_result_set_handle_cancellation:
290 * @simple: a #GSimpleAsyncResult.
291 * @handle_cancellation: a #gboolean.
293 * Sets whether to handle cancellation within the asynchronous operation.
297 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
298 gboolean handle_cancellation)
300 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
301 simple->handle_cancellation = handle_cancellation;
305 * g_simple_async_result_get_source_tag:
306 * @simple: a #GSimpleAsyncResult.
308 * Gets the source tag for the #GSimpleAsyncResult.
310 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
313 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
315 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
316 return simple->source_tag;
320 * g_simple_async_result_propagate_error:
321 * @simple: a #GSimpleAsyncResult.
322 * @dest: a location to propegate the error to.
324 * Propegates an error from within the simple asynchronous result to
325 * a given destination.
327 * Returns: %TRUE if the error was propegated to @dest. %FALSE otherwise.
330 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
333 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
337 g_propagate_error (dest, simple->error);
338 simple->error = NULL;
345 * g_simple_async_result_set_op_res_gpointer:
346 * @simple: a #GSimpleAsyncResult.
347 * @op_res: a pointer result from an asynchronous function.
348 * @destroy_op_res: a #GDestroyNotify function.
350 * Sets the operation result within the asynchronous result to a pointer.
353 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
355 GDestroyNotify destroy_op_res)
357 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
359 simple->op_res.v_pointer = op_res;
360 simple->destroy_op_res = destroy_op_res;
364 * g_simple_async_result_get_op_res_gpointer:
365 * @simple: a #GSimpleAsyncResult.
367 * Gets a pointer result as returned by the asynchronous function.
369 * Returns: a pointer from the result.
372 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
374 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
375 return simple->op_res.v_pointer;
379 * g_simple_async_result_set_op_res_gssize:
380 * @simple: a #GSimpleAsyncResult.
381 * @op_res: a #gssize.
383 * Sets the operation result within the asynchronous result to the given @op_res.
386 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
389 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
390 simple->op_res.v_ssize = op_res;
394 * g_simple_async_result_get_op_res_gssize:
395 * @simple: a #GSimpleAsyncResult.
397 * Gets a gssize from the asynchronous result.
399 * Returns: a gssize returned from the asynchronous function.
402 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
404 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
405 return simple->op_res.v_ssize;
409 * g_simple_async_result_set_op_res_gboolean:
410 * @simple: a #GSimpleAsyncResult.
411 * @op_res: a #gboolean.
413 * Sets the operation result to a boolean within the asynchronous result.
417 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
420 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
421 simple->op_res.v_boolean = !!op_res;
425 * g_simple_async_result_get_op_res_gboolean:
426 * @simple: a #GSimpleAsyncResult.
428 * Gets the operation result boolean from within the asynchronous result.
430 * Returns: %TRUE if the operation's result was %TRUE, %FALSE if the operation's
434 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
436 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
437 return simple->op_res.v_boolean;
441 * g_simple_async_result_set_from_error:
442 * @simple: a #GSimpleAsyncResult.
445 * Sets the result from a #GError.
448 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
451 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
452 g_return_if_fail (error != NULL);
454 simple->error = g_error_copy (error);
455 simple->failed = TRUE;
459 _g_error_new_valist (GQuark domain,
467 message = g_strdup_vprintf (format, args);
469 error = g_error_new_literal (domain, code, message);
476 * g_simple_async_result_set_error_va:
477 * @simple: a #GSimpleAsyncResult.
478 * @domain: a #GQuark (usually #G_IO_ERROR).
479 * @code: an error code.
480 * @format: a formatted error reporting string.
481 * @args: va_list of arguments.
483 * Sets an error within the asynchronous result without a #GError. Unless
484 * writing a binding, see g_simple_async_result_set_error().
487 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
493 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
494 g_return_if_fail (domain != 0);
495 g_return_if_fail (format != NULL);
497 simple->error = _g_error_new_valist (domain, code, format, args);
498 simple->failed = TRUE;
502 * g_simple_async_result_set_error:
503 * @simple: a #GSimpleAsyncResult.
504 * @domain: a #GQuark (usually #G_IO_ERROR).
505 * @code: an error code.
506 * @format: a formatted error reporting string.
507 * @Varargs: a list of variables to fill in @format.
509 * Sets an error within the asynchronous result without a #GError.
513 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
521 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
522 g_return_if_fail (domain != 0);
523 g_return_if_fail (format != NULL);
525 va_start (args, format);
526 g_simple_async_result_set_error_va (simple, domain, code, format, args);
531 * g_simple_async_result_complete:
532 * @simple: a #GSimpleAsyncResult.
534 * Completes an asynchronous I/O job.
538 g_simple_async_result_complete (GSimpleAsyncResult *simple)
540 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
542 if (simple->callback)
543 simple->callback (simple->source_object,
544 G_ASYNC_RESULT (simple),
549 complete_in_idle_cb (gpointer data)
551 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
553 g_simple_async_result_complete (simple);
559 * g_simple_async_result_complete_in_idle:
560 * @simple: a #GSimpleAsyncResult.
562 * Completes an asynchronous function in the main event loop using
567 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
572 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
574 g_object_ref (simple);
576 source = g_idle_source_new ();
577 g_source_set_priority (source, G_PRIORITY_DEFAULT);
578 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
580 id = g_source_attach (source, NULL);
581 g_source_unref (source);
585 GSimpleAsyncResult *simple;
586 GSimpleAsyncThreadFunc func;
590 run_in_thread (GIOJob *job,
594 RunInThreadData *data = _data;
595 GSimpleAsyncResult *simple = data->simple;
597 if (simple->handle_cancellation &&
598 g_cancellable_is_cancelled (c))
600 g_simple_async_result_set_error (simple,
602 G_IO_ERROR_CANCELLED,
603 _("Operation was cancelled"));
608 simple->source_object,
612 g_simple_async_result_complete_in_idle (data->simple);
613 g_object_unref (data->simple);
618 * g_simple_async_result_run_in_thread:
619 * @simple: a #GSimpleAsyncResult.
620 * @func: a #GSimpleAsyncThreadFunc.
621 * @io_priority: the io priority of the request.
622 * @cancellable: optional #GCancellable object, %NULL to ignore.
624 * 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:
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 * @Varargs: a list of variables to fill in @format.
654 * Reports an error in an idle function.
658 g_simple_async_report_error_in_idle (GObject *object,
659 GAsyncReadyCallback callback,
666 GSimpleAsyncResult *simple;
669 g_return_if_fail (G_IS_OBJECT (object));
670 g_return_if_fail (domain != 0);
671 g_return_if_fail (format != NULL);
673 simple = g_simple_async_result_new (object,
677 va_start (args, format);
678 g_simple_async_result_set_error_va (simple, domain, code, format, args);
680 g_simple_async_result_complete_in_idle (simple);
681 g_object_unref (simple);