Blame gio/gtask.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright 2011 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gio_trace.h"
Packit ae235b
Packit ae235b
#include "gtask.h"
Packit ae235b
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gcancellable.h"
Packit ae235b
#include "glib-private.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gtask
Packit ae235b
 * @short_description: Cancellable synchronous or asynchronous task
Packit ae235b
 *     and result
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GAsyncResult
Packit ae235b
 *
Packit ae235b
 * A #GTask represents and manages a cancellable "task".
Packit ae235b
 *
Packit ae235b
 * ## Asynchronous operations
Packit ae235b
 *
Packit ae235b
 * The most common usage of #GTask is as a #GAsyncResult, to
Packit ae235b
 * manage data during an asynchronous operation. You call
Packit ae235b
 * g_task_new() in the "start" method, followed by
Packit ae235b
 * g_task_set_task_data() and the like if you need to keep some
Packit ae235b
 * additional data associated with the task, and then pass the
Packit ae235b
 * task object around through your asynchronous operation.
Packit ae235b
 * Eventually, you will call a method such as
Packit ae235b
 * g_task_return_pointer() or g_task_return_error(), which will
Packit ae235b
 * save the value you give it and then invoke the task's callback
Packit ae235b
 * function in the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * where it was created (waiting until the next iteration of the main
Packit ae235b
 * loop first, if necessary). The caller will pass the #GTask back to
Packit ae235b
 * the operation's finish function (as a #GAsyncResult), and you can
Packit ae235b
 * can use g_task_propagate_pointer() or the like to extract the
Packit ae235b
 * return value.
Packit ae235b
 *
Packit ae235b
 * Here is an example for using GTask as a GAsyncResult:
