.\" $OpenBSD: task_add.9,v 1.11 2014/04/02 13:19:01 mpi Exp $ .\" .\" Copyright (c) 2013 David Gwynne .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above .\" copyright notice and this permission notice appear in all copies. .\" .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .Dd $Mdocdate: April 2 2014 $ .Dt TASK_ADD 9 .Os .Sh NAME .Nm taskq_create , .Nm taskq_destroy , .Nm task_set , .Nm task_add , .Nm task_del , .Nm TASK_INITIALIZER .Nd task queues .Sh SYNOPSIS .In sys/task.h .Ft struct taskq * .Fn "taskq_create" "const char *name" "unsigned int nthreads" "int ipl" .Ft void .Fn "taskq_destroy" "struct taskq *tq" .Ft void .Fn "task_set" "struct task *t" "void (*fn)(void *, void *)" "void *arg1" "void *arg2" .Ft int .Fn "task_add" "struct taskq *tq" "struct task *t" .Ft int .Fn "task_del" "struct taskq *tq" "struct task *t" .Vt extern struct taskq *const systq; .Fn "TASK_INITIALIZER" "void (*fn)(void *, void *)" "void *arg1" "void *arg2" .Sh DESCRIPTION The taskq API provides a mechanism to defer work to a process context. .Pp .Fn taskq_create allocates a taskq and a set of threads to be used to complete work that would be inappropriate for the shared system taskq. The .Fa name argument specifies the name of the kernel threads that are created to service the work on the taskq. .Fa nthreads specifies the number of threads that will be created to handle the work. .Fa ipl specifies the highest interrupt protection level at which .Fn task_add and .Fn task_del will be called against the created taskq. See .Xr spl 9 for a list of the IPLs. .Pp .Fn taskq_destroy causes the resources associated with a previously created taskq to be freed. It will wait till all the tasks in the work queue are completed before returning. Calling .Fn taskq_destroy against the system taskq is an error and will lead to undefined behaviour or a system fault. .Pp It is the responsibility of the caller to provide the .Fn task_set , .Fn task_add , and .Fn task_del functions with pre-allocated task structures. .Pp .Fn task_set prepares the task structure .Fa t to be used in future calls to .Fn task_add and .Fn task_del . .Fa t will be prepared to call the function .Fa fn with the arguments specified by .Fa arg1 and .Fa arg2 . Once initialised, the .Fa t structure can be used repeatedly in calls to .Fn task_add and .Fn task_del and does not need to be reinitialised unless the function called and/or its argument must change. .Pp .Fn task_add schedules the execution of the work specified by the task structure .Fa t on the .Fa tq taskq. The task structure must already be initialised by .Fn task_set . .Pp .Fn task_del will remove the task structure .Fa t from the taskq .Fa tq . If the work was already executed or has not been added to the taskq, the call will have no effect. Calling .Fn task_del against a different taskq than the one given in a previous call to .Fn task_add is an error and will lead to undefined behaviour. .Pp The kernel provides a system taskq .Va systq that can be used by any subsystem for short lived tasks. It is serviced by a single thread and can therefore provide predictable ordering of work. Work can be scheduled on the system taskq from callers at or below IPL_HIGH. .Pp A task declaration can be initialised with the .Fn TASK_INITIALIZER macro. The task will be prepared to call the function specified by the .Fa fn argument with the .Fa void * arguments given in .Fa arg1 and .Fa arg2 . .Sh CONTEXT .Fn taskq_create and .Fn taskq_destroy can be called during autoconf, or from process context. .Fn task_set , .Fn task_add , and .Fn task_del can be called during autoconf, from process context, or from interrupt context. .Sh RETURN VALUES .Fn taskq_create returns a pointer to a taskq structure on success or .Dv NULL on failure. .Pp .Fn task_add will return 1 if the task .Fa t was added to the taskq .Fa tq or 0 if the task was already queued. .Pp .Fn task_del will return 1 if the task .Fa t was removed from the taskq .Fa tq or 0 if the task was not already on the queue. .Sh SEE ALSO .Xr autoconf 9 , .Xr spl 9 .Sh HISTORY The task API was originally written by .An David Gwynne Aq Mt dlg@openbsd.org . The task API first appeared in .Ox 5.5 .