IrisTask

IrisTask — Writing concurrent and asynchronous tasks

Synopsis

#define             IRIS_TASK_CONST                     (obj)
#define             IRIS_TASK_THROW_NEW                 (t,q,c,f,...)
#define             IRIS_TASK_THROW                     (t,e)
#define             IRIS_TASK_CATCH                     (t,e)
#define             IRIS_TASK_RETURN_VALUE              (t,gt,v)
#define             IRIS_TASK_RETURN_TASK               (t,t2)
#define             IRIS_TASK_RETURN_TASK_NEW           (t,f,p,n)
                    IrisTaskPrivate;
void                (*IrisTaskFunc)                     (IrisTask *task,
                                                         gpointer user_data);
                    IrisTask;
IrisTask*           iris_task_new                       (void);
IrisTask*           iris_task_new_full                  (IrisTaskFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify,
                                                         gboolean async,
                                                         IrisScheduler *scheduler,
                                                         GMainContext *context);
void                iris_task_run                       (IrisTask *task);
void                iris_task_cancel                    (IrisTask *task);
void                iris_task_complete                  (IrisTask *task);
void                iris_task_add_callback              (IrisTask *task,
                                                         IrisTaskFunc callback,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);
void                iris_task_add_errback               (IrisTask *task,
                                                         IrisTaskFunc errback,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);
void                iris_task_add_callback_closure      (IrisTask *task,
                                                         GClosure *closure);
void                iris_task_add_errback_closure       (IrisTask *task,
                                                         GClosure *closure);
void                iris_task_add_dependency            (IrisTask *task,
                                                         IrisTask *dependency);
void                iris_task_remove_dependency         (IrisTask *task,
                                                         IrisTask *dependency);
gboolean            iris_task_is_async                  (IrisTask *task);
gboolean            iris_task_is_executing              (IrisTask *task);
gboolean            iris_task_is_canceled               (IrisTask *task);
gboolean            iris_task_is_finished               (IrisTask *task);
void                iris_task_set_main_context          (IrisTask *task,
                                                         GMainContext *context);
GMainContext*       iris_task_get_main_context          (IrisTask *task);
void                iris_task_set_scheduler             (IrisTask *task,
                                                         IrisScheduler *scheduler);
gboolean            iris_task_get_error                 (IrisTask *task,
                                                         GError **error);
void                iris_task_set_error                 (IrisTask *task,
                                                         const GError *error);
void                iris_task_take_error                (IrisTask *task,
                                                         GError *error);
void                iris_task_get_result                (IrisTask *task,
                                                         GValue *value);
void                iris_task_set_result                (IrisTask *task,
                                                         const GValue *value);
void                iris_task_set_result_gtype          (IrisTask *task,
                                                         GType type,
                                                         ...);
IrisTask*           iris_task_all_of                    (GList *tasks);
IrisTask*           iris_task_any_of                    (GList *tasks);

Object Hierarchy

  GObject
   +----IrisTask

Description

IrisTask is a single-shot work-item that performs an atomic piece of work. An example of this could be retrieving the contents of a web-page, removing a file from disk, or even generating a thumbnail of an image. Iris will do the work of compositing your work items onto the resources of the target system in such a way that will be efficient.

Upon completion of tasks, a series of callbacks or errbacks can be executed. This is called the post-processing phase. Callbacks and errbacks can alter the current result of the task or even yield new tasks that must be completed before further callbacks or errbacks can be executed.

Details

IRIS_TASK_CONST()

#define             IRIS_TASK_CONST(obj)

obj :


IRIS_TASK_THROW_NEW()

#define             IRIS_TASK_THROW_NEW(t,q,c,f,...)

Creates a new GError and attaches it to the task.

t :

An IrisTask

q :

A GQuark

c :

a gint containing the error code

f :

the error format string

... :

parameters for format

IRIS_TASK_THROW()

#define             IRIS_TASK_THROW(t,e)

Steals ownership of e and attaches the GError to the task.

t :

An IrisTask

e :

A GError

IRIS_TASK_CATCH()

#define             IRIS_TASK_CATCH(t,e)

Catches the currently set error on the task and stores it into e. The ownership of e is that of the caller so it should be freed with g_error_free() if non-NULL.

t :

An IrisTask

e :

a location for a GError or NULL

IRIS_TASK_RETURN_VALUE()

#define             IRIS_TASK_RETURN_VALUE(t,gt,v)

