Functions
g_task_new ()
GTask *
g_task_new (gpointer source_object,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer callback_data);
Creates a GTask acting on source_object
, which will eventually be
used to invoke callback
in the current
thread-default main context.
Call this in the "start" method of your asynchronous method, and
pass the GTask around throughout the asynchronous operation. You
can use g_task_set_task_data() to attach task-specific data to the
object, which you can retrieve later via g_task_get_task_data().
By default, if cancellable
is cancelled, then the return value of
the task will always be G_IO_ERROR_CANCELLED, even if the task had
already completed before the cancellation. This allows for
simplified handling in cases where cancellation may imply that
other objects that the task depends on have been destroyed. If you
do not want this behavior, you can use
g_task_set_check_cancellable() to change it.
Since: 2.36
g_task_set_task_data ()
void
g_task_set_task_data (GTask *task,
gpointer task_data,
GDestroyNotify task_data_destroy);
Sets task
's task data (freeing the existing task data, if any).
Since: 2.36
g_task_set_priority ()
void
g_task_set_priority (GTask *task,
gint priority);
Sets task
's priority. If you do not call this, it will default to
G_PRIORITY_DEFAULT.
This will affect the priority of GSources created with
g_task_attach_source() and the scheduling of tasks run in threads,
and can also be explicitly retrieved later via
g_task_get_priority().
Since: 2.36
g_task_set_check_cancellable ()
void
g_task_set_check_cancellable (GTask *task,
gboolean check_cancellable);
Sets or clears task
's check-cancellable flag. If this is TRUE
(the default), then g_task_propagate_pointer(), etc, and
g_task_had_error() will check the task's GCancellable first, and
if it has been cancelled, then they will consider the task to have
returned an "Operation was cancelled" error
(G_IO_ERROR_CANCELLED), regardless of any other error or return
value the task may have had.
If check_cancellable
is FALSE, then the GTask will not check the
cancellable itself, and it is up to task
's owner to do this (eg,
via g_task_return_error_if_cancelled()).
If you are using g_task_set_return_on_cancel() as well, then
you must leave check-cancellable set TRUE.
Since: 2.36
g_task_set_return_on_cancel ()
gboolean
g_task_set_return_on_cancel (GTask *task,
gboolean return_on_cancel);
Sets or clears task
's return-on-cancel flag. This is only
meaningful for tasks run via g_task_run_in_thread() or
g_task_run_in_thread_sync().
If return_on_cancel
is TRUE, then cancelling task
's
GCancellable will immediately cause it to return, as though the
task's GTaskThreadFunc had called
g_task_return_error_if_cancelled() and then returned.
This allows you to create a cancellable wrapper around an
uninterruptable function. The GTaskThreadFunc just needs to be
careful that it does not modify any externally-visible state after
it has been cancelled. To do that, the thread should call
g_task_set_return_on_cancel() again to (atomically) set
return-on-cancel FALSE before making externally-visible changes;
if the task gets cancelled before the return-on-cancel flag could
be changed, g_task_set_return_on_cancel() will indicate this by
returning FALSE.
You can disable and re-enable this flag multiple times if you wish.
If the task's GCancellable is cancelled while return-on-cancel is
FALSE, then calling g_task_set_return_on_cancel() to set it TRUE
again will cause the task to be cancelled at that point.
If the task's GCancellable is already cancelled before you call
g_task_run_in_thread()/g_task_run_in_thread_sync(), then the
GTaskThreadFunc will still be run (for consistency), but the task
will also be completed right away.
Returns
TRUE if task
's return-on-cancel flag was changed to
match return_on_cancel
. FALSE if task
has already been
cancelled.
Since: 2.36
g_task_set_source_tag ()
void
g_task_set_source_tag (GTask *task,
gpointer source_tag);
Sets task
's source tag. You can use this to tag a task return
value with a particular pointer (usually a pointer to the function
doing the tagging) and then later check it using
g_task_get_source_tag() (or g_async_result_is_tagged()) in the
task's "finish" function, to figure out if the response came from a
particular place.
Since: 2.36
g_task_set_name ()
void
g_task_set_name (GTask *task,
const gchar *name);
Sets task
’s name, used in debugging and profiling. The name defaults to
NULL.
The task name should describe in a human readable way what the task does.
For example, ‘Open file’ or ‘Connect to network host’. It is used to set the
name of the GSource used for idle completion of the task.
This function may only be called before the task
is first used in a thread
other than the one it was constructed in.
Since: 2.60
g_task_report_error ()
void
g_task_report_error (gpointer source_object,
GAsyncReadyCallback callback,
gpointer callback_data,
gpointer source_tag,
GError *error);
Creates a GTask and then immediately calls g_task_return_error()
on it. Use this in the wrapper function of an asynchronous method
when you want to avoid even calling the virtual method. You can
then use g_async_result_is_tagged() in the finish method wrapper to
check if the result there is tagged as having been created by the
wrapper method, and deal with it appropriately if so.
See also g_task_report_new_error().
Since: 2.36
g_task_report_new_error ()
void
g_task_report_new_error (gpointer source_object,
GAsyncReadyCallback callback,
gpointer callback_data,
gpointer source_tag,
GQuark domain,
gint code,
const char *format,
...);
Creates a GTask and then immediately calls
g_task_return_new_error() on it. Use this in the wrapper function
of an asynchronous method when you want to avoid even calling the
virtual method. You can then use g_async_result_is_tagged() in the
finish method wrapper to check if the result there is tagged as
having been created by the wrapper method, and deal with it
appropriately if so.
See also g_task_report_error().
Since: 2.36
g_task_get_task_data ()
gpointer
g_task_get_task_data (GTask *task);
Gets task
's task_data.
Returns
task
's task_data.
[transfer none]
Since: 2.36
g_task_get_priority ()
gint
g_task_get_priority (GTask *task);
Gets task
's priority
Since: 2.36
g_task_get_context ()
GMainContext *
g_task_get_context (GTask *task);
Gets the GMainContext that task
will return its result in (that
is, the context that was the
thread-default main context
at the point when task
was created).
This will always return a non-NULL value, even if the task's
context is the default GMainContext.
Returns
task
's GMainContext.
[transfer none]
Since: 2.36
g_task_get_source_object ()
gpointer
g_task_get_source_object (GTask *task);
Gets the source object from task
. Like
g_async_result_get_source_object(), but does not ref the object.
Returns
task
's source object, or NULL.
[transfer none][nullable][type GObject]
Since: 2.36
g_task_get_source_tag ()
gpointer
g_task_get_source_tag (GTask *task);
Gets task
's source tag. See g_task_set_source_tag().
Returns
task
's source tag.
[transfer none]
Since: 2.36
g_task_get_name ()
const gchar *
g_task_get_name (GTask *task);
Gets task
’s name. See g_task_set_name().
Returns
task
’s name, or NULL.
[nullable][transfer none]
Since: 2.60
g_task_return_boolean ()
void
g_task_return_boolean (GTask *task,
gboolean result);
Sets task
's result to result
and completes the task (see
g_task_return_pointer() for more discussion of exactly what this
means).
Since: 2.36
g_task_return_int ()
void
g_task_return_int (GTask *task,
gssize result);
Sets task
's result to result
and completes the task (see
g_task_return_pointer() for more discussion of exactly what this
means).
Since: 2.36
g_task_return_pointer ()
void
g_task_return_pointer (GTask *task,
gpointer result,
GDestroyNotify result_destroy);
Sets task
's result to result
and completes the task. If result
is not NULL, then result_destroy
will be used to free result
if
the caller does not take ownership of it with
g_task_propagate_pointer().
"Completes the task" means that for an ordinary asynchronous task
it will either invoke the task's callback, or else queue that
callback to be invoked in the proper GMainContext, or in the next
iteration of the current GMainContext. For a task run via
g_task_run_in_thread() or g_task_run_in_thread_sync(), calling this
method will save result
to be returned to the caller later, but
the task will not actually be completed until the GTaskThreadFunc
exits.
Note that since the task may be completed before returning from
g_task_return_pointer(), you cannot assume that result
is still
valid after calling this, unless you are still holding another
reference on it.
Since: 2.36
g_task_return_value ()
void
g_task_return_value (GTask *task,
GValue *result);
Sets task
's result to result
(by copying it) and completes the task.
If result
is NULL then a GValue of type G_TYPE_POINTER
with a value of NULL will be used for the result.
This is a very generic low-level method intended primarily for use
by language bindings; for C code, g_task_return_pointer() and the
like will normally be much easier to use.
Since: 2.64
g_task_return_error ()
void
g_task_return_error (GTask *task,
GError *error);
Sets task
's result to error
(which task
assumes ownership of)
and completes the task (see g_task_return_pointer() for more
discussion of exactly what this means).
Note that since the task takes ownership of error
, and since the
task may be completed before returning from g_task_return_error(),
you cannot assume that error
is still valid after calling this.
Call g_error_copy() on the error if you need to keep a local copy
as well.
See also g_task_return_new_error().
Since: 2.36
g_task_return_new_error ()
void
g_task_return_new_error (GTask *task,
GQuark domain,
gint code,
const char *format,
...);
Sets task
's result to a new GError created from domain
, code
,
format
, and the remaining arguments, and completes the task (see
g_task_return_pointer() for more discussion of exactly what this
means).
See also g_task_return_error().
Since: 2.36
g_task_return_error_if_cancelled ()
gboolean
g_task_return_error_if_cancelled (GTask *task);
Checks if task
's GCancellable has been cancelled, and if so, sets
task
's error accordingly and completes the task (see
g_task_return_pointer() for more discussion of exactly what this
means).
Returns
TRUE if task
has been cancelled, FALSE if not
Since: 2.36
g_task_propagate_boolean ()
gboolean
g_task_propagate_boolean (GTask *task,
GError **error);
Gets the result of task
as a gboolean.
If the task resulted in an error, or was cancelled, then this will
instead return FALSE and set error
.
Since this method transfers ownership of the return value (or
error) to the caller, you may only call it once.
Returns
the task result, or FALSE on error
Since: 2.36
g_task_propagate_int ()
gssize
g_task_propagate_int (GTask *task,
GError **error);
Gets the result of task
as an integer (gssize).
If the task resulted in an error, or was cancelled, then this will
instead return -1 and set error
.
Since this method transfers ownership of the return value (or
error) to the caller, you may only call it once.
Returns
the task result, or -1 on error
Since: 2.36
g_task_propagate_pointer ()
gpointer
g_task_propagate_pointer (GTask *task,
GError **error);
Gets the result of task
as a pointer, and transfers ownership
of that value to the caller.
If the task resulted in an error, or was cancelled, then this will
instead return NULL and set error
.
Since this method transfers ownership of the return value (or
error) to the caller, you may only call it once.
Returns
the task result, or NULL on error.
[transfer full]
Since: 2.36
g_task_propagate_value ()
gboolean
g_task_propagate_value (GTask *task,
GValue *value,
GError **error);
Gets the result of task
as a GValue, and transfers ownership of
that value to the caller. As with g_task_return_value(), this is
a generic low-level method; g_task_propagate_pointer() and the like
will usually be more useful for C code.
If the task resulted in an error, or was cancelled, then this will
instead set error
and return FALSE.
Since this method transfers ownership of the return value (or
error) to the caller, you may only call it once.
Returns
TRUE if task
succeeded, FALSE on error.
Since: 2.64
g_task_had_error ()
gboolean
g_task_had_error (GTask *task);
Tests if task
resulted in an error.
Returns
TRUE if the task resulted in an error, FALSE otherwise.
Since: 2.36
g_task_get_completed ()
gboolean
g_task_get_completed (GTask *task);
Gets the value of “completed”. This changes from FALSE to TRUE after
the task’s callback is invoked, and will return FALSE if called from inside
the callback.
Returns
TRUE if the task has completed, FALSE otherwise.
Since: 2.44
g_task_run_in_thread ()
void
g_task_run_in_thread (GTask *task,
GTaskThreadFunc task_func);
Runs task_func
in another thread. When task_func
returns, task
's
GAsyncReadyCallback will be invoked in task
's GMainContext.
This takes a ref on task
until the task completes.
See GTaskThreadFunc for more details about how task_func
is handled.
Although GLib currently rate-limits the tasks queued via
g_task_run_in_thread(), you should not assume that it will always
do this. If you have a very large number of tasks to run, but don't
want them to all run at once, you should only queue a limited
number of them at a time.
Since: 2.36
g_task_run_in_thread_sync ()
void
g_task_run_in_thread_sync (GTask *task,
GTaskThreadFunc task_func);
Runs task_func
in another thread, and waits for it to return or be
cancelled. You can use g_task_propagate_pointer(), etc, afterward
to get the result of task_func
.
See GTaskThreadFunc for more details about how task_func
is handled.
Normally this is used with tasks created with a NULL
callback, but note that even if the task does
have a callback, it will not be invoked when task_func
returns.
“completed” will be set to TRUE just before this function returns.
Although GLib currently rate-limits the tasks queued via
g_task_run_in_thread_sync(), you should not assume that it will
always do this. If you have a very large number of tasks to run,
but don't want them to all run at once, you should only queue a
limited number of them at a time.
Since: 2.36
GTaskThreadFunc ()
void
(*GTaskThreadFunc) (GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable);
The prototype for a task function to be run in a thread via
g_task_run_in_thread() or g_task_run_in_thread_sync().
If the return-on-cancel flag is set on task
, and cancellable
gets
cancelled, then the GTask will be completed immediately (as though
g_task_return_error_if_cancelled() had been called), without
waiting for the task function to complete. However, the task
function will continue running in its thread in the background. The
function therefore needs to be careful about how it uses
externally-visible state in this case. See
g_task_set_return_on_cancel() for more details.
Other than in that case, task
will be completed when the
GTaskThreadFunc returns, not when it calls a
g_task_return_ function.
Since: 2.36
g_task_attach_source ()
void
g_task_attach_source (GTask *task,
GSource *source,
GSourceFunc callback);
A utility function for dealing with async operations where you need
to wait for a GSource to trigger. Attaches source
to task
's
GMainContext with task
's priority, and sets source
's
callback to callback
, with task
as the callback's user_data.
It will set the source
’s name to the task’s name (as set with
g_task_set_name()), if one has been set.
This takes a reference on task
until source
is destroyed.
Since: 2.36
g_task_is_valid ()
gboolean
g_task_is_valid (gpointer result,
gpointer source_object);
Checks that result
is a GTask, and that source_object
is its
source object (or that source_object
is NULL and result
has no
source object). This can be used in g_return_if_fail() checks.
Returns
TRUE if result
and source_object
are valid, FALSE
if not
Since: 2.36