2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 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 <asm/atomic.h>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/taskqueue.h>
44 #include <sys/mutex.h>
46 #define WORK_CPU_UNBOUND MAXCPU
47 #define WQ_UNBOUND (1 << 0)
48 #define WQ_HIGHPRI (1 << 1)
51 typedef void (*work_func_t)(struct work_struct *);
54 TAILQ_ENTRY(work_exec) entry;
55 struct work_struct *target;
58 struct workqueue_struct {
59 struct taskqueue *taskqueue;
61 TAILQ_HEAD(, work_exec) exec_head;
65 #define WQ_EXEC_LOCK(wq) mtx_lock(&(wq)->exec_mtx)
66 #define WQ_EXEC_UNLOCK(wq) mtx_unlock(&(wq)->exec_mtx)
69 struct task work_task;
70 struct workqueue_struct *work_queue;
75 #define DECLARE_WORK(name, fn) \
76 struct work_struct name; \
77 static void name##_init(void *arg) \
79 INIT_WORK(&name, fn); \
81 SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
84 struct work_struct work;
86 struct callout callout;
92 #define DECLARE_DELAYED_WORK(name, fn) \
93 struct delayed_work name; \
94 static void __linux_delayed_ ## name ## _init(void *arg) \
96 linux_init_delayed_work(&name, fn); \
98 SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, \
99 __linux_delayed_ ## name##_init, NULL)
101 static inline struct delayed_work *
102 to_delayed_work(struct work_struct *work)
104 return (container_of(work, struct delayed_work, work));
107 #define INIT_WORK(work, fn) \
109 (work)->func = (fn); \
110 (work)->work_queue = NULL; \
111 atomic_set(&(work)->state, 0); \
112 TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work)); \
115 #define INIT_WORK_ONSTACK(work, fn) \
118 #define INIT_DELAYED_WORK(dwork, fn) \
119 linux_init_delayed_work(dwork, fn)
121 #define INIT_DELAYED_WORK_ONSTACK(dwork, fn) \
122 linux_init_delayed_work(dwork, fn)
124 #define INIT_DEFERRABLE_WORK(dwork, fn) \
125 INIT_DELAYED_WORK(dwork, fn)
127 #define flush_scheduled_work() \
128 taskqueue_drain_all(system_wq->taskqueue)
130 #define queue_work(wq, work) \
131 linux_queue_work_on(WORK_CPU_UNBOUND, wq, work)
133 #define schedule_work(work) \
134 linux_queue_work_on(WORK_CPU_UNBOUND, system_wq, work)
136 #define queue_delayed_work(wq, dwork, delay) \
137 linux_queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay)
139 #define schedule_delayed_work_on(cpu, dwork, delay) \
140 linux_queue_delayed_work_on(cpu, system_wq, dwork, delay)
142 #define queue_work_on(cpu, wq, work) \
143 linux_queue_work_on(cpu, wq, work)
145 #define schedule_delayed_work(dwork, delay) \
146 linux_queue_delayed_work_on(WORK_CPU_UNBOUND, system_wq, dwork, delay)
148 #define queue_delayed_work_on(cpu, wq, dwork, delay) \
149 linux_queue_delayed_work_on(cpu, wq, dwork, delay)
151 #define create_singlethread_workqueue(name) \
152 linux_create_workqueue_common(name, 1)
154 #define create_workqueue(name) \
155 linux_create_workqueue_common(name, mp_ncpus)
157 #define alloc_ordered_workqueue(name, flags) \
158 linux_create_workqueue_common(name, 1)
160 #define alloc_workqueue(name, flags, max_active) \
161 linux_create_workqueue_common(name, max_active)
163 #define flush_workqueue(wq) \
164 taskqueue_drain_all((wq)->taskqueue)
166 #define drain_workqueue(wq) do { \
167 atomic_inc(&(wq)->draining); \
168 taskqueue_drain_all((wq)->taskqueue); \
169 atomic_dec(&(wq)->draining); \
172 #define mod_delayed_work(wq, dwork, delay) ({ \
174 __retval = linux_cancel_delayed_work(dwork); \
175 linux_queue_delayed_work_on(WORK_CPU_UNBOUND, \
180 #define delayed_work_pending(dwork) \
181 linux_work_pending(&(dwork)->work)
183 #define cancel_delayed_work(dwork) \
184 linux_cancel_delayed_work(dwork)
186 #define cancel_work_sync(work) \
187 linux_cancel_work_sync(work)
189 #define cancel_delayed_work_sync(dwork) \
190 linux_cancel_delayed_work_sync(dwork)
192 #define flush_work(work) \
193 linux_flush_work(work)
195 #define flush_delayed_work(dwork) \
196 linux_flush_delayed_work(dwork)
198 #define work_pending(work) \
199 linux_work_pending(work)
201 #define work_busy(work) \
202 linux_work_busy(work)
204 #define destroy_work_on_stack(work) \
207 #define destroy_delayed_work_on_stack(dwork) \
210 #define destroy_workqueue(wq) \
211 linux_destroy_workqueue(wq)
213 #define current_work() \
218 extern struct workqueue_struct *system_wq;
219 extern struct workqueue_struct *system_long_wq;
220 extern struct workqueue_struct *system_unbound_wq;
221 extern struct workqueue_struct *system_highpri_wq;
222 extern struct workqueue_struct *system_power_efficient_wq;
224 extern void linux_init_delayed_work(struct delayed_work *, work_func_t);
225 extern void linux_work_fn(void *, int);
226 extern void linux_delayed_work_fn(void *, int);
227 extern struct workqueue_struct *linux_create_workqueue_common(const char *, int);
228 extern void linux_destroy_workqueue(struct workqueue_struct *);
229 extern bool linux_queue_work_on(int cpu, struct workqueue_struct *, struct work_struct *);
230 extern bool linux_queue_delayed_work_on(int cpu, struct workqueue_struct *,
231 struct delayed_work *, unsigned delay);
232 extern bool linux_cancel_delayed_work(struct delayed_work *);
233 extern bool linux_cancel_work_sync(struct work_struct *);
234 extern bool linux_cancel_delayed_work_sync(struct delayed_work *);
235 extern bool linux_flush_work(struct work_struct *);
236 extern bool linux_flush_delayed_work(struct delayed_work *);
237 extern bool linux_work_pending(struct work_struct *);
238 extern bool linux_work_busy(struct work_struct *);
239 extern struct work_struct *linux_current_work(void);
241 #endif /* _LINUX_WORKQUEUE_H_ */