This macro simplifies how you can store the current result for the task. It can transparently store the value of v no matter the type as long as your GType is accurate.

For example, to store an int you could

gint myint = 0;
IRIS_TASK_RETURN_VALUE (t, G_TYPE_INT, myint);

t :

An IrisTask

gt :

A GType

v :

the value to store

IRIS_TASK_RETURN_TASK()

#define             IRIS_TASK_RETURN_TASK(t,t2)

Sets the result of a task to another task. This will prevent the task from any further work in the callback/errback phase until t2 has completed.

t :

An IrisTask

t2 :

An IrisTask

IRIS_TASK_RETURN_TASK_NEW()

#define             IRIS_TASK_RETURN_TASK_NEW(t,f,p,n)

Helper macro to create and return a new task as the result in one step.

t :

An IrisTask

f :

An IrisTaskFunc

p :

user data for the newly created IrisTask

n :

A GDestroyNotify to be called when the new task is destroyed

IrisTaskPrivate

typedef struct _IrisTaskPrivate IrisTaskPrivate;


IrisTaskFunc ()

void                (*IrisTaskFunc)                     (IrisTask *task,
                                                         gpointer user_data);

Callback for tasks, callbacks, and errbacks. See iris_task_add_callback(), iris_task_add_errback().

task :

An IrisTask

user_data :

user specified data

IrisTask

typedef struct _IrisTask IrisTask;


iris_task_new ()

IrisTask*           iris_task_new                       (void);

Creates a new instance of IrisTask.

Returns :

the newly created instance of IrisTask

iris_task_new_full ()

IrisTask*           iris_task_new_full                  (IrisTaskFunc func,
                                                         gpointer user_data,
                                                         GDestroyNotify notify,
                                                         gboolean async,
                                                         IrisScheduler *scheduler,
                                                         GMainContext *context);

Creates a new instance of IrisTask. This method allows for setting if the task is asynchronous with async. An asynchronous task has the ability to not complete during the execution of the task's execution method (in this case func). To mark the task's execution as completed, g_task_complete() must be called for the task.

If you want errbacks and callbacks to complete within a GMainContext, you may specify context or NULL for the callbacks to happen within the worker thread.

scheduler allows you to set a specific IrisScheduler to perform execution of the task within. Note that all message passing associated with the tasks internal IrisPort's will also happen on this scheduler.

func :

An IrisTaskFunc

user_data :

data for func

notify :

A destroy notify after execution of the task

async :

Will the task complete during the execution of func

scheduler :

An IrisScheduler or NULL

context :

A GMainContext or NULL

Returns :

The newly created IrisTask instance.

iris_task_run ()

void                iris_task_run                       (IrisTask *task);

Asynchronously schedules the task for execution.

task :

An IrisTask

iris_task_cancel ()

void                iris_task_cancel                    (IrisTask *task);

Cancels a task. If the task is already executing, it is up to the executing task to periodically check the canceled state with iris_task_is_canceled() and quit execution.

task :

An IrisTask

iris_task_complete ()

void                iris_task_complete                  (IrisTask *task);

Marks the task as completing the execution phase. This can be used by asynchronous tasks to denote that they have completed.

Completion of the task will result in the callbacks phase being performed.

task :

An IrisTask

iris_task_add_callback ()

void                iris_task_add_callback              (IrisTask *task,
                                                         IrisTaskFunc callback,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);

Adds a callback to the callbacks phase. The callback will be executed in the order it was added. If the task has an error when the callback is reached, it will not be executed at all.

task :

An IrisTask

callback :

An IrisTaskFunc

user_data :

user data for callback

notify :

notify when the closure has executed

iris_task_add_errback ()

void                iris_task_add_errback               (IrisTask *task,
                                                         IrisTaskFunc errback,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);

Adds an errback to the callbacks phase. The errback will be executed in the order it was added. If errback will only execute if the task has an error when the errback is reached.

task :

An IrisTask

errback :

An IrisTaskFunc

user_data :

user data for callback

notify :

notify when the closure has executed

iris_task_add_callback_closure ()

void                iris_task_add_callback_closure      (IrisTask *task,
                                                         GClosure *closure);

Adds a callback closure to be executed in the callbacks phase.

See iris_task_add_callback().

task :

An IrisTask

closure :

A GClosure

iris_task_add_errback_closure ()

