*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->FreeBSD man pages -> taskqueue_run (9)              



NAME    [Toc]    [Back]

     taskqueue -- asynchronous task execution

SYNOPSIS    [Toc]    [Back]

     #include <sys/param.h>
     #include <sys/kernel.h>
     #include <sys/malloc.h>
     #include <sys/queue.h>
     #include <sys/taskqueue.h>

     typedef void (*task_fn)(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		     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);

     taskqueue_free(struct taskqueue *queue);

     struct taskqueue *
     taskqueue_find(const char *name);

     taskqueue_enqueue(struct taskqueue *queue, struct task *task);

     taskqueue_run(struct taskqueue *queue);

     TASK_INIT(struct task *task, int priority, task_fn_t *func,
	 void *context);


     TASKQUEUE_DEFINE(name, taskqueue_enqueue_fn enqueue, void *context,

DESCRIPTION    [Toc]    [Back]

     These functions provide a simple interface for asynchronous execution of

     The function taskqueue_create() is used to create new queues.  The arguments
 to taskqueue_create() include a name which should be unique, a set
     of malloc(9) flags which 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

     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
     which 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, otherwise

     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.

     A convenience macro, TASK_INIT(task, priority, func, context) is provided
     to initialise a task structure.  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(name) and TASKQUEUE_DEFINE(name, enqueue,
     context, init) 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 arguments 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 three global taskqueues, taskqueue_swi,
     taskqueue_swi_giant, and taskqueue_thread.  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 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 ( taskqueue_swi,
     taskqueue_swi_giant, or taskqueue_thread ).

     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.)

HISTORY    [Toc]    [Back]

     This interface first appeared in FreeBSD 5.0.  There is a similar facility
 called tqueue in the Linux kernel.

AUTHORS    [Toc]    [Back]

     This man page was written by Doug Rabson.

FreeBSD 5.2.1			 May 12, 2000			 FreeBSD 5.2.1
[ Back ]
 Similar pages
Name OS Title
taskdestroy IRIX destroy a task
taskctl IRIX operations on a task
taskcreate IRIX create a new task
task Tru64 General: Contains task-specific information
dcecp_cell HP-UX A dcecp task object that operates on a DCE cell
dmicwork IRIX call the image converter and have it perform a task
dcecp_cellalias HP-UX A dcecp task object that manages cell name aliases
dcecp_attrlist HP-UX A dcecp task object that manipulates attribute lists
dcecp_user HP-UX A dcecp task object that manipulates user information in a DCE cell
dcecp_host HP-UX A dcecp task object that manages host information in a DCE cell
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service