Class: Gio::Task

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#attach_source(source, callback) ⇒ nil

A utility function for dealing with async operations where you need to wait for a #GSource to trigger. Attaches source to task’s GMain::Context 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.

Parameters:

  • source (GLib::Source)

    the source to attach

  • callback (GLib::SourceFunc)

    the callback to invoke when source triggers

Returns:

  • (nil)

#cancellableGio::Cancellable

Gets task’s #GCancellable

Returns:

#check_cancellableTrueClass

Gets task’s check-cancellable flag. See g_task_set_check_cancellable() for more details.

Returns:

  • (TrueClass)

#check_cancellable=(check_cancellable) ⇒ nil

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.

Parameters:

  • check_cancellable (TrueClass)

    whether #GTask will check the state of its #GCancellable for you.

Returns:

  • (nil)

#completedTrueClass

Gets the value of #GTask: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:

  • (TrueClass)

    true if the task has completed, false otherwise.

#completed=(completed) ⇒ TrueClass

Whether the task has completed, meaning its callback (if set) has been invoked. This can only happen after g_task_return_pointer(), g_task_return_error() or one of the other return functions have been called on the task.

This property is guaranteed to change from false to true exactly once.

The #GObject::notify signal for this change is emitted in the same main context as the task’s callback, immediately after that callback is invoked.

Parameters:

  • completed (TrueClass)

Returns:

  • (TrueClass)

    completed

  • (TrueClass)

    completed

#completed?TrueClass

Whether the task has completed, meaning its callback (if set) has been invoked. This can only happen after g_task_return_pointer(), g_task_return_error() or one of the other return functions have been called on the task.

This property is guaranteed to change from false to true exactly once.

The #GObject::notify signal for this change is emitted in the same main context as the task’s callback, immediately after that callback is invoked.

Returns:

  • (TrueClass)

    completed

#contextGLib::MainContext

Gets the GMain::Context that task will return its result in (that is, the context that was the

thread-default main context][g-main-context-push-thread-default

at the point when task was created).

This will always return a non-nil value, even if the task’s context is the default GMain::Context.

Returns:

  • (GLib::MainContext)

    task’s GMain::Context

#had_errorTrueClass

Tests if task resulted in an error.

Returns:

  • (TrueClass)

    true if the task resulted in an error, false otherwise.

#nameString

Gets task’s name. See g_task_set_name().

Returns:

  • (String)

    task’s name, or nil

#name=(name) ⇒ nil

Sets task’s name, used in debugging and profiling. The name defaults to nil.

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.

Parameters:

  • name (String)

    a human readable name for the task, or nil to unset it

Returns:

  • (nil)

#new(source_object, cancellable, callback, callback_data) ⇒ Gio::Task

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.

Parameters:

  • source_object (GObject::Object)

    the #GObject that owns this task, or nil.

  • cancellable (Gio::Cancellable)

    optional #GCancellable object, nil to ignore.

  • callback (Gio::AsyncReadyCallback)

    a GAsync::ReadyCallback.

  • callback_data (GObject)

    user data passed to callback.

Returns:

#priorityInteger

Gets task’s priority

Returns:

  • (Integer)

    task’s priority

#priority=(priority) ⇒ nil

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().

Parameters:

  • priority (Integer)

    the [priority] of the request

Returns:

  • (nil)

#propagate_booleanTrueClass

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:

  • (TrueClass)

    the task result, or false on error

#propagate_intGio::gssize

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:

  • (Gio::gssize)

    the task result, or -1 on error

#propagate_pointerGObject

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 nil and set error.

Since this method transfers ownership of the return value (or error) to the caller, you may only call it once.

Returns:

  • (GObject)

    the task result, or nil on error

#propagate_value(value) ⇒ TrueClass

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.

Parameters:

  • value (GObject::Value)

    return location for the #GValue

Returns:

  • (TrueClass)

    true if task succeeded, false on error.

#return_boolean(result) ⇒ nil

Sets task’s result to result and completes the task (see g_task_return_pointer() for more discussion of exactly what this means).

