Class: Gio::Task
- Inherits:
-
Object
- Object
- Gio::Task
- Defined in:
- (unknown)
Instance Method Summary collapse
-
#attach_source(source, callback) ⇒ nil
A utility function for dealing with async operations where you need to wait for a #GSource to trigger.
-
#cancellable ⇒ Gio::Cancellable
Gets task’s #GCancellable.
-
#check_cancellable ⇒ TrueClass
Gets task’s check-cancellable flag.
-
#check_cancellable=(check_cancellable) ⇒ nil
Sets or clears task’s check-cancellable flag.
-
#completed ⇒ TrueClass
Gets the value of #GTask:completed.
-
#completed=(completed) ⇒ TrueClass
Whether the task has completed, meaning its callback (if set) has been invoked.
-
#completed? ⇒ TrueClass
Whether the task has completed, meaning its callback (if set) has been invoked.
-
#context ⇒ GLib::MainContext
Gets the GMain::Context 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).
-
#had_error ⇒ TrueClass
Tests if task resulted in an error.
-
#name ⇒ String
Gets task’s name.
-
#name=(name) ⇒ nil
Sets task’s name, used in debugging and profiling.
-
#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].
-
#priority ⇒ Integer
Gets task’s priority.
-
#priority=(priority) ⇒ nil
Sets task’s priority.
-
#propagate_boolean ⇒ TrueClass
Gets the result of task as a #gboolean.
-
#propagate_int ⇒ Gio::gssize
Gets the result of task as an integer (#gssize).
-
#propagate_pointer ⇒ GObject
Gets the result of task as a pointer, and transfers ownership of that value to the caller.
-
#propagate_value(value) ⇒ TrueClass
Gets the result of task as a #GValue, and transfers ownership of that value to the caller.
-
#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).
-
#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).
-
#return_error_if_cancelled ⇒ TrueClass
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).
-
#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).
-
#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).
-
#return_on_cancel ⇒ TrueClass
Gets task’s return-on-cancel flag.
-
#return_on_cancel=(return_on_cancel) ⇒ TrueClass
Sets or clears task’s return-on-cancel flag.
-
#return_pointer(result, result_destroy) ⇒ nil
Sets task’s result to result and completes the task.
-
#return_value(result) ⇒ nil
Sets task’s result to result (by copying it) and completes the task.
-
#run_in_thread(task_func) ⇒ nil
Runs task_func in another thread.
-
#run_in_thread_sync(task_func) ⇒ nil
Runs task_func in another thread, and waits for it to return or be cancelled.
-
#set_task_data(task_data, task_data_destroy) ⇒ nil
Sets task’s task data (freeing the existing task data, if any).
-
#source_object ⇒ GObject::Object
Gets the source object from task.
-
#source_tag ⇒ GObject
Gets task’s source tag.
-
#source_tag=(source_tag) ⇒ nil
Sets task’s source tag.
-
#task_data ⇒ GObject
Gets task’s ‘task_data`.
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.
#cancellable ⇒ Gio::Cancellable
Gets task’s #GCancellable
#check_cancellable ⇒ TrueClass
Gets task’s check-cancellable flag. See g_task_set_check_cancellable() for more details.
#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.
#completed ⇒ TrueClass
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.
#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.
#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.
#context ⇒ GLib::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.
#had_error ⇒ TrueClass
Tests if task resulted in an error.
#name ⇒ String
Gets task’s name. See g_task_set_name().
#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.
#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.
#priority ⇒ Integer
Gets 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().
#propagate_boolean ⇒ TrueClass
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.
#propagate_int ⇒ Gio::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.
#propagate_pointer ⇒ GObject
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.
#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.
#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).
#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().
#return_error_if_cancelled ⇒ TrueClass
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).
#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).
#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().
#return_on_cancel ⇒ TrueClass
Gets task’s return-on-cancel flag. See g_task_set_return_on_cancel() for more details.
#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.
#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.
#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.
#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.
#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.
#set_task_data(task_data, task_data_destroy) ⇒ nil
Sets task’s task data (freeing the existing task data, if any).
#source_object ⇒ GObject::Object
Gets the source object from task. Like g_async_result_get_source_object(), but does not ref the object.
#source_tag ⇒ GObject
Gets task’s source tag. See g_task_set_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.
#task_data ⇒ GObject
Gets task’s ‘task_data`.