2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _LINUX_WORKQUEUE_H_
32 #define _LINUX_WORKQUEUE_H_
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/timer.h>
37 #include <linux/slab.h>
39 #include <sys/taskqueue.h>
41 struct workqueue_struct {
42 struct taskqueue *taskqueue;
46 struct task work_task;
47 struct taskqueue *taskqueue;
48 void (*fn)(struct work_struct *);
52 struct work_struct work;
56 static inline struct delayed_work *
57 to_delayed_work(struct work_struct *work)
60 return container_of(work, struct delayed_work, work);
65 _work_fn(void *context, int pending)
67 struct work_struct *work;
73 #define INIT_WORK(work, func) \
75 (work)->fn = (func); \
76 (work)->taskqueue = NULL; \
77 TASK_INIT(&(work)->work_task, 0, _work_fn, (work)); \
80 #define INIT_DELAYED_WORK(_work, func) \
82 INIT_WORK(&(_work)->work, func); \
83 callout_init(&(_work)->timer, 1); \
86 #define INIT_DEFERRABLE_WORK INIT_DELAYED_WORK
88 #define schedule_work(work) \
90 (work)->taskqueue = taskqueue_thread; \
91 taskqueue_enqueue(taskqueue_thread, &(work)->work_task); \
94 #define flush_scheduled_work() flush_taskqueue(taskqueue_thread)
96 static inline int queue_work(struct workqueue_struct *q, struct work_struct *work)
98 (work)->taskqueue = (q)->taskqueue;
99 /* Return opposite val to align with Linux logic */
100 return !taskqueue_enqueue((q)->taskqueue, &(work)->work_task);
104 _delayed_work_fn(void *arg)
106 struct delayed_work *work;
109 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
113 queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work,
118 pending = work->work.work_task.ta_pending;
119 work->work.taskqueue = wq->taskqueue;
121 callout_reset(&work->timer, delay, _delayed_work_fn, work);
123 _delayed_work_fn((void *)work);
128 static inline bool schedule_delayed_work(struct delayed_work *dwork,
131 struct workqueue_struct wq;
132 wq.taskqueue = taskqueue_thread;
133 return queue_delayed_work(&wq, dwork, delay);
136 static inline struct workqueue_struct *
137 _create_workqueue_common(char *name, int cpus)
139 struct workqueue_struct *wq;
141 wq = kmalloc(sizeof(*wq), M_WAITOK);
142 wq->taskqueue = taskqueue_create((name), M_WAITOK,
143 taskqueue_thread_enqueue, &wq->taskqueue);
144 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
150 #define create_singlethread_workqueue(name) \
151 _create_workqueue_common(name, 1)
153 #define create_workqueue(name) \
154 _create_workqueue_common(name, MAXCPU)
156 #define alloc_ordered_workqueue(name, flags) \
157 _create_workqueue_common(name, 1)
159 #define alloc_workqueue(name, flags, max_active) \
160 _create_workqueue_common(name, max_active)
163 destroy_workqueue(struct workqueue_struct *wq)
165 taskqueue_free(wq->taskqueue);
169 #define flush_workqueue(wq) flush_taskqueue((wq)->taskqueue)
172 _flush_fn(void *context, int pending)
177 flush_taskqueue(struct taskqueue *tq)
179 struct task flushtask;
182 TASK_INIT(&flushtask, 0, _flush_fn, NULL);
183 taskqueue_enqueue(tq, &flushtask);
184 taskqueue_drain(tq, &flushtask);
189 cancel_work_sync(struct work_struct *work)
191 if (work->taskqueue &&
192 taskqueue_cancel(work->taskqueue, &work->work_task, NULL))
193 taskqueue_drain(work->taskqueue, &work->work_task);
198 * This may leave work running on another CPU as it does on Linux.
201 cancel_delayed_work(struct delayed_work *work)
204 callout_stop(&work->timer);
205 if (work->work.taskqueue)
206 return (taskqueue_cancel(work->work.taskqueue,
207 &work->work.work_task, NULL) == 0);
212 cancel_delayed_work_sync(struct delayed_work *work)
215 callout_drain(&work->timer);
216 if (work->work.taskqueue &&
217 taskqueue_cancel(work->work.taskqueue, &work->work.work_task, NULL))
218 taskqueue_drain(work->work.taskqueue, &work->work.work_task);
223 mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
226 cancel_delayed_work(dwork);
227 queue_delayed_work(wq, dwork, delay);
231 #endif /* _LINUX_WORKQUEUE_H_ */