1 /*-
2  * Copyright (c) 2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/kmem.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/queue.h>
36 #include <sys/taskqueue.h>
37 #include <sys/taskq.h>
38 
39 #include <vm/uma.h>
40 
41 static uma_zone_t taskq_zone;
42 
43 taskq_t *system_taskq = NULL;
44 
45 static void
46 system_taskq_init(void *arg)
47 {
48 
49 	taskq_zone = uma_zcreate("taskq_zone", sizeof(struct ostask),
50 	    NULL, NULL, NULL, NULL, 0, 0);
51 	system_taskq = taskq_create("system_taskq", mp_ncpus, 0, 0, 0, 0);
52 }
53 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init, NULL);
54 
55 static void
56 system_taskq_fini(void *arg)
57 {
58 
59 	taskq_destroy(system_taskq);
60 	uma_zdestroy(taskq_zone);
61 }
62 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini, NULL);
63 
64 taskq_t *
65 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
66     int maxalloc __unused, uint_t flags)
67 {
68 	taskq_t *tq;
69 
70 	if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
71 		nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
72 
73 	tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
74 	tq->tq_queue = taskqueue_create(name, M_WAITOK, taskqueue_thread_enqueue,
75 	    &tq->tq_queue);
76 	(void) taskqueue_start_threads(&tq->tq_queue, nthreads, pri, name);
77 
78 	return ((taskq_t *)tq);
79 }
80 
81 taskq_t *
82 taskq_create_proc(const char *name, int nthreads, pri_t pri, int minalloc,
83     int maxalloc, proc_t *proc __unused, uint_t flags)
84 {
85 
86 	return (taskq_create(name, nthreads, pri, minalloc, maxalloc, flags));
87 }
88 
89 void
90 taskq_destroy(taskq_t *tq)
91 {
92 
93 	taskqueue_free(tq->tq_queue);
94 	kmem_free(tq, sizeof(*tq));
95 }
96 
97 int
98 taskq_member(taskq_t *tq, kthread_t *thread)
99 {
100 
101 	return (taskqueue_member(tq->tq_queue, thread));
102 }
103 
104 static void
105 taskq_run(void *arg, int pending __unused)
106 {
107 	struct ostask *task = arg;
108 
109 	task->ost_func(task->ost_arg);
110 
111 	uma_zfree(taskq_zone, task);
112 }
113 
114 taskqid_t
115 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
116 {
117 	struct ostask *task;
118 	int mflag;
119 
120 	if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
121 		mflag = M_WAITOK;
122 	else
123 		mflag = M_NOWAIT;
124 
125 	task = uma_zalloc(taskq_zone, mflag);
126 	if (task == NULL)
127 		return (0);
128 
129 	task->ost_func = func;
130 	task->ost_arg = arg;
131 
132 	TASK_INIT(&task->ost_task, 0, taskq_run, task);
133 	taskqueue_enqueue(tq->tq_queue, &task->ost_task);
134 
135 	return ((taskqid_t)(void *)task);
136 }
137 
138 #define	TASKQ_MAGIC	0x74541c
139 
140 static void
141 taskq_run_safe(void *arg, int pending __unused)
142 {
143 	struct ostask *task = arg;
144 
145 	ASSERT(task->ost_magic == TASKQ_MAGIC);
146 	task->ost_func(task->ost_arg);
147 	task->ost_magic = 0;
148 }
149 
150 taskqid_t
151 taskq_dispatch_safe(taskq_t *tq, task_func_t func, void *arg,
152     struct ostask *task)
153 {
154 
155 	ASSERT(task->ost_magic != TASKQ_MAGIC);
156 
157 	task->ost_magic = TASKQ_MAGIC;
158 	task->ost_func = func;
159 	task->ost_arg = arg;
160 
161 	TASK_INIT(&task->ost_task, 0, taskq_run_safe, task);
162 	taskqueue_enqueue(tq->tq_queue, &task->ost_task);
163 
164 	return ((taskqid_t)(void *)task);
165 }
166