xref: /freebsd/sys/cddl/compat/opensolaris/kern/opensolaris_taskq.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 struct proc *system_proc;
46 
47 static void
48 system_taskq_init(void *arg)
49 {
50 
51 	taskq_zone = uma_zcreate("taskq_zone", sizeof(taskq_ent_t),
52 	    NULL, NULL, NULL, NULL, 0, 0);
53 	system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri,
54 	    0, 0, 0);
55 }
56 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init, NULL);
57 
58 static void
59 system_taskq_fini(void *arg)
60 {
61 
62 	taskq_destroy(system_taskq);
63 	uma_zdestroy(taskq_zone);
64 }
65 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini, NULL);
66 
67 static taskq_t *
68 taskq_create_impl(const char *name, int nthreads, pri_t pri, proc_t *proc,
69     uint_t flags)
70 {
71 	taskq_t *tq;
72 
73 	if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
74 		nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
75 
76 	tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
77 	tq->tq_queue = taskqueue_create(name, M_WAITOK, taskqueue_thread_enqueue,
78 	    &tq->tq_queue);
79 	(void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri,
80 	    proc, "%s", name);
81 
82 	return ((taskq_t *)tq);
83 }
84 
85 taskq_t *
86 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
87     int maxalloc __unused, uint_t flags)
88 {
89 	return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
90 }
91 
92 taskq_t *
93 taskq_create_proc(const char *name, int nthreads, pri_t pri, int minalloc,
94     int maxalloc, proc_t *proc, uint_t flags)
95 {
96 	return (taskq_create_impl(name, nthreads, pri, proc, flags));
97 }
98 
99 void
100 taskq_destroy(taskq_t *tq)
101 {
102 
103 	taskqueue_free(tq->tq_queue);
104 	kmem_free(tq, sizeof(*tq));
105 }
106 
107 int
108 taskq_member(taskq_t *tq, kthread_t *thread)
109 {
110 
111 	return (taskqueue_member(tq->tq_queue, thread));
112 }
113 
114 static void
115 taskq_run(void *arg, int pending __unused)
116 {
117 	taskq_ent_t *task = arg;
118 
119 	task->tqent_func(task->tqent_arg);
120 
121 	uma_zfree(taskq_zone, task);
122 }
123 
124 taskqid_t
125 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
126 {
127 	taskq_ent_t *task;
128 	int mflag, prio;
129 
130 	if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
131 		mflag = M_WAITOK;
132 	else
133 		mflag = M_NOWAIT;
134 	/*
135 	 * If TQ_FRONT is given, we want higher priority for this task, so it
136 	 * can go at the front of the queue.
137 	 */
138 	prio = !!(flags & TQ_FRONT);
139 
140 	task = uma_zalloc(taskq_zone, mflag);
141 	if (task == NULL)
142 		return (0);
143 
144 	task->tqent_func = func;
145 	task->tqent_arg = arg;
146 
147 	TASK_INIT(&task->tqent_task, prio, taskq_run, task);
148 	taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
149 
150 	return ((taskqid_t)(void *)task);
151 }
152 
153 static void
154 taskq_run_ent(void *arg, int pending __unused)
155 {
156 	taskq_ent_t *task = arg;
157 
158 	task->tqent_func(task->tqent_arg);
159 }
160 
161 void
162 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, u_int flags,
163     taskq_ent_t *task)
164 {
165 	int prio;
166 
167 	/*
168 	 * If TQ_FRONT is given, we want higher priority for this task, so it
169 	 * can go at the front of the queue.
170 	 */
171 	prio = !!(flags & TQ_FRONT);
172 
173 	task->tqent_func = func;
174 	task->tqent_arg = arg;
175 
176 	TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
177 	taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
178 }
179 
180 void
181 taskq_wait(taskq_t *tq)
182 {
183 	taskqueue_quiesce(tq->tq_queue);
184 }
185 
186 void
187 taskq_wait_id(taskq_t *tq, taskqid_t id)
188 {
189 	taskqueue_drain_all(tq->tq_queue);
190 }
191