DragonFly On-Line Manual Pages
TASKQUEUE(9) DragonFly Kernel Developer's Manual TASKQUEUE(9)
NAME
taskqueue_block, taskqueue_cancel, taskqueue_cancel_timeout,
taskqueue_create, taskqueue_drain, taskqueue_drain_timeout,
taskqueue_enqueue, taskqueue_enqueue_timeout, taskqueue_free,
taskqueue_find, taskqueue_run, taskqueue_start_threads,
taskqueue_unblock, TASK_INIT, TASKQUEUE_DECLARE, TASKQUEUE_DEFINE --
asynchronous task execution
SYNOPSIS
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/taskqueue.h>
typedef void (*task_fn_t)(void *context, int pending);
typedef void (*taskqueue_enqueue_fn)(void *context);
struct task {
STAILQ_ENTRY(task) ta_link; /* link for queue */
int ta_pending; /* count times queued */
int ta_priority; /* priority of task in queue */
task_fn_t ta_func; /* task handler */
void *ta_context; /* argument for handler */
};
struct taskqueue *
taskqueue_create(const char *name, int mflags,
taskqueue_enqueue_fn enqueue, void *context);
void
taskqueue_free(struct taskqueue *queue);
struct taskqueue *
taskqueue_find(const char *name);
int
taskqueue_enqueue(struct taskqueue *queue, struct task *task);
int
taskqueue_enqueue_timeout(struct taskqueue *queue,
struct timeout_task *timeout_task, int ticks);
int
taskqueue_cancel(struct taskqueue *queue, struct task *task,
u_int *pendp);
int
taskqueue_cancel_timeout(struct taskqueue *queue,
struct timeout_task *timeout_task, u_int *pendp);
void
taskqueue_run(struct taskqueue *queue);
void
taskqueue_drain(struct taskqueue *queue, struct task *task);
void
taskqueue_drain_timeout(struct taskqueue *queue,
struct timeout_task *timeout_task);
void
taskqueue_block(struct taskqueue *queue);
void
taskqueue_unblock(struct taskqueue *queue);
int
taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
int ncpu, const char *fmt, ...);
TASK_INIT(struct task *task, int priority, task_fn_t *func,
void *context);
TASKQUEUE_DECLARE(name);
TASKQUEUE_DEFINE(name, taskqueue_enqueue_fn enqueue, void *context,
init);
DESCRIPTION
These functions provide a simple interface for asynchronous execution of
code.
The function taskqueue_create() is used to create new queues. The argu-
ments to taskqueue_create() include a name that should be unique, a set
of kmalloc(9) flags that specify whether the call to malloc() is allowed
to sleep, and a function which is called from taskqueue_enqueue() when a
task is added to the queue to allow the queue to arrange to be run later
(for instance by scheduling a software interrupt or waking a kernel
thread).
The function taskqueue_free() should be used to remove the queue from the
global list of queues and free the memory used by the queue. Any tasks
that are on the queue will be executed at this time.
The system maintains a list of all queues which can be searched using
taskqueue_find(). The first queue whose name matches is returned, other-
wise NULL.
To add a task to the list of tasks queued on a taskqueue, call
taskqueue_enqueue() with pointers to the queue and task. If the task's
ta_pending field is non-zero, then it is simply incremented to reflect
the number of times the task was enqueued. Otherwise, the task is added
to the list before the first task which has a lower ta_priority value or
at the end of the list if no tasks have a lower priority. Enqueueing a
task does not perform any memory allocation which makes it suitable for
calling from an interrupt handler. This function will return EPIPE if
the queue is being freed.
To execute all the tasks on a queue, call taskqueue_run(). When a task
is executed, first it is removed from the queue, the value of ta_pending
is recorded and then the field is zeroed. The function ta_func from the
task structure is called with the value of the field ta_context as its
first argument and the value of ta_pending as its second argument.
The taskqueue_enqueue_timeout() is used to schedule the enqueue after the
specified amount of ticks. If the ticks argument is negative, the
already scheduled enqueueing is not re-scheduled. Otherwise, the task is
scheduled for enqueueing in the future, after the absolute value of ticks
is passed.
The taskqueue_cancel() function is used to cancel a task. The ta_pending
count is cleared, and the old value returned in the reference parameter
pendp, if it is non-NULL. If the task is currently running, EBUSY is
returned, otherwise 0. To implement a blocking taskqueue_cancel() that
waits for a running task to finish, it could look like:
while (taskqueue_cancel(tq, task, NULL) != 0)
taskqueue_drain(tq, task);
Note that, as with taskqueue_drain(), the caller is responsible for
ensuring that the task is not re-enqueued after being canceled.
Similarly, the taskqueue_cancel_timeout() function is used to cancel the
scheduled task execution.
The taskqueue_drain() function is used to wait for the task to finish,
and the taskqueue_drain_timeout() function is used to wait for the sched-
uled task to finish. There is no guarantee that the task will not be
enqueued after call to taskqueue_drain().
The taskqueue_block() function is used to block a taskqueue. When a
taskqueue is blocked, calls to taskqueue_enqueue() will still enqueue
tasks but they will not be run until the taskqueue is unblocked by call-
ing taskqueue_unblock().
The taskqueue_start_threads() function is used to create and start count
dedicated threads for the taskqueue specified by tqp. These threads will
be created with the priority specified by pri and the name given by fmt
with _N appended to it, where N is the number of the thread. If count >
1 and ncpu is -1, each of the count threads will be allocated to a dif-
ferent CPU among all available CPUs in a round robin fashion. The
taskqueue specified by tqp must be created previously by calling
taskqueue_create() with the argument enqueue set to
taskqueue_thread_enqueue.
A convenience macro, TASK_INIT() is provided to initialise a task struc-
ture. The values of priority, func, and context are simply copied into
the task structure fields and the ta_pending field is cleared.
Two macros, TASKQUEUE_DECLARE() and TASKQUEUE_DEFINE() are used to
declare a reference to a global queue, and to define the implementation
of the queue. The TASKQUEUE_DEFINE() macro arranges to call
taskqueue_create() with the values of its name, enqueue and context argu-
ments during system initialisation. After calling taskqueue_create(),
the init argument to the macro is executed as a C statement, allowing any
further initialisation to be performed (such as registering an interrupt
handler etc.)
The system provides two global taskqueues, taskqueue_swi and
taskqueue_swi_mp, which are run via a software interrupt mechanism. To
use these queues, call taskqueue_enqueue() with the value of the global
variable taskqueue_swi or taskqueue_swi_mp.
While taskqueue_swi acquires the mplock for its tasks, taskqueue_swi_mp
is intended for mpsafe tasks and no mplock will be acquired for them.
These queues can be used, for instance, for implementing interrupt han-
dlers which must perform a significant amount of processing in the han-
dler. The hardware interrupt handler would perform minimal processing of
the interrupt and then enqueue a task to finish the work. This reduces
to a minimum the amount of time spent with interrupts disabled.
HISTORY
This interface first appeared in FreeBSD 5.0. There is a similar facil-
ity called work_queue in the Linux kernel.
AUTHORS
This manual page was written by Doug Rabson.
DragonFly 3.5 July 24, 2013 DragonFly 3.5