Packit ae235b
 * |[
Packit ae235b
 *     typedef struct {
Packit ae235b
 *       CakeFrostingType frosting;
Packit ae235b
 *       char *message;
Packit ae235b
 *     } DecorationData;
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     decoration_data_free (DecorationData *decoration)
Packit ae235b
 *     {
Packit ae235b
 *       g_free (decoration->message);
Packit ae235b
 *       g_slice_free (DecorationData, decoration);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     baked_cb (Cake     *cake,
Packit ae235b
 *               gpointer  user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task = user_data;
Packit ae235b
 *       DecorationData *decoration = g_task_get_task_data (task);
Packit ae235b
 *       GError *error = NULL;
Packit ae235b
 *
Packit ae235b
 *       if (cake == NULL)
Packit ae235b
 *         {
Packit ae235b
 *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,
Packit ae235b
 *                                    "Go to the supermarket");
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       if (!cake_decorate (cake, decoration->frosting, decoration->message, &error))
Packit ae235b
 *         {
Packit ae235b
 *           g_object_unref (cake);
Packit ae235b
 *           // g_task_return_error() takes ownership of error
Packit ae235b
 *           g_task_return_error (task, error);
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       g_task_return_pointer (task, cake, g_object_unref);
Packit ae235b
 *       g_object_unref (task);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     void
Packit ae235b
 *     baker_bake_cake_async (Baker               *self,
Packit ae235b
 *                            guint                radius,
Packit ae235b
 *                            CakeFlavor           flavor,
Packit ae235b
 *                            CakeFrostingType     frosting,
Packit ae235b
 *                            const char          *message,
Packit ae235b
 *                            GCancellable        *cancellable,
Packit ae235b
 *                            GAsyncReadyCallback  callback,
Packit ae235b
 *                            gpointer             user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task;
Packit ae235b
 *       DecorationData *decoration;
Packit ae235b
 *       Cake  *cake;
Packit ae235b
 *
Packit ae235b
 *       task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
 *       if (radius < 3)
Packit ae235b
 *         {
Packit ae235b
 *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_TOO_SMALL,
Packit ae235b
 *                                    "%ucm radius cakes are silly",
Packit ae235b
 *                                    radius);
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       cake = _baker_get_cached_cake (self, radius, flavor, frosting, message);
Packit ae235b
 *       if (cake != NULL)
Packit ae235b
 *         {
Packit ae235b
 *           // _baker_get_cached_cake() returns a reffed cake
Packit ae235b
 *           g_task_return_pointer (task, cake, g_object_unref);
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       decoration = g_slice_new (DecorationData);
Packit ae235b
 *       decoration->frosting = frosting;
Packit ae235b
 *       decoration->message = g_strdup (message);
Packit ae235b
 *       g_task_set_task_data (task, decoration, (GDestroyNotify) decoration_data_free);
Packit ae235b
 *
Packit ae235b
 *       _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     Cake *
Packit ae235b
 *     baker_bake_cake_finish (Baker         *self,
Packit ae235b
 *                             GAsyncResult  *result,
Packit ae235b
 *                             GError       **error)
Packit ae235b
 *     {
Packit ae235b
 *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
Packit ae235b
 *
Packit ae235b
 *       return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
 *     }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * ## Chained asynchronous operations
Packit ae235b
 *
Packit ae235b
 * #GTask also tries to simplify asynchronous operations that
Packit ae235b
 * internally chain together several smaller asynchronous
Packit ae235b
 * operations. g_task_get_cancellable(), g_task_get_context(),
Packit ae235b
 * and g_task_get_priority() allow you to get back the task's
Packit ae235b
 * #GCancellable, #GMainContext, and [I/O priority][io-priority]
Packit ae235b
 * when starting a new subtask, so you don't have to keep track
Packit ae235b
 * of them yourself. g_task_attach_source() simplifies the case
Packit ae235b
 * of waiting for a source to fire (automatically using the correct
Packit ae235b
 * #GMainContext and priority).
Packit ae235b
 *
Packit ae235b
 * Here is an example for chained asynchronous operations:
Packit ae235b
 *   |[
Packit ae235b
 *     typedef struct {
Packit ae235b
 *       Cake *cake;
Packit ae235b
 *       CakeFrostingType frosting;
Packit ae235b
 *       char *message;
Packit ae235b
 *     } BakingData;
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     decoration_data_free (BakingData *bd)
Packit ae235b
 *     {
Packit ae235b
 *       if (bd->cake)
Packit ae235b
 *         g_object_unref (bd->cake);
Packit ae235b
 *       g_free (bd->message);
Packit ae235b
 *       g_slice_free (BakingData, bd);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     decorated_cb (Cake         *cake,
Packit ae235b
 *                   GAsyncResult *result,
Packit ae235b
 *                   gpointer      user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task = user_data;
Packit ae235b
 *       GError *error = NULL;
Packit ae235b
 *
Packit ae235b
 *       if (!cake_decorate_finish (cake, result, &error))
Packit ae235b
 *         {
Packit ae235b
 *           g_object_unref (cake);
Packit ae235b
 *           g_task_return_error (task, error);
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       // baking_data_free() will drop its ref on the cake, so we have to
Packit ae235b
 *       // take another here to give to the caller.
Packit ae235b
 *       g_task_return_pointer (task, g_object_ref (cake), g_object_unref);
Packit ae235b
 *       g_object_unref (task);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     static gboolean
Packit ae235b
 *     decorator_ready (gpointer user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task = user_data;
Packit ae235b
 *       BakingData *bd = g_task_get_task_data (task);
Packit ae235b
 *
Packit ae235b
 *       cake_decorate_async (bd->cake, bd->frosting, bd->message,
Packit ae235b
 *                            g_task_get_cancellable (task),
Packit ae235b
 *                            decorated_cb, task);
Packit ae235b
 *
Packit ae235b
 *       return G_SOURCE_REMOVE;
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     baked_cb (Cake     *cake,
Packit ae235b
 *               gpointer  user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task = user_data;
Packit ae235b
 *       BakingData *bd = g_task_get_task_data (task);
Packit ae235b
 *       GError *error = NULL;
Packit ae235b
 *
Packit ae235b
 *       if (cake == NULL)
Packit ae235b
 *         {
Packit ae235b
 *           g_task_return_new_error (task, BAKER_ERROR, BAKER_ERROR_NO_FLOUR,
Packit ae235b
 *                                    "Go to the supermarket");
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       bd->cake = cake;
Packit ae235b
 *
Packit ae235b
 *       // Bail out now if the user has already cancelled
Packit ae235b
 *       if (g_task_return_error_if_cancelled (task))
Packit ae235b
 *         {
Packit ae235b
 *           g_object_unref (task);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       if (cake_decorator_available (cake))
Packit ae235b
 *         decorator_ready (task);
Packit ae235b
 *       else
Packit ae235b
 *         {
Packit ae235b
 *           GSource *source;
Packit ae235b
 *
Packit ae235b
 *           source = cake_decorator_wait_source_new (cake);
Packit ae235b
 *           // Attach @source to @task's GMainContext and have it call
Packit ae235b
 *           // decorator_ready() when it is ready.
Packit ae235b
 *           g_task_attach_source (task, source, decorator_ready);
Packit ae235b
 *           g_source_unref (source);
Packit ae235b
 *         }
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     void
Packit ae235b
 *     baker_bake_cake_async (Baker               *self,
Packit ae235b
 *                            guint                radius,
Packit ae235b
 *                            CakeFlavor           flavor,
Packit ae235b
 *                            CakeFrostingType     frosting,
Packit ae235b
 *                            const char          *message,
Packit ae235b
 *                            gint                 priority,
Packit ae235b
 *                            GCancellable        *cancellable,
Packit ae235b
 *                            GAsyncReadyCallback  callback,
Packit ae235b
 *                            gpointer             user_data)
Packit ae235b
 *     {
Packit ae235b
 *       GTask *task;
Packit ae235b
 *       BakingData *bd;
Packit ae235b
 *
Packit ae235b
 *       task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
 *       g_task_set_priority (task, priority);
Packit ae235b
 *
Packit ae235b
 *       bd = g_slice_new0 (BakingData);
Packit ae235b
 *       bd->frosting = frosting;
Packit ae235b
 *       bd->message = g_strdup (message);
Packit ae235b
 *       g_task_set_task_data (task, bd, (GDestroyNotify) baking_data_free);
Packit ae235b
 *
Packit ae235b
 *       _baker_begin_cake (self, radius, flavor, cancellable, baked_cb, task);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     Cake *
Packit ae235b
 *     baker_bake_cake_finish (Baker         *self,
Packit ae235b
 *                             GAsyncResult  *result,
Packit ae235b
 *                             GError       **error)
Packit ae235b
 *     {
Packit ae235b
 *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
Packit ae235b
 *
Packit ae235b
 *       return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
 *     }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * ## Asynchronous operations from synchronous ones
Packit ae235b
 *
Packit ae235b
 * You can use g_task_run_in_thread() to turn a synchronous
Packit ae235b
 * operation into an asynchronous one, by running it in a thread.
Packit ae235b
 * When it completes, the result will be dispatched to the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * where the #GTask was created.
Packit ae235b
 *
Packit ae235b
 * Running a task in a thread:
Packit ae235b
 *   |[
Packit ae235b
 *     typedef struct {
Packit ae235b
 *       guint radius;
Packit ae235b
 *       CakeFlavor flavor;
Packit ae235b
 *       CakeFrostingType frosting;
Packit ae235b
 *       char *message;
Packit ae235b
 *     } CakeData;
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     cake_data_free (CakeData *cake_data)
Packit ae235b
 *     {
Packit ae235b
 *       g_free (cake_data->message);
Packit ae235b
 *       g_slice_free (CakeData, cake_data);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     static void
Packit ae235b
 *     bake_cake_thread (GTask         *task,
Packit ae235b
 *                       gpointer       source_object,
Packit ae235b
 *                       gpointer       task_data,
Packit ae235b
 *                       GCancellable  *cancellable)
Packit ae235b
 *     {
Packit ae235b
 *       Baker *self = source_object;
Packit ae235b
 *       CakeData *cake_data = task_data;
Packit ae235b
 *       Cake *cake;
Packit ae235b
 *       GError *error = NULL;
Packit ae235b
 *
Packit ae235b
 *       cake = bake_cake (baker, cake_data->radius, cake_data->flavor,
Packit ae235b
 *                         cake_data->frosting, cake_data->message,
Packit ae235b
 *                         cancellable, &error);
Packit ae235b
 *       if (cake)
Packit ae235b
 *         g_task_return_pointer (task, cake, g_object_unref);
Packit ae235b
 *       else
Packit ae235b
 *         g_task_return_error (task, error);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     void
Packit ae235b
 *     baker_bake_cake_async (Baker               *self,
Packit ae235b
 *                            guint                radius,
Packit ae235b
 *                            CakeFlavor           flavor,
Packit ae235b
 *                            CakeFrostingType     frosting,
Packit ae235b
 *                            const char          *message,
Packit ae235b
 *                            GCancellable        *cancellable,
Packit ae235b
 *                            GAsyncReadyCallback  callback,
Packit ae235b
 *                            gpointer             user_data)
Packit ae235b
 *     {
Packit ae235b
 *       CakeData *cake_data;
Packit ae235b
 *       GTask *task;
Packit ae235b
 *
Packit ae235b
 *       cake_data = g_slice_new (CakeData);
Packit ae235b
 *       cake_data->radius = radius;
Packit ae235b
 *       cake_data->flavor = flavor;
Packit ae235b
 *       cake_data->frosting = frosting;
Packit ae235b
 *       cake_data->message = g_strdup (message);
Packit ae235b
 *       task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
 *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
Packit ae235b
 *       g_task_run_in_thread (task, bake_cake_thread);
Packit ae235b
 *       g_object_unref (task);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     Cake *
Packit ae235b
 *     baker_bake_cake_finish (Baker         *self,
Packit ae235b
 *                             GAsyncResult  *result,
Packit ae235b
 *                             GError       **error)
Packit ae235b
 *     {
Packit ae235b
 *       g_return_val_if_fail (g_task_is_valid (result, self), NULL);
Packit ae235b
 *
Packit ae235b
 *       return g_task_propagate_pointer (G_TASK (result), error);
Packit ae235b
 *     }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * ## Adding cancellability to uncancellable tasks
Packit ae235b
 * 
Packit ae235b
 * Finally, g_task_run_in_thread() and g_task_run_in_thread_sync()
Packit ae235b
 * can be used to turn an uncancellable operation into a
Packit ae235b
 * cancellable one. If you call g_task_set_return_on_cancel(),
Packit ae235b
 * passing %TRUE, then if the task's #GCancellable is cancelled,
Packit ae235b
 * it will return control back to the caller immediately, while
Packit ae235b
 * allowing the task thread to continue running in the background
Packit ae235b
 * (and simply discarding its result when it finally does finish).
Packit ae235b
 * Provided that the task thread is careful about how it uses
Packit ae235b
 * locks and other externally-visible resources, this allows you
Packit ae235b
 * to make "GLib-friendly" asynchronous and cancellable
Packit ae235b
 * synchronous variants of blocking APIs.
Packit ae235b
 *
Packit ae235b
 * Cancelling a task:
Packit ae235b
 *   |[
Packit ae235b
 *     static void
Packit ae235b
 *     bake_cake_thread (GTask         *task,
Packit ae235b
 *                       gpointer       source_object,
Packit ae235b
 *                       gpointer       task_data,
Packit ae235b
 *                       GCancellable  *cancellable)
Packit ae235b
 *     {
Packit ae235b
 *       Baker *self = source_object;
Packit ae235b
 *       CakeData *cake_data = task_data;
Packit ae235b
 *       Cake *cake;
Packit ae235b
 *       GError *error = NULL;
Packit ae235b
 *
Packit ae235b
 *       cake = bake_cake (baker, cake_data->radius, cake_data->flavor,
Packit ae235b
 *                         cake_data->frosting, cake_data->message,
Packit ae235b
 *                         &error);
Packit ae235b
 *       if (error)
Packit ae235b
 *         {
Packit ae235b
 *           g_task_return_error (task, error);
Packit ae235b
 *           return;
Packit ae235b
 *         }
Packit ae235b
 *
Packit ae235b
 *       // If the task has already been cancelled, then we don't want to add
Packit ae235b
 *       // the cake to the cake cache. Likewise, we don't  want to have the
Packit ae235b
 *       // task get cancelled in the middle of updating the cache.
Packit ae235b
 *       // g_task_set_return_on_cancel() will return %TRUE here if it managed
Packit ae235b
 *       // to disable return-on-cancel, or %FALSE if the task was cancelled
Packit ae235b
 *       // before it could.
Packit ae235b
 *       if (g_task_set_return_on_cancel (task, FALSE))
Packit ae235b
 *         {
Packit ae235b
 *           // If the caller cancels at this point, their
Packit ae235b
 *           // GAsyncReadyCallback won't be invoked until we return,
Packit ae235b
 *           // so we don't have to worry that this code will run at
Packit ae235b
 *           // the same time as that code does. But if there were
Packit ae235b
 *           // other functions that might look at the cake cache,
Packit ae235b
 *           // then we'd probably need a GMutex here as well.
Packit ae235b
 *           baker_add_cake_to_cache (baker, cake);
Packit ae235b
 *           g_task_return_pointer (task, cake, g_object_unref);
Packit ae235b
 *         }
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     void
Packit ae235b
 *     baker_bake_cake_async (Baker               *self,
Packit ae235b
 *                            guint                radius,
Packit ae235b
 *                            CakeFlavor           flavor,
Packit ae235b
 *                            CakeFrostingType     frosting,
Packit ae235b
 *                            const char          *message,
Packit ae235b
 *                            GCancellable        *cancellable,
Packit ae235b
 *                            GAsyncReadyCallback  callback,
Packit ae235b
 *                            gpointer             user_data)
Packit ae235b
 *     {
Packit ae235b
 *       CakeData *cake_data;
Packit ae235b
 *       GTask *task;
Packit ae235b
 *
Packit ae235b
 *       cake_data = g_slice_new (CakeData);
Packit ae235b
 *
Packit ae235b
 *       ...
Packit ae235b
 *
Packit ae235b
 *       task = g_task_new (self, cancellable, callback, user_data);
Packit ae235b
 *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
Packit ae235b
 *       g_task_set_return_on_cancel (task, TRUE);
Packit ae235b
 *       g_task_run_in_thread (task, bake_cake_thread);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *     Cake *
Packit ae235b
 *     baker_bake_cake_sync (Baker               *self,
Packit ae235b
 *                           guint                radius,
Packit ae235b
 *                           CakeFlavor           flavor,
Packit ae235b
 *                           CakeFrostingType     frosting,
Packit ae235b
 *                           const char          *message,
Packit ae235b
 *                           GCancellable        *cancellable,
Packit ae235b
 *                           GError             **error)
Packit ae235b
 *     {
Packit ae235b
 *       CakeData *cake_data;
Packit ae235b
 *       GTask *task;
Packit ae235b
 *       Cake *cake;
Packit ae235b
 *
Packit ae235b
 *       cake_data = g_slice_new (CakeData);
Packit ae235b
 *
Packit ae235b
 *       ...
Packit ae235b
 *
Packit ae235b
 *       task = g_task_new (self, cancellable, NULL, NULL);
Packit ae235b
 *       g_task_set_task_data (task, cake_data, (GDestroyNotify) cake_data_free);
Packit ae235b
 *       g_task_set_return_on_cancel (task, TRUE);
Packit ae235b
 *       g_task_run_in_thread_sync (task, bake_cake_thread);
Packit ae235b
 *
Packit ae235b
 *       cake = g_task_propagate_pointer (task, error);
Packit ae235b
 *       g_object_unref (task);
Packit ae235b
 *       return cake;
Packit ae235b
 *     }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * ## Porting from GSimpleAsyncResult
Packit ae235b
 * 
Packit ae235b
 * #GTask's API attempts to be simpler than #GSimpleAsyncResult's
Packit ae235b
 * in several ways:
Packit ae235b
 * - You can save task-specific data with g_task_set_task_data(), and
Packit ae235b
 *   retrieve it later with g_task_get_task_data(). This replaces the
Packit ae235b
 *   abuse of g_simple_async_result_set_op_res_gpointer() for the same
Packit ae235b
 *   purpose with #GSimpleAsyncResult.
Packit ae235b
 * - In addition to the task data, #GTask also keeps track of the
Packit ae235b
 *   [priority][io-priority], #GCancellable, and
Packit ae235b
 *   #GMainContext associated with the task, so tasks that consist of
Packit ae235b
 *   a chain of simpler asynchronous operations will have easy access
Packit ae235b
 *   to those values when starting each sub-task.
Packit ae235b
 * - g_task_return_error_if_cancelled() provides simplified
Packit ae235b
 *   handling for cancellation. In addition, cancellation
Packit ae235b
 *   overrides any other #GTask return value by default, like
Packit ae235b
 *   #GSimpleAsyncResult does when
Packit ae235b
 *   g_simple_async_result_set_check_cancellable() is called.
Packit ae235b
 *   (You can use g_task_set_check_cancellable() to turn off that
Packit ae235b
 *   behavior.) On the other hand, g_task_run_in_thread()
Packit ae235b
 *   guarantees that it will always run your
Packit ae235b
 *   `task_func`, even if the task's #GCancellable
Packit ae235b
 *   is already cancelled before the task gets a chance to run;
Packit ae235b
 *   you can start your `task_func` with a
Packit ae235b
 *   g_task_return_error_if_cancelled() check if you need the
Packit ae235b
 *   old behavior.
Packit ae235b
 * - The "return" methods (eg, g_task_return_pointer())
Packit ae235b
 *   automatically cause the task to be "completed" as well, and
Packit ae235b
 *   there is no need to worry about the "complete" vs "complete
Packit ae235b
 *   in idle" distinction. (#GTask automatically figures out
Packit ae235b
 *   whether the task's callback can be invoked directly, or
Packit ae235b
 *   if it needs to be sent to another #GMainContext, or delayed
Packit ae235b
 *   until the next iteration of the current #GMainContext.)
Packit ae235b
 * - The "finish" functions for #GTask based operations are generally
Packit ae235b
 *   much simpler than #GSimpleAsyncResult ones, normally consisting
Packit ae235b
 *   of only a single call to g_task_propagate_pointer() or the like.
Packit ae235b
 *   Since g_task_propagate_pointer() "steals" the return value from
Packit ae235b
 *   the #GTask, it is not necessary to juggle pointers around to
Packit ae235b
 *   prevent it from being freed twice.
Packit ae235b
 * - With #GSimpleAsyncResult, it was common to call
Packit ae235b
 *   g_simple_async_result_propagate_error() from the
Packit ae235b
 *   `_finish()` wrapper function, and have
Packit ae235b
 *   virtual method implementations only deal with successful
Packit ae235b
 *   returns. This behavior is deprecated, because it makes it
Packit ae235b
 *   difficult for a subclass to chain to a parent class's async
Packit ae235b
 *   methods. Instead, the wrapper function should just be a
Packit ae235b
 *   simple wrapper, and the virtual method should call an
Packit ae235b
 *   appropriate `g_task_propagate_` function.
Packit ae235b
 *   Note that wrapper methods can now use
Packit ae235b
 *   g_async_result_legacy_propagate_error() to do old-style
Packit ae235b
 *   #GSimpleAsyncResult error-returning behavior, and
Packit ae235b
 *   g_async_result_is_tagged() to check if a result is tagged as
Packit ae235b
 *   having come from the `_async()` wrapper
Packit ae235b
 *   function (for "short-circuit" results, such as when passing
Packit ae235b
 *   0 to g_input_stream_read_async()).
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTask:
Packit ae235b
 *
Packit ae235b
 * The opaque object representing a synchronous or asynchronous task
Packit ae235b
 * and its result.
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GTask {
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  gpointer source_object;
Packit ae235b
  gpointer source_tag;
Packit ae235b
Packit ae235b
  gpointer task_data;
Packit ae235b
  GDestroyNotify task_data_destroy;
Packit ae235b
Packit ae235b
  GMainContext *context;
Packit ae235b
  gint64 creation_time;
Packit ae235b
  gint priority;
Packit ae235b
  GCancellable *cancellable;
Packit ae235b
  gboolean check_cancellable;
Packit ae235b
Packit ae235b
  GAsyncReadyCallback callback;
Packit ae235b
  gpointer callback_data;
Packit ae235b
  gboolean completed;
Packit ae235b
Packit ae235b
  GTaskThreadFunc task_func;
Packit ae235b
  GMutex lock;
Packit ae235b
  GCond cond;
Packit ae235b
  gboolean return_on_cancel;
Packit ae235b
  gboolean thread_cancelled;
Packit ae235b
  gboolean synchronous;
Packit ae235b
  gboolean thread_complete;
Packit ae235b
  gboolean blocking_other_task;
Packit ae235b
Packit ae235b
  GError *error;
Packit ae235b
  union {
Packit ae235b
    gpointer pointer;
Packit ae235b
    gssize   size;
Packit ae235b
    gboolean boolean;
Packit ae235b
  } result;
Packit ae235b
  GDestroyNotify result_destroy;
Packit ae235b
  gboolean had_error;
Packit ae235b
  gboolean result_set;
Packit ae235b
};
Packit ae235b
Packit ae235b
#define G_TASK_IS_THREADED(task) ((task)->task_func != NULL)
Packit ae235b
Packit ae235b
struct _GTaskClass
Packit ae235b
{
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  PROP_COMPLETED = 1,
Packit ae235b
} GTaskProperty;
Packit ae235b
Packit ae235b
static void g_task_async_result_iface_init (GAsyncResultIface *iface);
Packit ae235b
static void g_task_thread_pool_init (void);
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GTask, g_task, G_TYPE_OBJECT,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT,
Packit ae235b
                                                g_task_async_result_iface_init);
Packit ae235b
                         g_task_thread_pool_init ();)
Packit ae235b
Packit ae235b
static GThreadPool *task_pool;
Packit ae235b
static GMutex task_pool_mutex;
Packit ae235b
static GPrivate task_private = G_PRIVATE_INIT (NULL);
Packit ae235b
static GSource *task_pool_manager;
Packit ae235b
static guint64 task_wait_time;
Packit ae235b
static gint tasks_running;
Packit ae235b
Packit ae235b
/* When the task pool fills up and blocks, and the program keeps
Packit ae235b
 * queueing more tasks, we will slowly add more threads to the pool
Packit ae235b
 * (in case the existing tasks are trying to queue subtasks of their
Packit ae235b
 * own) until tasks start completing again. These "overflow" threads
Packit ae235b
 * will only run one task apiece, and then exit, so the pool will
Packit ae235b
 * eventually get back down to its base size.
Packit ae235b
 *
Packit ae235b
 * The base and multiplier below gives us 10 extra threads after about
Packit ae235b
 * a second of blocking, 30 after 5 seconds, 100 after a minute, and
Packit ae235b
 * 200 after 20 minutes.
Packit ae235b
 */
Packit ae235b
#define G_TASK_POOL_SIZE 10
Packit ae235b
#define G_TASK_WAIT_TIME_BASE 100000
Packit ae235b
#define G_TASK_WAIT_TIME_MULTIPLIER 1.03
Packit ae235b
#define G_TASK_WAIT_TIME_MAX (30 * 60 * 1000000)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_init (GTask *task)
Packit ae235b
{
Packit ae235b
  task->check_cancellable = TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GTask *task = G_TASK (object);
Packit ae235b
Packit ae235b
  g_clear_object (&task->source_object);
Packit ae235b
  g_clear_object (&task->cancellable);
Packit ae235b
Packit ae235b
  if (task->context)
Packit ae235b
    g_main_context_unref (task->context);
Packit ae235b
Packit ae235b
  if (task->task_data_destroy)
Packit ae235b
    task->task_data_destroy (task->task_data);
Packit ae235b
Packit ae235b
  if (task->result_destroy && task->result.pointer)
Packit ae235b
    task->result_destroy (task->result.pointer);
Packit ae235b
Packit ae235b
  if (task->error)
Packit ae235b
      g_error_free (task->error);
Packit ae235b
Packit ae235b
  if (G_TASK_IS_THREADED (task))
Packit ae235b
    {
Packit ae235b
      g_mutex_clear (&task->lock);
Packit ae235b
      g_cond_clear (&task->cond);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_task_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_new:
Packit ae235b
 * @source_object: (nullable) (type GObject): the #GObject that owns
Packit ae235b
 *   this task, or %NULL.
Packit ae235b
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @callback_data: (closure): user data passed to @callback.
Packit ae235b
 *
Packit ae235b
 * Creates a #GTask acting on @source_object, which will eventually be
Packit ae235b
 * used to invoke @callback in the current
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default].
Packit ae235b
 *
Packit ae235b
 * Call this in the "start" method of your asynchronous method, and
Packit ae235b
 * pass the #GTask around throughout the asynchronous operation. You
Packit ae235b
 * can use g_task_set_task_data() to attach task-specific data to the
Packit ae235b
 * object, which you can retrieve later via g_task_get_task_data().
Packit ae235b
 *
Packit ae235b
 * By default, if @cancellable is cancelled, then the return value of
Packit ae235b
 * the task will always be %G_IO_ERROR_CANCELLED, even if the task had
Packit ae235b
 * already completed before the cancellation. This allows for
Packit ae235b
 * simplified handling in cases where cancellation may imply that
Packit ae235b
 * other objects that the task depends on have been destroyed. If you
Packit ae235b
 * do not want this behavior, you can use
Packit ae235b
 * g_task_set_check_cancellable() to change it.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GTask.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GTask *
Packit ae235b
g_task_new (gpointer              source_object,
Packit ae235b
            GCancellable         *cancellable,
Packit ae235b
            GAsyncReadyCallback   callback,
Packit ae235b
            gpointer              callback_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  task = g_object_new (G_TYPE_TASK, NULL);
Packit ae235b
  task->source_object = source_object ? g_object_ref (source_object) : NULL;
Packit ae235b
  task->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
Packit ae235b
  task->callback = callback;
Packit ae235b
  task->callback_data = callback_data;
Packit ae235b
  task->context = g_main_context_ref_thread_default ();
Packit ae235b
Packit ae235b
  source = g_main_current_source ();
Packit ae235b
  if (source)
Packit ae235b
    task->creation_time = g_source_get_time (source);
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_NEW (task, source_object, cancellable,
Packit ae235b
                       callback, callback_data));
Packit ae235b
Packit ae235b
  return task;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_report_error:
Packit ae235b
 * @source_object: (nullable) (type GObject): the #GObject that owns
Packit ae235b
 *   this task, or %NULL.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @callback_data: (closure): user data passed to @callback.
Packit ae235b
 * @source_tag: an opaque pointer indicating the source of this task
Packit ae235b
 * @error: (transfer full): error to report
Packit ae235b
 *
Packit ae235b
 * Creates a #GTask and then immediately calls g_task_return_error()
Packit ae235b
 * on it. Use this in the wrapper function of an asynchronous method
Packit ae235b
 * when you want to avoid even calling the virtual method. You can
Packit ae235b
 * then use g_async_result_is_tagged() in the finish method wrapper to
Packit ae235b
 * check if the result there is tagged as having been created by the
Packit ae235b
 * wrapper method, and deal with it appropriately if so.
Packit ae235b
 *
Packit ae235b
 * See also g_task_report_new_error().
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_report_error (gpointer             source_object,
Packit ae235b
                     GAsyncReadyCallback  callback,
Packit ae235b
                     gpointer             callback_data,
Packit ae235b
                     gpointer             source_tag,
Packit ae235b
                     GError              *error)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (source_object, NULL, callback, callback_data);
Packit ae235b
  g_task_set_source_tag (task, source_tag);
Packit ae235b
  g_task_return_error (task, error);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_report_new_error:
Packit ae235b
 * @source_object: (nullable) (type GObject): the #GObject that owns
Packit ae235b
 *   this task, or %NULL.
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback.
Packit ae235b
 * @callback_data: (closure): user data passed to @callback.
Packit ae235b
 * @source_tag: an opaque pointer indicating the source of this task
Packit ae235b
 * @domain: a #GQuark.
Packit ae235b
 * @code: an error code.
Packit ae235b
 * @format: a string with format characters.
Packit ae235b
 * @...: a list of values to insert into @format.
Packit ae235b
 *
Packit ae235b
 * Creates a #GTask and then immediately calls
Packit ae235b
 * g_task_return_new_error() on it. Use this in the wrapper function
Packit ae235b
 * of an asynchronous method when you want to avoid even calling the
Packit ae235b
 * virtual method. You can then use g_async_result_is_tagged() in the
Packit ae235b
 * finish method wrapper to check if the result there is tagged as
Packit ae235b
 * having been created by the wrapper method, and deal with it
Packit ae235b
 * appropriately if so.
Packit ae235b
 *
Packit ae235b
 * See also g_task_report_error().
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_report_new_error (gpointer             source_object,
Packit ae235b
                         GAsyncReadyCallback  callback,
Packit ae235b
                         gpointer             callback_data,
Packit ae235b
                         gpointer             source_tag,
Packit ae235b
                         GQuark               domain,
Packit ae235b
                         gint                 code,
Packit ae235b
                         const char          *format,
Packit ae235b
                         ...)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  va_start (ap, format);
Packit ae235b
  error = g_error_new_valist (domain, code, format, ap);
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  g_task_report_error (source_object, callback, callback_data,
Packit ae235b
                       source_tag, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_set_task_data:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @task_data: (nullable): task-specific data
Packit ae235b
 * @task_data_destroy: (nullable): #GDestroyNotify for @task_data
Packit ae235b
 *
Packit ae235b
 * Sets @task's task data (freeing the existing task data, if any).
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_set_task_data (GTask          *task,
Packit ae235b
                      gpointer        task_data,
Packit ae235b
                      GDestroyNotify  task_data_destroy)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  if (task->task_data_destroy)
Packit ae235b
    task->task_data_destroy (task->task_data);
Packit ae235b
Packit ae235b
  task->task_data = task_data;
Packit ae235b
  task->task_data_destroy = task_data_destroy;
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_SET_TASK_DATA (task, task_data, task_data_destroy));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_set_priority:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @priority: the [priority][io-priority] of the request
Packit ae235b
 *
Packit ae235b
 * Sets @task's priority. If you do not call this, it will default to
Packit ae235b
 * %G_PRIORITY_DEFAULT.
Packit ae235b
 *
Packit ae235b
 * This will affect the priority of #GSources created with
Packit ae235b
 * g_task_attach_source() and the scheduling of tasks run in threads,
Packit ae235b
 * and can also be explicitly retrieved later via
Packit ae235b
 * g_task_get_priority().
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_set_priority (GTask *task,
Packit ae235b
                     gint   priority)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  task->priority = priority;
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_SET_PRIORITY (task, priority));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_set_check_cancellable:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @check_cancellable: whether #GTask will check the state of
Packit ae235b
 *   its #GCancellable for you.
Packit ae235b
 *
Packit ae235b
 * Sets or clears @task's check-cancellable flag. If this is %TRUE
Packit ae235b
 * (the default), then g_task_propagate_pointer(), etc, and
Packit ae235b
 * g_task_had_error() will check the task's #GCancellable first, and
Packit ae235b
 * if it has been cancelled, then they will consider the task to have
Packit ae235b
 * returned an "Operation was cancelled" error
Packit ae235b
 * (%G_IO_ERROR_CANCELLED), regardless of any other error or return
Packit ae235b
 * value the task may have had.
Packit ae235b
 *
Packit ae235b
 * If @check_cancellable is %FALSE, then the #GTask will not check the
Packit ae235b
 * cancellable itself, and it is up to @task's owner to do this (eg,
Packit ae235b
 * via g_task_return_error_if_cancelled()).
Packit ae235b
 *
Packit ae235b
 * If you are using g_task_set_return_on_cancel() as well, then
Packit ae235b
 * you must leave check-cancellable set %TRUE.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_set_check_cancellable (GTask    *task,
Packit ae235b
                              gboolean  check_cancellable)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
  g_return_if_fail (check_cancellable || !task->return_on_cancel);
Packit ae235b
Packit ae235b
  task->check_cancellable = check_cancellable;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void g_task_thread_complete (GTask *task);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_set_return_on_cancel:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @return_on_cancel: whether the task returns automatically when
Packit ae235b
 *   it is cancelled.
Packit ae235b
 *
Packit ae235b
 * Sets or clears @task's return-on-cancel flag. This is only
Packit ae235b
 * meaningful for tasks run via g_task_run_in_thread() or
Packit ae235b
 * g_task_run_in_thread_sync().
Packit ae235b
 *
Packit ae235b
 * If @return_on_cancel is %TRUE, then cancelling @task's
Packit ae235b
 * #GCancellable will immediately cause it to return, as though the
Packit ae235b
 * task's #GTaskThreadFunc had called
Packit ae235b
 * g_task_return_error_if_cancelled() and then returned.
Packit ae235b
 *
Packit ae235b
 * This allows you to create a cancellable wrapper around an
Packit ae235b
 * uninterruptable function. The #GTaskThreadFunc just needs to be
Packit ae235b
 * careful that it does not modify any externally-visible state after
Packit ae235b
 * it has been cancelled. To do that, the thread should call
Packit ae235b
 * g_task_set_return_on_cancel() again to (atomically) set
Packit ae235b
 * return-on-cancel %FALSE before making externally-visible changes;
Packit ae235b
 * if the task gets cancelled before the return-on-cancel flag could
Packit ae235b
 * be changed, g_task_set_return_on_cancel() will indicate this by
Packit ae235b
 * returning %FALSE.
Packit ae235b
 *
Packit ae235b
 * You can disable and re-enable this flag multiple times if you wish.
Packit ae235b
 * If the task's #GCancellable is cancelled while return-on-cancel is
Packit ae235b
 * %FALSE, then calling g_task_set_return_on_cancel() to set it %TRUE
Packit ae235b
 * again will cause the task to be cancelled at that point.
Packit ae235b
 *
Packit ae235b
 * If the task's #GCancellable is already cancelled before you call
Packit ae235b
 * g_task_run_in_thread()/g_task_run_in_thread_sync(), then the
Packit ae235b
 * #GTaskThreadFunc will still be run (for consistency), but the task
Packit ae235b
 * will also be completed right away.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @task's return-on-cancel flag was changed to
Packit ae235b
 *   match @return_on_cancel. %FALSE if @task has already been
Packit ae235b
 *   cancelled.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_set_return_on_cancel (GTask    *task,
Packit ae235b
                             gboolean  return_on_cancel)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
  g_return_val_if_fail (task->check_cancellable || !return_on_cancel, FALSE);
Packit ae235b
Packit ae235b
  if (!G_TASK_IS_THREADED (task))
Packit ae235b
    {
Packit ae235b
      task->return_on_cancel = return_on_cancel;
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_mutex_lock (&task->lock);
Packit ae235b
  if (task->thread_cancelled)
Packit ae235b
    {
Packit ae235b
      if (return_on_cancel && !task->return_on_cancel)
Packit ae235b
        {
Packit ae235b
          g_mutex_unlock (&task->lock);
Packit ae235b
          g_task_thread_complete (task);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_mutex_unlock (&task->lock);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  task->return_on_cancel = return_on_cancel;
Packit ae235b
  g_mutex_unlock (&task->lock);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_set_source_tag:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @source_tag: an opaque pointer indicating the source of this task
Packit ae235b
 *
Packit ae235b
 * Sets @task's source tag. You can use this to tag a task return
Packit ae235b
 * value with a particular pointer (usually a pointer to the function
Packit ae235b
 * doing the tagging) and then later check it using
Packit ae235b
 * g_task_get_source_tag() (or g_async_result_is_tagged()) in the
Packit ae235b
 * task's "finish" function, to figure out if the response came from a
Packit ae235b
 * particular place.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_set_source_tag (GTask    *task,
Packit ae235b
                       gpointer  source_tag)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  task->source_tag = source_tag;
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_SET_SOURCE_TAG (task, source_tag));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_source_object:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets the source object from @task. Like
Packit ae235b
 * g_async_result_get_source_object(), but does not ref the object.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none) (nullable) (type GObject): @task's source object, or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_task_get_source_object (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  return task->source_object;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GObject *
Packit ae235b
g_task_ref_source_object (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  GTask *task = G_TASK (res);
Packit ae235b
Packit ae235b
  if (task->source_object)
Packit ae235b
    return g_object_ref (task->source_object);
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_task_data:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's `task_data`.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @task's `task_data`.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_task_get_task_data (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  return task->task_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_priority:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's priority
Packit ae235b
 *
Packit ae235b
 * Returns: @task's priority
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_task_get_priority (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), G_PRIORITY_DEFAULT);
Packit ae235b
Packit ae235b
  return task->priority;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_context:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets the #GMainContext that @task will return its result in (that
Packit ae235b
 * is, the context that was the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * at the point when @task was created).
Packit ae235b
 *
Packit ae235b
 * This will always return a non-%NULL value, even if the task's
Packit ae235b
 * context is the default #GMainContext.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @task's #GMainContext
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GMainContext *
Packit ae235b
g_task_get_context (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  return task->context;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_cancellable:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's #GCancellable
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @task's #GCancellable
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
GCancellable *
Packit ae235b
g_task_get_cancellable (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  return task->cancellable;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_check_cancellable:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's check-cancellable flag. See
Packit ae235b
 * g_task_set_check_cancellable() for more details.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_get_check_cancellable (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
Packit ae235b
  return task->check_cancellable;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_return_on_cancel:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's return-on-cancel flag. See
Packit ae235b
 * g_task_set_return_on_cancel() for more details.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_get_return_on_cancel (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
Packit ae235b
  return task->return_on_cancel;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_source_tag:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Gets @task's source tag. See g_task_set_source_tag().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): @task's source tag
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_task_get_source_tag (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  return task->source_tag;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_return_now (GTask *task)
Packit ae235b
{
Packit ae235b
  TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object, task->callback,
Packit ae235b
                                 task->callback_data));
Packit ae235b
Packit ae235b
  g_main_context_push_thread_default (task->context);
Packit ae235b
Packit ae235b
  if (task->callback != NULL)
Packit ae235b
    {
Packit ae235b
      task->callback (task->source_object,
Packit ae235b
                      G_ASYNC_RESULT (task),
Packit ae235b
                      task->callback_data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  task->completed = TRUE;
Packit ae235b
  g_object_notify (G_OBJECT (task), "completed");
Packit ae235b
Packit ae235b
  g_main_context_pop_thread_default (task->context);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
complete_in_idle_cb (gpointer task)
Packit ae235b
{
Packit ae235b
  g_task_return_now (task);
Packit ae235b
  g_object_unref (task);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  G_TASK_RETURN_SUCCESS,
Packit ae235b
  G_TASK_RETURN_ERROR,
Packit ae235b
  G_TASK_RETURN_FROM_THREAD
Packit ae235b
} GTaskReturnType;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_return (GTask           *task,
Packit ae235b
               GTaskReturnType  type)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  if (type == G_TASK_RETURN_SUCCESS)
Packit ae235b
    task->result_set = TRUE;
Packit ae235b
Packit ae235b
  if (task->synchronous)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  /* Normally we want to invoke the task's callback when its return
Packit ae235b
   * value is set. But if the task is running in a thread, then we
Packit ae235b
   * want to wait until after the task_func returns, to simplify
Packit ae235b
   * locking/refcounting/etc.
Packit ae235b
   */
Packit ae235b
  if (G_TASK_IS_THREADED (task) && type != G_TASK_RETURN_FROM_THREAD)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_object_ref (task);
Packit ae235b
Packit ae235b
  /* See if we can complete the task immediately. First, we have to be
Packit ae235b
   * running inside the task's thread/GMainContext.
Packit ae235b
   */
Packit ae235b
  source = g_main_current_source ();
Packit ae235b
  if (source && g_source_get_context (source) == task->context)
Packit ae235b
    {
Packit ae235b
      /* Second, we can only complete immediately if this is not the
Packit ae235b
       * same iteration of the main loop that the task was created in.
Packit ae235b
       */
Packit ae235b
      if (g_source_get_time (source) > task->creation_time)
Packit ae235b
        {
Packit ae235b
          g_task_return_now (task);
Packit ae235b
          g_object_unref (task);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Otherwise, complete in the next iteration */
Packit ae235b
  source = g_idle_source_new ();
Packit ae235b
  g_source_set_name (source, "[gio] complete_in_idle_cb");
Packit ae235b
  g_task_attach_source (task, source, complete_in_idle_cb);
Packit ae235b
  g_source_unref (source);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTaskThreadFunc:
Packit ae235b
 * @task: the #GTask
Packit ae235b
 * @source_object: (type GObject): @task's source object
Packit ae235b
 * @task_data: @task's task data
Packit ae235b
 * @cancellable: @task's #GCancellable, or %NULL
Packit ae235b
 *
Packit ae235b
 * The prototype for a task function to be run in a thread via
Packit ae235b
 * g_task_run_in_thread() or g_task_run_in_thread_sync().
Packit ae235b
 *
Packit ae235b
 * If the return-on-cancel flag is set on @task, and @cancellable gets
Packit ae235b
 * cancelled, then the #GTask will be completed immediately (as though
Packit ae235b
 * g_task_return_error_if_cancelled() had been called), without
Packit ae235b
 * waiting for the task function to complete. However, the task
Packit ae235b
 * function will continue running in its thread in the background. The
Packit ae235b
 * function therefore needs to be careful about how it uses
Packit ae235b
 * externally-visible state in this case. See
Packit ae235b
 * g_task_set_return_on_cancel() for more details.
Packit ae235b
 *
Packit ae235b
 * Other than in that case, @task will be completed when the
Packit ae235b
 * #GTaskThreadFunc returns, not when it calls a
Packit ae235b
 * `g_task_return_` function.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void task_thread_cancelled (GCancellable *cancellable,
Packit ae235b
                                   gpointer      user_data);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_thread_complete (GTask *task)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&task->lock);
Packit ae235b
  if (task->thread_complete)
Packit ae235b
    {
Packit ae235b
      /* The task belatedly completed after having been cancelled
Packit ae235b
       * (or was cancelled in the midst of being completed).
Packit ae235b
       */
Packit ae235b
      g_mutex_unlock (&task->lock);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));
Packit ae235b
Packit ae235b
  task->thread_complete = TRUE;
Packit ae235b
  g_mutex_unlock (&task->lock);
Packit ae235b
Packit ae235b
  if (task->cancellable)
Packit ae235b
    g_signal_handlers_disconnect_by_func (task->cancellable, task_thread_cancelled, task);
Packit ae235b
Packit ae235b
  if (task->synchronous)
Packit ae235b
    g_cond_signal (&task->cond);
Packit ae235b
  else
Packit ae235b
    g_task_return (task, G_TASK_RETURN_FROM_THREAD);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
task_pool_manager_timeout (gpointer user_data)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&task_pool_mutex);
Packit ae235b
  g_thread_pool_set_max_threads (task_pool, tasks_running + 1, NULL);
Packit ae235b
  g_source_set_ready_time (task_pool_manager, -1);
Packit ae235b
  g_mutex_unlock (&task_pool_mutex);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_thread_setup (void)
Packit ae235b
{
Packit ae235b
  g_private_set (&task_private, GUINT_TO_POINTER (TRUE));
Packit ae235b
  g_mutex_lock (&task_pool_mutex);
Packit ae235b
  tasks_running++;
Packit ae235b
Packit ae235b
  if (tasks_running == G_TASK_POOL_SIZE)
Packit ae235b
    task_wait_time = G_TASK_WAIT_TIME_BASE;
Packit ae235b
  else if (tasks_running > G_TASK_POOL_SIZE && task_wait_time < G_TASK_WAIT_TIME_MAX)
Packit ae235b
    task_wait_time *= G_TASK_WAIT_TIME_MULTIPLIER;
Packit ae235b
Packit ae235b
  if (tasks_running >= G_TASK_POOL_SIZE)
Packit ae235b
    g_source_set_ready_time (task_pool_manager, g_get_monotonic_time () + task_wait_time);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&task_pool_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_thread_cleanup (void)
Packit ae235b
{
Packit ae235b
  gint tasks_pending;
Packit ae235b
Packit ae235b
  g_mutex_lock (&task_pool_mutex);
Packit ae235b
  tasks_pending = g_thread_pool_unprocessed (task_pool);
Packit ae235b
Packit ae235b
  if (tasks_running > G_TASK_POOL_SIZE)
Packit ae235b
    g_thread_pool_set_max_threads (task_pool, tasks_running - 1, NULL);
Packit ae235b
  else if (tasks_running + tasks_pending < G_TASK_POOL_SIZE)
Packit ae235b
    g_source_set_ready_time (task_pool_manager, -1);
Packit ae235b
Packit ae235b
  tasks_running--;
Packit ae235b
  g_mutex_unlock (&task_pool_mutex);
Packit ae235b
  g_private_set (&task_private, GUINT_TO_POINTER (FALSE));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_thread_pool_thread (gpointer thread_data,
Packit ae235b
                           gpointer pool_data)
Packit ae235b
{
Packit ae235b
  GTask *task = thread_data;
Packit ae235b
Packit ae235b
  g_task_thread_setup ();
Packit ae235b
Packit ae235b
  task->task_func (task, task->source_object, task->task_data,
Packit ae235b
                   task->cancellable);
Packit ae235b
  g_task_thread_complete (task);
Packit ae235b
  g_object_unref (task);
Packit ae235b
Packit ae235b
  g_task_thread_cleanup ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
task_thread_cancelled (GCancellable *cancellable,
Packit ae235b
                       gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
Packit ae235b
  /* Move this task to the front of the queue - no need for
Packit ae235b
   * a complete resorting of the queue.
Packit ae235b
   */
Packit ae235b
  g_thread_pool_move_to_front (task_pool, task);
Packit ae235b
Packit ae235b
  g_mutex_lock (&task->lock);
Packit ae235b
  task->thread_cancelled = TRUE;
Packit ae235b
Packit ae235b
  if (!task->return_on_cancel)
Packit ae235b
    {
Packit ae235b
      g_mutex_unlock (&task->lock);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We don't actually set task->error; g_task_return_error() doesn't
Packit ae235b
   * use a lock, and g_task_propagate_error() will call
Packit ae235b
   * g_cancellable_set_error_if_cancelled() anyway.
Packit ae235b
   */
Packit ae235b
  g_mutex_unlock (&task->lock);
Packit ae235b
  g_task_thread_complete (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
task_thread_cancelled_disconnect_notify (gpointer  task,
Packit ae235b
                                         GClosure *closure)
Packit ae235b
{
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_start_task_thread (GTask           *task,
Packit ae235b
                          GTaskThreadFunc  task_func)
Packit ae235b
{
Packit ae235b
  g_mutex_init (&task->lock);
Packit ae235b
  g_cond_init (&task->cond);
Packit ae235b
Packit ae235b
  g_mutex_lock (&task->lock);
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_BEFORE_RUN_IN_THREAD (task, task_func));
Packit ae235b
Packit ae235b
  task->task_func = task_func;
Packit ae235b
Packit ae235b
  if (task->cancellable)
Packit ae235b
    {
Packit ae235b
      if (task->return_on_cancel &&
Packit ae235b
          g_cancellable_set_error_if_cancelled (task->cancellable,
Packit ae235b
                                                &task->error))
Packit ae235b
        {
Packit ae235b
          task->thread_cancelled = task->thread_complete = TRUE;
Packit ae235b
          TRACE (GIO_TASK_AFTER_RUN_IN_THREAD (task, task->thread_cancelled));
Packit ae235b
          g_thread_pool_push (task_pool, g_object_ref (task), NULL);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* This introduces a reference count loop between the GTask and
Packit ae235b
       * GCancellable, but is necessary to avoid a race on finalising the GTask
Packit ae235b
       * between task_thread_cancelled() (in one thread) and
Packit ae235b
       * g_task_thread_complete() (in another).
Packit ae235b
       *
Packit ae235b
       * Accordingly, the signal handler *must* be removed once the task has
Packit ae235b
       * completed.
Packit ae235b
       */
Packit ae235b
      g_signal_connect_data (task->cancellable, "cancelled",
Packit ae235b
                             G_CALLBACK (task_thread_cancelled),
Packit ae235b
                             g_object_ref (task),
Packit ae235b
                             task_thread_cancelled_disconnect_notify, 0);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_private_get (&task_private))
Packit ae235b
    task->blocking_other_task = TRUE;
Packit ae235b
  g_thread_pool_push (task_pool, g_object_ref (task), NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_run_in_thread:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 * @task_func: a #GTaskThreadFunc
Packit ae235b
 *
Packit ae235b
 * Runs @task_func in another thread. When @task_func returns, @task's
Packit ae235b
 * #GAsyncReadyCallback will be invoked in @task's #GMainContext.
Packit ae235b
 *
Packit ae235b
 * This takes a ref on @task until the task completes.
Packit ae235b
 *
Packit ae235b
 * See #GTaskThreadFunc for more details about how @task_func is handled.
Packit ae235b
 *
Packit ae235b
 * Although GLib currently rate-limits the tasks queued via
Packit ae235b
 * g_task_run_in_thread(), you should not assume that it will always
Packit ae235b
 * do this. If you have a very large number of tasks to run, but don't
Packit ae235b
 * want them to all run at once, you should only queue a limited
Packit ae235b
 * number of them at a time.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_run_in_thread (GTask           *task,
Packit ae235b
                      GTaskThreadFunc  task_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  g_object_ref (task);
Packit ae235b
  g_task_start_task_thread (task, task_func);
Packit ae235b
Packit ae235b
  /* The task may already be cancelled, or g_thread_pool_push() may
Packit ae235b
   * have failed.
Packit ae235b
   */
Packit ae235b
  if (task->thread_complete)
Packit ae235b
    {
Packit ae235b
      g_mutex_unlock (&task->lock);
Packit ae235b
      g_task_return (task, G_TASK_RETURN_FROM_THREAD);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_mutex_unlock (&task->lock);
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_run_in_thread_sync:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 * @task_func: a #GTaskThreadFunc
Packit ae235b
 *
Packit ae235b
 * Runs @task_func in another thread, and waits for it to return or be
Packit ae235b
 * cancelled. You can use g_task_propagate_pointer(), etc, afterward
Packit ae235b
 * to get the result of @task_func.
Packit ae235b
 *
Packit ae235b
 * See #GTaskThreadFunc for more details about how @task_func is handled.
Packit ae235b
 *
Packit ae235b
 * Normally this is used with tasks created with a %NULL
Packit ae235b
 * `callback`, but note that even if the task does
Packit ae235b
 * have a callback, it will not be invoked when @task_func returns.
Packit ae235b
 * #GTask:completed will be set to %TRUE just before this function returns.
Packit ae235b
 *
Packit ae235b
 * Although GLib currently rate-limits the tasks queued via
Packit ae235b
 * g_task_run_in_thread_sync(), you should not assume that it will
Packit ae235b
 * always do this. If you have a very large number of tasks to run,
Packit ae235b
 * but don't want them to all run at once, you should only queue a
Packit ae235b
 * limited number of them at a time.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_run_in_thread_sync (GTask           *task,
Packit ae235b
                           GTaskThreadFunc  task_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  g_object_ref (task);
Packit ae235b
Packit ae235b
  task->synchronous = TRUE;
Packit ae235b
  g_task_start_task_thread (task, task_func);
Packit ae235b
Packit ae235b
  while (!task->thread_complete)
Packit ae235b
    g_cond_wait (&task->cond, &task->lock);
Packit ae235b
Packit ae235b
  g_mutex_unlock (&task->lock);
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_BEFORE_RETURN (task, task->source_object,
Packit ae235b
                                 NULL  /* callback */,
Packit ae235b
                                 NULL  /* callback data */));
Packit ae235b
Packit ae235b
  /* Notify of completion in this thread. */
Packit ae235b
  task->completed = TRUE;
Packit ae235b
  g_object_notify (G_OBJECT (task), "completed");
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_attach_source:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 * @source: the source to attach
Packit ae235b
 * @callback: the callback to invoke when @source triggers
Packit ae235b
 *
Packit ae235b
 * A utility function for dealing with async operations where you need
Packit ae235b
 * to wait for a #GSource to trigger. Attaches @source to @task's
Packit ae235b
 * #GMainContext with @task's [priority][io-priority], and sets @source's
Packit ae235b
 * callback to @callback, with @task as the callback's `user_data`.
Packit ae235b
 *
Packit ae235b
 * This takes a reference on @task until @source is destroyed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_attach_source (GTask       *task,
Packit ae235b
                      GSource     *source,
Packit ae235b
                      GSourceFunc  callback)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
Packit ae235b
  g_source_set_callback (source, callback,
Packit ae235b
                         g_object_ref (task), g_object_unref);
Packit ae235b
  g_source_set_priority (source, task->priority);
Packit ae235b
  g_source_attach (source, task->context);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_task_propagate_error (GTask   *task,
Packit ae235b
                        GError **error)
Packit ae235b
{
Packit ae235b
  gboolean error_set;
Packit ae235b
Packit ae235b
  if (task->check_cancellable &&
Packit ae235b
      g_cancellable_set_error_if_cancelled (task->cancellable, error))
Packit ae235b
    error_set = TRUE;
Packit ae235b
  else if (task->error)
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, task->error);
Packit ae235b
      task->error = NULL;
Packit ae235b
      task->had_error = TRUE;
Packit ae235b
      error_set = TRUE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    error_set = FALSE;
Packit ae235b
Packit ae235b
  TRACE (GIO_TASK_PROPAGATE (task, error_set));
Packit ae235b
Packit ae235b
  return error_set;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_pointer:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 * @result: (nullable) (transfer full): the pointer result of a task
Packit ae235b
 *     function
Packit ae235b
 * @result_destroy: (nullable): a #GDestroyNotify function.
Packit ae235b
 *
Packit ae235b
 * Sets @task's result to @result and completes the task. If @result
Packit ae235b
 * is not %NULL, then @result_destroy will be used to free @result if
Packit ae235b
 * the caller does not take ownership of it with
Packit ae235b
 * g_task_propagate_pointer().
Packit ae235b
 *
Packit ae235b
 * "Completes the task" means that for an ordinary asynchronous task
Packit ae235b
 * it will either invoke the task's callback, or else queue that
Packit ae235b
 * callback to be invoked in the proper #GMainContext, or in the next
Packit ae235b
 * iteration of the current #GMainContext. For a task run via
Packit ae235b
 * g_task_run_in_thread() or g_task_run_in_thread_sync(), calling this
Packit ae235b
 * method will save @result to be returned to the caller later, but
Packit ae235b
 * the task will not actually be completed until the #GTaskThreadFunc
Packit ae235b
 * exits.
Packit ae235b
 *
Packit ae235b
 * Note that since the task may be completed before returning from
Packit ae235b
 * g_task_return_pointer(), you cannot assume that @result is still
Packit ae235b
 * valid after calling this, unless you are still holding another
Packit ae235b
 * reference on it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_return_pointer (GTask          *task,
Packit ae235b
                       gpointer        result,
Packit ae235b
                       GDestroyNotify  result_destroy)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
  g_return_if_fail (task->result_set == FALSE);
Packit ae235b
Packit ae235b
  task->result.pointer = result;
Packit ae235b
  task->result_destroy = result_destroy;
Packit ae235b
Packit ae235b
  g_task_return (task, G_TASK_RETURN_SUCCESS);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_propagate_pointer:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Gets the result of @task as a pointer, and transfers ownership
Packit ae235b
 * of that value to the caller.
Packit ae235b
 *
Packit ae235b
 * If the task resulted in an error, or was cancelled, then this will
Packit ae235b
 * instead return %NULL and set @error.
Packit ae235b
 *
Packit ae235b
 * Since this method transfers ownership of the return value (or
Packit ae235b
 * error) to the caller, you may only call it once.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the task result, or %NULL on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_task_propagate_pointer (GTask   *task,
Packit ae235b
                          GError **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), NULL);
Packit ae235b
Packit ae235b
  if (g_task_propagate_error (task, error))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (task->result_set == TRUE, NULL);
Packit ae235b
Packit ae235b
  task->result_destroy = NULL;
Packit ae235b
  task->result_set = FALSE;
Packit ae235b
  return task->result.pointer;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_int:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @result: the integer (#gssize) result of a task function.
Packit ae235b
 *
Packit ae235b
 * Sets @task's result to @result and completes the task (see
Packit ae235b
 * g_task_return_pointer() for more discussion of exactly what this
Packit ae235b
 * means).
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_return_int (GTask  *task,
Packit ae235b
                   gssize  result)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
  g_return_if_fail (task->result_set == FALSE);
Packit ae235b
Packit ae235b
  task->result.size = result;
Packit ae235b
Packit ae235b
  g_task_return (task, G_TASK_RETURN_SUCCESS);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_propagate_int:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Gets the result of @task as an integer (#gssize).
Packit ae235b
 *
Packit ae235b
 * If the task resulted in an error, or was cancelled, then this will
Packit ae235b
 * instead return -1 and set @error.
Packit ae235b
 *
Packit ae235b
 * Since this method transfers ownership of the return value (or
Packit ae235b
 * error) to the caller, you may only call it once.
Packit ae235b
 *
Packit ae235b
 * Returns: the task result, or -1 on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gssize
Packit ae235b
g_task_propagate_int (GTask   *task,
Packit ae235b
                      GError **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), -1);
Packit ae235b
Packit ae235b
  if (g_task_propagate_error (task, error))
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (task->result_set == TRUE, -1);
Packit ae235b
Packit ae235b
  task->result_set = FALSE;
Packit ae235b
  return task->result.size;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_boolean:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @result: the #gboolean result of a task function.
Packit ae235b
 *
Packit ae235b
 * Sets @task's result to @result and completes the task (see
Packit ae235b
 * g_task_return_pointer() for more discussion of exactly what this
Packit ae235b
 * means).
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_return_boolean (GTask    *task,
Packit ae235b
                       gboolean  result)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
  g_return_if_fail (task->result_set == FALSE);
Packit ae235b
Packit ae235b
  task->result.boolean = result;
Packit ae235b
Packit ae235b
  g_task_return (task, G_TASK_RETURN_SUCCESS);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_propagate_boolean:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Gets the result of @task as a #gboolean.
Packit ae235b
 *
Packit ae235b
 * If the task resulted in an error, or was cancelled, then this will
Packit ae235b
 * instead return %FALSE and set @error.
Packit ae235b
 *
Packit ae235b
 * Since this method transfers ownership of the return value (or
Packit ae235b
 * error) to the caller, you may only call it once.
Packit ae235b
 *
Packit ae235b
 * Returns: the task result, or %FALSE on error
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_propagate_boolean (GTask   *task,
Packit ae235b
                          GError **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
Packit ae235b
  if (g_task_propagate_error (task, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (task->result_set == TRUE, FALSE);
Packit ae235b
Packit ae235b
  task->result_set = FALSE;
Packit ae235b
  return task->result.boolean;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_error:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @error: (transfer full): the #GError result of a task function.
Packit ae235b
 *
Packit ae235b
 * Sets @task's result to @error (which @task assumes ownership of)
Packit ae235b
 * and completes the task (see g_task_return_pointer() for more
Packit ae235b
 * discussion of exactly what this means).
Packit ae235b
 *
Packit ae235b
 * Note that since the task takes ownership of @error, and since the
Packit ae235b
 * task may be completed before returning from g_task_return_error(),
Packit ae235b
 * you cannot assume that @error is still valid after calling this.
Packit ae235b
 * Call g_error_copy() on the error if you need to keep a local copy
Packit ae235b
 * as well.
Packit ae235b
 *
Packit ae235b
 * See also g_task_return_new_error().
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_return_error (GTask  *task,
Packit ae235b
                     GError *error)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_TASK (task));
Packit ae235b
  g_return_if_fail (task->result_set == FALSE);
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  task->error = error;
Packit ae235b
Packit ae235b
  g_task_return (task, G_TASK_RETURN_ERROR);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_new_error:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 * @domain: a #GQuark.
Packit ae235b
 * @code: an error code.
Packit ae235b
 * @format: a string with format characters.
Packit ae235b
 * @...: a list of values to insert into @format.
Packit ae235b
 *
Packit ae235b
 * Sets @task's result to a new #GError created from @domain, @code,
Packit ae235b
 * @format, and the remaining arguments, and completes the task (see
Packit ae235b
 * g_task_return_pointer() for more discussion of exactly what this
Packit ae235b
 * means).
Packit ae235b
 *
Packit ae235b
 * See also g_task_return_error().
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_task_return_new_error (GTask           *task,
Packit ae235b
                         GQuark           domain,
Packit ae235b
                         gint             code,
Packit ae235b
                         const char      *format,
Packit ae235b
                         ...)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  error = g_error_new_valist (domain, code, format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_task_return_error (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_return_error_if_cancelled:
Packit ae235b
 * @task: a #GTask
Packit ae235b
 *
Packit ae235b
 * Checks if @task's #GCancellable has been cancelled, and if so, sets
Packit ae235b
 * @task's error accordingly and completes the task (see
Packit ae235b
 * g_task_return_pointer() for more discussion of exactly what this
Packit ae235b
 * means).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @task has been cancelled, %FALSE if not
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_return_error_if_cancelled (GTask *task)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
  g_return_val_if_fail (task->result_set == FALSE, FALSE);
Packit ae235b
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (task->cancellable, &error))
Packit ae235b
    {
Packit ae235b
      /* We explicitly set task->error so this works even when
Packit ae235b
       * check-cancellable is not set.
Packit ae235b
       */
Packit ae235b
      g_clear_error (&task->error);
Packit ae235b
      task->error = error;
Packit ae235b
Packit ae235b
      g_task_return (task, G_TASK_RETURN_ERROR);
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_had_error:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 *
Packit ae235b
 * Tests if @task resulted in an error.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the task resulted in an error, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_had_error (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
Packit ae235b
  if (task->error != NULL || task->had_error)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (task->check_cancellable && g_cancellable_is_cancelled (task->cancellable))
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_get_completed:
Packit ae235b
 * @task: a #GTask.
Packit ae235b
 *
Packit ae235b
 * Gets the value of #GTask:completed. This changes from %FALSE to %TRUE after
Packit ae235b
 * the task’s callback is invoked, and will return %FALSE if called from inside
Packit ae235b
 * the callback.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the task has completed, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_get_completed (GTask *task)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_TASK (task), FALSE);
Packit ae235b
Packit ae235b
  return task->completed;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_task_is_valid:
Packit ae235b
 * @result: (type Gio.AsyncResult): A #GAsyncResult
Packit ae235b
 * @source_object: (nullable) (type GObject): the source object
Packit ae235b
 *   expected to be associated with the task
Packit ae235b
 *
Packit ae235b
 * Checks that @result is a #GTask, and that @source_object is its
Packit ae235b
 * source object (or that @source_object is %NULL and @result has no
Packit ae235b
 * source object). This can be used in g_return_if_fail() checks.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @result and @source_object are valid, %FALSE
Packit ae235b
 * if not
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_task_is_valid (gpointer result,
Packit ae235b
                 gpointer source_object)
Packit ae235b
{
Packit ae235b
  if (!G_IS_TASK (result))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  return G_TASK (result)->source_object == source_object;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
g_task_compare_priority (gconstpointer a,
Packit ae235b
                         gconstpointer b,
Packit ae235b
                         gpointer      user_data)
Packit ae235b
{
Packit ae235b
  const GTask *ta = a;
Packit ae235b
  const GTask *tb = b;
Packit ae235b
  gboolean a_cancelled, b_cancelled;
Packit ae235b
Packit ae235b
  /* Tasks that are causing other tasks to block have higher
Packit ae235b
   * priority.
Packit ae235b
   */
Packit ae235b
  if (ta->blocking_other_task && !tb->blocking_other_task)
Packit ae235b
    return -1;
Packit ae235b
  else if (tb->blocking_other_task && !ta->blocking_other_task)
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  /* Let already-cancelled tasks finish right away */
Packit ae235b
  a_cancelled = (ta->check_cancellable &&
Packit ae235b
                 g_cancellable_is_cancelled (ta->cancellable));
Packit ae235b
  b_cancelled = (tb->check_cancellable &&
Packit ae235b
                 g_cancellable_is_cancelled (tb->cancellable));
Packit ae235b
  if (a_cancelled && !b_cancelled)
Packit ae235b
    return -1;
Packit ae235b
  else if (b_cancelled && !a_cancelled)
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  /* Lower priority == run sooner == negative return value */
Packit ae235b
  return ta->priority - tb->priority;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
trivial_source_dispatch (GSource     *source,
Packit ae235b
                         GSourceFunc  callback,
Packit ae235b
                         gpointer     user_data)
Packit ae235b
{
Packit ae235b
  return callback (user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
GSourceFuncs trivial_source_funcs = {
Packit ae235b
  NULL, /* prepare */
Packit ae235b
  NULL, /* check */
Packit ae235b
  trivial_source_dispatch,
Packit ae235b
  NULL
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_thread_pool_init (void)
Packit ae235b
{
Packit ae235b
  task_pool = g_thread_pool_new (g_task_thread_pool_thread, NULL,
Packit ae235b
                                 G_TASK_POOL_SIZE, FALSE, NULL);
Packit ae235b
  g_assert (task_pool != NULL);
Packit ae235b
Packit ae235b
  g_thread_pool_set_sort_function (task_pool, g_task_compare_priority, NULL);
Packit ae235b
Packit ae235b
  task_pool_manager = g_source_new (&trivial_source_funcs, sizeof (GSource));
Packit ae235b
  g_source_set_callback (task_pool_manager, task_pool_manager_timeout, NULL, NULL);
Packit ae235b
  g_source_set_ready_time (task_pool_manager, -1);
Packit ae235b
  g_source_attach (task_pool_manager,
Packit ae235b
                   GLIB_PRIVATE_CALL (g_get_worker_context ()));
Packit ae235b
  g_source_unref (task_pool_manager);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_get_property (GObject    *object,
Packit ae235b
                     guint       prop_id,
Packit ae235b
                     GValue     *value,
Packit ae235b
                     GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GTask *task = G_TASK (object);
Packit ae235b
Packit ae235b
  switch ((GTaskProperty) prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_COMPLETED:
Packit ae235b
      g_value_set_boolean (value, task->completed);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_class_init (GTaskClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->get_property = g_task_get_property;
Packit ae235b
  gobject_class->finalize = g_task_finalize;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GTask:completed:
Packit ae235b
   *
Packit ae235b
   * Whether the task has completed, meaning its callback (if set) has been
Packit ae235b
   * invoked. This can only happen after g_task_return_pointer(),
Packit ae235b
   * g_task_return_error() or one of the other return functions have been called
Packit ae235b
   * on the task.
Packit ae235b
   *
Packit ae235b
   * This property is guaranteed to change from %FALSE to %TRUE exactly once.
Packit ae235b
   *
Packit ae235b
   * The #GObject::notify signal for this change is emitted in the same main
Packit ae235b
   * context as the task’s callback, immediately after that callback is invoked.
Packit ae235b
   *
Packit ae235b
   * Since: 2.44
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class, PROP_COMPLETED,
Packit ae235b
    g_param_spec_boolean ("completed",
Packit ae235b
                          P_("Task completed"),
Packit ae235b
                          P_("Whether the task has completed yet"),
Packit ae235b
                          FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
g_task_get_user_data (GAsyncResult *res)
Packit ae235b
{
Packit ae235b
  return G_TASK (res)->callback_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_task_is_tagged (GAsyncResult *res,
Packit ae235b
                  gpointer      source_tag)
Packit ae235b
{
Packit ae235b
  return G_TASK (res)->source_tag == source_tag;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_task_async_result_iface_init (GAsyncResultIface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_user_data = g_task_get_user_data;
Packit ae235b
  iface->get_source_object = g_task_ref_source_object;
Packit ae235b
  iface->is_tagged = g_task_is_tagged;
Packit ae235b
}