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