void                iris_task_add_errback_closure       (IrisTask *task,
                                                         GClosure *closure);

Adds an errback closure to be executed in the callbacks phase.

See iris_task_add_errback().

task :

An IrisTask

closure :

A GClosure

iris_task_add_dependency ()

void                iris_task_add_dependency            (IrisTask *task,
                                                         IrisTask *dependency);

Prevents further execution of the task or callbacks phase until the dependency task has completed.

task :

An IrisTask

dependency :

An IrisTask

iris_task_remove_dependency ()

void                iris_task_remove_dependency         (IrisTask *task,
                                                         IrisTask *dependency);

Removes dependency from preventing the tasks execution. If the task is ready to execute it will be scheduled for execution.

task :

An IrisTask

dependency :

An IrisTask

iris_task_is_async ()

gboolean            iris_task_is_async                  (IrisTask *task);

Checks if the task is an asynchronous task, meaning it will not complete when the tasks execute method returns.

task :

An IrisTask

Returns :

TRUE if the task is asynchronous

iris_task_is_executing ()

gboolean            iris_task_is_executing              (IrisTask *task);

Checks if the task is currently executing.

task :

An IrisTask

Returns :

TRUE if the task is executing

iris_task_is_canceled ()

gboolean            iris_task_is_canceled               (IrisTask *task);

Checks if a task has been canceled. Note that if the task handles the cancel and chooses to ignore it, FALSE will be returned.

task :

An IrisTask

Returns :

TRUE if the task was canceled.

iris_task_is_finished ()

gboolean            iris_task_is_finished               (IrisTask *task);

Checks to see if the task has executed and the callbacks phase has completed.

task :

An IrisTask

Returns :

TRUE if the task has completed

iris_task_set_main_context ()

void                iris_task_set_main_context          (IrisTask *task,
                                                         GMainContext *context);

Sets a GMainContext to use to perform the errbacks and callbacks from. All future callbacks and errbacks will be executed from within the context.

task :

An IrisTask

context :

A GMainContext

iris_task_get_main_context ()

GMainContext*       iris_task_get_main_context          (IrisTask *task);

Retrieves the GMainContext associated with the task or NULL.

task :

An IrisTask

Returns :

The GMainContext or NULL

iris_task_set_scheduler ()

void                iris_task_set_scheduler             (IrisTask *task,
                                                         IrisScheduler *scheduler);

Sets the scheduler used to execute future work items.

task :

An IrisTask

scheduler :

An IrisScheduler

iris_task_get_error ()

gboolean            iris_task_get_error                 (IrisTask *task,
                                                         GError **error);

Stores a copy of the current error for the task into the location error. If no error currently exists, the value stored will be NULL. The error must be freed by the caller using g_error_free().

task :

An IrisTask

error :

A location for a GError

Returns :

TRUE if the task had an error and was copied.

iris_task_set_error ()

void                iris_task_set_error                 (IrisTask *task,
                                                         const GError *error);

Sets the error for the task. If in the callback phase, the next iteration will execute the errback instead of the callback.

task :

An IrisTask

error :

A GError

iris_task_take_error ()

void                iris_task_take_error                (IrisTask *task,
                                                         GError *error);

Steals the ownership of error and attaches it to the task.

task :

An IrisTask

error :

A GError

iris_task_get_result ()

void                iris_task_get_result                (IrisTask *task,
                                                         GValue *value);

Retreives the current value for the task and stores it to the GValue value.

task :

An IrisTask

value :

A GValue to store the current result

iris_task_set_result ()

void                iris_task_set_result                (IrisTask *task,
                                                         const GValue *value);

Sets the current result for the task.

task :

An IrisTask

value :

A GValue

iris_task_set_result_gtype ()

void                iris_task_set_result_gtype          (IrisTask *task,
                                                         GType type,
                                                         ...);

Sets the current value for the task without needing to use a GValue container. You can pass a single value after type.

task :

An IrisTask

type :

A GType

... :


iris_task_all_of ()

IrisTask*           iris_task_all_of                    (GList *tasks);

Creates a new task that will complete when each of the passed IrisTask's complete.

tasks :

Returns :

the newly created IrisTask instance.

iris_task_any_of ()

IrisTask*           iris_task_any_of                    (GList *tasks);

Creates a new task that will not complete until any one of the IrisTask's completes.

tasks :

A GList of IrisTask's

Returns :

the newly created IrisTask instance.