NAME
taskqueue — asynchronous task executionSYNOPSIS
#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 */ u_short ta_pending; /* count times queued */ u_short ta_priority; /* priority of task in queue */ task_fn_t ta_func; /* task handler */ void *ta_context; /* argument for handler */ }; enum taskqueue_callback_type { TASKQUEUE_CALLBACK_TYPE_INIT, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN, }; typedef void (*taskqueue_callback_fn)(void *context); struct timeout_task;
struct taskqueue *
taskqueue_create(const char *name, int mflags, taskqueue_enqueue_fn enqueue, void *context); struct taskqueue *
taskqueue_create_fast(const char *name, int mflags, taskqueue_enqueue_fn enqueue, void *context); int
taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, const char *name, ...); int
taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri, cpuset_t *mask, const char *name, ...); int
taskqueue_start_threads_in_proc(struct taskqueue **tqp, int count, int pri, struct proc *proc, const char *name, ...); void
taskqueue_set_callback(struct taskqueue *queue, enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback, void *context); void
taskqueue_free(struct taskqueue *queue); 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_enqueue_timeout_sbt(struct taskqueue *queue, struct timeout_task *timeout_task, sbintime_t sbt, sbintime_t pr, int flags); 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_drain(struct taskqueue *queue, struct task *task); void
taskqueue_drain_timeout(struct taskqueue *queue, struct timeout_task *timeout_task); void
taskqueue_drain_all(struct taskqueue *queue); void
taskqueue_quiesce(struct taskqueue *queue); void
taskqueue_block(struct taskqueue *queue); void
taskqueue_unblock(struct taskqueue *queue); int
taskqueue_member(struct taskqueue *queue, struct thread *td); void
taskqueue_run(struct taskqueue *queue); TASK_INIT(struct task *task, int priority, task_fn_t func, void *context); TASK_INITIALIZER(int priority, task_fn_t func, void *context); TASKQUEUE_DECLARE(name); TASKQUEUE_DEFINE(name, taskqueue_enqueue_fn enqueue, void *context, init); TASKQUEUE_FAST_DEFINE(name, taskqueue_enqueue_fn enqueue, void *context, init); TASKQUEUE_DEFINE_THREAD(name); TASKQUEUE_FAST_DEFINE_THREAD(name); TIMEOUT_TASK_INIT(struct taskqueue *queue, struct timeout_task *timeout_task, int priority, task_fn_t func, void *context);
DESCRIPTION
These functions provide a simple interface for asynchronous execution of code. The function taskqueue_create() is used to create new queues. The arguments to taskqueue_create() include a name that should be unique, a set of malloc(9) flags that specify whether the call to malloc() is allowed to sleep, a function that is called from taskqueue_enqueue() when a task is added to the queue, and a pointer to the memory location where the identity of the thread that services the queue is recorded. The function called from taskqueue_enqueue() must arrange for the queue to be processed (for instance by scheduling a software interrupt or waking a kernel thread). The memory location where the thread identity is recorded is used to signal the service thread(s) to terminate--when this value is set to zero and the thread is signaled it will terminate. If the queue is intended for use in fast interrupt handlers taskqueue_create_fast() should be used in place of taskqueue_create(). The function taskqueue_free() should be used to free the memory used by the queue. Any tasks that are on the queue will be executed at this time after which the thread servicing the queue will be signaled that it should exit. Once a taskqueue has been created, its threads should be started using taskqueue_start_threads(), taskqueue_start_threads_cpuset() or taskqueue_start_threads_in_proc(). taskqueue_start_threads_cpuset() takes a cpuset argument which will cause the threads which are started for the taskqueue to be restricted to run on the given CPUs. taskqueue_start_threads_in_proc() takes a proc argument which will cause the threads which are started for the taskqueue to be assigned to the given kernel process. Callbacks may optionally be registered using taskqueue_set_callback(). Currently, callbacks may be registered for the following purposes:TASKQUEUE_CALLBACK_TYPE_INIT
- This callback is called by every thread in the taskqueue, before it executes any tasks. This callback must be set before the taskqueue's threads are started.
TASKQUEUE_CALLBACK_TYPE_SHUTDOWN
- This callback is called by every thread in the taskqueue, after it executes its last task. This callback will always be called before the taskqueue structure is reclaimed.
EPIPE
if
the queue is being freed.
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.
After the function ta_func returns,
wakeup(9) is called on the task pointer passed to
taskqueue_enqueue().
The taskqueue_enqueue_timeout() function is used to
schedule the enqueue after the specified number of
ticks. The
taskqueue_enqueue_timeout_sbt() function provides
finer control over the scheduling based on
sbt, pr, and
flags, as detailed in
timeout(9). Only non-fast task queues can be used
for timeout_task scheduling. 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. This function returns -1 if
the task is being drained. Otherwise, the number of pending calls is returned.
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);
Predefined Task Queues
The system provides four global taskqueues, taskqueue_fast, taskqueue_swi, taskqueue_swi_giant, and taskqueue_thread. The taskqueue_fast queue is for swi handlers dispatched from fast interrupt handlers, where sleep mutexes cannot be used. The swi taskqueues are run via a software interrupt mechanism. The taskqueue_swi queue runs without the protection of the Giant kernel lock, and the taskqueue_swi_giant queue runs with the protection of the Giant kernel lock. The thread taskqueue taskqueue_thread runs in a kernel thread context, and tasks run from this thread do not run under the Giant kernel lock. If the caller wants to run under Giant, he should explicitly acquire and release Giant in his taskqueue handler routine. To use these queues, call taskqueue_enqueue() with the value of the global taskqueue variable for the queue you wish to use. The software interrupt queues can be used, for instance, for implementing interrupt handlers which must perform a significant amount of processing in the handler. 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. The thread queue can be used, for instance, by interrupt level routines that need to call kernel functions that do things that can only be done from a thread context. (e.g., call malloc with the M_WAITOK flag.) Note that tasks queued on shared taskqueues such as taskqueue_swi may be delayed an indeterminate amount of time before execution. If queueing delays cannot be tolerated then a private taskqueue should be created with a dedicated processing thread.SEE ALSO
ithread(9), kthread(9), swi(9) timeout(9)HISTORY
This interface first appeared in FreeBSD 5.0. There is a similar facility called work_queue in the Linux kernel.AUTHORS
This manual page was written by Doug Rabson.October 17, 2019 | Debian |