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