eb47118531d4654567fd0bdd2c37bb48ab276c87
[platform/upstream/glib.git] / gio / gsimpleasyncresult.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include "gsimpleasyncresult.h"
35 #include "gasyncresult.h"
36 #include "gcancellable.h"
37 #include "gioscheduler.h"
38 #include <gio/gioerror.h>
39 #include "glibintl.h"
40
41
42 /**
43  * SECTION:gsimpleasyncresult
44  * @short_description: Simple asynchronous results implementation
45  * @include: gio/gio.h
46  * @see_also: #GAsyncResult
47  *
48  * Implements #GAsyncResult for simple cases. Most of the time, this
49  * will be all an application needs, and will be used transparently.
50  * Because of this, #GSimpleAsyncResult is used throughout GIO for
51  * handling asynchronous functions.
52  *
53  * GSimpleAsyncResult handles #GAsyncReadyCallback<!-- -->s, error
54  * reporting, operation cancellation and the final state of an operation,
55  * completely transparent to the application. Results can be returned
56  * as a pointer e.g. for functions that return data that is collected
57  * asynchronously, a boolean value for checking the success or failure
58  * of an operation, or a #gssize for operations which return the number
59  * of bytes modified by the operation; all of the simple return cases
60  * are covered.
61  *
62  * Most of the time, an application will not need to know of the details
63  * of this API; it is handled transparently, and any necessary operations
64  * are handled by #GAsyncResult's interface. However, if implementing a
65  * new GIO module, for writing language bindings, or for complex
66  * applications that need better control of how asynchronous operations
67  * are completed, it is important to understand this functionality.
68  *
69  * GSimpleAsyncResults are tagged with the calling function to ensure
70  * that asynchronous functions and their finishing functions are used
71  * together correctly.
72  *
73  * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
74  * If the result needs to be created for a #GError, use
75  * g_simple_async_result_new_from_error(). If a #GError is not available
76  * (e.g. the asynchronous operation's doesn't take a #GError argument),
77  * but the result still needs to be created for an error condition, use
78  * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
79  * if your application or binding requires passing a variable argument list
80  * directly), and the error can then be propagated through the use of
81  * g_simple_async_result_propagate_error().
82  *
83  * An asynchronous operation can be made to ignore a cancellation event by
84  * calling g_simple_async_result_set_handle_cancellation() with a
85  * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
86  * operations that are dangerous to cancel, such as close (which would
87  * cause a leak if cancelled before being run).
88  *
89  * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
90  * or it can use #GThread<!-- -->s if available.
91  * g_simple_async_result_complete() will finish an I/O task directly
92  * from the point where it is called. g_simple_async_result_complete_in_idle()
93  * will finish it from an idle handler in the <link
94  * linkend="g-main-context-push-thread-default">thread-default main
95  * context</link>. g_simple_async_result_run_in_thread() will run the
96  * job in a separate thread and then deliver the result to the
97  * thread-default main context.
98  *
99  * To set the results of an asynchronous function,
100  * g_simple_async_result_set_op_res_gpointer(),
101  * g_simple_async_result_set_op_res_gboolean(), and
102  * g_simple_async_result_set_op_res_gssize()
103  * are provided, setting the operation's result to a gpointer, gboolean, or
104  * gssize, respectively.
105  *
106  * Likewise, to get the result of an asynchronous function,
107  * g_simple_async_result_get_op_res_gpointer(),
108  * g_simple_async_result_get_op_res_gboolean(), and
109  * g_simple_async_result_get_op_res_gssize() are
110  * provided, getting the operation's result as a gpointer, gboolean, and
111  * gssize, respectively.
112  **/
113
114 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface       *iface);
115
116 struct _GSimpleAsyncResult
117 {
118   GObject parent_instance;
119
120   GObject *source_object;
121   GAsyncReadyCallback callback;
122   gpointer user_data;
123   GMainContext *context;
124   GError *error;
125   gboolean failed;
126   gboolean handle_cancellation;
127
128   gpointer source_tag;
129
130   union {
131     gpointer v_pointer;
132     gboolean v_boolean;
133     gssize   v_ssize;
134   } op_res;
135
136   GDestroyNotify destroy_op_res;
137 };
138
139 struct _GSimpleAsyncResultClass
140 {
141   GObjectClass parent_class;
142 };
143
144
145 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
146                          G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
147                                                 g_simple_async_result_async_result_iface_init))
148
149 static void
150 clear_op_res (GSimpleAsyncResult *simple)
151 {
152   if (simple->destroy_op_res)
153     simple->destroy_op_res (simple->op_res.v_pointer);
154   simple->destroy_op_res = NULL;
155   simple->op_res.v_ssize = 0;
156 }
157
158 static void
159 g_simple_async_result_finalize (GObject *object)
160 {
161   GSimpleAsyncResult *simple;
162
163   simple = G_SIMPLE_ASYNC_RESULT (object);
164
165   if (simple->source_object)
166     g_object_unref (simple->source_object);
167
168   if (simple->context)
169     g_main_context_unref (simple->context);
170
171   clear_op_res (simple);
172
173   if (simple->error)
174     g_error_free (simple->error);
175
176   G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
177 }
178
179 static void
180 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
181 {
182   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183  
184   gobject_class->finalize = g_simple_async_result_finalize;
185 }
186
187 static void
188 g_simple_async_result_init (GSimpleAsyncResult *simple)
189 {
190   simple->handle_cancellation = TRUE;
191
192   simple->context = g_main_context_get_thread_default ();
193   if (simple->context)
194     g_main_context_ref (simple->context);
195 }
196
197 /**
198  * g_simple_async_result_new:
199  * @source_object: a #GObject the asynchronous function was called with,
200  * or %NULL.
201  * @callback: a #GAsyncReadyCallback.
202  * @user_data: user data passed to @callback.
203  * @source_tag: the asynchronous function.
204  *
205  * Creates a #GSimpleAsyncResult.
206  *
207  * Returns: a #GSimpleAsyncResult.
208  **/
209 GSimpleAsyncResult *
210 g_simple_async_result_new (GObject             *source_object,
211                            GAsyncReadyCallback  callback,
212                            gpointer             user_data,
213                            gpointer             source_tag)
214 {
215   GSimpleAsyncResult *simple;
216
217   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
218
219   simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
220   simple->callback = callback;
221   if (source_object)
222     simple->source_object = g_object_ref (source_object);
223   else
224     simple->source_object = NULL;
225   simple->user_data = user_data;
226   simple->source_tag = source_tag;
227  
228   return simple;
229 }
230
231 /**
232  * g_simple_async_result_new_from_error:
233  * @source_object: a #GObject, or %NULL.
234  * @callback: a #GAsyncReadyCallback.
235  * @user_data: user data passed to @callback.
236  * @error: a #GError location.
237  *
238  * Creates a #GSimpleAsyncResult from an error condition.
239  *
240  * Returns: a #GSimpleAsyncResult.
241  **/
242 GSimpleAsyncResult *
243 g_simple_async_result_new_from_error (GObject             *source_object,
244                                       GAsyncReadyCallback  callback,
245                                       gpointer             user_data,
246                                       GError              *error)
247 {
248   GSimpleAsyncResult *simple;
249
250   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
251
252   simple = g_simple_async_result_new (source_object,
253                                       callback,
254                                       user_data, NULL);
255   g_simple_async_result_set_from_error (simple, error);
256
257   return simple;
258 }
259
260 /**
261  * g_simple_async_result_new_error:
262  * @source_object: a #GObject, or %NULL.
263  * @callback: a #GAsyncReadyCallback.
264  * @user_data: user data passed to @callback.
265  * @domain: a #GQuark.
266  * @code: an error code.
267  * @format: a string with format characters.
268  * @...: a list of values to insert into @format.
269  *
270  * Creates a new #GSimpleAsyncResult with a set error.
271  *
272  * Returns: a #GSimpleAsyncResult.
273  **/
274 GSimpleAsyncResult *
275 g_simple_async_result_new_error (GObject             *source_object,
276                                  GAsyncReadyCallback  callback,
277                                  gpointer             user_data,
278                                  GQuark               domain,
279                                  gint                 code,
280                                  const char          *format,
281                                  ...)
282 {
283   GSimpleAsyncResult *simple;
284   va_list args;
285  
286   g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
287   g_return_val_if_fail (domain != 0, NULL);
288   g_return_val_if_fail (format != NULL, NULL);
289
290   simple = g_simple_async_result_new (source_object,
291                                       callback,
292                                       user_data, NULL);
293
294   va_start (args, format);
295   g_simple_async_result_set_error_va (simple, domain, code, format, args);
296   va_end (args);
297  
298   return simple;
299 }
300
301
302 static gpointer
303 g_simple_async_result_get_user_data (GAsyncResult *res)
304 {
305   return G_SIMPLE_ASYNC_RESULT (res)->user_data;
306 }
307
308 static GObject *
309 g_simple_async_result_get_source_object (GAsyncResult *res)
310 {
311   if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
312     return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
313   return NULL;
314 }
315
316 static void
317 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
318 {
319   iface->get_user_data = g_simple_async_result_get_user_data;
320   iface->get_source_object = g_simple_async_result_get_source_object;
321 }
322
323 /**
324  * g_simple_async_result_set_handle_cancellation:
325  * @simple: a #GSimpleAsyncResult.
326  * @handle_cancellation: a #gboolean.
327  *
328  * Sets whether to handle cancellation within the asynchronous operation.
329  *
330  **/
331 void
332 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
333                                                gboolean            handle_cancellation)
334 {
335   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
336   simple->handle_cancellation = handle_cancellation;
337 }
338
339 /**
340  * g_simple_async_result_get_source_tag:
341  * @simple: a #GSimpleAsyncResult.
342  *
343  * Gets the source tag for the #GSimpleAsyncResult.
344  *
345  * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
346  **/
347 gpointer
348 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
349 {
350   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
351   return simple->source_tag;
352 }
353
354 /**
355  * g_simple_async_result_propagate_error:
356  * @simple: a #GSimpleAsyncResult.
357  * @dest: a location to propegate the error to.
358  *
359  * Propagates an error from within the simple asynchronous result to
360  * a given destination.
361  *
362  * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
363  **/
364 gboolean
365 g_simple_async_result_propagate_error (GSimpleAsyncResult  *simple,
366                                        GError             **dest)
367 {
368   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
369
370   if (simple->failed)
371     {
372       g_propagate_error (dest, simple->error);
373       simple->error = NULL;
374       return TRUE;
375     }
376
377   return FALSE;
378 }
379
380 /**
381  * g_simple_async_result_set_op_res_gpointer:
382  * @simple: a #GSimpleAsyncResult.
383  * @op_res: a pointer result from an asynchronous function.
384  * @destroy_op_res: a #GDestroyNotify function.
385  *
386  * Sets the operation result within the asynchronous result to a pointer.
387  **/
388 void
389 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
390                                            gpointer            op_res,
391                                            GDestroyNotify      destroy_op_res)
392 {
393   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
394
395   clear_op_res (simple);
396   simple->op_res.v_pointer = op_res;
397   simple->destroy_op_res = destroy_op_res;
398 }
399
400 /**
401  * g_simple_async_result_get_op_res_gpointer:
402  * @simple: a #GSimpleAsyncResult.
403  *
404  * Gets a pointer result as returned by the asynchronous function.
405  *
406  * Returns: a pointer from the result.
407  **/
408 gpointer
409 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
410 {
411   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
412   return simple->op_res.v_pointer;
413 }
414
415 /**
416  * g_simple_async_result_set_op_res_gssize:
417  * @simple: a #GSimpleAsyncResult.
418  * @op_res: a #gssize.
419  *
420  * Sets the operation result within the asynchronous result to
421  * the given @op_res.
422  **/
423 void
424 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
425                                          gssize              op_res)
426 {
427   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
428   clear_op_res (simple);
429   simple->op_res.v_ssize = op_res;
430 }
431
432 /**
433  * g_simple_async_result_get_op_res_gssize:
434  * @simple: a #GSimpleAsyncResult.
435  *
436  * Gets a gssize from the asynchronous result.
437  *
438  * Returns: a gssize returned from the asynchronous function.
439  **/
440 gssize
441 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
442 {
443   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
444   return simple->op_res.v_ssize;
445 }
446
447 /**
448  * g_simple_async_result_set_op_res_gboolean:
449  * @simple: a #GSimpleAsyncResult.
450  * @op_res: a #gboolean.
451  *
452  * Sets the operation result to a boolean within the asynchronous result.
453  **/
454 void
455 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
456                                            gboolean            op_res)
457 {
458   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
459   clear_op_res (simple);
460   simple->op_res.v_boolean = !!op_res;
461 }
462
463 /**
464  * g_simple_async_result_get_op_res_gboolean:
465  * @simple: a #GSimpleAsyncResult.
466  *
467  * Gets the operation result boolean from within the asynchronous result.
468  *
469  * Returns: %TRUE if the operation's result was %TRUE, %FALSE
470  *     if the operation's result was %FALSE.
471  **/
472 gboolean
473 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
474 {
475   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
476   return simple->op_res.v_boolean;
477 }
478
479 /**
480  * g_simple_async_result_set_from_error:
481  * @simple: a #GSimpleAsyncResult.
482  * @error: #GError.
483  *
484  * Sets the result from a #GError.
485  **/
486 void
487 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
488                                       const GError       *error)
489 {
490   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
491   g_return_if_fail (error != NULL);
492
493   if (simple->error)
494     g_error_free (simple->error);
495   simple->error = g_error_copy (error);
496   simple->failed = TRUE;
497 }
498
499 /**
500  * g_simple_async_result_set_error_va:
501  * @simple: a #GSimpleAsyncResult.
502  * @domain: a #GQuark (usually #G_IO_ERROR).
503  * @code: an error code.
504  * @format: a formatted error reporting string.
505  * @args: va_list of arguments.
506  *
507  * Sets an error within the asynchronous result without a #GError.
508  * Unless writing a binding, see g_simple_async_result_set_error().
509  **/
510 void
511 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
512                                     GQuark              domain,
513                                     gint                code,
514                                     const char         *format,
515                                     va_list             args)
516 {
517   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
518   g_return_if_fail (domain != 0);
519   g_return_if_fail (format != NULL);
520
521   if (simple->error)
522     g_error_free (simple->error);
523   simple->error = g_error_new_valist (domain, code, format, args);
524   simple->failed = TRUE;
525 }
526
527 /**
528  * g_simple_async_result_set_error:
529  * @simple: a #GSimpleAsyncResult.
530  * @domain: a #GQuark (usually #G_IO_ERROR).
531  * @code: an error code.
532  * @format: a formatted error reporting string.
533  * @...: a list of variables to fill in @format.
534  *
535  * Sets an error within the asynchronous result without a #GError.
536  **/
537 void
538 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
539                                  GQuark              domain,
540                                  gint                code,
541                                  const char         *format,
542                                  ...)
543 {
544   va_list args;
545
546   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
547   g_return_if_fail (domain != 0);
548   g_return_if_fail (format != NULL);
549
550   va_start (args, format);
551   g_simple_async_result_set_error_va (simple, domain, code, format, args);
552   va_end (args);
553 }
554
555 /**
556  * g_simple_async_result_complete:
557  * @simple: a #GSimpleAsyncResult.
558  *
559  * Completes an asynchronous I/O job immediately. Must be called in
560  * the thread where the asynchronous result was to be delivered, as it
561  * invokes the callback directly. If you are in a different thread use
562  * g_simple_async_result_complete_in_idle().
563  *
564  * Calling this function takes a reference to @simple for as long as
565  * is needed to complete the call.
566  **/
567 void
568 g_simple_async_result_complete (GSimpleAsyncResult *simple)
569 {
570 #ifndef G_DISABLE_CHECKS
571   GSource *current_source;
572   GMainContext *current_context;
573 #endif
574
575   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
576
577 #ifndef G_DISABLE_CHECKS
578   current_source = g_main_current_source ();
579   if (current_source && !g_source_is_destroyed (current_source))
580     {
581       current_context = g_source_get_context (current_source);
582       if (current_context == g_main_context_default ())
583         current_context = NULL;
584       if (simple->context != current_context)
585         g_warning ("g_simple_async_result_complete() called from wrong context!");
586     }
587 #endif
588
589   if (simple->callback)
590     simple->callback (simple->source_object,
591                       G_ASYNC_RESULT (simple),
592                       simple->user_data);
593 }
594
595 static gboolean
596 complete_in_idle_cb (gpointer data)
597 {
598   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
599
600   g_simple_async_result_complete (simple);
601
602   return FALSE;
603 }
604
605 /**
606  * g_simple_async_result_complete_in_idle:
607  * @simple: a #GSimpleAsyncResult.
608  *
609  * Completes an asynchronous function in an idle handler in the <link
610  * linkend="g-main-context-push-thread-default">thread-default main
611  * loop</link> of the thread that @simple was initially created in.
612  *
613  * Calling this function takes a reference to @simple for as long as
614  * is needed to complete the call.
615  */
616 void
617 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
618 {
619   GSource *source;
620
621   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
622
623   g_object_ref (simple);
624  
625   source = g_idle_source_new ();
626   g_source_set_priority (source, G_PRIORITY_DEFAULT);
627   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
628
629   g_source_attach (source, simple->context);
630   g_source_unref (source);
631 }
632
633 typedef struct {
634   GSimpleAsyncResult *simple;
635   GCancellable *cancellable;
636   GSimpleAsyncThreadFunc func;
637 } RunInThreadData;
638
639
640 static gboolean
641 complete_in_idle_cb_for_thread (gpointer _data)
642 {
643   RunInThreadData *data = _data;
644   GSimpleAsyncResult *simple;
645
646   simple = data->simple;
647  
648   if (simple->handle_cancellation &&
649       g_cancellable_is_cancelled (data->cancellable))
650     g_simple_async_result_set_error (simple,
651                                      G_IO_ERROR,
652                                      G_IO_ERROR_CANCELLED,
653                                      "%s", _("Operation was cancelled"));
654  
655   g_simple_async_result_complete (simple);
656
657   if (data->cancellable)
658     g_object_unref (data->cancellable);
659   g_object_unref (data->simple);
660   g_free (data);
661  
662   return FALSE;
663 }
664
665 static gboolean
666 run_in_thread (GIOSchedulerJob *job,
667                GCancellable    *c,
668                gpointer         _data)
669 {
670   RunInThreadData *data = _data;
671   GSimpleAsyncResult *simple = data->simple;
672   GSource *source;
673  
674   if (simple->handle_cancellation &&
675       g_cancellable_is_cancelled (c))
676     g_simple_async_result_set_error (simple,
677                                      G_IO_ERROR,
678                                      G_IO_ERROR_CANCELLED,
679                                      "%s", _("Operation was cancelled"));
680   else
681     data->func (simple,
682                 simple->source_object,
683                 c);
684
685   source = g_idle_source_new ();
686   g_source_set_priority (source, G_PRIORITY_DEFAULT);
687   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
688
689   g_source_attach (source, simple->context);
690   g_source_unref (source);
691
692   return FALSE;
693 }
694
695 /**
696  * g_simple_async_result_run_in_thread:
697  * @simple: a #GSimpleAsyncResult.
698  * @func: a #GSimpleAsyncThreadFunc.
699  * @io_priority: the io priority of the request.
700  * @cancellable: optional #GCancellable object, %NULL to ignore.
701  *
702  * Runs the asynchronous job in a separate thread and then calls
703  * g_simple_async_result_complete_in_idle() on @simple to return
704  * the result to the appropriate main loop.
705  *
706  * Calling this function takes a reference to @simple for as long as
707  * is needed to run the job and report its completion.
708  */
709 void
710 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
711                                      GSimpleAsyncThreadFunc  func,
712                                      int                     io_priority,
713                                      GCancellable           *cancellable)
714 {
715   RunInThreadData *data;
716
717   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
718   g_return_if_fail (func != NULL);
719
720   data = g_new (RunInThreadData, 1);
721   data->func = func;
722   data->simple = g_object_ref (simple);
723   data->cancellable = cancellable;
724   if (cancellable)
725     g_object_ref (cancellable);
726   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
727 }
728
729 /**
730  * g_simple_async_result_is_valid:
731  * @result: the #GAsyncResult passed to the _finish function.
732  * @source: the #GObject passed to the _finish function.
733  * @source_tag: the asynchronous function.
734  *
735  * Ensures that the data passed to the _finish function of an async
736  * operation is consistent.  Three checks are performed.
737  *
738  * First, @result is checked to ensure that it is really a
739  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
740  * matches the source object of @result.  Third, @source_tag is
741  * checked to ensure that it is either %NULL (as it is when the result was
742  * created by g_simple_async_report_error_in_idle() or
743  * g_simple_async_report_gerror_in_idle()) or equal to the
744  * @source_tag argument given to g_simple_async_result_new() (which, by
745  * convention, is a pointer to the _async function corresponding to the
746  * _finish function from which this function is called).
747  *
748  * Returns: #TRUE if all checks passed or #FALSE if any failed.
749  **/
750 gboolean
751 g_simple_async_result_is_valid (GAsyncResult *result,
752                                 GObject      *source,
753                                 gpointer      source_tag)
754 {
755   GSimpleAsyncResult *simple;
756   GObject *cmp_source;
757
758   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
759     return FALSE;
760   simple = (GSimpleAsyncResult *)result;
761
762   cmp_source = g_async_result_get_source_object (result);
763   if (cmp_source != source)
764     {
765       if (cmp_source != NULL)
766         g_object_unref (cmp_source);
767       return FALSE;
768     }
769   if (cmp_source != NULL)
770     g_object_unref (cmp_source);
771
772   return source_tag == NULL ||
773          source_tag == g_simple_async_result_get_source_tag (simple);
774 }
775
776 /**
777  * g_simple_async_report_error_in_idle:
778  * @object: a #GObject.
779  * @callback: a #GAsyncReadyCallback.
780  * @user_data: user data passed to @callback.
781  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
782  * @code: a specific error code.
783  * @format: a formatted error reporting string.
784  * @...: a list of variables to fill in @format.
785  *
786  * Reports an error in an asynchronous function in an idle function by
787  * directly setting the contents of the #GAsyncResult with the given error
788  * information.
789  **/
790 void
791 g_simple_async_report_error_in_idle (GObject             *object,
792                                      GAsyncReadyCallback  callback,
793                                      gpointer             user_data,
794                                      GQuark               domain,
795                                      gint                 code,
796                                      const char          *format,
797                                      ...)
798 {
799   GSimpleAsyncResult *simple;
800   va_list args;
801  
802   g_return_if_fail (G_IS_OBJECT (object));
803   g_return_if_fail (domain != 0);
804   g_return_if_fail (format != NULL);
805
806   simple = g_simple_async_result_new (object,
807                                       callback,
808                                       user_data, NULL);
809
810   va_start (args, format);
811   g_simple_async_result_set_error_va (simple, domain, code, format, args);
812   va_end (args);
813   g_simple_async_result_complete_in_idle (simple);
814   g_object_unref (simple);
815 }
816
817 /**
818  * g_simple_async_report_gerror_in_idle:
819  * @object: a #GObject.
820  * @callback: a #GAsyncReadyCallback.
821  * @user_data: user data passed to @callback.
822  * @error: the #GError to report
823  *
824  * Reports an error in an idle function. Similar to
825  * g_simple_async_report_error_in_idle(), but takes a #GError rather
826  * than building a new one.
827  **/
828 void
829 g_simple_async_report_gerror_in_idle (GObject *object,
830                                       GAsyncReadyCallback callback,
831                                       gpointer user_data,
832                                       GError *error)
833 {
834   GSimpleAsyncResult *simple;
835  
836   g_return_if_fail (G_IS_OBJECT (object));
837   g_return_if_fail (error != NULL);
838
839   simple = g_simple_async_result_new_from_error (object,
840                                                  callback,
841                                                  user_data,
842                                                  error);
843   g_simple_async_result_complete_in_idle (simple);
844   g_object_unref (simple);
845 }