taskqueue.h revision 61033
1139965Simp/*- 21556Srgrimes * Copyright (c) 2000 Doug Rabson 31556Srgrimes * All rights reserved. 496196Sdes * 596196Sdes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 796196Sdes * are met: 896196Sdes * 1. Redistributions of source code must retain the above copyright 996196Sdes * notice, this list of conditions and the following disclaimer. 1096196Sdes * 2. Redistributions in binary form must reproduce the above copyright 1196196Sdes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 141556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241556Srgrimes * SUCH DAMAGE. 251556Srgrimes * 261556Srgrimes * $FreeBSD: head/sys/sys/taskqueue.h 61033 2000-05-28 15:45:30Z dfr $ 271556Srgrimes */ 281556Srgrimes 291556Srgrimes#ifndef _SYS_TASKQUEUE_H_ 301556Srgrimes#define _SYS_TASKQUEUE_H_ 311556Srgrimes 321556Srgrimes#ifdef _KERNEL 331556Srgrimes 341556Srgrimesstruct taskqueue; 351556Srgrimes 361556Srgrimes/* 37114433Sobrien * Each task includes a function which is called from 381556Srgrimes * taskqueue_run(). The first argument is taken from the 'ta_context' 3920420Ssteve * field of struct task and the second argument is a count of how many 401556Srgrimes * times the task was enqueued before the call to taskqueue_run(). 411556Srgrimes */ 421556Srgrimestypedef void (*task_fn)(void *context, int pending); 431556Srgrimes 441556Srgrimes/* 4536149Scharnier * A notification callback function which is called from 46104559Scharnier * taskqueue_enqueue(). The context argument is given in the call to 4736149Scharnier * taskqueue_create(). This function would normally be used to allow the 4899110Sobrien * queue to arrange to run itself later (e.g. by scheduling a software 4999110Sobrien * interrupt or waking a kernel thread). 501556Srgrimes */ 511556Srgrimestypedef void (*taskqueue_enqueue_fn)(void *context); 521556Srgrimes 531556Srgrimesstruct task { 541556Srgrimes STAILQ_ENTRY(task) ta_link; /* link for queue */ 551556Srgrimes int ta_pending; /* count times queued */ 561556Srgrimes int ta_priority; /* priority of task in queue */ 571556Srgrimes task_fn ta_func; /* task handler */ 581556Srgrimes void *ta_context; /* argument for handler */ 591556Srgrimes}; 601556Srgrimes 611556Srgrimesstruct taskqueue *taskqueue_create(const char *name, int mflags, 621556Srgrimes taskqueue_enqueue_fn enqueue, 631556Srgrimes void *context); 6477462Simpvoid taskqueue_free(struct taskqueue *queue); 6577462Simpstruct taskqueue *taskqueue_find(const char *name); 661556Srgrimesint taskqueue_enqueue(struct taskqueue *queue, 6796196Sdes struct task *task); 681556Srgrimesvoid taskqueue_run(struct taskqueue *queue); 691556Srgrimes 70104549Stjr/* 711556Srgrimes * Initialise a task structure. 721556Srgrimes */ 731556Srgrimes#define TASK_INIT(task, priority, func, context) do { \ 741556Srgrimes task->ta_pending = 0; \ 751556Srgrimes task->ta_priority = priority; \ 761556Srgrimes task->ta_func = func; \ 771556Srgrimes task->ta_context = context; \ 781556Srgrimes} while (0) 79101591Sume 801556Srgrimes/* 811556Srgrimes * Declare a reference to a taskqueue. 821556Srgrimes */ 831556Srgrimes#define TASKQUEUE_DECLARE(name) \ 841556Srgrimes \ 851556Srgrimesextern struct taskqueue *taskqueue_##name 86101591Sume 871556Srgrimes/* 8834898Smarkm * Define and initialise a taskqueue. 89114583Smarkm */ 9034898Smarkm#define TASKQUEUE_DEFINE(name, enqueue, context, init) \ 91114583Smarkm \ 92114583Smarkmstruct taskqueue *taskqueue_##name; \ 931556Srgrimes \ 941556Srgrimesstatic void \ 951556Srgrimestaskqueue_define_##name(void *arg) \ 9690110Simp{ \ 9790110Simp taskqueue_##name = \ 9890110Simp taskqueue_create(#name, M_NOWAIT, enqueue, context); \ 9990110Simp init; \ 10090110Simp} \ 10190110Simp \ 10290110SimpSYSINIT(taskqueue_##name, SI_SUB_CONFIGURE, SI_ORDER_SECOND, \ 1031556Srgrimes taskqueue_define_##name, NULL); 1041556Srgrimes 10590110Simp/* 1061556Srgrimes * This queue is serviced by a software interrupt handler. To enqueue 1071556Srgrimes * a task, call taskqueue_enqueue(&taskqueue_swi, &task). 10848560Sbde */ 109114583SmarkmTASKQUEUE_DECLARE(swi); 1101556Srgrimes 11134898Smarkm#endif /* _KERNEL */ 11234898Smarkm 11334898Smarkm#endif /* !_SYS_TASKQUEUE_H_ */ 11434898Smarkm