workqueue.h revision 335418
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: stable/11/sys/compat/linuxkpi/common/include/linux/workqueue.h 335418 2018-06-20 06:41:15Z hselasky $
30 */
31#ifndef	_LINUX_WORKQUEUE_H_
32#define	_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
50struct work_struct;
51typedef void (*work_func_t)(struct work_struct *);
52
53struct work_exec {
54	TAILQ_ENTRY(work_exec) entry;
55	struct work_struct *target;
56};
57
58struct 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
68struct 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#define	DECLARE_WORK(name, fn)						\
76	struct work_struct name;					\
77	static void name##_init(void *arg)				\
78	{								\
79		INIT_WORK(&name, fn);					\
80	}								\
81	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
82
83struct delayed_work {
84	struct work_struct work;
85	struct {
86		struct callout callout;
87		struct mtx mtx;
88		int	expires;
89	} timer;
90};
91
92#define	DECLARE_DELAYED_WORK(name, fn)					\
93	struct delayed_work name;					\
94	static void name##_init(void *arg)				\
95	{								\
96		linux_init_delayed_work(&name, fn);			\
97	}								\
98	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
99
100static inline struct delayed_work *
101to_delayed_work(struct work_struct *work)
102{
103	return (container_of(work, struct delayed_work, work));
104}
105
106#define	INIT_WORK(work, fn)						\
107do {									\
108	(work)->func = (fn);						\
109	(work)->work_queue = NULL;					\
110	atomic_set(&(work)->state, 0);					\
111	TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work));	\
112} while (0)
113
114#define	INIT_WORK_ONSTACK(work, fn) \
115	INIT_WORK(work, fn)
116
117#define	INIT_DELAYED_WORK(dwork, fn) \
118	linux_init_delayed_work(dwork, fn)
119
120#define	INIT_DELAYED_WORK_ONSTACK(dwork, fn) \
121	linux_init_delayed_work(dwork, fn)
122
123#define	INIT_DEFERRABLE_WORK(dwork, fn) \
124	INIT_DELAYED_WORK(dwork, fn)
125
126#define	flush_scheduled_work() \
127	taskqueue_drain_all(system_wq->taskqueue)
128
129#define	queue_work(wq, work) \
130	linux_queue_work_on(WORK_CPU_UNBOUND, wq, work)
131
132#define	schedule_work(work) \
133	linux_queue_work_on(WORK_CPU_UNBOUND, system_wq, work)
134
135#define	queue_delayed_work(wq, dwork, delay) \
136	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay)
137
138#define	schedule_delayed_work_on(cpu, dwork, delay) \
139	linux_queue_delayed_work_on(cpu, system_wq, dwork, delay)
140
141#define	queue_work_on(cpu, wq, work) \
142	linux_queue_work_on(cpu, wq, work)
143
144#define	schedule_delayed_work(dwork, delay) \
145	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, system_wq, dwork, delay)
146
147#define	queue_delayed_work_on(cpu, wq, dwork, delay) \
148	linux_queue_delayed_work_on(cpu, wq, dwork, delay)
149
150#define	create_singlethread_workqueue(name) \
151	linux_create_workqueue_common(name, 1)
152
153#define	create_workqueue(name) \
154	linux_create_workqueue_common(name, mp_ncpus)
155
156#define	alloc_ordered_workqueue(name, flags) \
157	linux_create_workqueue_common(name, 1)
158
159#define	alloc_workqueue(name, flags, max_active) \
160	linux_create_workqueue_common(name, max_active)
161
162#define	flush_workqueue(wq) \
163	taskqueue_drain_all((wq)->taskqueue)
164
165#define	drain_workqueue(wq) do {		\
166	atomic_inc(&(wq)->draining);		\
167	taskqueue_drain_all((wq)->taskqueue);	\
168	atomic_dec(&(wq)->draining);		\
169} while (0)
170
171#define	mod_delayed_work(wq, dwork, delay) ({		\
172	bool __retval;					\
173	__retval = linux_cancel_delayed_work(dwork);	\
174	linux_queue_delayed_work_on(WORK_CPU_UNBOUND,	\
175	    wq, dwork, delay);				\
176	__retval;					\
177})
178
179#define	delayed_work_pending(dwork) \
180	linux_work_pending(&(dwork)->work)
181
182#define	cancel_delayed_work(dwork) \
183	linux_cancel_delayed_work(dwork)
184
185#define	cancel_work_sync(work) \
186	linux_cancel_work_sync(work)
187
188#define	cancel_delayed_work_sync(dwork) \
189	linux_cancel_delayed_work_sync(dwork)
190
191#define	flush_work(work) \
192	linux_flush_work(work)
193
194#define	flush_delayed_work(dwork) \
195	linux_flush_delayed_work(dwork)
196
197#define	work_pending(work) \
198	linux_work_pending(work)
199
200#define	work_busy(work) \
201	linux_work_busy(work)
202
203#define	destroy_work_on_stack(work) \
204	do { } while (0)
205
206#define	destroy_delayed_work_on_stack(dwork) \
207	do { } while (0)
208
209#define	destroy_workqueue(wq) \
210	linux_destroy_workqueue(wq)
211
212/* prototypes */
213
214extern struct workqueue_struct *system_wq;
215extern struct workqueue_struct *system_long_wq;
216extern struct workqueue_struct *system_unbound_wq;
217extern struct workqueue_struct *system_power_efficient_wq;
218
219extern void linux_init_delayed_work(struct delayed_work *, work_func_t);
220extern void linux_work_fn(void *, int);
221extern void linux_delayed_work_fn(void *, int);
222extern struct workqueue_struct *linux_create_workqueue_common(const char *, int);
223extern void linux_destroy_workqueue(struct workqueue_struct *);
224extern bool linux_queue_work_on(int cpu, struct workqueue_struct *, struct work_struct *);
225extern bool linux_queue_delayed_work_on(int cpu, struct workqueue_struct *,
226    struct delayed_work *, unsigned delay);
227extern bool linux_cancel_delayed_work(struct delayed_work *);
228extern bool linux_cancel_work_sync(struct work_struct *);
229extern bool linux_cancel_delayed_work_sync(struct delayed_work *);
230extern bool linux_flush_work(struct work_struct *);
231extern bool linux_flush_delayed_work(struct delayed_work *);
232extern bool linux_work_pending(struct work_struct *);
233extern bool linux_work_busy(struct work_struct *);
234
235#endif					/* _LINUX_WORKQUEUE_H_ */
236