Deleted Added
full compact
subr_taskqueue.c (153676) subr_taskqueue.c (154167)
1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/subr_taskqueue.c 153676 2005-12-23 06:18:33Z scottl $");
28__FBSDID("$FreeBSD: head/sys/kern/subr_taskqueue.c 154167 2006-01-10 06:31:12Z scottl $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/interrupt.h>
34#include <sys/kernel.h>
35#include <sys/kthread.h>
36#include <sys/lock.h>

--- 13 unchanged lines hidden (view full) ---

50 STAILQ_ENTRY(taskqueue) tq_link;
51 STAILQ_HEAD(, task) tq_queue;
52 const char *tq_name;
53 taskqueue_enqueue_fn tq_enqueue;
54 void *tq_context;
55 struct task *tq_running;
56 struct mtx tq_mutex;
57 struct proc **tq_pproc;
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/interrupt.h>
34#include <sys/kernel.h>
35#include <sys/kthread.h>
36#include <sys/lock.h>

--- 13 unchanged lines hidden (view full) ---

50 STAILQ_ENTRY(taskqueue) tq_link;
51 STAILQ_HEAD(, task) tq_queue;
52 const char *tq_name;
53 taskqueue_enqueue_fn tq_enqueue;
54 void *tq_context;
55 struct task *tq_running;
56 struct mtx tq_mutex;
57 struct proc **tq_pproc;
58 int tq_spin;
58};
59
59};
60
61static __inline void
62TQ_LOCK(struct taskqueue *tq)
63{
64 if (tq->tq_spin)
65 mtx_lock_spin(&tq->tq_mutex);
66 else
67 mtx_lock(&tq->tq_mutex);
68}
69
70static __inline void
71TQ_UNLOCK(struct taskqueue *tq)
72{
73 if (tq->tq_spin)
74 mtx_unlock_spin(&tq->tq_mutex);
75 else
76 mtx_unlock(&tq->tq_mutex);
77}
78
60static void init_taskqueue_list(void *data);
61
79static void init_taskqueue_list(void *data);
80
81static __inline int
82TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
83 int t)
84{
85 if (tq->tq_spin)
86 return (msleep_spin(p, m, wm, t));
87 return (msleep(p, m, pri, wm, t));
88}
89
62static void
63init_taskqueue_list(void *data __unused)
64{
65
66 mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
67 STAILQ_INIT(&taskqueue_queues);
68}
69SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
70 NULL);
71
90static void
91init_taskqueue_list(void *data __unused)
92{
93
94 mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
95 STAILQ_INIT(&taskqueue_queues);
96}
97SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
98 NULL);
99
72struct taskqueue *
73taskqueue_create(const char *name, int mflags,
100static struct taskqueue *
101_taskqueue_create(const char *name, int mflags,
74 taskqueue_enqueue_fn enqueue, void *context,
102 taskqueue_enqueue_fn enqueue, void *context,
75 struct proc **pp)
103 struct proc **pp,
104 int mtxflags, const char *mtxname)
76{
77 struct taskqueue *queue;
78
79 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
80 if (!queue)
81 return 0;
82
83 STAILQ_INIT(&queue->tq_queue);
84 queue->tq_name = name;
85 queue->tq_enqueue = enqueue;
86 queue->tq_context = context;
87 queue->tq_pproc = pp;
105{
106 struct taskqueue *queue;
107
108 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
109 if (!queue)
110 return 0;
111
112 STAILQ_INIT(&queue->tq_queue);
113 queue->tq_name = name;
114 queue->tq_enqueue = enqueue;
115 queue->tq_context = context;
116 queue->tq_pproc = pp;
88 mtx_init(&queue->tq_mutex, "taskqueue", NULL, MTX_DEF);
117 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
118 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
89
90 mtx_lock(&taskqueue_queues_mutex);
91 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
92 mtx_unlock(&taskqueue_queues_mutex);
93
94 return queue;
95}
96
119
120 mtx_lock(&taskqueue_queues_mutex);
121 STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
122 mtx_unlock(&taskqueue_queues_mutex);
123
124 return queue;
125}
126
127struct taskqueue *
128taskqueue_create(const char *name, int mflags,
129 taskqueue_enqueue_fn enqueue, void *context,
130 struct proc **pp)
131{
132 return _taskqueue_create(name, mflags, enqueue, context, pp,
133 MTX_DEF, "taskqueue");
134}
135
97/*
98 * Signal a taskqueue thread to terminate.
99 */
100static void
101taskqueue_terminate(struct proc **pp, struct taskqueue *tq)
102{
103 struct proc *p;
104
105 p = *pp;
106 *pp = NULL;
107 if (p) {
108 wakeup_one(tq);
136/*
137 * Signal a taskqueue thread to terminate.
138 */
139static void
140taskqueue_terminate(struct proc **pp, struct taskqueue *tq)
141{
142 struct proc *p;
143
144 p = *pp;
145 *pp = NULL;
146 if (p) {
147 wakeup_one(tq);
109 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
110 mtx_unlock(&tq->tq_mutex); /* let taskqueue thread run */
111 msleep(p, &p->p_mtx, PWAIT, "taskqueue_destroy", 0);
148 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
149 TQ_UNLOCK(tq); /* let taskqueue thread run */
150 TQ_SLEEP(tq, p, &p->p_mtx, PWAIT, "taskqueue_destroy", 0);
112 PROC_UNLOCK(p);
151 PROC_UNLOCK(p);
113 mtx_lock(&tq->tq_mutex);
152 TQ_LOCK(tq);
114 }
115}
116
117void
118taskqueue_free(struct taskqueue *queue)
119{
120
121 mtx_lock(&taskqueue_queues_mutex);
122 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
123 mtx_unlock(&taskqueue_queues_mutex);
124
153 }
154}
155
156void
157taskqueue_free(struct taskqueue *queue)
158{
159
160 mtx_lock(&taskqueue_queues_mutex);
161 STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
162 mtx_unlock(&taskqueue_queues_mutex);
163
125 mtx_lock(&queue->tq_mutex);
164 TQ_LOCK(queue);
126 taskqueue_run(queue);
127 taskqueue_terminate(queue->tq_pproc, queue);
128 mtx_destroy(&queue->tq_mutex);
129 free(queue, M_TASKQUEUE);
130}
131
132/*
133 * Returns with the taskqueue locked.
134 */
135struct taskqueue *
136taskqueue_find(const char *name)
137{
138 struct taskqueue *queue;
139
140 mtx_lock(&taskqueue_queues_mutex);
141 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
142 if (strcmp(queue->tq_name, name) == 0) {
165 taskqueue_run(queue);
166 taskqueue_terminate(queue->tq_pproc, queue);
167 mtx_destroy(&queue->tq_mutex);
168 free(queue, M_TASKQUEUE);
169}
170
171/*
172 * Returns with the taskqueue locked.
173 */
174struct taskqueue *
175taskqueue_find(const char *name)
176{
177 struct taskqueue *queue;
178
179 mtx_lock(&taskqueue_queues_mutex);
180 STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
181 if (strcmp(queue->tq_name, name) == 0) {
143 mtx_lock(&queue->tq_mutex);
182 TQ_LOCK(queue);
144 mtx_unlock(&taskqueue_queues_mutex);
145 return queue;
146 }
147 }
148 mtx_unlock(&taskqueue_queues_mutex);
149 return NULL;
150}
151
152int
153taskqueue_enqueue(struct taskqueue *queue, struct task *task)
154{
155 struct task *ins;
156 struct task *prev;
157
183 mtx_unlock(&taskqueue_queues_mutex);
184 return queue;
185 }
186 }
187 mtx_unlock(&taskqueue_queues_mutex);
188 return NULL;
189}
190
191int
192taskqueue_enqueue(struct taskqueue *queue, struct task *task)
193{
194 struct task *ins;
195 struct task *prev;
196
158 mtx_lock(&queue->tq_mutex);
197 TQ_LOCK(queue);
159
160 /*
161 * Count multiple enqueues.
162 */
163 if (task->ta_pending) {
164 task->ta_pending++;
198
199 /*
200 * Count multiple enqueues.
201 */
202 if (task->ta_pending) {
203 task->ta_pending++;
165 mtx_unlock(&queue->tq_mutex);
204 TQ_UNLOCK(queue);
166 return 0;
167 }
168
169 /*
170 * Optimise the case when all tasks have the same priority.
171 */
172 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
173 if (!prev || prev->ta_priority >= task->ta_priority) {

--- 9 unchanged lines hidden (view full) ---

183 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
184 else
185 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
186 }
187
188 task->ta_pending = 1;
189 queue->tq_enqueue(queue->tq_context);
190
205 return 0;
206 }
207
208 /*
209 * Optimise the case when all tasks have the same priority.
210 */
211 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
212 if (!prev || prev->ta_priority >= task->ta_priority) {

--- 9 unchanged lines hidden (view full) ---

222 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
223 else
224 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
225 }
226
227 task->ta_pending = 1;
228 queue->tq_enqueue(queue->tq_context);
229
191 mtx_unlock(&queue->tq_mutex);
230 TQ_UNLOCK(queue);
192
193 return 0;
194}
195
196void
197taskqueue_run(struct taskqueue *queue)
198{
199 struct task *task;
200 int owned, pending;
201
202 owned = mtx_owned(&queue->tq_mutex);
203 if (!owned)
231
232 return 0;
233}
234
235void
236taskqueue_run(struct taskqueue *queue)
237{
238 struct task *task;
239 int owned, pending;
240
241 owned = mtx_owned(&queue->tq_mutex);
242 if (!owned)
204 mtx_lock(&queue->tq_mutex);
243 TQ_LOCK(queue);
205 while (STAILQ_FIRST(&queue->tq_queue)) {
206 /*
207 * Carefully remove the first task from the queue and
208 * zero its pending count.
209 */
210 task = STAILQ_FIRST(&queue->tq_queue);
211 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
212 pending = task->ta_pending;
213 task->ta_pending = 0;
214 queue->tq_running = task;
244 while (STAILQ_FIRST(&queue->tq_queue)) {
245 /*
246 * Carefully remove the first task from the queue and
247 * zero its pending count.
248 */
249 task = STAILQ_FIRST(&queue->tq_queue);
250 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
251 pending = task->ta_pending;
252 task->ta_pending = 0;
253 queue->tq_running = task;
215 mtx_unlock(&queue->tq_mutex);
254 TQ_UNLOCK(queue);
216
217 task->ta_func(task->ta_context, pending);
218
255
256 task->ta_func(task->ta_context, pending);
257
219 mtx_lock(&queue->tq_mutex);
258 TQ_LOCK(queue);
220 queue->tq_running = NULL;
221 wakeup(task);
222 }
223
224 /*
225 * For compatibility, unlock on return if the queue was not locked
226 * on entry, although this opens a race window.
227 */
228 if (!owned)
259 queue->tq_running = NULL;
260 wakeup(task);
261 }
262
263 /*
264 * For compatibility, unlock on return if the queue was not locked
265 * on entry, although this opens a race window.
266 */
267 if (!owned)
229 mtx_unlock(&queue->tq_mutex);
268 TQ_UNLOCK(queue);
230}
231
232void
233taskqueue_drain(struct taskqueue *queue, struct task *task)
234{
269}
270
271void
272taskqueue_drain(struct taskqueue *queue, struct task *task)
273{
235 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "taskqueue_drain");
274 if (queue->tq_spin) { /* XXX */
275 mtx_lock_spin(&queue->tq_mutex);
276 while (task->ta_pending != 0 || task == queue->tq_running)
277 msleep_spin(task, &queue->tq_mutex, "-", 0);
278 mtx_unlock_spin(&queue->tq_mutex);
279 } else {
280 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
236
281
237 mtx_lock(&queue->tq_mutex);
238 while (task->ta_pending != 0 || task == queue->tq_running)
239 msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
240 mtx_unlock(&queue->tq_mutex);
282 mtx_lock(&queue->tq_mutex);
283 while (task->ta_pending != 0 || task == queue->tq_running)
284 msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
285 mtx_unlock(&queue->tq_mutex);
286 }
241}
242
243static void
244taskqueue_swi_enqueue(void *context)
245{
246 swi_sched(taskqueue_ih, 0);
247}
248

