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, 2014 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 _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 <sys/taskqueue.h> |
---|
40 | |
---|
41 | struct workqueue_struct { |
---|
42 | struct taskqueue *taskqueue; |
---|
43 | }; |
---|
44 | |
---|
45 | struct work_struct { |
---|
46 | struct task work_task; |
---|
47 | struct taskqueue *taskqueue; |
---|
48 | void (*fn)(struct work_struct *); |
---|
49 | }; |
---|
50 | |
---|
51 | struct delayed_work { |
---|
52 | struct work_struct work; |
---|
53 | struct callout timer; |
---|
54 | }; |
---|
55 | |
---|
56 | static inline struct delayed_work * |
---|
57 | to_delayed_work(struct work_struct *work) |
---|
58 | { |
---|
59 | |
---|
60 | return container_of(work, struct delayed_work, work); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | static inline void |
---|
65 | _work_fn(void *context, int pending) |
---|
66 | { |
---|
67 | struct work_struct *work; |
---|
68 | |
---|
69 | work = context; |
---|
70 | work->fn(work); |
---|
71 | } |
---|
72 | |
---|
73 | #define INIT_WORK(work, func) \ |
---|
74 | do { \ |
---|
75 | (work)->fn = (func); \ |
---|
76 | (work)->taskqueue = NULL; \ |
---|
77 | TASK_INIT(&(work)->work_task, 0, _work_fn, (work)); \ |
---|
78 | } while (0) |
---|
79 | |
---|
80 | #define INIT_DELAYED_WORK(_work, func) \ |
---|
81 | do { \ |
---|
82 | INIT_WORK(&(_work)->work, func); \ |
---|
83 | callout_init(&(_work)->timer, 1); \ |
---|
84 | } while (0) |
---|
85 | |
---|
86 | #define INIT_DEFERRABLE_WORK INIT_DELAYED_WORK |
---|
87 | |
---|
88 | #define schedule_work(work) \ |
---|
89 | do { \ |
---|
90 | (work)->taskqueue = taskqueue_thread; \ |
---|
91 | taskqueue_enqueue(taskqueue_thread, &(work)->work_task); \ |
---|
92 | } while (0) |
---|
93 | |
---|
94 | #define flush_scheduled_work() flush_taskqueue(taskqueue_thread) |
---|
95 | |
---|
96 | static inline int queue_work(struct workqueue_struct *q, struct work_struct *work) |
---|
97 | { |
---|
98 | (work)->taskqueue = (q)->taskqueue; |
---|
99 | /* Return opposite val to align with Linux logic */ |
---|
100 | return !taskqueue_enqueue((q)->taskqueue, &(work)->work_task); |
---|
101 | } |
---|
102 | |
---|
103 | #define queue_work_on(cpu, q, work) queue_work(q, work) |
---|
104 | |
---|
105 | static inline void |
---|
106 | _delayed_work_fn(void *arg) |
---|
107 | { |
---|
108 | struct delayed_work *work; |
---|
109 | |
---|
110 | work = arg; |
---|
111 | taskqueue_enqueue(work->work.taskqueue, &work->work.work_task); |
---|
112 | } |
---|
113 | |
---|
114 | static inline int |
---|
115 | queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, |
---|
116 | unsigned long delay) |
---|
117 | { |
---|
118 | int pending; |
---|
119 | |
---|
120 | pending = work->work.work_task.ta_pending; |
---|
121 | work->work.taskqueue = wq->taskqueue; |
---|
122 | if (delay != 0) |
---|
123 | callout_reset(&work->timer, delay, _delayed_work_fn, work); |
---|
124 | else |
---|
125 | _delayed_work_fn((void *)work); |
---|
126 | |
---|
127 | return (!pending); |
---|
128 | } |
---|
129 | |
---|
130 | static inline bool schedule_delayed_work(struct delayed_work *dwork, |
---|
131 | unsigned long delay) |
---|
132 | { |
---|
133 | struct workqueue_struct wq; |
---|
134 | wq.taskqueue = taskqueue_thread; |
---|
135 | return queue_delayed_work(&wq, dwork, delay); |
---|
136 | } |
---|
137 | |
---|
138 | static inline struct workqueue_struct * |
---|
139 | _create_workqueue_common(char *name, int cpus) |
---|
140 | { |
---|
141 | struct workqueue_struct *wq; |
---|
142 | |
---|
143 | wq = kmalloc(sizeof(*wq), M_WAITOK); |
---|
144 | wq->taskqueue = taskqueue_create((name), M_WAITOK, |
---|
145 | taskqueue_thread_enqueue, &wq->taskqueue); |
---|
146 | taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name); |
---|
147 | |
---|
148 | return (wq); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | #define create_singlethread_workqueue(name) \ |
---|
153 | _create_workqueue_common(name, 1) |
---|
154 | |
---|
155 | #define create_workqueue(name) \ |
---|
156 | _create_workqueue_common(name, MAXCPU) |
---|
157 | |
---|
158 | #define alloc_ordered_workqueue(name, flags) \ |
---|
159 | _create_workqueue_common(name, 1) |
---|
160 | |
---|
161 | #define alloc_workqueue(name, flags, max_active) \ |
---|
162 | _create_workqueue_common(name, max_active) |
---|
163 | |
---|
164 | static inline void |
---|
165 | destroy_workqueue(struct workqueue_struct *wq) |
---|
166 | { |
---|
167 | taskqueue_free(wq->taskqueue); |
---|
168 | kfree(wq); |
---|
169 | } |
---|
170 | |
---|
171 | #define flush_workqueue(wq) flush_taskqueue((wq)->taskqueue) |
---|
172 | |
---|
173 | static inline void |
---|
174 | _flush_fn(void *context, int pending) |
---|
175 | { |
---|
176 | } |
---|
177 | |
---|
178 | static inline void |
---|
179 | flush_taskqueue(struct taskqueue *tq) |
---|
180 | { |
---|
181 | struct task flushtask; |
---|
182 | |
---|
183 | PHOLD(curproc); |
---|
184 | TASK_INIT(&flushtask, 0, _flush_fn, NULL); |
---|
185 | taskqueue_enqueue(tq, &flushtask); |
---|
186 | taskqueue_drain(tq, &flushtask); |
---|
187 | PRELE(curproc); |
---|
188 | } |
---|
189 | |
---|
190 | static inline int |
---|
191 | cancel_work_sync(struct work_struct *work) |
---|
192 | { |
---|
193 | if (work->taskqueue && |
---|
194 | taskqueue_cancel(work->taskqueue, &work->work_task, NULL)) |
---|
195 | taskqueue_drain(work->taskqueue, &work->work_task); |
---|
196 | return 0; |
---|
197 | } |
---|
198 | |
---|
199 | /* |
---|
200 | * This may leave work running on another CPU as it does on Linux. |
---|
201 | */ |
---|
202 | static inline int |
---|
203 | cancel_delayed_work(struct delayed_work *work) |
---|
204 | { |
---|
205 | |
---|
206 | callout_stop(&work->timer); |
---|
207 | if (work->work.taskqueue) |
---|
208 | return (taskqueue_cancel(work->work.taskqueue, |
---|
209 | &work->work.work_task, NULL) == 0); |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | static inline int |
---|
214 | cancel_delayed_work_sync(struct delayed_work *work) |
---|
215 | { |
---|
216 | |
---|
217 | callout_drain(&work->timer); |
---|
218 | if (work->work.taskqueue && |
---|
219 | taskqueue_cancel(work->work.taskqueue, &work->work.work_task, NULL)) |
---|
220 | taskqueue_drain(work->work.taskqueue, &work->work.work_task); |
---|
221 | return 0; |
---|
222 | } |
---|
223 | |
---|
224 | static inline bool |
---|
225 | mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, |
---|
226 | unsigned long delay) |
---|
227 | { |
---|
228 | cancel_delayed_work(dwork); |
---|
229 | queue_delayed_work(wq, dwork, delay); |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | #endif /* _LINUX_WORKQUEUE_H_ */ |
---|