_simple_async_result_is_valid: allow tag to be NULL
[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 void
565 g_simple_async_result_complete (GSimpleAsyncResult *simple)
566 {
567 #ifndef G_DISABLE_CHECKS
568   GSource *current_source;
569   GMainContext *current_context;
570 #endif
571
572   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
573
574 #ifndef G_DISABLE_CHECKS
575   current_source = g_main_current_source ();
576   if (current_source && !g_source_is_destroyed (current_source))
577     {
578       current_context = g_source_get_context (current_source);
579       if (current_context == g_main_context_default ())
580         current_context = NULL;
581       if (simple->context != current_context)
582         g_warning ("g_simple_async_result_complete() called from wrong context!");
583     }
584 #endif
585
586   if (simple->callback)
587     simple->callback (simple->source_object,
588                       G_ASYNC_RESULT (simple),
589                       simple->user_data);
590 }
591
592 static gboolean
593 complete_in_idle_cb (gpointer data)
594 {
595   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
596
597   g_simple_async_result_complete (simple);
598
599   return FALSE;
600 }
601
602 /**
603  * g_simple_async_result_complete_in_idle:
604  * @simple: a #GSimpleAsyncResult.
605  *
606  * Completes an asynchronous function in an idle handler in the <link
607  * linkend="g-main-context-push-thread-default">thread-default main
608  * loop</link> of the thread that @simple was initially created in.
609  *
610  * Calling this function takes a reference to @simple for as long as
611  * is needed to complete the call.
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  * Calling this function takes a reference to @simple for as long as
704  * is needed to run the job and report its completion.
705  */
706 void
707 g_simple_async_result_run_in_thread (GSimpleAsyncResult     *simple,
708                                      GSimpleAsyncThreadFunc  func,
709                                      int                     io_priority,
710                                      GCancellable           *cancellable)
711 {
712   RunInThreadData *data;
713
714   g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
715   g_return_if_fail (func != NULL);
716
717   data = g_new (RunInThreadData, 1);
718   data->func = func;
719   data->simple = g_object_ref (simple);
720   data->cancellable = cancellable;
721   if (cancellable)
722     g_object_ref (cancellable);
723   g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
724 }
725
726 /**
727  * g_simple_async_result_is_valid:
728  * @result: the #GAsyncResult passed to the _finish function.
729  * @source: the #GObject passed to the _finish function.
730  * @source_tag: the asynchronous function.
731  *
732  * Ensures that the data passed to the _finish function of an async
733  * operation is consistent.  Three checks are performed.
734  *
735  * First, @result is checked to ensure that it is really a
736  * #GSimpleAsyncResult.  Second, @source is checked to ensure that it
737  * matches the source object of @result.  Third, @source_tag is
738  * checked to ensure that it is either %NULL (as it is when the result was
739  * created by g_simple_async_report_error_in_idle() or
740  * g_simple_async_report_gerror_in_idle()) or equal to the
741  * @source_tag argument given to g_simple_async_result_new() (which, by
742  * convention, is a pointer to the _async function corresponding to the
743  * _finish function from which this function is called).
744  *
745  * Returns: #TRUE if all checks passed or #FALSE if any failed.
746  **/
747 gboolean
748 g_simple_async_result_is_valid (GAsyncResult *result,
749                                 GObject      *source,
750                                 gpointer      source_tag)
751 {
752   GSimpleAsyncResult *simple;
753   GObject *cmp_source;
754
755   if (!G_IS_SIMPLE_ASYNC_RESULT (result))
756     return FALSE;
757   simple = (GSimpleAsyncResult *)result;
758
759   cmp_source = g_async_result_get_source_object (result);
760   if (cmp_source != source)
761     {
762       if (cmp_source != NULL)
763         g_object_unref (cmp_source);
764       return FALSE;
765     }
766   if (cmp_source != NULL)
767     g_object_unref (cmp_source);
768
769   return source_tag == NULL ||
770          source_tag == g_simple_async_result_get_source_tag (simple);
771 }
772
773 /**
774  * g_simple_async_report_error_in_idle:
775  * @object: a #GObject.
776  * @callback: a #GAsyncReadyCallback.
777  * @user_data: user data passed to @callback.
778  * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
779  * @code: a specific error code.
780  * @format: a formatted error reporting string.
781  * @...: a list of variables to fill in @format.
782  *
783  * Reports an error in an asynchronous function in an idle function by
784  * directly setting the contents of the #GAsyncResult with the given error
785  * information.
786  **/
787 void
788 g_simple_async_report_error_in_idle (GObject             *object,
789                                      GAsyncReadyCallback  callback,
790                                      gpointer             user_data,
791                                      GQuark               domain,
792                                      gint                 code,
793                                      const char          *format,
794                                      ...)
795 {
796   GSimpleAsyncResult *simple;
797   va_list args;
798  
799   g_return_if_fail (G_IS_OBJECT (object));
800   g_return_if_fail (domain != 0);
801   g_return_if_fail (format != NULL);
802
803   simple = g_simple_async_result_new (object,
804                                       callback,
805                                       user_data, NULL);
806
807   va_start (args, format);
808   g_simple_async_result_set_error_va (simple, domain, code, format, args);
809   va_end (args);
810   g_simple_async_result_complete_in_idle (simple);
811   g_object_unref (simple);
812 }
813
814 /**
815  * g_simple_async_report_gerror_in_idle:
816  * @object: a #GObject.
817  * @callback: a #GAsyncReadyCallback.
818  * @user_data: user data passed to @callback.
819  * @error: the #GError to report
820  *
821  * Reports an error in an idle function. Similar to
822  * g_simple_async_report_error_in_idle(), but takes a #GError rather
823  * than building a new one.
824  **/
825 void
826 g_simple_async_report_gerror_in_idle (GObject *object,
827                                       GAsyncReadyCallback callback,
828                                       gpointer user_data,
829                                       GError *error)
830 {
831   GSimpleAsyncResult *simple;
832  
833   g_return_if_fail (G_IS_OBJECT (object));
834   g_return_if_fail (error != NULL);
835
836   simple = g_simple_async_result_new_from_error (object,
837                                                  callback,
838                                                  user_data,
839                                                  error);
840   g_simple_async_result_complete_in_idle (simple);
841   g_object_unref (simple);
842 }