--- 17 unchanged lines hidden (view full) ---

266
267void
268taskqueue_thread_loop(void *arg)
269{
270 struct taskqueue **tqp, *tq;
271
272 tqp = arg;
273 tq = *tqp;
287}
288
289static void
290taskqueue_swi_enqueue(void *context)
291{
292 swi_sched(taskqueue_ih, 0);
293}
294

--- 17 unchanged lines hidden (view full) ---

312
313void
314taskqueue_thread_loop(void *arg)
315{
316 struct taskqueue **tqp, *tq;
317
318 tqp = arg;
319 tq = *tqp;
274 mtx_lock(&tq->tq_mutex);
320 TQ_LOCK(tq);
275 do {
276 taskqueue_run(tq);
321 do {
322 taskqueue_run(tq);
277 msleep(tq, &tq->tq_mutex, PWAIT, "-", 0);
323 TQ_SLEEP(tq, tq, &tq->tq_mutex, curthread->td_priority, "-", 0);
278 } while (*tq->tq_pproc != NULL);
279
280 /* rendezvous with thread that asked us to terminate */
281 wakeup_one(tq);
324 } while (*tq->tq_pproc != NULL);
325
326 /* rendezvous with thread that asked us to terminate */
327 wakeup_one(tq);
282 mtx_unlock(&tq->tq_mutex);
328 TQ_UNLOCK(tq);
283 kthread_exit(0);
284}
285
286void
287taskqueue_thread_enqueue(void *context)
288{
289 struct taskqueue **tqp, *tq;
290

--- 9 unchanged lines hidden (view full) ---

300 INTR_MPSAFE, &taskqueue_ih));
301
302TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
303 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
304 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
305
306TASKQUEUE_DEFINE_THREAD(thread);
307
329 kthread_exit(0);
330}
331
332void
333taskqueue_thread_enqueue(void *context)
334{
335 struct taskqueue **tqp, *tq;
336

--- 9 unchanged lines hidden (view full) ---

346 INTR_MPSAFE, &taskqueue_ih));
347
348TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
349 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
350 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
351
352TASKQUEUE_DEFINE_THREAD(thread);
353
308int
309taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
354struct taskqueue *
355taskqueue_create_fast(const char *name, int mflags,
356 taskqueue_enqueue_fn enqueue, void *context,
357 struct proc **pp)
310{
358{
311 struct task *ins;
312 struct task *prev;
313
314 mtx_lock_spin(&queue->tq_mutex);
315
316 /*
317 * Count multiple enqueues.
318 */
319 if (task->ta_pending) {
320 task->ta_pending++;
321 mtx_unlock_spin(&queue->tq_mutex);
322 return 0;
323 }
324
325 /*
326 * Optimise the case when all tasks have the same priority.
327 */
328 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
329 if (!prev || prev->ta_priority >= task->ta_priority) {
330 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
331 } else {
332 prev = 0;
333 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
334 prev = ins, ins = STAILQ_NEXT(ins, ta_link))
335 if (ins->ta_priority < task->ta_priority)
336 break;
337
338 if (prev)
339 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
340 else
341 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
342 }
343
344 task->ta_pending = 1;
345 queue->tq_enqueue(queue->tq_context);
346
347 mtx_unlock_spin(&queue->tq_mutex);
348
349 return 0;
359 return _taskqueue_create(name, mflags, enqueue, context, pp,
360 MTX_SPIN, "fast_taskqueue");
350}
351
361}
362
352static void
353taskqueue_run_fast(struct taskqueue *queue)
363/* NB: for backwards compatibility */
364int
365taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
354{
366{
355 struct task *task;
356 int pending;
357
358 mtx_lock_spin(&queue->tq_mutex);
359 while (STAILQ_FIRST(&queue->tq_queue)) {
360 /*
361 * Carefully remove the first task from the queue and
362 * zero its pending count.
363 */
364 task = STAILQ_FIRST(&queue->tq_queue);
365 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
366 pending = task->ta_pending;
367 task->ta_pending = 0;
368 mtx_unlock_spin(&queue->tq_mutex);
369
370 task->ta_func(task->ta_context, pending);
371
372 mtx_lock_spin(&queue->tq_mutex);
373 }
374 mtx_unlock_spin(&queue->tq_mutex);
367 return taskqueue_enqueue(queue, task);
375}
376
368}
369
377struct taskqueue *taskqueue_fast;
378static void *taskqueue_fast_ih;
379
380static void
370static void *taskqueue_fast_ih;
371
372static void
381taskqueue_fast_schedule(void *context)
373taskqueue_fast_enqueue(void *context)
382{
383 swi_sched(taskqueue_fast_ih, 0);
384}
385
386static void
387taskqueue_fast_run(void *dummy)
388{
374{
375 swi_sched(taskqueue_fast_ih, 0);
376}
377
378static void
379taskqueue_fast_run(void *dummy)
380{
389 taskqueue_run_fast(taskqueue_fast);
381 taskqueue_run(taskqueue_fast);
390}
391
382}
383
392static void
393taskqueue_define_fast(void *arg)
394{
395
396 taskqueue_fast = malloc(sizeof(struct taskqueue), M_TASKQUEUE,
397 M_NOWAIT | M_ZERO);
398 if (!taskqueue_fast) {
399 printf("%s: Unable to allocate fast task queue!\n", __func__);
400 return;
401 }
402
403 STAILQ_INIT(&taskqueue_fast->tq_queue);
404 taskqueue_fast->tq_name = "fast";
405 taskqueue_fast->tq_enqueue = taskqueue_fast_schedule;
406 mtx_init(&taskqueue_fast->tq_mutex, "taskqueue_fast", NULL, MTX_SPIN);
407
408 mtx_lock(&taskqueue_queues_mutex);
409 STAILQ_INSERT_TAIL(&taskqueue_queues, taskqueue_fast, tq_link);
410 mtx_unlock(&taskqueue_queues_mutex);
411
412 swi_add(NULL, "Fast taskq", taskqueue_fast_run,
413 NULL, SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih);
414}
415SYSINIT(taskqueue_fast, SI_SUB_CONFIGURE, SI_ORDER_SECOND,
416 taskqueue_define_fast, NULL);
384TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, 0,
385 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
386 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));