2 * Copyright (c) 2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
34 #include <sys/mutex.h>
35 #include <sys/queue.h>
36 #include <sys/taskqueue.h>
37 #include <sys/taskq.h>
41 static uma_zone_t taskq_zone;
45 task_func_t *ost_func;
49 taskq_t *system_taskq = NULL;
52 system_taskq_init(void *arg)
55 system_taskq = (taskq_t *)taskqueue_thread;
56 taskq_zone = uma_zcreate("taskq_zone", sizeof(struct ostask),
57 NULL, NULL, NULL, NULL, 0, 0);
59 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init, NULL);
62 system_taskq_fini(void *arg)
65 uma_zdestroy(taskq_zone);
67 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini, NULL);
70 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
71 int maxalloc __unused, uint_t flags)
75 if ((flags & TASKQ_THREADS_CPU_PCT) != 0) {
76 /* TODO: Calculate number od threads. */
77 printf("%s: TASKQ_THREADS_CPU_PCT\n", __func__);
80 tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
81 tq->tq_queue = taskqueue_create(name, M_WAITOK, taskqueue_thread_enqueue,
83 (void) taskqueue_start_threads(&tq->tq_queue, nthreads, pri, name);
85 return ((taskq_t *)tq);
89 taskq_destroy(taskq_t *tq)
92 taskqueue_free(tq->tq_queue);
93 kmem_free(tq, sizeof(*tq));
97 taskq_member(taskq_t *tq, kthread_t *thread)
100 return (taskqueue_member(tq->tq_queue, thread));
104 taskq_run(void *arg, int pending __unused)
106 struct ostask *task = arg;
108 task->ost_func(task->ost_arg);
110 uma_zfree(taskq_zone, task);
114 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
119 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
124 task = uma_zalloc(taskq_zone, mflag);
128 task->ost_func = func;
131 TASK_INIT(&task->ost_task, 0, taskq_run, task);
132 taskqueue_enqueue(tq->tq_queue, &task->ost_task);
134 return ((taskqid_t)(void *)task);