Merge remote branch 'gvdb/master'
[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 propagated 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 propagated 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                                       const 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 && !g_source_is_destroyed (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 #endif
586
587   if (simple->callback)
588     simple->callback (simple->source_object,
589                       G_ASYNC_RESULT (simple),
590                       simple->user_data);
591 }
592
593 static gboolean
594 complete_in_idle_cb (gpointer data)
595 {
596   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
597
598   g_simple_async_result_complete (simple);
599
600   return FALSE;
601 }
602
603 /**
604  * g_simple_async_result_complete_in_idle:
605  * @simple: a #GSimpleAsyncResult.
606  *
607  * Completes an asynchronous function in an idle handler in the <link
608  * linkend="g-main-context-push-thread-default">thread-default main
609  * loop</link> of the thread that @simple was initially created in.
610  *
611  * Calling this function takes a reference to @simple for as long as
612  * is needed to complete the call.
613  */
614 void
615 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
616 {
617   GSource *source;
618
619   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
620
621   g_object_ref (simple);
622  
623   source = g_idle_source_new ();
624   g_source_set_priority (source, G_PRIORITY_DEFAULT);
625   g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
626
627   g_source_attach (source, simple->context);
628   g_source_unref (source);
629 }
630
631 typedef struct {
632   GSimpleAsyncResult *simple;
633   GCancellable *cancellable;
634   GSimpleAsyncThreadFunc func;
635 } RunInThreadData;
636
637
638 static gboolean
639 complete_in_idle_cb_for_thread (gpointer _data)
640 {
641   RunInThreadData *data = _data;
642   GSimpleAsyncResult *simple;
643
644   simple = data->simple;
645  
646   if (simple->handle_cancellation &&
647       g_cancellable_is_cancelled (data->cancellable))
648     g_simple_async_result_set_error (simple,
649                                      G_IO_ERROR,
650                                      G_IO_ERROR_CANCELLED,
651                                      "%s", _("Operation was cancelled"));
652  
653   g_simple_async_result_complete (simple);
654
655   if (data->cancellable)
656     g_object_unref (data->cancellable);
657   g_object_unref (data->simple);
658   g_free (data);
659  
660   return FALSE;
661 }
662
663 static gboolean
664 run_in_thread (GIOSchedulerJob *job,
665                GCancellable    *c,
666                gpointer         _data)
667 {
668   RunInThreadData *data = _data;
669   GSimpleAsyncResult *simple = data->simple;
670   GSource *source;
671  
672   if (simple->handle_cancellation &&
673       g_cancellable_is_cancelled (c))
674     g_simple_async_result_set_error (simple,
675                                      G_IO_ERROR,
676                                      G_IO_ERROR_CANCELLED,
677                                      "%s", _("Operation was cancelled"));
678   else
679     data->func (simple,
680                 simple->source_object,
681                 c);
682
683   source = g_idle_source_new ();
684   g_source_set_priority (source, G_PRIORITY_DEFAULT);
685   g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
686
687   g_source_attach (source, simple->context);
688   g_source_unref (source);
689
690   return FALSE;
691 }
692
693 /**
694  * g_simple_async_result_run_in_thread:
695  * @simple: a #GSimpleAsyncResult.
696  * @func: a #GSimpleAsyncThreadFunc.
697  * @io_priority: the io priority of the request.
698  * @cancellable: optional #GCancellable object, %NULL to ignore.
699  *
700  * Runs the asynchronous job in a separate thread and then calls
701  * g_simple_async_result_complete_in_idle() on @simple to return
702  * the result to the appropriate main loop.
703  *
704  * Calling this function takes a reference to @simple for as long as
705  * is needed to run the job and report its completion.
706  */
707 void
708 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
709                                      GSimpleAsyncThreadFunc  func,
710                                      int                     io_priority,
711                                      GCancellable           *cancellable)
712 {
713   RunInThreadData *data;
714
715   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
716   g_return_if_fail (func != NULL);
717
718   data = g_new (RunInThreadData, 1);
719   data->func = func;
720   data->simple = g_object_ref (simple);
721   data->cancellable = cancellable;
722   if (cancellable)
723     g_object_ref (cancellable);
724   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
725 }
726
727 /**
728  * g_simple_async_result_is_valid:
729  * @result: the #GAsyncResult passed to the _finish function.
730  * @source: the #GObject passed to the _finish function.
731  * @source_tag: the asynchronous function.
732  *
733  * Ensures that the data passed to the _finish function of an async
734  * operation is consistent.  Three checks are performed.
735  *
736  * First, @result is checked to ensure that it is really a
737  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
738  * matches the source object of @result.  Third, @source_tag is
739  * checked to ensure that it is equal to the source_tag argument given
740  * to g_simple_async_result_new() (which, by convention, is a pointer
741  * to the _async function corresponding to the _finish function from
742  * which this function is called).
743  *
744  * Returns: #TRUE if all checks passed or #FALSE if any failed.
745  **/
746 gboolean
747 g_simple_async_result_is_valid (GAsyncResult *result,
748                                 GObject      *source,
749                                 gpointer      source_tag)
750 {
751   GSimpleAsyncResult *simple;
752   GObject *cmp_source;
753
754   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
755     return FALSE;
756   simple = (GSimpleAsyncResult *)result;
757
758   cmp_source = g_async_result_get_source_object (result);
759   if (cmp_source != source)
760     {
761       g_object_unref (cmp_source);
762       return FALSE;
763     }
764   g_object_unref (cmp_source);
765
766   return source_tag == g_simple_async_result_get_source_tag (simple);
767 }
768
769 /**
770  * g_simple_async_report_error_in_idle:
771  * @object: a #GObject.
772  * @callback: a #GAsyncReadyCallback.
773  * @user_data: user data passed to @callback.
774  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
775  * @code: a specific error code.
776  * @format: a formatted error reporting string.
777  * @...: a list of variables to fill in @format.
778  *
779  * Reports an error in an asynchronous function in an idle function by
780  * directly setting the contents of the #GAsyncResult with the given error
781  * information.
782  **/
783 void
784 g_simple_async_report_error_in_idle (GObject             *object,
785                                      GAsyncReadyCallback  callback,
786                                      gpointer             user_data,
787                                      GQuark               domain,
788                                      gint                 code,
789                                      const char          *format,
790                                      ...)
791 {
792   GSimpleAsyncResult *simple;
793   va_list args;
794  
795   g_return_if_fail (G_IS_OBJECT (object));
796   g_return_if_fail (domain != 0);
797   g_return_if_fail (format != NULL);
798
799   simple = g_simple_async_result_new (object,
800                                       callback,
801                                       user_data, NULL);
802
803   va_start (args, format);
804   g_simple_async_result_set_error_va (simple, domain, code, format, args);
805   va_end (args);
806   g_simple_async_result_complete_in_idle (simple);
807   g_object_unref (simple);
808 }
809
810 /**
811  * g_simple_async_report_gerror_in_idle:
812  * @object: a #GObject.
813  * @callback: a #GAsyncReadyCallback.
814  * @user_data: user data passed to @callback.
815  * @error: the #GError to report
816  *
817  * Reports an error in an idle function. Similar to
818  * g_simple_async_report_error_in_idle(), but takes a #GError rather
819  * than building a new one.
820  **/
821 void
822 g_simple_async_report_gerror_in_idle (GObject *object,
823                                       GAsyncReadyCallback callback,
824                                       gpointer user_data,
825                                       GError *error)
826 {
827   GSimpleAsyncResult *simple;
828  
829   g_return_if_fail (G_IS_OBJECT (object));
830   g_return_if_fail (error != NULL);
831
832   simple = g_simple_async_result_new_from_error (object,
833                                                  callback,
834                                                  user_data,
835                                                  error);
836   g_simple_async_result_complete_in_idle (simple);
837   g_object_unref (simple);
838 }
839
840 #define __G_SIMPLE_ASYNC_RESULT_C__
841 #include "gioaliasdef.c"