2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LINUX_WORKQUEUE_H_
29 #define _LINUX_WORKQUEUE_H_
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/timer.h>
34 #include <linux/slab.h>
36 #include <sys/taskqueue.h>
38 struct workqueue_struct {
39 struct taskqueue *taskqueue;
43 struct task work_task;
44 struct taskqueue *taskqueue;
45 void (*fn)(struct work_struct *);
49 struct work_struct work;
53 static inline struct delayed_work *
54 to_delayed_work(struct work_struct *work)
57 return container_of(work, struct delayed_work, work);
62 _work_fn(void *context, int pending)
64 struct work_struct *work;
70 #define INIT_WORK(work, func) \
72 (work)->fn = (func); \
73 (work)->taskqueue = NULL; \
74 TASK_INIT(&(work)->work_task, 0, _work_fn, (work)); \
77 #define INIT_DELAYED_WORK(_work, func) \
79 INIT_WORK(&(_work)->work, func); \
80 callout_init(&(_work)->timer, CALLOUT_MPSAFE); \
83 #define INIT_DELAYED_WORK_DEFERRABLE INIT_DELAYED_WORK
85 #define schedule_work(work) \
87 (work)->taskqueue = taskqueue_thread; \
88 taskqueue_enqueue(taskqueue_thread, &(work)->work_task); \
91 #define flush_scheduled_work() flush_taskqueue(taskqueue_thread)
93 #define queue_work(q, work) \
95 (work)->taskqueue = (q)->taskqueue; \
96 taskqueue_enqueue((q)->taskqueue, &(work)->work_task); \
100 _delayed_work_fn(void *arg)
102 struct delayed_work *work;
105 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
109 queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work,
114 pending = work->work.work_task.ta_pending;
115 work->work.taskqueue = wq->taskqueue;
117 callout_reset(&work->timer, delay, _delayed_work_fn, work);
119 _delayed_work_fn((void *)work);
124 static inline struct workqueue_struct *
125 _create_workqueue_common(char *name, int cpus)
127 struct workqueue_struct *wq;
129 wq = kmalloc(sizeof(*wq), M_WAITOK);
130 wq->taskqueue = taskqueue_create((name), M_WAITOK,
131 taskqueue_thread_enqueue, &wq->taskqueue);
132 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
138 #define create_singlethread_workqueue(name) \
139 _create_workqueue_common(name, 1)
141 #define create_workqueue(name) \
142 _create_workqueue_common(name, MAXCPU)
145 destroy_workqueue(struct workqueue_struct *wq)
147 taskqueue_free(wq->taskqueue);
151 #define flush_workqueue(wq) flush_taskqueue((wq)->taskqueue)
154 _flush_fn(void *context, int pending)
159 flush_taskqueue(struct taskqueue *tq)
161 struct task flushtask;
164 TASK_INIT(&flushtask, 0, _flush_fn, NULL);
165 taskqueue_enqueue(tq, &flushtask);
166 taskqueue_drain(tq, &flushtask);
171 cancel_work_sync(struct work_struct *work)
173 if (work->taskqueue &&
174 taskqueue_cancel(work->taskqueue, &work->work_task, NULL))
175 taskqueue_drain(work->taskqueue, &work->work_task);
180 * This may leave work running on another CPU as it does on Linux.
183 cancel_delayed_work(struct delayed_work *work)
186 callout_stop(&work->timer);
187 if (work->work.taskqueue)
188 return (taskqueue_cancel(work->work.taskqueue,
189 &work->work.work_task, NULL) == 0);
193 #endif /* _LINUX_WORKQUEUE_H_ */