Parameters:

  • result (TrueClass)

    the #gboolean result of a task function.

Returns:

  • (nil)

#return_error(error) ⇒ nil

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().

Parameters:

  • error (GLib::Error)

    the #GError result of a task function.

Returns:

  • (nil)

#return_error_if_cancelledTrueClass

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:

  • (TrueClass)

    true if task has been cancelled, false if not

#return_int(result) ⇒ nil

Sets task’s result to result and completes the task (see g_task_return_pointer() for more discussion of exactly what this means).

Parameters:

  • result (Gio::gssize)

    the integer (#gssize) result of a task function.

Returns:

  • (nil)

#return_new_error(domain, code, format, array) ⇒ nil

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().

Parameters:

  • domain (GLib::Quark)

    a #GQuark.

  • code (Integer)

    an error code.

  • format (String)

    a string with format characters.

  • array (Array)

    a list of values to insert into format.

Returns:

  • (nil)

#return_on_cancelTrueClass

Gets task’s return-on-cancel flag. See g_task_set_return_on_cancel() for more details.

Returns:

  • (TrueClass)

#return_on_cancel=(return_on_cancel) ⇒ TrueClass

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 GTask::ThreadFunc had called g_task_return_error_if_cancelled() and then returned.

This allows you to create a cancellable wrapper around an uninterruptable function. The GTask::ThreadFunc 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 GTask::ThreadFunc will still be run (for consistency), but the task will also be completed right away.

Parameters:

  • return_on_cancel (TrueClass)

    whether the task returns automatically when it is cancelled.

Returns:

  • (TrueClass)

    true if task’s return-on-cancel flag was changed to match return_on_cancel. false if task has already been cancelled.

#return_pointer(result, result_destroy) ⇒ nil

Sets task’s result to result and completes the task. If result is not nil, 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 GMain::Context, or in the next iteration of the current GMain::Context. 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 GTask::ThreadFunc 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.

Parameters:

  • result (GObject)

    the pointer result of a task function

  • result_destroy (GLib::DestroyNotify)

    a GDestroy::Notify function.

Returns:

  • (nil)

#return_value(result) ⇒ nil

Sets task’s result to result (by copying it) and completes the task.

If result is nil then a #GValue of type #G_TYPE_POINTER with a value of nil 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.

Parameters:

  • result (GObject::Value)

    the #GValue result of a task function

Returns:

  • (nil)

#run_in_thread(task_func) ⇒ nil

Runs task_func in another thread. When task_func returns, task’s GAsync::ReadyCallback will be invoked in task’s #GMainContext.

This takes a ref on task until the task completes.

See GTask::ThreadFunc 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.

Parameters:

  • task_func (Gio::TaskThreadFunc)

    a GTask::ThreadFunc

Returns:

  • (nil)

#run_in_thread_sync(task_func) ⇒ nil

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 GTask::ThreadFunc for more details about how task_func is handled.

Normally this is used with tasks created with a nil ‘callback`, but note that even if the task does have a callback, it will not be invoked when task_func returns. #GTask: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.

Parameters:

  • task_func (Gio::TaskThreadFunc)

    a GTask::ThreadFunc

Returns:

  • (nil)

#set_task_data(task_data, task_data_destroy) ⇒ nil

Sets task’s task data (freeing the existing task data, if any).

Parameters:

  • task_data (GObject)

    task-specific data

  • task_data_destroy (GLib::DestroyNotify)

    GDestroy::Notify for task_data

Returns:

  • (nil)

#source_objectGObject::Object

Gets the source object from task. Like g_async_result_get_source_object(), but does not ref the object.

Returns:

  • (GObject::Object)

    task’s source object, or nil

#source_tagGObject

Gets task’s source tag. See g_task_set_source_tag().

Returns:

  • (GObject)

    task’s source tag

#source_tag=(source_tag) ⇒ nil

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.

Parameters:

  • source_tag (GObject)

    an opaque pointer indicating the source of this task

Returns:

  • (nil)

#task_dataGObject

Gets task’s ‘task_data`.

Returns:

  • (GObject)

    task’s ‘task_data`.