nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Alexander Larsson <alexl@redhat.com>
19 */
20  
21 #include "config.h"
22  
23 #include <string.h>
24  
25 #include "gsimpleasyncresult.h"
26 #include "gasyncresult.h"
27 #include "gcancellable.h"
28 #include "gioscheduler.h"
29 #include <gio/gioerror.h>
30 #include "glibintl.h"
31  
32  
33 /**
34 * SECTION:gsimpleasyncresult
35 * @short_description: Simple asynchronous results implementation
36 * @include: gio/gio.h
37 * @see_also: #GAsyncResult, #GTask
38 *
39 * As of GLib 2.46, #GSimpleAsyncResult is deprecated in favor of
40 * #GTask, which provides a simpler API.
41 *
42 * #GSimpleAsyncResult implements #GAsyncResult.
43 *
44 * GSimpleAsyncResult handles #GAsyncReadyCallbacks, error
45 * reporting, operation cancellation and the final state of an operation,
46 * completely transparent to the application. Results can be returned
47 * as a pointer e.g. for functions that return data that is collected
48 * asynchronously, a boolean value for checking the success or failure
49 * of an operation, or a #gssize for operations which return the number
50 * of bytes modified by the operation; all of the simple return cases
51 * are covered.
52 *
53 * Most of the time, an application will not need to know of the details
54 * of this API; it is handled transparently, and any necessary operations
55 * are handled by #GAsyncResult's interface. However, if implementing a
56 * new GIO module, for writing language bindings, or for complex
57 * applications that need better control of how asynchronous operations
58 * are completed, it is important to understand this functionality.
59 *
60 * GSimpleAsyncResults are tagged with the calling function to ensure
61 * that asynchronous functions and their finishing functions are used
62 * together correctly.
63 *
64 * To create a new #GSimpleAsyncResult, call g_simple_async_result_new().
65 * If the result needs to be created for a #GError, use
66 * g_simple_async_result_new_from_error() or
67 * g_simple_async_result_new_take_error(). If a #GError is not available
68 * (e.g. the asynchronous operation's doesn't take a #GError argument),
69 * but the result still needs to be created for an error condition, use
70 * g_simple_async_result_new_error() (or g_simple_async_result_set_error_va()
71 * if your application or binding requires passing a variable argument list
72 * directly), and the error can then be propagated through the use of
73 * g_simple_async_result_propagate_error().
74 *
75 * An asynchronous operation can be made to ignore a cancellation event by
76 * calling g_simple_async_result_set_handle_cancellation() with a
77 * #GSimpleAsyncResult for the operation and %FALSE. This is useful for
78 * operations that are dangerous to cancel, such as close (which would
79 * cause a leak if cancelled before being run).
80 *
81 * GSimpleAsyncResult can integrate into GLib's event loop, #GMainLoop,
82 * or it can use #GThreads.
83 * g_simple_async_result_complete() will finish an I/O task directly
84 * from the point where it is called. g_simple_async_result_complete_in_idle()
85 * will finish it from an idle handler in the
86 * [thread-default main context][g-main-context-push-thread-default]
87 * . g_simple_async_result_run_in_thread() will run the
88 * job in a separate thread and then deliver the result to the
89 * thread-default main context.
90 *
91 * To set the results of an asynchronous function,
92 * g_simple_async_result_set_op_res_gpointer(),
93 * g_simple_async_result_set_op_res_gboolean(), and
94 * g_simple_async_result_set_op_res_gssize()
95 * are provided, setting the operation's result to a gpointer, gboolean, or
96 * gssize, respectively.
97 *
98 * Likewise, to get the result of an asynchronous function,
99 * g_simple_async_result_get_op_res_gpointer(),
100 * g_simple_async_result_get_op_res_gboolean(), and
101 * g_simple_async_result_get_op_res_gssize() are
102 * provided, getting the operation's result as a gpointer, gboolean, and
103 * gssize, respectively.
104 *
105 * For the details of the requirements implementations must respect, see
106 * #GAsyncResult. A typical implementation of an asynchronous operation
107 * using GSimpleAsyncResult looks something like this:
108 *
109 * |[<!-- language="C" -->
110 * static void
111 * baked_cb (Cake *cake,
112 * gpointer user_data)
113 * {
114 * // In this example, this callback is not given a reference to the cake,
115 * // so the GSimpleAsyncResult has to take a reference to it.
116 * GSimpleAsyncResult *result = user_data;
117 *
118 * if (cake == NULL)
119 * g_simple_async_result_set_error (result,
120 * BAKER_ERRORS,
121 * BAKER_ERROR_NO_FLOUR,
122 * "Go to the supermarket");
123 * else
124 * g_simple_async_result_set_op_res_gpointer (result,
125 * g_object_ref (cake),
126 * g_object_unref);
127 *
128 *
129 * // In this example, we assume that baked_cb is called as a callback from
130 * // the mainloop, so it's safe to complete the operation synchronously here.
131 * // If, however, _baker_prepare_cake () might call its callback without
132 * // first returning to the mainloop — inadvisable, but some APIs do so —
133 * // we would need to use g_simple_async_result_complete_in_idle().
134 * g_simple_async_result_complete (result);
135 * g_object_unref (result);
136 * }
137 *
138 * void
139 * baker_bake_cake_async (Baker *self,
140 * guint radius,
141 * GAsyncReadyCallback callback,
142 * gpointer user_data)
143 * {
144 * GSimpleAsyncResult *simple;
145 * Cake *cake;
146 *
147 * if (radius < 3)
148 * {
149 * g_simple_async_report_error_in_idle (G_OBJECT (self),
150 * callback,
151 * user_data,
152 * BAKER_ERRORS,
153 * BAKER_ERROR_TOO_SMALL,
154 * "%ucm radius cakes are silly",
155 * radius);
156 * return;
157 * }
158 *
159 * simple = g_simple_async_result_new (G_OBJECT (self),
160 * callback,
161 * user_data,
162 * baker_bake_cake_async);
163 * cake = _baker_get_cached_cake (self, radius);
164 *
165 * if (cake != NULL)
166 * {
167 * g_simple_async_result_set_op_res_gpointer (simple,
168 * g_object_ref (cake),
169 * g_object_unref);
170 * g_simple_async_result_complete_in_idle (simple);
171 * g_object_unref (simple);
172 * // Drop the reference returned by _baker_get_cached_cake();
173 * // the GSimpleAsyncResult has taken its own reference.
174 * g_object_unref (cake);
175 * return;
176 * }
177 *
178 * _baker_prepare_cake (self, radius, baked_cb, simple);
179 * }
180 *
181 * Cake *
182 * baker_bake_cake_finish (Baker *self,
183 * GAsyncResult *result,
184 * GError **error)
185 * {
186 * GSimpleAsyncResult *simple;
187 * Cake *cake;
188 *
189 * g_return_val_if_fail (g_simple_async_result_is_valid (result,
190 * G_OBJECT (self),
191 * baker_bake_cake_async),
192 * NULL);
193 *
194 * simple = (GSimpleAsyncResult *) result;
195 *
196 * if (g_simple_async_result_propagate_error (simple, error))
197 * return NULL;
198 *
199 * cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
200 * return g_object_ref (cake);
201 * }
202 * ]|
203 */
204  
205 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
206  
207 static void g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface);
208  
209 struct _GSimpleAsyncResult
210 {
211 GObject parent_instance;
212  
213 GObject *source_object;
214 GAsyncReadyCallback callback;
215 gpointer user_data;
216 GMainContext *context;
217 GError *error;
218 gboolean failed;
219 gboolean handle_cancellation;
220 GCancellable *check_cancellable;
221  
222 gpointer source_tag;
223  
224 union {
225 gpointer v_pointer;
226 gboolean v_boolean;
227 gssize v_ssize;
228 } op_res;
229  
230 GDestroyNotify destroy_op_res;
231 };
232  
233 struct _GSimpleAsyncResultClass
234 {
235 GObjectClass parent_class;
236 };
237  
238  
239 G_DEFINE_TYPE_WITH_CODE (GSimpleAsyncResult, g_simple_async_result, G_TYPE_OBJECT,
240 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
241 g_simple_async_result_async_result_iface_init))
242  
243 static void
244 clear_op_res (GSimpleAsyncResult *simple)
245 {
246 if (simple->destroy_op_res)
247 simple->destroy_op_res (simple->op_res.v_pointer);
248 simple->destroy_op_res = NULL;
249 simple->op_res.v_ssize = 0;
250 }
251  
252 static void
253 g_simple_async_result_finalize (GObject *object)
254 {
255 GSimpleAsyncResult *simple;
256  
257 simple = G_SIMPLE_ASYNC_RESULT (object);
258  
259 if (simple->source_object)
260 g_object_unref (simple->source_object);
261  
262 if (simple->check_cancellable)
263 g_object_unref (simple->check_cancellable);
264  
265 g_main_context_unref (simple->context);
266  
267 clear_op_res (simple);
268  
269 if (simple->error)
270 g_error_free (simple->error);
271  
272 G_OBJECT_CLASS (g_simple_async_result_parent_class)->finalize (object);
273 }
274  
275 static void
276 g_simple_async_result_class_init (GSimpleAsyncResultClass *klass)
277 {
278 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
279  
280 gobject_class->finalize = g_simple_async_result_finalize;
281 }
282  
283 static void
284 g_simple_async_result_init (GSimpleAsyncResult *simple)
285 {
286 simple->handle_cancellation = TRUE;
287  
288 simple->context = g_main_context_ref_thread_default ();
289 }
290  
291 /**
292 * g_simple_async_result_new:
293 * @source_object: (allow-none): a #GObject, or %NULL.
294 * @callback: (scope async): a #GAsyncReadyCallback.
295 * @user_data: (closure): user data passed to @callback.
296 * @source_tag: the asynchronous function.
297 *
298 * Creates a #GSimpleAsyncResult.
299 *
300 * The common convention is to create the #GSimpleAsyncResult in the
301 * function that starts the asynchronous operation and use that same
302 * function as the @source_tag.
303 *
304 * If your operation supports cancellation with #GCancellable (which it
305 * probably should) then you should provide the user's cancellable to
306 * g_simple_async_result_set_check_cancellable() immediately after
307 * this function returns.
308 *
309 * Returns: a #GSimpleAsyncResult.
310 *
311 * Deprecated: 2.46: Use g_task_new() instead.
312 **/
313 GSimpleAsyncResult *
314 g_simple_async_result_new (GObject *source_object,
315 GAsyncReadyCallback callback,
316 gpointer user_data,
317 gpointer source_tag)
318 {
319 GSimpleAsyncResult *simple;
320  
321 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
322  
323 simple = g_object_new (G_TYPE_SIMPLE_ASYNC_RESULT, NULL);
324 simple->callback = callback;
325 if (source_object)
326 simple->source_object = g_object_ref (source_object);
327 else
328 simple->source_object = NULL;
329 simple->user_data = user_data;
330 simple->source_tag = source_tag;
331  
332 return simple;
333 }
334  
335 /**
336 * g_simple_async_result_new_from_error:
337 * @source_object: (allow-none): a #GObject, or %NULL.
338 * @callback: (scope async): a #GAsyncReadyCallback.
339 * @user_data: (closure): user data passed to @callback.
340 * @error: a #GError
341 *
342 * Creates a #GSimpleAsyncResult from an error condition.
343 *
344 * Returns: a #GSimpleAsyncResult.
345 *
346 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
347 **/
348 GSimpleAsyncResult *
349 g_simple_async_result_new_from_error (GObject *source_object,
350 GAsyncReadyCallback callback,
351 gpointer user_data,
352 const GError *error)
353 {
354 GSimpleAsyncResult *simple;
355  
356 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
357  
358 simple = g_simple_async_result_new (source_object,
359 callback,
360 user_data, NULL);
361 g_simple_async_result_set_from_error (simple, error);
362  
363 return simple;
364 }
365  
366 /**
367 * g_simple_async_result_new_take_error: (skip)
368 * @source_object: (allow-none): a #GObject, or %NULL
369 * @callback: (scope async): a #GAsyncReadyCallback
370 * @user_data: (closure): user data passed to @callback
371 * @error: a #GError
372 *
373 * Creates a #GSimpleAsyncResult from an error condition, and takes over the
374 * caller's ownership of @error, so the caller does not need to free it anymore.
375 *
376 * Returns: a #GSimpleAsyncResult
377 *
378 * Since: 2.28
379 *
380 * Deprecated: 2.46: Use g_task_new() and g_task_return_error() instead.
381 **/
382 GSimpleAsyncResult *
383 g_simple_async_result_new_take_error (GObject *source_object,
384 GAsyncReadyCallback callback,
385 gpointer user_data,
386 GError *error)
387 {
388 GSimpleAsyncResult *simple;
389  
390 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
391  
392 simple = g_simple_async_result_new (source_object,
393 callback,
394 user_data, NULL);
395 g_simple_async_result_take_error (simple, error);
396  
397 return simple;
398 }
399  
400 /**
401 * g_simple_async_result_new_error:
402 * @source_object: (allow-none): a #GObject, or %NULL.
403 * @callback: (scope async): a #GAsyncReadyCallback.
404 * @user_data: (closure): user data passed to @callback.
405 * @domain: a #GQuark.
406 * @code: an error code.
407 * @format: a string with format characters.
408 * @...: a list of values to insert into @format.
409 *
410 * Creates a new #GSimpleAsyncResult with a set error.
411 *
412 * Returns: a #GSimpleAsyncResult.
413 *
414 * Deprecated: 2.46: Use g_task_new() and g_task_return_new_error() instead.
415 **/
416 GSimpleAsyncResult *
417 g_simple_async_result_new_error (GObject *source_object,
418 GAsyncReadyCallback callback,
419 gpointer user_data,
420 GQuark domain,
421 gint code,
422 const char *format,
423 ...)
424 {
425 GSimpleAsyncResult *simple;
426 va_list args;
427  
428 g_return_val_if_fail (!source_object || G_IS_OBJECT (source_object), NULL);
429 g_return_val_if_fail (domain != 0, NULL);
430 g_return_val_if_fail (format != NULL, NULL);
431  
432 simple = g_simple_async_result_new (source_object,
433 callback,
434 user_data, NULL);
435  
436 va_start (args, format);
437 g_simple_async_result_set_error_va (simple, domain, code, format, args);
438 va_end (args);
439  
440 return simple;
441 }
442  
443  
444 static gpointer
445 g_simple_async_result_get_user_data (GAsyncResult *res)
446 {
447 return G_SIMPLE_ASYNC_RESULT (res)->user_data;
448 }
449  
450 static GObject *
451 g_simple_async_result_get_source_object (GAsyncResult *res)
452 {
453 if (G_SIMPLE_ASYNC_RESULT (res)->source_object)
454 return g_object_ref (G_SIMPLE_ASYNC_RESULT (res)->source_object);
455 return NULL;
456 }
457  
458 static gboolean
459 g_simple_async_result_is_tagged (GAsyncResult *res,
460 gpointer source_tag)
461 {
462 return G_SIMPLE_ASYNC_RESULT (res)->source_tag == source_tag;
463 }
464  
465 static void
466 g_simple_async_result_async_result_iface_init (GAsyncResultIface *iface)
467 {
468 iface->get_user_data = g_simple_async_result_get_user_data;
469 iface->get_source_object = g_simple_async_result_get_source_object;
470 iface->is_tagged = g_simple_async_result_is_tagged;
471 }
472  
473 /**
474 * g_simple_async_result_set_handle_cancellation:
475 * @simple: a #GSimpleAsyncResult.
476 * @handle_cancellation: a #gboolean.
477 *
478 * Sets whether to handle cancellation within the asynchronous operation.
479 *
480 * This function has nothing to do with
481 * g_simple_async_result_set_check_cancellable(). It only refers to the
482 * #GCancellable passed to g_simple_async_result_run_in_thread().
483 *
484 * Deprecated: 2.46
485 **/
486 void
487 g_simple_async_result_set_handle_cancellation (GSimpleAsyncResult *simple,
488 gboolean handle_cancellation)
489 {
490 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
491 simple->handle_cancellation = handle_cancellation;
492 }
493  
494 /**
495 * g_simple_async_result_get_source_tag: (skip)
496 * @simple: a #GSimpleAsyncResult.
497 *
498 * Gets the source tag for the #GSimpleAsyncResult.
499 *
500 * Returns: a #gpointer to the source object for the #GSimpleAsyncResult.
501 *
502 * Deprecated: 2.46. Use #GTask and g_task_get_source_tag() instead.
503 **/
504 gpointer
505 g_simple_async_result_get_source_tag (GSimpleAsyncResult *simple)
506 {
507 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
508 return simple->source_tag;
509 }
510  
511 /**
512 * g_simple_async_result_propagate_error:
513 * @simple: a #GSimpleAsyncResult.
514 * @dest: (out): a location to propagate the error to.
515 *
516 * Propagates an error from within the simple asynchronous result to
517 * a given destination.
518 *
519 * If the #GCancellable given to a prior call to
520 * g_simple_async_result_set_check_cancellable() is cancelled then this
521 * function will return %TRUE with @dest set appropriately.
522 *
523 * Returns: %TRUE if the error was propagated to @dest. %FALSE otherwise.
524 *
525 * Deprecated: 2.46: Use #GTask instead.
526 **/
527 gboolean
528 g_simple_async_result_propagate_error (GSimpleAsyncResult *simple,
529 GError **dest)
530 {
531 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
532  
533 if (g_cancellable_set_error_if_cancelled (simple->check_cancellable, dest))
534 return TRUE;
535  
536 if (simple->failed)
537 {
538 g_propagate_error (dest, simple->error);
539 simple->error = NULL;
540 return TRUE;
541 }
542  
543 return FALSE;
544 }
545  
546 /**
547 * g_simple_async_result_set_op_res_gpointer: (skip)
548 * @simple: a #GSimpleAsyncResult.
549 * @op_res: a pointer result from an asynchronous function.
550 * @destroy_op_res: a #GDestroyNotify function.
551 *
552 * Sets the operation result within the asynchronous result to a pointer.
553 *
554 * Deprecated: 2.46: Use #GTask and g_task_return_pointer() instead.
555 **/
556 void
557 g_simple_async_result_set_op_res_gpointer (GSimpleAsyncResult *simple,
558 gpointer op_res,
559 GDestroyNotify destroy_op_res)
560 {
561 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
562  
563 clear_op_res (simple);
564 simple->op_res.v_pointer = op_res;
565 simple->destroy_op_res = destroy_op_res;
566 }
567  
568 /**
569 * g_simple_async_result_get_op_res_gpointer: (skip)
570 * @simple: a #GSimpleAsyncResult.
571 *
572 * Gets a pointer result as returned by the asynchronous function.
573 *
574 * Returns: a pointer from the result.
575 *
576 * Deprecated: 2.46: Use #GTask and g_task_propagate_pointer() instead.
577 **/
578 gpointer
579 g_simple_async_result_get_op_res_gpointer (GSimpleAsyncResult *simple)
580 {
581 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), NULL);
582 return simple->op_res.v_pointer;
583 }
584  
585 /**
586 * g_simple_async_result_set_op_res_gssize:
587 * @simple: a #GSimpleAsyncResult.
588 * @op_res: a #gssize.
589 *
590 * Sets the operation result within the asynchronous result to
591 * the given @op_res.
592 *
593 * Deprecated: 2.46: Use #GTask and g_task_return_int() instead.
594 **/
595 void
596 g_simple_async_result_set_op_res_gssize (GSimpleAsyncResult *simple,
597 gssize op_res)
598 {
599 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
600 clear_op_res (simple);
601 simple->op_res.v_ssize = op_res;
602 }
603  
604 /**
605 * g_simple_async_result_get_op_res_gssize:
606 * @simple: a #GSimpleAsyncResult.
607 *
608 * Gets a gssize from the asynchronous result.
609 *
610 * Returns: a gssize returned from the asynchronous function.
611 *
612 * Deprecated: 2.46: Use #GTask and g_task_propagate_int() instead.
613 **/
614 gssize
615 g_simple_async_result_get_op_res_gssize (GSimpleAsyncResult *simple)
616 {
617 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), 0);
618 return simple->op_res.v_ssize;
619 }
620  
621 /**
622 * g_simple_async_result_set_op_res_gboolean:
623 * @simple: a #GSimpleAsyncResult.
624 * @op_res: a #gboolean.
625 *
626 * Sets the operation result to a boolean within the asynchronous result.
627 *
628 * Deprecated: 2.46: Use #GTask and g_task_return_boolean() instead.
629 **/
630 void
631 g_simple_async_result_set_op_res_gboolean (GSimpleAsyncResult *simple,
632 gboolean op_res)
633 {
634 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
635 clear_op_res (simple);
636 simple->op_res.v_boolean = !!op_res;
637 }
638  
639 /**
640 * g_simple_async_result_get_op_res_gboolean:
641 * @simple: a #GSimpleAsyncResult.
642 *
643 * Gets the operation result boolean from within the asynchronous result.
644 *
645 * Returns: %TRUE if the operation's result was %TRUE, %FALSE
646 * if the operation's result was %FALSE.
647 *
648 * Deprecated: 2.46: Use #GTask and g_task_propagate_boolean() instead.
649 **/
650 gboolean
651 g_simple_async_result_get_op_res_gboolean (GSimpleAsyncResult *simple)
652 {
653 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple), FALSE);
654 return simple->op_res.v_boolean;
655 }
656  
657 /**
658 * g_simple_async_result_set_from_error:
659 * @simple: a #GSimpleAsyncResult.
660 * @error: #GError.
661 *
662 * Sets the result from a #GError.
663 *
664 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
665 **/
666 void
667 g_simple_async_result_set_from_error (GSimpleAsyncResult *simple,
668 const GError *error)
669 {
670 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
671 g_return_if_fail (error != NULL);
672  
673 if (simple->error)
674 g_error_free (simple->error);
675 simple->error = g_error_copy (error);
676 simple->failed = TRUE;
677 }
678  
679 /**
680 * g_simple_async_result_take_error: (skip)
681 * @simple: a #GSimpleAsyncResult
682 * @error: a #GError
683 *
684 * Sets the result from @error, and takes over the caller's ownership
685 * of @error, so the caller does not need to free it any more.
686 *
687 * Since: 2.28
688 *
689 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
690 **/
691 void
692 g_simple_async_result_take_error (GSimpleAsyncResult *simple,
693 GError *error)
694 {
695 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
696 g_return_if_fail (error != NULL);
697  
698 if (simple->error)
699 g_error_free (simple->error);
700 simple->error = error;
701 simple->failed = TRUE;
702 }
703  
704 /**
705 * g_simple_async_result_set_error_va: (skip)
706 * @simple: a #GSimpleAsyncResult.
707 * @domain: a #GQuark (usually #G_IO_ERROR).
708 * @code: an error code.
709 * @format: a formatted error reporting string.
710 * @args: va_list of arguments.
711 *
712 * Sets an error within the asynchronous result without a #GError.
713 * Unless writing a binding, see g_simple_async_result_set_error().
714 *
715 * Deprecated: 2.46: Use #GTask and g_task_return_error() instead.
716 **/
717 void
718 g_simple_async_result_set_error_va (GSimpleAsyncResult *simple,
719 GQuark domain,
720 gint code,
721 const char *format,
722 va_list args)
723 {
724 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
725 g_return_if_fail (domain != 0);
726 g_return_if_fail (format != NULL);
727  
728 if (simple->error)
729 g_error_free (simple->error);
730 simple->error = g_error_new_valist (domain, code, format, args);
731 simple->failed = TRUE;
732 }
733  
734 /**
735 * g_simple_async_result_set_error: (skip)
736 * @simple: a #GSimpleAsyncResult.
737 * @domain: a #GQuark (usually #G_IO_ERROR).
738 * @code: an error code.
739 * @format: a formatted error reporting string.
740 * @...: a list of variables to fill in @format.
741 *
742 * Sets an error within the asynchronous result without a #GError.
743 *
744 * Deprecated: 2.46: Use #GTask and g_task_return_new_error() instead.
745 **/
746 void
747 g_simple_async_result_set_error (GSimpleAsyncResult *simple,
748 GQuark domain,
749 gint code,
750 const char *format,
751 ...)
752 {
753 va_list args;
754  
755 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
756 g_return_if_fail (domain != 0);
757 g_return_if_fail (format != NULL);
758  
759 va_start (args, format);
760 g_simple_async_result_set_error_va (simple, domain, code, format, args);
761 va_end (args);
762 }
763  
764 /**
765 * g_simple_async_result_complete:
766 * @simple: a #GSimpleAsyncResult.
767 *
768 * Completes an asynchronous I/O job immediately. Must be called in
769 * the thread where the asynchronous result was to be delivered, as it
770 * invokes the callback directly. If you are in a different thread use
771 * g_simple_async_result_complete_in_idle().
772 *
773 * Calling this function takes a reference to @simple for as long as
774 * is needed to complete the call.
775 *
776 * Deprecated: 2.46: Use #GTask instead.
777 **/
778 void
779 g_simple_async_result_complete (GSimpleAsyncResult *simple)
780 {
781 #ifndef G_DISABLE_CHECKS
782 GSource *current_source;
783 GMainContext *current_context;
784 #endif
785  
786 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
787  
788 #ifndef G_DISABLE_CHECKS
789 current_source = g_main_current_source ();
790 if (current_source && !g_source_is_destroyed (current_source))
791 {
792 current_context = g_source_get_context (current_source);
793 if (simple->context != current_context)
794 g_warning ("g_simple_async_result_complete() called from wrong context!");
795 }
796 #endif
797  
798 if (simple->callback)
799 {
800 g_main_context_push_thread_default (simple->context);
801 simple->callback (simple->source_object,
802 G_ASYNC_RESULT (simple),
803 simple->user_data);
804 g_main_context_pop_thread_default (simple->context);
805 }
806 }
807  
808 static gboolean
809 complete_in_idle_cb (gpointer data)
810 {
811 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (data);
812  
813 g_simple_async_result_complete (simple);
814  
815 return FALSE;
816 }
817  
818 /**
819 * g_simple_async_result_complete_in_idle:
820 * @simple: a #GSimpleAsyncResult.
821 *
822 * Completes an asynchronous function in an idle handler in the
823 * [thread-default main context][g-main-context-push-thread-default]
824 * of the thread that @simple was initially created in
825 * (and re-pushes that context around the invocation of the callback).
826 *
827 * Calling this function takes a reference to @simple for as long as
828 * is needed to complete the call.
829 *
830 * Deprecated: 2.46: Use #GTask instead.
831 */
832 void
833 g_simple_async_result_complete_in_idle (GSimpleAsyncResult *simple)
834 {
835 GSource *source;
836  
837 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
838  
839 g_object_ref (simple);
840  
841 source = g_idle_source_new ();
842 g_source_set_priority (source, G_PRIORITY_DEFAULT);
843 g_source_set_callback (source, complete_in_idle_cb, simple, g_object_unref);
844 g_source_set_name (source, "[gio] complete_in_idle_cb");
845  
846 g_source_attach (source, simple->context);
847 g_source_unref (source);
848 }
849  
850 typedef struct {
851 GSimpleAsyncResult *simple;
852 GCancellable *cancellable;
853 GSimpleAsyncThreadFunc func;
854 } RunInThreadData;
855  
856  
857 static gboolean
858 complete_in_idle_cb_for_thread (gpointer _data)
859 {
860 RunInThreadData *data = _data;
861 GSimpleAsyncResult *simple;
862  
863 simple = data->simple;
864  
865 if (simple->handle_cancellation &&
866 g_cancellable_is_cancelled (data->cancellable))
867 g_simple_async_result_set_error (simple,
868 G_IO_ERROR,
869 G_IO_ERROR_CANCELLED,
870 "%s", _("Operation was cancelled"));
871  
872 g_simple_async_result_complete (simple);
873  
874 if (data->cancellable)
875 g_object_unref (data->cancellable);
876 g_object_unref (data->simple);
877 g_free (data);
878  
879 return FALSE;
880 }
881  
882 static gboolean
883 run_in_thread (GIOSchedulerJob *job,
884 GCancellable *c,
885 gpointer _data)
886 {
887 RunInThreadData *data = _data;
888 GSimpleAsyncResult *simple = data->simple;
889 GSource *source;
890  
891 if (simple->handle_cancellation &&
892 g_cancellable_is_cancelled (c))
893 g_simple_async_result_set_error (simple,
894 G_IO_ERROR,
895 G_IO_ERROR_CANCELLED,
896 "%s", _("Operation was cancelled"));
897 else
898 data->func (simple,
899 simple->source_object,
900 c);
901  
902 source = g_idle_source_new ();
903 g_source_set_priority (source, G_PRIORITY_DEFAULT);
904 g_source_set_callback (source, complete_in_idle_cb_for_thread, data, NULL);
905 g_source_set_name (source, "[gio] complete_in_idle_cb_for_thread");
906  
907 g_source_attach (source, simple->context);
908 g_source_unref (source);
909  
910 return FALSE;
911 }
912  
913 /**
914 * g_simple_async_result_run_in_thread: (skip)
915 * @simple: a #GSimpleAsyncResult.
916 * @func: a #GSimpleAsyncThreadFunc.
917 * @io_priority: the io priority of the request.
918 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
919 *
920 * Runs the asynchronous job in a separate thread and then calls
921 * g_simple_async_result_complete_in_idle() on @simple to return
922 * the result to the appropriate main loop.
923 *
924 * Calling this function takes a reference to @simple for as long as
925 * is needed to run the job and report its completion.
926 *
927 * Deprecated: 2.46: Use #GTask and g_task_run_in_thread() instead.
928 */
929 void
930 g_simple_async_result_run_in_thread (GSimpleAsyncResult *simple,
931 GSimpleAsyncThreadFunc func,
932 int io_priority,
933 GCancellable *cancellable)
934 {
935 RunInThreadData *data;
936  
937 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
938 g_return_if_fail (func != NULL);
939  
940 data = g_new (RunInThreadData, 1);
941 data->func = func;
942 data->simple = g_object_ref (simple);
943 data->cancellable = cancellable;
944 if (cancellable)
945 g_object_ref (cancellable);
946 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
947 g_io_scheduler_push_job (run_in_thread, data, NULL, io_priority, cancellable);
948 G_GNUC_END_IGNORE_DEPRECATIONS;
949 }
950  
951 /**
952 * g_simple_async_result_is_valid:
953 * @result: the #GAsyncResult passed to the _finish function.
954 * @source: (allow-none): the #GObject passed to the _finish function.
955 * @source_tag: (allow-none): the asynchronous function.
956 *
957 * Ensures that the data passed to the _finish function of an async
958 * operation is consistent. Three checks are performed.
959 *
960 * First, @result is checked to ensure that it is really a
961 * #GSimpleAsyncResult. Second, @source is checked to ensure that it
962 * matches the source object of @result. Third, @source_tag is
963 * checked to ensure that it is equal to the @source_tag argument given
964 * to g_simple_async_result_new() (which, by convention, is a pointer
965 * to the _async function corresponding to the _finish function from
966 * which this function is called). (Alternatively, if either
967 * @source_tag or @result's source tag is %NULL, then the source tag
968 * check is skipped.)
969 *
970 * Returns: #TRUE if all checks passed or #FALSE if any failed.
971 *
972 * Since: 2.20
973 *
974 * Deprecated: 2.46: Use #GTask and g_task_is_valid() instead.
975 **/
976 gboolean
977 g_simple_async_result_is_valid (GAsyncResult *result,
978 GObject *source,
979 gpointer source_tag)
980 {
981 GSimpleAsyncResult *simple;
982 GObject *cmp_source;
983 gpointer result_source_tag;
984  
985 if (!G_IS_SIMPLE_ASYNC_RESULT (result))
986 return FALSE;
987 simple = (GSimpleAsyncResult *)result;
988  
989 cmp_source = g_async_result_get_source_object (result);
990 if (cmp_source != source)
991 {
992 if (cmp_source != NULL)
993 g_object_unref (cmp_source);
994 return FALSE;
995 }
996 if (cmp_source != NULL)
997 g_object_unref (cmp_source);
998  
999 result_source_tag = g_simple_async_result_get_source_tag (simple);
1000 return source_tag == NULL || result_source_tag == NULL ||
1001 source_tag == result_source_tag;
1002 }
1003  
1004 /**
1005 * g_simple_async_report_error_in_idle: (skip)
1006 * @object: (allow-none): a #GObject, or %NULL.
1007 * @callback: a #GAsyncReadyCallback.
1008 * @user_data: user data passed to @callback.
1009 * @domain: a #GQuark containing the error domain (usually #G_IO_ERROR).
1010 * @code: a specific error code.
1011 * @format: a formatted error reporting string.
1012 * @...: a list of variables to fill in @format.
1013 *
1014 * Reports an error in an asynchronous function in an idle function by
1015 * directly setting the contents of the #GAsyncResult with the given error
1016 * information.
1017 *
1018 * Deprecated: 2.46: Use g_task_report_error().
1019 **/
1020 void
1021 g_simple_async_report_error_in_idle (GObject *object,
1022 GAsyncReadyCallback callback,
1023 gpointer user_data,
1024 GQuark domain,
1025 gint code,
1026 const char *format,
1027 ...)
1028 {
1029 GSimpleAsyncResult *simple;
1030 va_list args;
1031  
1032 g_return_if_fail (!object || G_IS_OBJECT (object));
1033 g_return_if_fail (domain != 0);
1034 g_return_if_fail (format != NULL);
1035  
1036 simple = g_simple_async_result_new (object,
1037 callback,
1038 user_data, NULL);
1039  
1040 va_start (args, format);
1041 g_simple_async_result_set_error_va (simple, domain, code, format, args);
1042 va_end (args);
1043 g_simple_async_result_complete_in_idle (simple);
1044 g_object_unref (simple);
1045 }
1046  
1047 /**
1048 * g_simple_async_report_gerror_in_idle:
1049 * @object: (allow-none): a #GObject, or %NULL
1050 * @callback: (scope async): a #GAsyncReadyCallback.
1051 * @user_data: (closure): user data passed to @callback.
1052 * @error: the #GError to report
1053 *
1054 * Reports an error in an idle function. Similar to
1055 * g_simple_async_report_error_in_idle(), but takes a #GError rather
1056 * than building a new one.
1057 *
1058 * Deprecated: 2.46: Use g_task_report_error().
1059 **/
1060 void
1061 g_simple_async_report_gerror_in_idle (GObject *object,
1062 GAsyncReadyCallback callback,
1063 gpointer user_data,
1064 const GError *error)
1065 {
1066 GSimpleAsyncResult *simple;
1067  
1068 g_return_if_fail (!object || G_IS_OBJECT (object));
1069 g_return_if_fail (error != NULL);
1070  
1071 simple = g_simple_async_result_new_from_error (object,
1072 callback,
1073 user_data,
1074 error);
1075 g_simple_async_result_complete_in_idle (simple);
1076 g_object_unref (simple);
1077 }
1078  
1079 /**
1080 * g_simple_async_report_take_gerror_in_idle: (skip)
1081 * @object: (allow-none): a #GObject, or %NULL
1082 * @callback: a #GAsyncReadyCallback.
1083 * @user_data: user data passed to @callback.
1084 * @error: the #GError to report
1085 *
1086 * Reports an error in an idle function. Similar to
1087 * g_simple_async_report_gerror_in_idle(), but takes over the caller's
1088 * ownership of @error, so the caller does not have to free it any more.
1089 *
1090 * Since: 2.28
1091 *
1092 * Deprecated: 2.46: Use g_task_report_error().
1093 **/
1094 void
1095 g_simple_async_report_take_gerror_in_idle (GObject *object,
1096 GAsyncReadyCallback callback,
1097 gpointer user_data,
1098 GError *error)
1099 {
1100 GSimpleAsyncResult *simple;
1101  
1102 g_return_if_fail (!object || G_IS_OBJECT (object));
1103 g_return_if_fail (error != NULL);
1104  
1105 simple = g_simple_async_result_new_take_error (object,
1106 callback,
1107 user_data,
1108 error);
1109 g_simple_async_result_complete_in_idle (simple);
1110 g_object_unref (simple);
1111 }
1112  
1113 /**
1114 * g_simple_async_result_set_check_cancellable:
1115 * @simple: a #GSimpleAsyncResult
1116 * @check_cancellable: (allow-none): a #GCancellable to check, or %NULL to unset
1117 *
1118 * Sets a #GCancellable to check before dispatching results.
1119 *
1120 * This function has one very specific purpose: the provided cancellable
1121 * is checked at the time of g_simple_async_result_propagate_error() If
1122 * it is cancelled, these functions will return an "Operation was
1123 * cancelled" error (%G_IO_ERROR_CANCELLED).
1124 *
1125 * Implementors of cancellable asynchronous functions should use this in
1126 * order to provide a guarantee to their callers that cancelling an
1127 * async operation will reliably result in an error being returned for
1128 * that operation (even if a positive result for the operation has
1129 * already been sent as an idle to the main context to be dispatched).
1130 *
1131 * The checking described above is done regardless of any call to the
1132 * unrelated g_simple_async_result_set_handle_cancellation() function.
1133 *
1134 * Since: 2.32
1135 *
1136 * Deprecated: 2.46: Use #GTask instead.
1137 **/
1138 void
1139 g_simple_async_result_set_check_cancellable (GSimpleAsyncResult *simple,
1140 GCancellable *check_cancellable)
1141 {
1142 g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (simple));
1143 g_return_if_fail (check_cancellable == NULL || G_IS_CANCELLABLE (check_cancellable));
1144  
1145 g_clear_object (&simple->check_cancellable);
1146 if (check_cancellable)
1147 simple->check_cancellable = g_object_ref (check_cancellable);
1148 }
1149  
1150 G_GNUC_END_IGNORE_DEPRECATIONS