xref: /freebsd/sys/compat/linuxkpi/common/include/linux/workqueue.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
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.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
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.
17  *
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.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUXKPI_LINUX_WORKQUEUE_H_
32 #define	_LINUXKPI_LINUX_WORKQUEUE_H_
33 
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/timer.h>
37 #include <linux/slab.h>
38 
39 #include <asm/atomic.h>
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/taskqueue.h>
44 #include <sys/mutex.h>
45 
46 #define	WORK_CPU_UNBOUND MAXCPU
47 #define	WQ_UNBOUND (1 << 0)
48 #define	WQ_HIGHPRI (1 << 1)
49 
50 struct work_struct;
51 typedef void (*work_func_t)(struct work_struct *);
52 
53 struct work_exec {
54 	TAILQ_ENTRY(work_exec) entry;
55 	struct work_struct *target;
56 };
57 
58 struct workqueue_struct {
59 	struct taskqueue *taskqueue;
60 	struct mtx exec_mtx;
61 	TAILQ_HEAD(, work_exec) exec_head;
62 	atomic_t draining;
63 };
64 
65 #define	WQ_EXEC_LOCK(wq) mtx_lock(&(wq)->exec_mtx)
66 #define	WQ_EXEC_UNLOCK(wq) mtx_unlock(&(wq)->exec_mtx)
67 
68 struct work_struct {
69 	struct task work_task;
70 	struct workqueue_struct *work_queue;
71 	work_func_t func;
72 	atomic_t state;
73 };
74 
75 struct rcu_work {
76 	struct work_struct work;
77 	struct rcu_head rcu;
78 
79 	struct workqueue_struct *wq;
80 };
81 
82 #define	DECLARE_WORK(name, fn)						\
83 	struct work_struct name;					\
84 	static void name##_init(void *arg)				\
85 	{								\
86 		INIT_WORK(&name, fn);					\
87 	}								\
88 	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
89 
90 struct delayed_work {
91 	struct work_struct work;
92 	struct {
93 		struct callout callout;
94 		struct mtx mtx;
95 		int	expires;
96 	} timer;
97 };
98 
99 #define	DECLARE_DELAYED_WORK(name, fn)					\
100 	struct delayed_work name;					\
101 	static void __linux_delayed_ ## name ## _init(void *arg)	\
102 	{								\
103 		linux_init_delayed_work(&name, fn);			\
104 	}								\
105 	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND,			\
106 	    __linux_delayed_ ## name##_init, NULL)
107 
108 static inline struct delayed_work *
109 to_delayed_work(struct work_struct *work)
110 {
111 	return (container_of(work, struct delayed_work, work));
112 }
113 
114 #define	INIT_WORK(work, fn)						\
115 do {									\
116 	(work)->func = (fn);						\
117 	(work)->work_queue = NULL;					\
118 	atomic_set(&(work)->state, 0);					\
119 	TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work));	\
120 } while (0)
121 
122 #define INIT_RCU_WORK(_work, _fn) \
123 	INIT_WORK(&(_work)->work, (_fn))
124 
125 #define	INIT_WORK_ONSTACK(work, fn) \
126 	INIT_WORK(work, fn)
127 
128 #define	INIT_DELAYED_WORK(dwork, fn) \
129 	linux_init_delayed_work(dwork, fn)
130 
131 #define	INIT_DELAYED_WORK_ONSTACK(dwork, fn) \
132 	linux_init_delayed_work(dwork, fn)
133 
134 #define	INIT_DEFERRABLE_WORK(dwork, fn) \
135 	INIT_DELAYED_WORK(dwork, fn)
136 
137 #define	flush_scheduled_work() \
138 	taskqueue_drain_all(system_wq->taskqueue)
139 
140 #define	queue_work(wq, work) \
141 	linux_queue_work_on(WORK_CPU_UNBOUND, wq, work)
142 
143 #define	schedule_work(work) \
144 	linux_queue_work_on(WORK_CPU_UNBOUND, system_wq, work)
145 
146 #define	queue_delayed_work(wq, dwork, delay) \
147 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay)
148 
149 #define	schedule_delayed_work_on(cpu, dwork, delay) \
150 	linux_queue_delayed_work_on(cpu, system_wq, dwork, delay)
151 
152 #define	queue_work_on(cpu, wq, work) \
153 	linux_queue_work_on(cpu, wq, work)
154 
155 #define	schedule_delayed_work(dwork, delay) \
156 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, system_wq, dwork, delay)
157 
158 #define	queue_delayed_work_on(cpu, wq, dwork, delay) \
159 	linux_queue_delayed_work_on(cpu, wq, dwork, delay)
160 
161 #define	create_singlethread_workqueue(name) \
162 	linux_create_workqueue_common(name, 1)
163 
164 #define	create_workqueue(name) \
165 	linux_create_workqueue_common(name, mp_ncpus)
166 
167 #define	alloc_ordered_workqueue(name, flags) \
168 	linux_create_workqueue_common(name, 1)
169 
170 #define	alloc_workqueue(name, flags, max_active) \
171 	linux_create_workqueue_common(name, max_active)
172 
173 #define	flush_workqueue(wq) \
174 	taskqueue_drain_all((wq)->taskqueue)
175 
176 #define	drain_workqueue(wq) do {		\
177 	atomic_inc(&(wq)->draining);		\
178 	taskqueue_drain_all((wq)->taskqueue);	\
179 	atomic_dec(&(wq)->draining);		\
180 } while (0)
181 
182 #define	mod_delayed_work(wq, dwork, delay) ({		\
183 	bool __retval;					\
184 	__retval = linux_cancel_delayed_work(dwork);	\
185 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND,	\
186 	    wq, dwork, delay);				\
187 	__retval;					\
188 })
189 
190 #define	delayed_work_pending(dwork) \
191 	linux_work_pending(&(dwork)->work)
192 
193 #define	cancel_delayed_work(dwork) \
194 	linux_cancel_delayed_work(dwork)
195 
196 #define	cancel_work_sync(work) \
197 	linux_cancel_work_sync(work)
198 
199 #define	cancel_delayed_work_sync(dwork) \
200 	linux_cancel_delayed_work_sync(dwork)
201 
202 #define	flush_work(work) \
203 	linux_flush_work(work)
204 
205 #define	queue_rcu_work(wq, rwork) \
206 	linux_queue_rcu_work(wq, rwork)
207 
208 #define	flush_rcu_work(rwork) \
209 	linux_flush_rcu_work(rwork)
210 
211 #define	flush_delayed_work(dwork) \
212 	linux_flush_delayed_work(dwork)
213 
214 #define	work_pending(work) \
215 	linux_work_pending(work)
216 
217 #define	work_busy(work) \
218 	linux_work_busy(work)
219 
220 #define	destroy_work_on_stack(work) \
221 	do { } while (0)
222 
223 #define	destroy_delayed_work_on_stack(dwork) \
224 	do { } while (0)
225 
226 #define	destroy_workqueue(wq) \
227 	linux_destroy_workqueue(wq)
228 
229 #define	current_work() \
230 	linux_current_work()
231 
232 /* prototypes */
233 
234 extern struct workqueue_struct *system_wq;
235 extern struct workqueue_struct *system_long_wq;
236 extern struct workqueue_struct *system_unbound_wq;
237 extern struct workqueue_struct *system_highpri_wq;
238 extern struct workqueue_struct *system_power_efficient_wq;
239 
240 extern void linux_init_delayed_work(struct delayed_work *, work_func_t);
241 extern void linux_work_fn(void *, int);
242 extern void linux_delayed_work_fn(void *, int);
243 extern struct workqueue_struct *linux_create_workqueue_common(const char *, int);
244 extern void linux_destroy_workqueue(struct workqueue_struct *);
245 extern bool linux_queue_work_on(int cpu, struct workqueue_struct *, struct work_struct *);
246 extern bool linux_queue_delayed_work_on(int cpu, struct workqueue_struct *,
247     struct delayed_work *, unsigned delay);
248 extern bool linux_cancel_delayed_work(struct delayed_work *);
249 extern bool linux_cancel_work_sync(struct work_struct *);
250 extern bool linux_cancel_delayed_work_sync(struct delayed_work *);
251 extern bool linux_flush_work(struct work_struct *);
252 extern bool linux_flush_delayed_work(struct delayed_work *);
253 extern bool linux_work_pending(struct work_struct *);
254 extern bool linux_work_busy(struct work_struct *);
255 extern struct work_struct *linux_current_work(void);
256 extern bool linux_queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
257 extern bool linux_flush_rcu_work(struct rcu_work *rwork);
258 
259 #endif					/* _LINUXKPI_LINUX_WORKQUEUE_H_ */
260