1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright 2011-2018 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.1 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
19 #include "config.h"
20 #include "gio_trace.h"
21
22 #include "gtask.h"
23
24 #include "gasyncresult.h"
25 #include "gcancellable.h"
26 #include "glib-private.h"
27
28 #include "glibintl.h"
29
30 /**
31 * SECTION:gtask
32 * @short_description: Cancellable synchronous or asynchronous task
33 * and result
34 * @include: gio/gio.h
35 * @see_also: #GAsyncResult
36 *
37 * A #GTask represents and manages a cancellable "task".
38 *
39 * ## Asynchronous operations
40 *
41 * The most common usage of #GTask is as a #GAsyncResult, to
42 * manage data during an asynchronous operation. You call
43 * g_task_new() in the "start" method, followed by
44 * g_task_set_task_data() and the like if you need to keep some
45 * additional data associated with the task, and then pass the
46 * task object around through your asynchronous operation.
47 * Eventually, you will call a method such as
48 * g_task_return_pointer() or g_task_return_error(), which will
49 * save the value you give it and then invoke the task's callback
50 * function in the
51 * [thread-default main context][g-main-context-push-thread-default]
52 * where it was created (waiting until the next iteration of the main
53 * loop first, if necessary). The caller will pass the #GTask back to
54 * the operation's finish function (as a #GAsyncResult), and you can
55 * use g_task_propagate_pointer() or the like to extract the
56 * return value.
57 *
58 * Here is an example for using GTask as a GAsyncResult:
59 * |[<!-- language="C" -->
60 * typedef struct {
61 * CakeFrostingType frosting;
62 * char *message;
63 * } DecorationData;
64 *
65 * static void
66 * decoration_data_free (DecorationData *decoration)
67 * {
68 * g_free (decoration->message);
69 * g_slice_free (DecorationData, decoration);
70 * }
71 *
72 * static void
73 * baked_cb (Cake *cake,
74 * gpointer user_data)
75 * {
76 * GTask *task = user_data;
77 * DecorationData *decoration = g_task_get_task_data (task);
78 * GError *error = NULL;
79 *
80 * if (cake == NULL)
81 * {
82 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,
83 * "Go to the supermarket");
84 * g_object_unref (task);
85 * return;
86 * }
87 *
88 * if (!cake_decorate (cake, decoration->frosting, decoration->message, &error))
89 * {
90 * g_object_unref (cake);
91 * // g_task_return_error() takes ownership of error
92 * g_task_return_error (task, error);
93 * g_object_unref (task);
94 * return;
95 * }
96 *
97 * g_task_return_pointer (task, cake, g_object_unref);
98 * g_object_unref (task);
99 * }
100 *
101 * void
102 * baker_bake_cake_async (Baker *self,
103 * guint radius,
104 * CakeFlavor flavor,
105 * CakeFrostingType frosting,
106 * const char *message,
107 * GCancellable *cancellable,
108 * GAsyncReadyCallback callback,
109 * gpointer user_data)
110 * {
111 * GTask *task;
112 * DecorationData *decoration;
113 * Cake *cake;
114 *
115 * task = g_task_new (self, cancellable, callback, user_data);
116 * if (radius < 3)
117 * {
118 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_TOO_SMALL,
119 * "%ucm radius cakes are silly",
120 * radius);
121 * g_object_unref (task);
122 * return;
123 * }
124 *
125 * cake = _baker_get_cached_cake (self, radius, flavor, frosting, message);
126 * if (cake != NULL)
127 * {
128 * // _baker_get_cached_cake() returns a reffed cake
129 * g_task_return_pointer (task, cake, g_object_unref);
130 * g_object_unref (task);
131 * return;
132 * }
133 *
134 * decoration = g_slice_new (DecorationData);
135 * decoration->frosting = frosting;
136 * decoration->message = g_strdup (message);
137 * g_task_set_task_data (task, decoration, (GDestroyNotify) decoration_data_free);
138 *
139 * _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);
140 * }
141 *
142 * Cake *
143 * baker_bake_cake_finish (Baker *self,
144 * GAsyncResult *result,
145 * GError **error)
146 * {
147 * g_return_val_if_fail (g_task_is_valid (result, self), NULL);
148 *
149 * return g_task_propagate_pointer (G_TASK (result), error);
150 * }
151 * ]|
152 *
153 * ## Chained asynchronous operations
154 *
155 * #GTask also tries to simplify asynchronous operations that
156 * internally chain together several smaller asynchronous
157 * operations. g_task_get_cancellable(), g_task_get_context(),
158 * and g_task_get_priority() allow you to get back the task's
159 * #GCancellable, #GMainContext, and [I/O priority][io-priority]
160 * when starting a new subtask, so you don't have to keep track
161 * of them yourself. g_task_attach_source() simplifies the case
162 * of waiting for a source to fire (automatically using the correct
163 * #GMainContext and priority).
164 *
165 * Here is an example for chained asynchronous operations:
166 * |[<!-- language="C" -->
167 * typedef struct {
168 * Cake *cake;
169 * CakeFrostingType frosting;
170 * char *message;
171 * } BakingData;
172 *
173 * static void
174 * decoration_data_free (BakingData *bd)
175 * {
176 * if (bd->cake)
177 * g_object_unref (bd->cake);
178 * g_free (bd->message);
179 * g_slice_free (BakingData, bd);
180 * }
181 *
182 * static void
183 * decorated_cb (Cake *cake,
184 * GAsyncResult *result,
185 * gpointer user_data)
186 * {
187 * GTask *task = user_data;
188 * GError *error = NULL;
189 *
190 * if (!cake_decorate_finish (cake, result, &error))
191 * {
192 * g_object_unref (cake);
193 * g_task_return_error (task, error);
194 * g_object_unref (task);
195 * return;
196 * }
197 *
198 * // baking_data_free() will drop its ref on the cake, so we have to
199 * // take another here to give to the caller.
200 * g_task_return_pointer (task, g_object_ref (cake), g_object_unref);
201 * g_object_unref (task);
202 * }
203 *
204 * static gboolean
205 * decorator_ready (gpointer user_data)
206 * {
207 * GTask *task = user_data;
208 * BakingData *bd = g_task_get_task_data (task);
209 *
210 * cake_decorate_async (bd->cake, bd->frosting, bd->message,
211 * g_task_get_cancellable (task),
212 * decorated_cb, task);
213 *
214 * return G_SOURCE_REMOVE;
215 * }
216 *
217 * static void
218 * baked_cb (Cake *cake,
219 * gpointer user_data)
220 * {
221 * GTask *task = user_data;
222 * BakingData *bd = g_task_get_task_data (task);
223 * GError *error = NULL;
224 *
225 * if (cake == NULL)
226 * {
227 * g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,
228 * "Go to the supermarket");
229 * g_object_unref (task);
230 * return;
231 * }
232 *
233 * bd->cake = cake;
234 *
235 * // Bail out now if the user has already cancelled
236 * if (g_task_return_error_if_cancelled (task))
237 * {
238 * g_object_unref (task);
239 * return;
240 * }
241 *
242 * if (cake_decorator_available (cake))
243 * decorator_ready (task);
244 * else
245 * {
246 * GSource *source;
247 *
248 * source = cake_decorator_wait_source_new (cake);
249 * // Attach @source to @task's GMainContext and have it call
250 * // decorator_ready() when it is ready.
251 * g_task_attach_source (task, source, decorator_ready);
252 * g_source_unref (source);
253 * }
254 * }
255 *
256 * void
257 * baker_bake_cake_async (Baker *self,
258 * guint radius,
259 * CakeFlavor flavor,
260 * CakeFrostingType frosting,
261 * const char *message,
262 * gint priority,
263 * GCancellable *cancellable,
264 * GAsyncReadyCallback callback,
265 * gpointer user_data)
266 * {
267 * GTask *task;
268 * BakingData *bd;
269 *
270 * task = g_task_new (self, cancellable, callback, user_data);
271 * g_task_set_priority (task, priority);
272 *
273 * bd = g_slice_new0 (BakingData);
274 * bd->frosting = frosting;
275 * bd->message = g_strdup (message);
276 * g_task_set_task_data (task, bd, (GDestroyNotify) baking_data_free);
277 *
278 * _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);
279 * }
280 *
281 * Cake *
282 * baker_bake_cake_finish (Baker *self,
283 * GAsyncResult *result,
284 * GError **error)
285 * {
286 * g_return_val_if_fail (g_task_is_valid (result, self), NULL);
287 *
288 * return g_task_propagate_pointer (G_TASK (result), error);
289 * }
290 * ]|
291 *
292 * ## Asynchronous operations from synchronous ones
293 *
294 * You can use g_task_run_in_thread() to turn a synchronous
295 * operation into an asynchronous one, by running it in a thread.
296 * When it completes, the result will be dispatched to the
297 * [thread-default main context][g-main-context-push-thread-default]
298 * where the #GTask was created.
299 *
300 * Running a task in a thread:
301 * |[<!-- language="C" -->
302 * typedef struct {
303 * guint radius;
304 * CakeFlavor flavor;
305 * CakeFrostingType frosting;
306 * char *message;
307 * } CakeData;
308 *
309 * static void
310 * cake_data_free (CakeData *cake_data)
311 * {
312 * g_free (cake_data->message);
313 * g_slice_free (CakeData, cake_data);
314 * }
315 *
316 * static void
317 * bake_cake_thread (GTask *task,
318 * gpointer source_object,
319 * gpointer task_data,
320 * GCancellable *cancellable)
321 * {
322 * Baker *self = source_object;
323 * CakeData *cake_data = task_data;
324 * Cake *cake;
325 * GError *error = NULL;
326 *
327 * cake = bake_cake (baker, cake_data->radius, cake_data->flavor,
328 * cake_data->frosting, cake_data->message,
329 * cancellable, &error);
330 * if (cake)
331 * g_task_return_pointer (task, cake, g_object_unref);
332 * else
333 * g_task_return_error (task, error);
334 * }
335 *
336 * void
337 * baker_bake_cake_async (Baker *self,
338 * guint radius,
339 * CakeFlavor flavor,
340 * CakeFrostingType frosting,
341 * const char *message,
342 * GCancellable *cancellable,
343 * GAsyncReadyCallback callback,
344 * gpointer user_data)
345 * {
346 * CakeData *cake_data;
347 * GTask *task;
348 *
349 * cake_data = g_slice_new (CakeData);
350 * cake_data->radius = radius;
351 * cake_data->flavor = flavor;
352 * cake_data->frosting = frosting;
353 * cake_data->message = g_strdup (message);
354 * task = g_task_new (self, cancellable, callback, user_data);
355 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
356 * g_task_run_in_thread (task, bake_cake_thread);
357 * g_object_unref (task);
358 * }
359 *
360 * Cake *
361 * baker_bake_cake_finish (Baker *self,
362 * GAsyncResult *result,
363 * GError **error)
364 * {
365 * g_return_val_if_fail (g_task_is_valid (result, self), NULL);
366 *
367 * return g_task_propagate_pointer (G_TASK (result), error);
368 * }
369 * ]|
370 *
371 * ## Adding cancellability to uncancellable tasks
372 *
373 * Finally, g_task_run_in_thread() and g_task_run_in_thread_sync()
374 * can be used to turn an uncancellable operation into a
375 * cancellable one. If you call g_task_set_return_on_cancel(),
376 * passing %TRUE, then if the task's #GCancellable is cancelled,
377 * it will return control back to the caller immediately, while
378 * allowing the task thread to continue running in the background
379 * (and simply discarding its result when it finally does finish).
380 * Provided that the task thread is careful about how it uses
381 * locks and other externally-visible resources, this allows you
382 * to make "GLib-friendly" asynchronous and cancellable
383 * synchronous variants of blocking APIs.
384 *
385 * Cancelling a task:
386 * |[<!-- language="C" -->
387 * static void
388 * bake_cake_thread (GTask *task,
389 * gpointer source_object,
390 * gpointer task_data,
391 * GCancellable *cancellable)
392 * {
393 * Baker *self = source_object;
394 * CakeData *cake_data = task_data;
395 * Cake *cake;
396 * GError *error = NULL;
397 *
398 * cake = bake_cake (baker, cake_data->radius, cake_data->flavor,
399 * cake_data->frosting, cake_data->message,
400 * &error);
401 * if (error)
402 * {
403 * g_task_return_error (task, error);
404 * return;
405 * }
406 *
407 * // If the task has already been cancelled, then we don't want to add
408 * // the cake to the cake cache. Likewise, we don't want to have the
409 * // task get cancelled in the middle of updating the cache.
410 * // g_task_set_return_on_cancel() will return %TRUE here if it managed
411 * // to disable return-on-cancel, or %FALSE if the task was cancelled
412 * // before it could.
413 * if (g_task_set_return_on_cancel (task, FALSE))
414 * {
415 * // If the caller cancels at this point, their
416 * // GAsyncReadyCallback won't be invoked until we return,
417 * // so we don't have to worry that this code will run at
418 * // the same time as that code does. But if there were
419 * // other functions that might look at the cake cache,
420 * // then we'd probably need a GMutex here as well.
421 * baker_add_cake_to_cache (baker, cake);
422 * g_task_return_pointer (task, cake, g_object_unref);
423 * }
424 * }
425 *
426 * void
427 * baker_bake_cake_async (Baker *self,
428 * guint radius,
429 * CakeFlavor flavor,
430 * CakeFrostingType frosting,
431 * const char *message,
432 * GCancellable *cancellable,
433 * GAsyncReadyCallback callback,
434 * gpointer user_data)
435 * {
436 * CakeData *cake_data;
437 * GTask *task;
438 *
439 * cake_data = g_slice_new (CakeData);
440 *
441 * ...
442 *
443 * task = g_task_new (self, cancellable, callback, user_data);
444 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
445 * g_task_set_return_on_cancel (task, TRUE);
446 * g_task_run_in_thread (task, bake_cake_thread);
447 * }
448 *
449 * Cake *
450 * baker_bake_cake_sync (Baker *self,
451 * guint radius,
452 * CakeFlavor flavor,
453 * CakeFrostingType frosting,
454 * const char *message,
455 * GCancellable *cancellable,
456 * GError **error)
457 * {
458 * CakeData *cake_data;
459 * GTask *task;
460 * Cake *cake;
461 *
462 * cake_data = g_slice_new (CakeData);
463 *
464 * ...
465 *
466 * task = g_task_new (self, cancellable, NULL, NULL);
467 * g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
468 * g_task_set_return_on_cancel (task, TRUE);
469 * g_task_run_in_thread_sync (task, bake_cake_thread);
470 *
471 * cake = g_task_propagate_pointer (task, error);
472 * g_object_unref (task);
473 * return cake;
474 * }
475 * ]|
476 *
477 * ## Porting from GSimpleAsyncResult
478 *
479 * #GTask's API attempts to be simpler than #GSimpleAsyncResult's
480 * in several ways:
481 * - You can save task-specific data with g_task_set_task_data(), and
482 * retrieve it later with g_task_get_task_data(). This replaces the
483 * abuse of g_simple_async_result_set_op_res_gpointer() for the same
484 * purpose with #GSimpleAsyncResult.
485 * - In addition to the task data, #GTask also keeps track of the
486 * [priority][io-priority], #GCancellable, and
487 * #GMainContext associated with the task, so tasks that consist of
488 * a chain of simpler asynchronous operations will have easy access
489 * to those values when starting each sub-task.
490 * - g_task_return_error_if_cancelled() provides simplified
491 * handling for cancellation. In addition, cancellation
492 * overrides any other #GTask return value by default, like
493 * #GSimpleAsyncResult does when
494 * g_simple_async_result_set_check_cancellable() is called.
495 * (You can use g_task_set_check_cancellable() to turn off that
496 * behavior.) On the other hand, g_task_run_in_thread()
497 * guarantees that it will always run your
498 * `task_func`, even if the task's #GCancellable
499 * is already cancelled before the task gets a chance to run;
500 * you can start your `task_func` with a
501 * g_task_return_error_if_cancelled() check if you need the
502 * old behavior.
503 * - The "return" methods (eg, g_task_return_pointer())
504 * automatically cause the task to be "completed" as well, and
505 * there is no need to worry about the "complete" vs "complete
506 * in idle" distinction. (#GTask automatically figures out
507 * whether the task's callback can be invoked directly, or
508 * if it needs to be sent to another #GMainContext, or delayed
509 * until the next iteration of the current #GMainContext.)
510 * - The "finish" functions for #GTask based operations are generally
511 * much simpler than #GSimpleAsyncResult ones, normally consisting
512 * of only a single call to g_task_propagate_pointer() or the like.
513 * Since g_task_propagate_pointer() "steals" the return value from
514 * the #GTask, it is not necessary to juggle pointers around to
515 * prevent it from being freed twice.
516 * - With #GSimpleAsyncResult, it was common to call
517 * g_simple_async_result_propagate_error() from the
518 * `_finish()` wrapper function, and have
519 * virtual method implementations only deal with successful
520 * returns. This behavior is deprecated, because it makes it
521 * difficult for a subclass to chain to a parent class's async
522 * methods. Instead, the wrapper function should just be a
523 * simple wrapper, and the virtual method should call an
524 * appropriate `g_task_propagate_` function.
525 * Note that wrapper methods can now use
526 * g_async_result_legacy_propagate_error() to do old-style
527 * #GSimpleAsyncResult error-returning behavior, and
528 * g_async_result_is_tagged() to check if a result is tagged as
529 * having come from the `_async()` wrapper
530 * function (for "short-circuit" results, such as when passing
531 * 0 to g_input_stream_read_async()).
532 */
533
534 /**
535 * GTask:
536 *
537 * The opaque object representing a synchronous or asynchronous task
538 * and its result.
539 */
540
541 struct _GTask {
542 GObject parent_instance;
543
544 gpointer source_object;
545 gpointer source_tag;
546 gchar *name; /* (owned); may only be modified before the #GTask is threaded */
547
548 gpointer task_data;
549 GDestroyNotify task_data_destroy;
550
551 GMainContext *context;
552 gint64 creation_time;
553 gint priority;
554 GCancellable *cancellable;
555
556 GAsyncReadyCallback callback;
557 gpointer callback_data;
558
559 GTaskThreadFunc task_func;
560 GMutex lock;
561 GCond cond;
562
563 /* This can’t be in the bit field because we access it from TRACE(). */
564 gboolean thread_cancelled;
565
566 /* Protected by the lock when task is threaded: */
567 gboolean thread_complete : 1;
568 gboolean return_on_cancel : 1;
569 gboolean : 0;
570
571 /* Unprotected, but written to when task runs in thread: */
572 gboolean completed : 1;
573 gboolean had_error : 1;
574 gboolean result_set : 1;
575 gboolean ever_returned : 1;
576 gboolean : 0;
577
578 /* Read-only once task runs in thread: */
579 gboolean check_cancellable : 1;
580 gboolean synchronous : 1;
581 gboolean blocking_other_task : 1;
582
583 GError *error;
584 union {
585 gpointer pointer;
586 gssize size;
587 gboolean boolean;
588 } result;
589 GDestroyNotify result_destroy;
590 };
591
592 #define G_TASK_IS_THREADED(task) ((task)->task_func != NULL)
593
594 struct _GTaskClass
595 {
596 GObjectClass parent_class;
597 };
598
599 typedef enum
600 {
601 PROP_COMPLETED = 1,
602 } GTaskProperty;
603
604 static void g_task_async_result_iface_init (GAsyncResultIface *iface);
605 static void g_task_thread_pool_init (void);
606
607 G_DEFINE_TYPE_WITH_CODE (GTask, g_task, G_TYPE_OBJECT,
608 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
609 g_task_async_result_iface_init);
610 g_task_thread_pool_init ();)
611
612 static GThreadPool *task_pool;
613 static GMutex task_pool_mutex;
614 static GPrivate task_private = G_PRIVATE_INIT (NULL);
615 static GSource *task_pool_manager;
616 static guint64 task_wait_time;
617 static gint tasks_running;
618
619 /* When the task pool fills up and blocks, and the program keeps
620 * queueing more tasks, we will slowly add more threads to the pool
621 * (in case the existing tasks are trying to queue subtasks of their
622 * own) until tasks start completing again. These "overflow" threads
623 * will only run one task apiece, and then exit, so the pool will
624 * eventually get back down to its base size.
625 *
626 * The base and multiplier below gives us 10 extra threads after about
627 * a second of blocking, 30 after 5 seconds, 100 after a minute, and
628 * 200 after 20 minutes.
629 *
630 * We specify maximum pool size of 330 to increase the waiting time up
631 * to around 30 minutes.
632 */
633 #define G_TASK_POOL_SIZE 10
634 #define G_TASK_WAIT_TIME_BASE 100000
635 #define G_TASK_WAIT_TIME_MULTIPLIER 1.03
636 #define G_TASK_WAIT_TIME_MAX_POOL_SIZE 330
637
638 static void
g_task_init(GTask * task)639 g_task_init (GTask *task)
640 {
641 task->check_cancellable = TRUE;
642 }
643
644 static void
g_task_finalize(GObject * object)645 g_task_finalize (GObject *object)
646 {
647 GTask *task = G_TASK (object);
648
649 g_clear_object (&task->source_object);
650 g_clear_object (&task->cancellable);
651 g_free (task->name);
652
653 if (task->context)
654 g_main_context_unref (task->context);
655
656 if (task->task_data_destroy)
657 task->task_data_destroy (task->task_data);
658
659 if (task->result_destroy && task->result.pointer)
660 task->result_destroy (task->result.pointer);
661
662 if (task->error)
663 g_error_free (task->error);
664
665 if (G_TASK_IS_THREADED (task))
666 {
667 g_mutex_clear (&task->lock);
668 g_cond_clear (&task->cond);
669 }
670
671 G_OBJECT_CLASS (g_task_parent_class)->finalize (object);
672 }
673
674 /**
675 * g_task_new:
676 * @source_object: (nullable) (type GObject): the #GObject that owns
677 * this task, or %NULL.
678 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
679 * @callback: (scope async): a #GAsyncReadyCallback.
680 * @callback_data: (closure): user data passed to @callback.
681 *
682 * Creates a #GTask acting on @source_object, which will eventually be
683 * used to invoke @callback in the current
684 * [thread-default main context][g-main-context-push-thread-default].
685 *
686 * Call this in the "start" method of your asynchronous method, and
687 * pass the #GTask around throughout the asynchronous operation. You
688 * can use g_task_set_task_data() to attach task-specific data to the
689 * object, which you can retrieve later via g_task_get_task_data().
690 *
691 * By default, if @cancellable is cancelled, then the return value of
692 * the task will always be %G_IO_ERROR_CANCELLED, even if the task had
693 * already completed before the cancellation. This allows for
694 * simplified handling in cases where cancellation may imply that
695 * other objects that the task depends on have been destroyed. If you
696 * do not want this behavior, you can use
697 * g_task_set_check_cancellable() to change it.
698 *
699 * Returns: a #GTask.
700 *
701 * Since: 2.36
702 */
703 GTask *
g_task_new(gpointer source_object,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer callback_data)704 g_task_new (gpointer source_object,
705 GCancellable *cancellable,
706 GAsyncReadyCallback callback,
707 gpointer callback_data)
708 {
709 GTask *task;
710 GSource *source;
711
712 task = g_object_new (G_TYPE_TASK, NULL);
713 task->source_object = source_object ? g_object_ref (source_object) : NULL;
714 task->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
715 task->callback = callback;
716 task->callback_data = callback_data;
717 task->context = g_main_context_ref_thread_default ();
718
719 source = g_main_current_source ();
720 if (source)
721 task->creation_time = g_source_get_time (source);
722
723 TRACE (GIO_TASK_NEW (task, source_object, cancellable,
724 callback, callback_data));
725
726 return task;
727 }
728
729 /**
730 * g_task_report_error:
731 * @source_object: (nullable) (type GObject): the #GObject that owns
732 * this task, or %NULL.
733 * @callback: (scope async): a #GAsyncReadyCallback.
734 * @callback_data: (closure): user data passed to @callback.
735 * @source_tag: an opaque pointer indicating the source of this task
736 * @error: (transfer full): error to report
737 *
738 * Creates a #GTask and then immediately calls g_task_return_error()
739 * on it. Use this in the wrapper function of an asynchronous method
740 * when you want to avoid even calling the virtual method. You can
741 * then use g_async_result_is_tagged() in the finish method wrapper to
742 * check if the result there is tagged as having been created by the
743 * wrapper method, and deal with it appropriately if so.
744 *
745 * See also g_task_report_new_error().
746 *
747 * Since: 2.36
748 */
749 void
g_task_report_error(gpointer source_object,GAsyncReadyCallback callback,gpointer callback_data,gpointer source_tag,GError * error)750 g_task_report_error (gpointer source_object,
751 GAsyncReadyCallback callback,
752 gpointer callback_data,
753 gpointer source_tag,
754 GError *error)
755 {
756 GTask *task;
757
758 task = g_task_new (source_object, NULL, callback, callback_data);
759 g_task_set_source_tag (task, source_tag);
760 g_task_return_error (task, error);
761 g_object_unref (task);
762 }
763
764 /**
765 * g_task_report_new_error:
766 * @source_object: (nullable) (type GObject): the #GObject that owns
767 * this task, or %NULL.
768 * @callback: (scope async): a #GAsyncReadyCallback.
769 * @callback_data: (closure): user data passed to @callback.
770 * @source_tag: an opaque pointer indicating the source of this task
771 * @domain: a #GQuark.
772 * @code: an error code.
773 * @format: a string with format characters.
774 * @...: a list of values to insert into @format.
775 *
776 * Creates a #GTask and then immediately calls
777 * g_task_return_new_error() on it. Use this in the wrapper function
778 * of an asynchronous method when you want to avoid even calling the
779 * virtual method. You can then use g_async_result_is_tagged() in the
780 * finish method wrapper to check if the result there is tagged as
781 * having been created by the wrapper method, and deal with it
782 * appropriately if so.
783 *
784 * See also g_task_report_error().
785 *
786 * Since: 2.36
787 */
788 void
g_task_report_new_error(gpointer source_object,GAsyncReadyCallback callback,gpointer callback_data,gpointer source_tag,GQuark domain,gint code,const char * format,...)789 g_task_report_new_error (gpointer source_object,
790 GAsyncReadyCallback callback,
791 gpointer callback_data,
792 gpointer source_tag,
793 GQuark domain,
794 gint code,
795 const char *format,
796 ...)
797 {
798 GError *error;
799 va_list ap;
800
801 va_start (ap, format);
802 error = g_error_new_valist (domain, code, format, ap);
803 va_end (ap);
804
805 g_task_report_error (source_object, callback, callback_data,
806 source_tag, error);
807 }
808
809 /**
810 * g_task_set_task_data:
811 * @task: the #GTask
812 * @task_data: (nullable): task-specific data
813 * @task_data_destroy: (nullable): #GDestroyNotify for @task_data
814 *
815 * Sets @task's task data (freeing the existing task data, if any).
816 *
817 * Since: 2.36
818 */
819 void
g_task_set_task_data(GTask * task,gpointer task_data,GDestroyNotify task_data_destroy)820 g_task_set_task_data (GTask *task,
821 gpointer task_data,
822 GDestroyNotify task_data_destroy)
823 {
824 g_return_if_fail (G_IS_TASK (task));
825
826 if (task->task_data_destroy)
827 task->task_data_destroy (task->task_data);
828
829 task->task_data = task_data;
830 task->task_data_destroy = task_data_destroy;
831
832 TRACE (GIO_TASK_SET_TASK_DATA (task, task_data, task_data_destroy));
833 }
834
835 /**
836 * g_task_set_priority:
837 * @task: the #GTask
838 * @priority: the [priority][io-priority] of the request
839 *
840 * Sets @task's priority. If you do not call this, it will default to
841 * %G_PRIORITY_DEFAULT.
842 *
843 * This will affect the priority of #GSources created with
844 * g_task_attach_source() and the scheduling of tasks run in threads,
845 * and can also be explicitly retrieved later via
846 * g_task_get_priority().
847 *
848 * Since: 2.36
849 */
850 void
g_task_set_priority(GTask * task,gint priority)851 g_task_set_priority (GTask *task,
852 gint priority)
853 {
854 g_return_if_fail (G_IS_TASK (task));
855
856 task->priority = priority;
857
858 TRACE (GIO_TASK_SET_PRIORITY (task, priority));
859 }
860
861 /**
862 * g_task_set_check_cancellable:
863 * @task: the #GTask
864 * @check_cancellable: whether #GTask will check the state of
865 * its #GCancellable for you.
866 *
867 * Sets or clears @task's check-cancellable flag. If this is %TRUE
868 * (the default), then g_task_propagate_pointer(), etc, and
869 * g_task_had_error() will check the task's #GCancellable first, and
870 * if it has been cancelled, then they will consider the task to have
871 * returned an "Operation was cancelled" error
872 * (%G_IO_ERROR_CANCELLED), regardless of any other error or return
873 * value the task may have had.
874 *
875 * If @check_cancellable is %FALSE, then the #GTask will not check the
876 * cancellable itself, and it is up to @task's owner to do this (eg,
877 * via g_task_return_error_if_cancelled()).
878 *
879 * If you are using g_task_set_return_on_cancel() as well, then
880 * you must leave check-cancellable set %TRUE.
881 *
882 * Since: 2.36
883 */
884 void
g_task_set_check_cancellable(GTask * task,gboolean check_cancellable)885 g_task_set_check_cancellable (GTask *task,
886 gboolean check_cancellable)
887 {
888 g_return_if_fail (G_IS_TASK (task));
889 g_return_if_fail (check_cancellable || !task->return_on_cancel);
890
891 task->check_cancellable = check_cancellable;
892 }
893
894 static void g_task_thread_complete (GTask *task);
895
896 /**
897 * g_task_set_return_on_cancel:
898 * @task: the #GTask
899 * @return_on_cancel: whether the task returns automatically when
900 * it is cancelled.
901 *
902 * Sets or clears @task's return-on-cancel flag. This is only
903 * meaningful for tasks run via g_task_run_in_thread() or
904 * g_task_run_in_thread_sync().
905 *
906 * If @return_on_cancel is %TRUE, then cancelling @task's
907 * #GCancellable will immediately cause it to return, as though the
908 * task's #GTaskThreadFunc had called
909 * g_task_return_error_if_cancelled() and then returned.
910 *
911 * This allows you to create a cancellable wrapper around an
912 * uninterruptable function. The #GTaskThreadFunc just needs to be
913 * careful that it does not modify any externally-visible state after
914 * it has been cancelled. To do that, the thread should call
915 * g_task_set_return_on_cancel() again to (atomically) set
916 * return-on-cancel %FALSE before making externally-visible changes;
917 * if the task gets cancelled before the return-on-cancel flag could
918 * be changed, g_task_set_return_on_cancel() will indicate this by
919 * returning %FALSE.
920 *
921 * You can disable and re-enable this flag multiple times if you wish.
922 * If the task's #GCancellable is cancelled while return-on-cancel is
923 * %FALSE, then calling g_task_set_return_on_cancel() to set it %TRUE
924 * again will cause the task to be cancelled at that point.
925 *
926 * If the task's #GCancellable is already cancelled before you call
927 * g_task_run_in_thread()/g_task_run_in_thread_sync(), then the
928 * #GTaskThreadFunc will still be run (for consistency), but the task
929 * will also be completed right away.
930 *
931 * Returns: %TRUE if @task's return-on-cancel flag was changed to
932 * match @return_on_cancel. %FALSE if @task has already been
933 * cancelled.
934 *
935 * Since: 2.36
936 */
937 gboolean
g_task_set_return_on_cancel(GTask * task,gboolean return_on_cancel)938 g_task_set_return_on_cancel (GTask *task,
939 gboolean return_on_cancel)
940 {
941 g_return_val_if_fail (G_IS_TASK (task), FALSE);
942 g_return_val_if_fail (task->check_cancellable || !return_on_cancel, FALSE);
943
944 if (!G_TASK_IS_THREADED (task))
945 {
946 task->return_on_cancel = return_on_cancel;
947 return TRUE;
948 }
949
950 g_mutex_lock (&task->lock);
951 if (task->thread_cancelled)
952 {
953 if (return_on_cancel && !task->return_on_cancel)
954 {
955 g_mutex_unlock (&task->lock);
956 g_task_thread_complete (task);
957 }
958 else
959 g_mutex_unlock (&task->lock);
960 return FALSE;
961 }
962 task->return_on_cancel = return_on_cancel;
963 g_mutex_unlock (&task->lock);
964
965 return TRUE;
966 }
967
968 /**
969 * g_task_set_source_tag:
970 * @task: the #GTask
971 * @source_tag: an opaque pointer indicating the source of this task
972 *
973 * Sets @task's source tag. You can use this to tag a task return
974 * value with a particular pointer (usually a pointer to the function
975 * doing the tagging) and then later check it using
976 * g_task_get_source_tag() (or g_async_result_is_tagged()) in the
977 * task's "finish" function, to figure out if the response came from a
978 * particular place.
979 *
980 * Since: 2.36
981 */
982 void
g_task_set_source_tag(GTask * task,gpointer source_tag)983 g_task_set_source_tag (GTask *task,
984 gpointer source_tag)
985 {
986 g_return_if_fail (G_IS_TASK (task));
987
988 task->source_tag = source_tag;
989
990 TRACE (GIO_TASK_SET_SOURCE_TAG (task, source_tag));
991 }
992
993 /**
994 * g_task_set_name:
995 * @task: a #GTask
996 * @name: (nullable): a human readable name for the task, or %NULL to unset it
997 *
998 * Sets @task’s name, used in debugging and profiling. The name defaults to
999 * %NULL.
1000 *
1001 * The task name should describe in a human readable way what the task does.
1002 * For example, ‘Open file’ or ‘Connect to network host’. It is used to set the
1003 * name of the #GSource used for idle completion of the task.
1004 *
1005 * This function may only be called before the @task is first used in a thread
1006 * other than the one it was constructed in.
1007 *
1008 * Since: 2.60
1009 */
1010 void
g_task_set_name(GTask * task,const gchar * name)1011 g_task_set_name (GTask *task,
1012 const gchar *name)
1013 {
1014 gchar *new_name;
1015
1016 g_return_if_fail (G_IS_TASK (task));
1017
1018 new_name = g_strdup (name);
1019 g_free (task->name);
1020 task->name = g_steal_pointer (&new_name);
1021 }
1022
1023 /**
1024 * g_task_get_source_object:
1025 * @task: a #GTask
1026 *
1027 * Gets the source object from @task. Like
1028 * g_async_result_get_source_object(), but does not ref the object.
1029 *
1030 * Returns: (transfer none) (nullable) (type GObject): @task's source object, or %NULL
1031 *
1032 * Since: 2.36
1033 */
1034 gpointer
g_task_get_source_object(GTask * task)1035 g_task_get_source_object (GTask *task)
1036 {
1037 g_return_val_if_fail (G_IS_TASK (task), NULL);
1038
1039 return task->source_object;
1040 }
1041
1042 static GObject *
g_task_ref_source_object(GAsyncResult * res)1043 g_task_ref_source_object (GAsyncResult *res)
1044 {
1045 GTask *task = G_TASK (res);
1046
1047 if (task->source_object)
1048 return g_object_ref (task->source_object);
1049 else
1050 return NULL;
1051 }
1052
1053 /**
1054 * g_task_get_task_data:
1055 * @task: a #GTask
1056 *
1057 * Gets @task's `task_data`.
1058 *
1059 * Returns: (transfer none): @task's `task_data`.
1060 *
1061 * Since: 2.36
1062 */
1063 gpointer
g_task_get_task_data(GTask * task)1064 g_task_get_task_data (GTask *task)
1065 {
1066 g_return_val_if_fail (G_IS_TASK (task), NULL);
1067
1068 return task->task_data;
1069 }
1070
1071 /**
1072 * g_task_get_priority:
1073 * @task: a #GTask
1074 *
1075 * Gets @task's priority
1076 *
1077 * Returns: @task's priority
1078 *
1079 * Since: 2.36
1080 */
1081 gint
g_task_get_priority(GTask * task)1082 g_task_get_priority (GTask *task)
1083 {
1084 g_return_val_if_fail (G_IS_TASK (task), G_PRIORITY_DEFAULT);
1085
1086 return task->priority;
1087 }
1088
1089 /**
1090 * g_task_get_context:
1091 * @task: a #GTask
1092 *
1093 * Gets the #GMainContext that @task will return its result in (that
1094 * is, the context that was the
1095 * [thread-default main context][g-main-context-push-thread-default]
1096 * at the point when @task was created).
1097 *
1098 * This will always return a non-%NULL value, even if the task's
1099 * context is the default #GMainContext.
1100 *
1101 * Returns: (transfer none): @task's #GMainContext
1102 *
1103 * Since: 2.36
1104 */
1105 GMainContext *
g_task_get_context(GTask * task)1106 g_task_get_context (GTask *task)
1107 {
1108 g_return_val_if_fail (G_IS_TASK (task), NULL);
1109
1110 return task->context;
1111 }
1112
1113 /**
1114 * g_task_get_cancellable:
1115 * @task: a #GTask
1116 *
1117 * Gets @task's #GCancellable
1118 *
1119 * Returns: (transfer none): @task's #GCancellable
1120 *
1121 * Since: 2.36
1122 */
1123 GCancellable *
g_task_get_cancellable(GTask * task)1124 g_task_get_cancellable (GTask *task)
1125 {
1126 g_return_val_if_fail (G_IS_TASK (task), NULL);
1127
1128 return task->cancellable;
1129 }
1130
1131 /**
1132 * g_task_get_check_cancellable:
1133 * @task: the #GTask
1134 *
1135 * Gets @task's check-cancellable flag. See
1136 * g_task_set_check_cancellable() for more details.
1137 *
1138 * Since: 2.36
1139 */
1140 gboolean
g_task_get_check_cancellable(GTask * task)1141 g_task_get_check_cancellable (GTask *task)
1142 {
1143 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1144
1145 /* Convert from a bit field to a boolean. */
1146 return task->check_cancellable ? TRUE : FALSE;
1147 }
1148
1149 /**
1150 * g_task_get_return_on_cancel:
1151 * @task: the #GTask
1152 *
1153 * Gets @task's return-on-cancel flag. See
1154 * g_task_set_return_on_cancel() for more details.
1155 *
1156 * Since: 2.36
1157 */
1158 gboolean
g_task_get_return_on_cancel(GTask * task)1159 g_task_get_return_on_cancel (GTask *task)
1160 {
1161 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1162
1163 /* Convert from a bit field to a boolean. */
1164 return task->return_on_cancel ? TRUE : FALSE;
1165 }
1166
1167 /**
1168 * g_task_get_source_tag:
1169 * @task: a #GTask
1170 *
1171 * Gets @task's source tag. See g_task_set_source_tag().
1172 *
1173 * Returns: (transfer none): @task's source tag
1174 *
1175 * Since: 2.36
1176 */
1177 gpointer
g_task_get_source_tag(GTask * task)1178 g_task_get_source_tag (GTask *task)
1179 {
1180 g_return_val_if_fail (G_IS_TASK (task), NULL);
1181
1182 return task->source_tag;
1183 }
1184
1185 /**
1186 * g_task_get_name:
1187 * @task: a #GTask
1188 *
1189 * Gets @task’s name. See g_task_set_name().
1190 *
1191 * Returns: (nullable) (transfer none): @task’s name, or %NULL
1192 * Since: 2.60
1193 */
1194 const gchar *
g_task_get_name(GTask * task)1195 g_task_get_name (GTask *task)
1196 {
1197 g_return_val_if_fail (G_IS_TASK (task), NULL);
1198
1199 return task->name;
1200 }
1201
1202 static void
g_task_return_now(GTask * task)1203 g_task_return_now (GTask *task)
1204 {
1205 TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object, task->callback,
1206 task->callback_data));
1207
1208 g_main_context_push_thread_default (task->context);
1209
1210 if (task->callback != NULL)
1211 {
1212 task->callback (task->source_object,
1213 G_ASYNC_RESULT (task),
1214 task->callback_data);
1215 }
1216
1217 task->completed = TRUE;
1218 g_object_notify (G_OBJECT (task), "completed");
1219
1220 g_main_context_pop_thread_default (task->context);
1221 }
1222
1223 static gboolean
complete_in_idle_cb(gpointer task)1224 complete_in_idle_cb (gpointer task)
1225 {
1226 g_task_return_now (task);
1227 g_object_unref (task);
1228 return FALSE;
1229 }
1230
1231 typedef enum {
1232 G_TASK_RETURN_SUCCESS,
1233 G_TASK_RETURN_ERROR,
1234 G_TASK_RETURN_FROM_THREAD
1235 } GTaskReturnType;
1236
1237 static void
g_task_return(GTask * task,GTaskReturnType type)1238 g_task_return (GTask *task,
1239 GTaskReturnType type)
1240 {
1241 GSource *source;
1242
1243 if (type != G_TASK_RETURN_FROM_THREAD)
1244 task->ever_returned = TRUE;
1245
1246 if (type == G_TASK_RETURN_SUCCESS)
1247 task->result_set = TRUE;
1248
1249 if (task->synchronous)
1250 return;
1251
1252 /* Normally we want to invoke the task's callback when its return
1253 * value is set. But if the task is running in a thread, then we
1254 * want to wait until after the task_func returns, to simplify
1255 * locking/refcounting/etc.
1256 */
1257 if (G_TASK_IS_THREADED (task) && type != G_TASK_RETURN_FROM_THREAD)
1258 return;
1259
1260 g_object_ref (task);
1261
1262 /* See if we can complete the task immediately. First, we have to be
1263 * running inside the task's thread/GMainContext.
1264 */
1265 source = g_main_current_source ();
1266 if (source && g_source_get_context (source) == task->context)
1267 {
1268 /* Second, we can only complete immediately if this is not the
1269 * same iteration of the main loop that the task was created in.
1270 */
1271 if (g_source_get_time (source) > task->creation_time)
1272 {
1273 /* Finally, if the task has been cancelled, we shouldn't
1274 * return synchronously from inside the
1275 * GCancellable::cancelled handler. It's easier to run
1276 * another iteration of the main loop than tracking how the
1277 * cancellation was handled.
1278 */
1279 if (!g_cancellable_is_cancelled (task->cancellable))
1280 {
1281 g_task_return_now (task);
1282 g_object_unref (task);
1283 return;
1284 }
1285 }
1286 }
1287
1288 /* Otherwise, complete in the next iteration */
1289 source = g_idle_source_new ();
1290 g_source_set_name (source, "[gio] complete_in_idle_cb");
1291 g_task_attach_source (task, source, complete_in_idle_cb);
1292 g_source_unref (source);
1293 }
1294
1295
1296 /**
1297 * GTaskThreadFunc:
1298 * @task: the #GTask
1299 * @source_object: (type GObject): @task's source object
1300 * @task_data: @task's task data
1301 * @cancellable: @task's #GCancellable, or %NULL
1302 *
1303 * The prototype for a task function to be run in a thread via
1304 * g_task_run_in_thread() or g_task_run_in_thread_sync().
1305 *
1306 * If the return-on-cancel flag is set on @task, and @cancellable gets
1307 * cancelled, then the #GTask will be completed immediately (as though
1308 * g_task_return_error_if_cancelled() had been called), without
1309 * waiting for the task function to complete. However, the task
1310 * function will continue running in its thread in the background. The
1311 * function therefore needs to be careful about how it uses
1312 * externally-visible state in this case. See
1313 * g_task_set_return_on_cancel() for more details.
1314 *
1315 * Other than in that case, @task will be completed when the
1316 * #GTaskThreadFunc returns, not when it calls a
1317 * `g_task_return_` function.
1318 *
1319 * Since: 2.36
1320 */
1321
1322 static void task_thread_cancelled (GCancellable *cancellable,
1323 gpointer user_data);
1324
1325 static void
g_task_thread_complete(GTask * task)1326 g_task_thread_complete (GTask *task)
1327 {
1328 g_mutex_lock (&task->lock);
1329 if (task->thread_complete)
1330 {
1331 /* The task belatedly completed after having been cancelled
1332 * (or was cancelled in the midst of being completed).
1333 */
1334 g_mutex_unlock (&task->lock);
1335 return;
1336 }
1337
1338 TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));
1339
1340 task->thread_complete = TRUE;
1341 g_mutex_unlock (&task->lock);
1342
1343 if (task->cancellable)
1344 g_signal_handlers_disconnect_by_func (task->cancellable, task_thread_cancelled, task);
1345
1346 if (task->synchronous)
1347 g_cond_signal (&task->cond);
1348 else
1349 g_task_return (task, G_TASK_RETURN_FROM_THREAD);
1350 }
1351
1352 static gboolean
task_pool_manager_timeout(gpointer user_data)1353 task_pool_manager_timeout (gpointer user_data)
1354 {
1355 g_mutex_lock (&task_pool_mutex);
1356 g_thread_pool_set_max_threads (task_pool, tasks_running + 1, NULL);
1357 g_source_set_ready_time (task_pool_manager, -1);
1358 g_mutex_unlock (&task_pool_mutex);
1359
1360 return TRUE;
1361 }
1362
1363 static void
g_task_thread_setup(void)1364 g_task_thread_setup (void)
1365 {
1366 g_private_set (&task_private, GUINT_TO_POINTER (TRUE));
1367 g_mutex_lock (&task_pool_mutex);
1368 tasks_running++;
1369
1370 if (tasks_running == G_TASK_POOL_SIZE)
1371 task_wait_time = G_TASK_WAIT_TIME_BASE;
1372 else if (tasks_running > G_TASK_POOL_SIZE && tasks_running < G_TASK_WAIT_TIME_MAX_POOL_SIZE)
1373 task_wait_time *= G_TASK_WAIT_TIME_MULTIPLIER;
1374
1375 if (tasks_running >= G_TASK_POOL_SIZE)
1376 g_source_set_ready_time (task_pool_manager, g_get_monotonic_time () + task_wait_time);
1377
1378 g_mutex_unlock (&task_pool_mutex);
1379 }
1380
1381 static void
g_task_thread_cleanup(void)1382 g_task_thread_cleanup (void)
1383 {
1384 gint tasks_pending;
1385
1386 g_mutex_lock (&task_pool_mutex);
1387 tasks_pending = g_thread_pool_unprocessed (task_pool);
1388
1389 if (tasks_running > G_TASK_POOL_SIZE)
1390 g_thread_pool_set_max_threads (task_pool, tasks_running - 1, NULL);
1391 else if (tasks_running + tasks_pending < G_TASK_POOL_SIZE)
1392 g_source_set_ready_time (task_pool_manager, -1);
1393
1394 if (tasks_running > G_TASK_POOL_SIZE && tasks_running < G_TASK_WAIT_TIME_MAX_POOL_SIZE)
1395 task_wait_time /= G_TASK_WAIT_TIME_MULTIPLIER;
1396
1397 tasks_running--;
1398 g_mutex_unlock (&task_pool_mutex);
1399 g_private_set (&task_private, GUINT_TO_POINTER (FALSE));
1400 }
1401
1402 static void
g_task_thread_pool_thread(gpointer thread_data,gpointer pool_data)1403 g_task_thread_pool_thread (gpointer thread_data,
1404 gpointer pool_data)
1405 {
1406 GTask *task = thread_data;
1407
1408 g_task_thread_setup ();
1409
1410 task->task_func (task, task->source_object, task->task_data,
1411 task->cancellable);
1412 g_task_thread_complete (task);
1413 g_object_unref (task);
1414
1415 g_task_thread_cleanup ();
1416 }
1417
1418 static void
task_thread_cancelled(GCancellable * cancellable,gpointer user_data)1419 task_thread_cancelled (GCancellable *cancellable,
1420 gpointer user_data)
1421 {
1422 GTask *task = user_data;
1423
1424 /* Move this task to the front of the queue - no need for
1425 * a complete resorting of the queue.
1426 */
1427 g_thread_pool_move_to_front (task_pool, task);
1428
1429 g_mutex_lock (&task->lock);
1430 task->thread_cancelled = TRUE;
1431
1432 if (!task->return_on_cancel)
1433 {
1434 g_mutex_unlock (&task->lock);
1435 return;
1436 }
1437
1438 /* We don't actually set task->error; g_task_return_error() doesn't
1439 * use a lock, and g_task_propagate_error() will call
1440 * g_cancellable_set_error_if_cancelled() anyway.
1441 */
1442 g_mutex_unlock (&task->lock);
1443 g_task_thread_complete (task);
1444 }
1445
1446 static void
task_thread_cancelled_disconnect_notify(gpointer task,GClosure * closure)1447 task_thread_cancelled_disconnect_notify (gpointer task,
1448 GClosure *closure)
1449 {
1450 g_object_unref (task);
1451 }
1452
1453 static void
g_task_start_task_thread(GTask * task,GTaskThreadFunc task_func)1454 g_task_start_task_thread (GTask *task,
1455 GTaskThreadFunc task_func)
1456 {
1457 g_mutex_init (&task->lock);
1458 g_cond_init (&task->cond);
1459
1460 g_mutex_lock (&task->lock);
1461
1462 TRACE (GIO_TASK_BEFORE_RUN_IN_THREAD (task, task_func));
1463
1464 task->task_func = task_func;
1465
1466 if (task->cancellable)
1467 {
1468 if (task->return_on_cancel &&
1469 g_cancellable_set_error_if_cancelled (task->cancellable,
1470 &task->error))
1471 {
1472 task->thread_cancelled = task->thread_complete = TRUE;
1473 TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));
1474 g_thread_pool_push (task_pool, g_object_ref (task), NULL);
1475 return;
1476 }
1477
1478 /* This introduces a reference count loop between the GTask and
1479 * GCancellable, but is necessary to avoid a race on finalising the GTask
1480 * between task_thread_cancelled() (in one thread) and
1481 * g_task_thread_complete() (in another).
1482 *
1483 * Accordingly, the signal handler *must* be removed once the task has
1484 * completed.
1485 */
1486 g_signal_connect_data (task->cancellable, "cancelled",
1487 G_CALLBACK (task_thread_cancelled),
1488 g_object_ref (task),
1489 task_thread_cancelled_disconnect_notify, 0);
1490 }
1491
1492 if (g_private_get (&task_private))
1493 task->blocking_other_task = TRUE;
1494 g_thread_pool_push (task_pool, g_object_ref (task), NULL);
1495 }
1496
1497 /**
1498 * g_task_run_in_thread:
1499 * @task: a #GTask
1500 * @task_func: a #GTaskThreadFunc
1501 *
1502 * Runs @task_func in another thread. When @task_func returns, @task's
1503 * #GAsyncReadyCallback will be invoked in @task's #GMainContext.
1504 *
1505 * This takes a ref on @task until the task completes.
1506 *
1507 * See #GTaskThreadFunc for more details about how @task_func is handled.
1508 *
1509 * Although GLib currently rate-limits the tasks queued via
1510 * g_task_run_in_thread(), you should not assume that it will always
1511 * do this. If you have a very large number of tasks to run, but don't
1512 * want them to all run at once, you should only queue a limited
1513 * number of them at a time.
1514 *
1515 * Since: 2.36
1516 */
1517 void
g_task_run_in_thread(GTask * task,GTaskThreadFunc task_func)1518 g_task_run_in_thread (GTask *task,
1519 GTaskThreadFunc task_func)
1520 {
1521 g_return_if_fail (G_IS_TASK (task));
1522
1523 g_object_ref (task);
1524 g_task_start_task_thread (task, task_func);
1525
1526 /* The task may already be cancelled, or g_thread_pool_push() may
1527 * have failed.
1528 */
1529 if (task->thread_complete)
1530 {
1531 g_mutex_unlock (&task->lock);
1532 g_task_return (task, G_TASK_RETURN_FROM_THREAD);
1533 }
1534 else
1535 g_mutex_unlock (&task->lock);
1536
1537 g_object_unref (task);
1538 }
1539
1540 /**
1541 * g_task_run_in_thread_sync:
1542 * @task: a #GTask
1543 * @task_func: a #GTaskThreadFunc
1544 *
1545 * Runs @task_func in another thread, and waits for it to return or be
1546 * cancelled. You can use g_task_propagate_pointer(), etc, afterward
1547 * to get the result of @task_func.
1548 *
1549 * See #GTaskThreadFunc for more details about how @task_func is handled.
1550 *
1551 * Normally this is used with tasks created with a %NULL
1552 * `callback`, but note that even if the task does
1553 * have a callback, it will not be invoked when @task_func returns.
1554 * #GTask:completed will be set to %TRUE just before this function returns.
1555 *
1556 * Although GLib currently rate-limits the tasks queued via
1557 * g_task_run_in_thread_sync(), you should not assume that it will
1558 * always do this. If you have a very large number of tasks to run,
1559 * but don't want them to all run at once, you should only queue a
1560 * limited number of them at a time.
1561 *
1562 * Since: 2.36
1563 */
1564 void
g_task_run_in_thread_sync(GTask * task,GTaskThreadFunc task_func)1565 g_task_run_in_thread_sync (GTask *task,
1566 GTaskThreadFunc task_func)
1567 {
1568 g_return_if_fail (G_IS_TASK (task));
1569
1570 g_object_ref (task);
1571
1572 task->synchronous = TRUE;
1573 g_task_start_task_thread (task, task_func);
1574
1575 while (!task->thread_complete)
1576 g_cond_wait (&task->cond, &task->lock);
1577
1578 g_mutex_unlock (&task->lock);
1579
1580 TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object,
1581 NULL /* callback */,
1582 NULL /* callback data */));
1583
1584 /* Notify of completion in this thread. */
1585 task->completed = TRUE;
1586 g_object_notify (G_OBJECT (task), "completed");
1587
1588 g_object_unref (task);
1589 }
1590
1591 /**
1592 * g_task_attach_source:
1593 * @task: a #GTask
1594 * @source: the source to attach
1595 * @callback: the callback to invoke when @source triggers
1596 *
1597 * A utility function for dealing with async operations where you need
1598 * to wait for a #GSource to trigger. Attaches @source to @task's
1599 * #GMainContext with @task's [priority][io-priority], and sets @source's
1600 * callback to @callback, with @task as the callback's `user_data`.
1601 *
1602 * It will set the @source’s name to the task’s name (as set with
1603 * g_task_set_name()), if one has been set.
1604 *
1605 * This takes a reference on @task until @source is destroyed.
1606 *
1607 * Since: 2.36
1608 */
1609 void
g_task_attach_source(GTask * task,GSource * source,GSourceFunc callback)1610 g_task_attach_source (GTask *task,
1611 GSource *source,
1612 GSourceFunc callback)
1613 {
1614 g_return_if_fail (G_IS_TASK (task));
1615
1616 g_source_set_callback (source, callback,
1617 g_object_ref (task), g_object_unref);
1618 g_source_set_priority (source, task->priority);
1619 if (task->name != NULL)
1620 g_source_set_name (source, task->name);
1621
1622 g_source_attach (source, task->context);
1623 }
1624
1625
1626 static gboolean
g_task_propagate_error(GTask * task,GError ** error)1627 g_task_propagate_error (GTask *task,
1628 GError **error)
1629 {
1630 gboolean error_set;
1631
1632 if (task->check_cancellable &&
1633 g_cancellable_set_error_if_cancelled (task->cancellable, error))
1634 error_set = TRUE;
1635 else if (task->error)
1636 {
1637 g_propagate_error (error, task->error);
1638 task->error = NULL;
1639 task->had_error = TRUE;
1640 error_set = TRUE;
1641 }
1642 else
1643 error_set = FALSE;
1644
1645 TRACE (GIO_TASK_PROPAGATE (task, error_set));
1646
1647 return error_set;
1648 }
1649
1650 /**
1651 * g_task_return_pointer:
1652 * @task: a #GTask
1653 * @result: (nullable) (transfer full): the pointer result of a task
1654 * function
1655 * @result_destroy: (nullable): a #GDestroyNotify function.
1656 *
1657 * Sets @task's result to @result and completes the task. If @result
1658 * is not %NULL, then @result_destroy will be used to free @result if
1659 * the caller does not take ownership of it with
1660 * g_task_propagate_pointer().
1661 *
1662 * "Completes the task" means that for an ordinary asynchronous task
1663 * it will either invoke the task's callback, or else queue that
1664 * callback to be invoked in the proper #GMainContext, or in the next
1665 * iteration of the current #GMainContext. For a task run via
1666 * g_task_run_in_thread() or g_task_run_in_thread_sync(), calling this
1667 * method will save @result to be returned to the caller later, but
1668 * the task will not actually be completed until the #GTaskThreadFunc
1669 * exits.
1670 *
1671 * Note that since the task may be completed before returning from
1672 * g_task_return_pointer(), you cannot assume that @result is still
1673 * valid after calling this, unless you are still holding another
1674 * reference on it.
1675 *
1676 * Since: 2.36
1677 */
1678 void
g_task_return_pointer(GTask * task,gpointer result,GDestroyNotify result_destroy)1679 g_task_return_pointer (GTask *task,
1680 gpointer result,
1681 GDestroyNotify result_destroy)
1682 {
1683 g_return_if_fail (G_IS_TASK (task));
1684 g_return_if_fail (!task->ever_returned);
1685
1686 task->result.pointer = result;
1687 task->result_destroy = result_destroy;
1688
1689 g_task_return (task, G_TASK_RETURN_SUCCESS);
1690 }
1691
1692 /**
1693 * g_task_propagate_pointer:
1694 * @task: a #GTask
1695 * @error: return location for a #GError
1696 *
1697 * Gets the result of @task as a pointer, and transfers ownership
1698 * of that value to the caller.
1699 *
1700 * If the task resulted in an error, or was cancelled, then this will
1701 * instead return %NULL and set @error.
1702 *
1703 * Since this method transfers ownership of the return value (or
1704 * error) to the caller, you may only call it once.
1705 *
1706 * Returns: (transfer full): the task result, or %NULL on error
1707 *
1708 * Since: 2.36
1709 */
1710 gpointer
g_task_propagate_pointer(GTask * task,GError ** error)1711 g_task_propagate_pointer (GTask *task,
1712 GError **error)
1713 {
1714 g_return_val_if_fail (G_IS_TASK (task), NULL);
1715
1716 if (g_task_propagate_error (task, error))
1717 return NULL;
1718
1719 g_return_val_if_fail (task->result_set, NULL);
1720
1721 task->result_destroy = NULL;
1722 task->result_set = FALSE;
1723 return task->result.pointer;
1724 }
1725
1726 /**
1727 * g_task_return_int:
1728 * @task: a #GTask.
1729 * @result: the integer (#gssize) result of a task function.
1730 *
1731 * Sets @task's result to @result and completes the task (see
1732 * g_task_return_pointer() for more discussion of exactly what this
1733 * means).
1734 *
1735 * Since: 2.36
1736 */
1737 void
g_task_return_int(GTask * task,gssize result)1738 g_task_return_int (GTask *task,
1739 gssize result)
1740 {
1741 g_return_if_fail (G_IS_TASK (task));
1742 g_return_if_fail (!task->ever_returned);
1743
1744 task->result.size = result;
1745
1746 g_task_return (task, G_TASK_RETURN_SUCCESS);
1747 }
1748
1749 /**
1750 * g_task_propagate_int:
1751 * @task: a #GTask.
1752 * @error: return location for a #GError
1753 *
1754 * Gets the result of @task as an integer (#gssize).
1755 *
1756 * If the task resulted in an error, or was cancelled, then this will
1757 * instead return -1 and set @error.
1758 *
1759 * Since this method transfers ownership of the return value (or
1760 * error) to the caller, you may only call it once.
1761 *
1762 * Returns: the task result, or -1 on error
1763 *
1764 * Since: 2.36
1765 */
1766 gssize
g_task_propagate_int(GTask * task,GError ** error)1767 g_task_propagate_int (GTask *task,
1768 GError **error)
1769 {
1770 g_return_val_if_fail (G_IS_TASK (task), -1);
1771
1772 if (g_task_propagate_error (task, error))
1773 return -1;
1774
1775 g_return_val_if_fail (task->result_set, -1);
1776
1777 task->result_set = FALSE;
1778 return task->result.size;
1779 }
1780
1781 /**
1782 * g_task_return_boolean:
1783 * @task: a #GTask.
1784 * @result: the #gboolean result of a task function.
1785 *
1786 * Sets @task's result to @result and completes the task (see
1787 * g_task_return_pointer() for more discussion of exactly what this
1788 * means).
1789 *
1790 * Since: 2.36
1791 */
1792 void
g_task_return_boolean(GTask * task,gboolean result)1793 g_task_return_boolean (GTask *task,
1794 gboolean result)
1795 {
1796 g_return_if_fail (G_IS_TASK (task));
1797 g_return_if_fail (!task->ever_returned);
1798
1799 task->result.boolean = result;
1800
1801 g_task_return (task, G_TASK_RETURN_SUCCESS);
1802 }
1803
1804 /**
1805 * g_task_propagate_boolean:
1806 * @task: a #GTask.
1807 * @error: return location for a #GError
1808 *
1809 * Gets the result of @task as a #gboolean.
1810 *
1811 * If the task resulted in an error, or was cancelled, then this will
1812 * instead return %FALSE and set @error.
1813 *
1814 * Since this method transfers ownership of the return value (or
1815 * error) to the caller, you may only call it once.
1816 *
1817 * Returns: the task result, or %FALSE on error
1818 *
1819 * Since: 2.36
1820 */
1821 gboolean
g_task_propagate_boolean(GTask * task,GError ** error)1822 g_task_propagate_boolean (GTask *task,
1823 GError **error)
1824 {
1825 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1826
1827 if (g_task_propagate_error (task, error))
1828 return FALSE;
1829
1830 g_return_val_if_fail (task->result_set, FALSE);
1831
1832 task->result_set = FALSE;
1833 return task->result.boolean;
1834 }
1835
1836 /**
1837 * g_task_return_error:
1838 * @task: a #GTask.
1839 * @error: (transfer full): the #GError result of a task function.
1840 *
1841 * Sets @task's result to @error (which @task assumes ownership of)
1842 * and completes the task (see g_task_return_pointer() for more
1843 * discussion of exactly what this means).
1844 *
1845 * Note that since the task takes ownership of @error, and since the
1846 * task may be completed before returning from g_task_return_error(),
1847 * you cannot assume that @error is still valid after calling this.
1848 * Call g_error_copy() on the error if you need to keep a local copy
1849 * as well.
1850 *
1851 * See also g_task_return_new_error().
1852 *
1853 * Since: 2.36
1854 */
1855 void
g_task_return_error(GTask * task,GError * error)1856 g_task_return_error (GTask *task,
1857 GError *error)
1858 {
1859 g_return_if_fail (G_IS_TASK (task));
1860 g_return_if_fail (!task->ever_returned);
1861 g_return_if_fail (error != NULL);
1862
1863 task->error = error;
1864
1865 g_task_return (task, G_TASK_RETURN_ERROR);
1866 }
1867
1868 /**
1869 * g_task_return_new_error:
1870 * @task: a #GTask.
1871 * @domain: a #GQuark.
1872 * @code: an error code.
1873 * @format: a string with format characters.
1874 * @...: a list of values to insert into @format.
1875 *
1876 * Sets @task's result to a new #GError created from @domain, @code,
1877 * @format, and the remaining arguments, and completes the task (see
1878 * g_task_return_pointer() for more discussion of exactly what this
1879 * means).
1880 *
1881 * See also g_task_return_error().
1882 *
1883 * Since: 2.36
1884 */
1885 void
g_task_return_new_error(GTask * task,GQuark domain,gint code,const char * format,...)1886 g_task_return_new_error (GTask *task,
1887 GQuark domain,
1888 gint code,
1889 const char *format,
1890 ...)
1891 {
1892 GError *error;
1893 va_list args;
1894
1895 va_start (args, format);
1896 error = g_error_new_valist (domain, code, format, args);
1897 va_end (args);
1898
1899 g_task_return_error (task, error);
1900 }
1901
1902 /**
1903 * g_task_return_error_if_cancelled:
1904 * @task: a #GTask
1905 *
1906 * Checks if @task's #GCancellable has been cancelled, and if so, sets
1907 * @task's error accordingly and completes the task (see
1908 * g_task_return_pointer() for more discussion of exactly what this
1909 * means).
1910 *
1911 * Returns: %TRUE if @task has been cancelled, %FALSE if not
1912 *
1913 * Since: 2.36
1914 */
1915 gboolean
g_task_return_error_if_cancelled(GTask * task)1916 g_task_return_error_if_cancelled (GTask *task)
1917 {
1918 GError *error = NULL;
1919
1920 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1921 g_return_val_if_fail (!task->ever_returned, FALSE);
1922
1923 if (g_cancellable_set_error_if_cancelled (task->cancellable, &error))
1924 {
1925 /* We explicitly set task->error so this works even when
1926 * check-cancellable is not set.
1927 */
1928 g_clear_error (&task->error);
1929 task->error = error;
1930
1931 g_task_return (task, G_TASK_RETURN_ERROR);
1932 return TRUE;
1933 }
1934 else
1935 return FALSE;
1936 }
1937
1938 /**
1939 * g_task_had_error:
1940 * @task: a #GTask.
1941 *
1942 * Tests if @task resulted in an error.
1943 *
1944 * Returns: %TRUE if the task resulted in an error, %FALSE otherwise.
1945 *
1946 * Since: 2.36
1947 */
1948 gboolean
g_task_had_error(GTask * task)1949 g_task_had_error (GTask *task)
1950 {
1951 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1952
1953 if (task->error != NULL || task->had_error)
1954 return TRUE;
1955
1956 if (task->check_cancellable && g_cancellable_is_cancelled (task->cancellable))
1957 return TRUE;
1958
1959 return FALSE;
1960 }
1961
1962 /**
1963 * g_task_get_completed:
1964 * @task: a #GTask.
1965 *
1966 * Gets the value of #GTask:completed. This changes from %FALSE to %TRUE after
1967 * the task’s callback is invoked, and will return %FALSE if called from inside
1968 * the callback.
1969 *
1970 * Returns: %TRUE if the task has completed, %FALSE otherwise.
1971 *
1972 * Since: 2.44
1973 */
1974 gboolean
g_task_get_completed(GTask * task)1975 g_task_get_completed (GTask *task)
1976 {
1977 g_return_val_if_fail (G_IS_TASK (task), FALSE);
1978
1979 /* Convert from a bit field to a boolean. */
1980 return task->completed ? TRUE : FALSE;
1981 }
1982
1983 /**
1984 * g_task_is_valid:
1985 * @result: (type Gio.AsyncResult): A #GAsyncResult
1986 * @source_object: (nullable) (type GObject): the source object
1987 * expected to be associated with the task
1988 *
1989 * Checks that @result is a #GTask, and that @source_object is its
1990 * source object (or that @source_object is %NULL and @result has no
1991 * source object). This can be used in g_return_if_fail() checks.
1992 *
1993 * Returns: %TRUE if @result and @source_object are valid, %FALSE
1994 * if not
1995 *
1996 * Since: 2.36
1997 */
1998 gboolean
g_task_is_valid(gpointer result,gpointer source_object)1999 g_task_is_valid (gpointer result,
2000 gpointer source_object)
2001 {
2002 if (!G_IS_TASK (result))
2003 return FALSE;
2004
2005 return G_TASK (result)->source_object == source_object;
2006 }
2007
2008 static gint
g_task_compare_priority(gconstpointer a,gconstpointer b,gpointer user_data)2009 g_task_compare_priority (gconstpointer a,
2010 gconstpointer b,
2011 gpointer user_data)
2012 {
2013 const GTask *ta = a;
2014 const GTask *tb = b;
2015 gboolean a_cancelled, b_cancelled;
2016
2017 /* Tasks that are causing other tasks to block have higher
2018 * priority.
2019 */
2020 if (ta->blocking_other_task && !tb->blocking_other_task)
2021 return -1;
2022 else if (tb->blocking_other_task && !ta->blocking_other_task)
2023 return 1;
2024
2025 /* Let already-cancelled tasks finish right away */
2026 a_cancelled = (ta->check_cancellable &&
2027 g_cancellable_is_cancelled (ta->cancellable));
2028 b_cancelled = (tb->check_cancellable &&
2029 g_cancellable_is_cancelled (tb->cancellable));
2030 if (a_cancelled && !b_cancelled)
2031 return -1;
2032 else if (b_cancelled && !a_cancelled)
2033 return 1;
2034
2035 /* Lower priority == run sooner == negative return value */
2036 return ta->priority - tb->priority;
2037 }
2038
2039 static gboolean
trivial_source_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)2040 trivial_source_dispatch (GSource *source,
2041 GSourceFunc callback,
2042 gpointer user_data)
2043 {
2044 return callback (user_data);
2045 }
2046
2047 GSourceFuncs trivial_source_funcs = {
2048 NULL, /* prepare */
2049 NULL, /* check */
2050 trivial_source_dispatch,
2051 NULL
2052 };
2053
2054 static void
g_task_thread_pool_init(void)2055 g_task_thread_pool_init (void)
2056 {
2057 task_pool = g_thread_pool_new (g_task_thread_pool_thread, NULL,
2058 G_TASK_POOL_SIZE, FALSE, NULL);
2059 g_assert (task_pool != NULL);
2060
2061 g_thread_pool_set_sort_function (task_pool, g_task_compare_priority, NULL);
2062
2063 task_pool_manager = g_source_new (&trivial_source_funcs, sizeof (GSource));
2064 g_source_set_name (task_pool_manager, "GTask thread pool manager");
2065 g_source_set_callback (task_pool_manager, task_pool_manager_timeout, NULL, NULL);
2066 g_source_set_ready_time (task_pool_manager, -1);
2067 g_source_attach (task_pool_manager,
2068 GLIB_PRIVATE_CALL (g_get_worker_context ()));
2069 g_source_unref (task_pool_manager);
2070 }
2071
2072 static void
g_task_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2073 g_task_get_property (GObject *object,
2074 guint prop_id,
2075 GValue *value,
2076 GParamSpec *pspec)
2077 {
2078 GTask *task = G_TASK (object);
2079
2080 switch ((GTaskProperty) prop_id)
2081 {
2082 case PROP_COMPLETED:
2083 g_value_set_boolean (value, g_task_get_completed (task));
2084 break;
2085 }
2086 }
2087
2088 static void
g_task_class_init(GTaskClass * klass)2089 g_task_class_init (GTaskClass *klass)
2090 {
2091 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2092
2093 gobject_class->get_property = g_task_get_property;
2094 gobject_class->finalize = g_task_finalize;
2095
2096 /**
2097 * GTask:completed:
2098 *
2099 * Whether the task has completed, meaning its callback (if set) has been
2100 * invoked. This can only happen after g_task_return_pointer(),
2101 * g_task_return_error() or one of the other return functions have been called
2102 * on the task.
2103 *
2104 * This property is guaranteed to change from %FALSE to %TRUE exactly once.
2105 *
2106 * The #GObject::notify signal for this change is emitted in the same main
2107 * context as the task’s callback, immediately after that callback is invoked.
2108 *
2109 * Since: 2.44
2110 */
2111 g_object_class_install_property (gobject_class, PROP_COMPLETED,
2112 g_param_spec_boolean ("completed",
2113 P_("Task completed"),
2114 P_("Whether the task has completed yet"),
2115 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
2116 }
2117
2118 static gpointer
g_task_get_user_data(GAsyncResult * res)2119 g_task_get_user_data (GAsyncResult *res)
2120 {
2121 return G_TASK (res)->callback_data;
2122 }
2123
2124 static gboolean
g_task_is_tagged(GAsyncResult * res,gpointer source_tag)2125 g_task_is_tagged (GAsyncResult *res,
2126 gpointer source_tag)
2127 {
2128 return G_TASK (res)->source_tag == source_tag;
2129 }
2130
2131 static void
g_task_async_result_iface_init(GAsyncResultIface * iface)2132 g_task_async_result_iface_init (GAsyncResultIface *iface)
2133 {
2134 iface->get_user_data = g_task_get_user_data;
2135 iface->get_source_object = g_task_ref_source_object;
2136 iface->is_tagged = g_task_is_tagged;
2137 }
2138