xref: /freebsd/sys/contrib/openzfs/lib/libzpool/taskq.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy  * See the License for the specific language governing permissions
12eda14cbcSMatt Macy  * and limitations under the License.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  * CDDL HEADER END
21eda14cbcSMatt Macy  */
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Use is subject to license terms.
25eda14cbcSMatt Macy  */
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28eda14cbcSMatt Macy  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
29eda14cbcSMatt Macy  * Copyright (c) 2014 by Delphix. All rights reserved.
30eda14cbcSMatt Macy  */
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #include <sys/zfs_context.h>
33eda14cbcSMatt Macy 
34eda14cbcSMatt Macy int taskq_now;
35eda14cbcSMatt Macy taskq_t *system_taskq;
36eda14cbcSMatt Macy taskq_t *system_delay_taskq;
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy static pthread_key_t taskq_tsd;
39eda14cbcSMatt Macy 
40eda14cbcSMatt Macy #define	TASKQ_ACTIVE	0x00010000
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy static taskq_ent_t *
task_alloc(taskq_t * tq,int tqflags)43eda14cbcSMatt Macy task_alloc(taskq_t *tq, int tqflags)
44eda14cbcSMatt Macy {
45eda14cbcSMatt Macy 	taskq_ent_t *t;
46eda14cbcSMatt Macy 	int rv;
47eda14cbcSMatt Macy 
48eda14cbcSMatt Macy again:	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
49eda14cbcSMatt Macy 		ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
50eda14cbcSMatt Macy 		tq->tq_freelist = t->tqent_next;
51eda14cbcSMatt Macy 	} else {
52eda14cbcSMatt Macy 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
53eda14cbcSMatt Macy 			if (!(tqflags & KM_SLEEP))
54eda14cbcSMatt Macy 				return (NULL);
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy 			/*
57eda14cbcSMatt Macy 			 * We don't want to exceed tq_maxalloc, but we can't
58eda14cbcSMatt Macy 			 * wait for other tasks to complete (and thus free up
59eda14cbcSMatt Macy 			 * task structures) without risking deadlock with
60eda14cbcSMatt Macy 			 * the caller.  So, we just delay for one second
61eda14cbcSMatt Macy 			 * to throttle the allocation rate. If we have tasks
62eda14cbcSMatt Macy 			 * complete before one second timeout expires then
63eda14cbcSMatt Macy 			 * taskq_ent_free will signal us and we will
64eda14cbcSMatt Macy 			 * immediately retry the allocation.
65eda14cbcSMatt Macy 			 */
66eda14cbcSMatt Macy 			tq->tq_maxalloc_wait++;
67eda14cbcSMatt Macy 			rv = cv_timedwait(&tq->tq_maxalloc_cv,
68eda14cbcSMatt Macy 			    &tq->tq_lock, ddi_get_lbolt() + hz);
69eda14cbcSMatt Macy 			tq->tq_maxalloc_wait--;
70eda14cbcSMatt Macy 			if (rv > 0)
71eda14cbcSMatt Macy 				goto again;		/* signaled */
72eda14cbcSMatt Macy 		}
73eda14cbcSMatt Macy 		mutex_exit(&tq->tq_lock);
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy 		t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy 		mutex_enter(&tq->tq_lock);
78eda14cbcSMatt Macy 		if (t != NULL) {
79eda14cbcSMatt Macy 			/* Make sure we start without any flags */
80eda14cbcSMatt Macy 			t->tqent_flags = 0;
81eda14cbcSMatt Macy 			tq->tq_nalloc++;
82eda14cbcSMatt Macy 		}
83eda14cbcSMatt Macy 	}
84eda14cbcSMatt Macy 	return (t);
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy static void
task_free(taskq_t * tq,taskq_ent_t * t)88eda14cbcSMatt Macy task_free(taskq_t *tq, taskq_ent_t *t)
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy 	if (tq->tq_nalloc <= tq->tq_minalloc) {
91eda14cbcSMatt Macy 		t->tqent_next = tq->tq_freelist;
92eda14cbcSMatt Macy 		tq->tq_freelist = t;
93eda14cbcSMatt Macy 	} else {
94eda14cbcSMatt Macy 		tq->tq_nalloc--;
95eda14cbcSMatt Macy 		mutex_exit(&tq->tq_lock);
96eda14cbcSMatt Macy 		kmem_free(t, sizeof (taskq_ent_t));
97eda14cbcSMatt Macy 		mutex_enter(&tq->tq_lock);
98eda14cbcSMatt Macy 	}
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy 	if (tq->tq_maxalloc_wait)
101eda14cbcSMatt Macy 		cv_signal(&tq->tq_maxalloc_cv);
102eda14cbcSMatt Macy }
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy taskqid_t
taskq_dispatch(taskq_t * tq,task_func_t func,void * arg,uint_t tqflags)105eda14cbcSMatt Macy taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
106eda14cbcSMatt Macy {
107eda14cbcSMatt Macy 	taskq_ent_t *t;
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	if (taskq_now) {
110eda14cbcSMatt Macy 		func(arg);
111eda14cbcSMatt Macy 		return (1);
112eda14cbcSMatt Macy 	}
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy 	mutex_enter(&tq->tq_lock);
115eda14cbcSMatt Macy 	ASSERT(tq->tq_flags & TASKQ_ACTIVE);
116eda14cbcSMatt Macy 	if ((t = task_alloc(tq, tqflags)) == NULL) {
117eda14cbcSMatt Macy 		mutex_exit(&tq->tq_lock);
118eda14cbcSMatt Macy 		return (0);
119eda14cbcSMatt Macy 	}
120eda14cbcSMatt Macy 	if (tqflags & TQ_FRONT) {
121eda14cbcSMatt Macy 		t->tqent_next = tq->tq_task.tqent_next;
122eda14cbcSMatt Macy 		t->tqent_prev = &tq->tq_task;
123eda14cbcSMatt Macy 	} else {
124eda14cbcSMatt Macy 		t->tqent_next = &tq->tq_task;
125eda14cbcSMatt Macy 		t->tqent_prev = tq->tq_task.tqent_prev;
126eda14cbcSMatt Macy 	}
127eda14cbcSMatt Macy 	t->tqent_next->tqent_prev = t;
128eda14cbcSMatt Macy 	t->tqent_prev->tqent_next = t;
129eda14cbcSMatt Macy 	t->tqent_func = func;
130eda14cbcSMatt Macy 	t->tqent_arg = arg;
131eda14cbcSMatt Macy 	t->tqent_flags = 0;
132eda14cbcSMatt Macy 	cv_signal(&tq->tq_dispatch_cv);
133eda14cbcSMatt Macy 	mutex_exit(&tq->tq_lock);
134eda14cbcSMatt Macy 	return (1);
135eda14cbcSMatt Macy }
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy taskqid_t
taskq_dispatch_delay(taskq_t * tq,task_func_t func,void * arg,uint_t tqflags,clock_t expire_time)138eda14cbcSMatt Macy taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
139eda14cbcSMatt Macy     clock_t expire_time)
140eda14cbcSMatt Macy {
141e92ffd9bSMartin Matuska 	(void) tq, (void) func, (void) arg, (void) tqflags, (void) expire_time;
142eda14cbcSMatt Macy 	return (0);
143eda14cbcSMatt Macy }
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy int
taskq_empty_ent(taskq_ent_t * t)146eda14cbcSMatt Macy taskq_empty_ent(taskq_ent_t *t)
147eda14cbcSMatt Macy {
148eda14cbcSMatt Macy 	return (t->tqent_next == NULL);
149eda14cbcSMatt Macy }
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy void
taskq_init_ent(taskq_ent_t * t)152eda14cbcSMatt Macy taskq_init_ent(taskq_ent_t *t)
153eda14cbcSMatt Macy {
154eda14cbcSMatt Macy 	t->tqent_next = NULL;
155eda14cbcSMatt Macy 	t->tqent_prev = NULL;
156eda14cbcSMatt Macy 	t->tqent_func = NULL;
157eda14cbcSMatt Macy 	t->tqent_arg = NULL;
158eda14cbcSMatt Macy 	t->tqent_flags = 0;
159eda14cbcSMatt Macy }
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy void
taskq_dispatch_ent(taskq_t * tq,task_func_t func,void * arg,uint_t flags,taskq_ent_t * t)162eda14cbcSMatt Macy taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
163eda14cbcSMatt Macy     taskq_ent_t *t)
164eda14cbcSMatt Macy {
165eda14cbcSMatt Macy 	ASSERT(func != NULL);
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	/*
168eda14cbcSMatt Macy 	 * Mark it as a prealloc'd task.  This is important
169eda14cbcSMatt Macy 	 * to ensure that we don't free it later.
170eda14cbcSMatt Macy 	 */
171eda14cbcSMatt Macy 	t->tqent_flags |= TQENT_FLAG_PREALLOC;
172eda14cbcSMatt Macy 	/*
173eda14cbcSMatt Macy 	 * Enqueue the task to the underlying queue.
174eda14cbcSMatt Macy 	 */
175eda14cbcSMatt Macy 	mutex_enter(&tq->tq_lock);
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy 	if (flags & TQ_FRONT) {
178eda14cbcSMatt Macy 		t->tqent_next = tq->tq_task.tqent_next;
179eda14cbcSMatt Macy 		t->tqent_prev = &tq->tq_task;
180eda14cbcSMatt Macy 	} else {
181eda14cbcSMatt Macy 		t->tqent_next = &tq->tq_task;
182eda14cbcSMatt Macy 		t->tqent_prev = tq->tq_task.tqent_prev;
183eda14cbcSMatt Macy 	}
184eda14cbcSMatt Macy 	t->tqent_next->tqent_prev = t;
185eda14cbcSMatt Macy 	t->tqent_prev->tqent_next = t;
186eda14cbcSMatt Macy 	t->tqent_func = func;
187eda14cbcSMatt Macy 	t->tqent_arg = arg;
188eda14cbcSMatt Macy 	cv_signal(&tq->tq_dispatch_cv);
189eda14cbcSMatt Macy 	mutex_exit(&tq->tq_lock);
190eda14cbcSMatt Macy }
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy void
taskq_wait(taskq_t * tq)193eda14cbcSMatt Macy taskq_wait(taskq_t *tq)
194eda14cbcSMatt Macy {
195eda14cbcSMatt Macy 	mutex_enter(&tq->tq_lock);
196eda14cbcSMatt Macy 	while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
197eda14cbcSMatt Macy 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
198eda14cbcSMatt Macy 	mutex_exit(&tq->tq_lock);
199eda14cbcSMatt Macy }
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy void
taskq_wait_id(taskq_t * tq,taskqid_t id)202eda14cbcSMatt Macy taskq_wait_id(taskq_t *tq, taskqid_t id)
203eda14cbcSMatt Macy {
204e92ffd9bSMartin Matuska 	(void) id;
205eda14cbcSMatt Macy 	taskq_wait(tq);
206eda14cbcSMatt Macy }
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy void
taskq_wait_outstanding(taskq_t * tq,taskqid_t id)209eda14cbcSMatt Macy taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
210eda14cbcSMatt Macy {
211e92ffd9bSMartin Matuska 	(void) id;
212eda14cbcSMatt Macy 	taskq_wait(tq);
213eda14cbcSMatt Macy }
214eda14cbcSMatt Macy 
215da5137abSMartin Matuska static __attribute__((noreturn)) void
taskq_thread(void * arg)216eda14cbcSMatt Macy taskq_thread(void *arg)
217eda14cbcSMatt Macy {
218eda14cbcSMatt Macy 	taskq_t *tq = arg;
219eda14cbcSMatt Macy 	taskq_ent_t *t;
220eda14cbcSMatt Macy 	boolean_t prealloc;
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy 	VERIFY0(pthread_setspecific(taskq_tsd, tq));
223eda14cbcSMatt Macy 
224eda14cbcSMatt Macy 	mutex_enter(&tq->tq_lock);
225eda14cbcSMatt Macy 	while (tq->tq_flags & TASKQ_ACTIVE) {
226eda14cbcSMatt Macy 		if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
227eda14cbcSMatt Macy 			if (--tq->tq_active == 0)
228eda14cbcSMatt Macy 				cv_broadcast(&tq->tq_wait_cv);
229eda14cbcSMatt Macy 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
230eda14cbcSMatt Macy 			tq->tq_active++;
231eda14cbcSMatt Macy 			continue;
232eda14cbcSMatt Macy 		}
233eda14cbcSMatt Macy 		t->tqent_prev->tqent_next = t->tqent_next;
234eda14cbcSMatt Macy 		t->tqent_next->tqent_prev = t->tqent_prev;
235eda14cbcSMatt Macy 		t->tqent_next = NULL;
236eda14cbcSMatt Macy 		t->tqent_prev = NULL;
237eda14cbcSMatt Macy 		prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
238eda14cbcSMatt Macy 		mutex_exit(&tq->tq_lock);
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy 		rw_enter(&tq->tq_threadlock, RW_READER);
241eda14cbcSMatt Macy 		t->tqent_func(t->tqent_arg);
242eda14cbcSMatt Macy 		rw_exit(&tq->tq_threadlock);
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 		mutex_enter(&tq->tq_lock);
245eda14cbcSMatt Macy 		if (!prealloc)
246eda14cbcSMatt Macy 			task_free(tq, t);
247eda14cbcSMatt Macy 	}
248eda14cbcSMatt Macy 	tq->tq_nthreads--;
249eda14cbcSMatt Macy 	cv_broadcast(&tq->tq_wait_cv);
250eda14cbcSMatt Macy 	mutex_exit(&tq->tq_lock);
251eda14cbcSMatt Macy 	thread_exit();
252eda14cbcSMatt Macy }
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy taskq_t *
taskq_create(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags)255eda14cbcSMatt Macy taskq_create(const char *name, int nthreads, pri_t pri,
256eda14cbcSMatt Macy     int minalloc, int maxalloc, uint_t flags)
257eda14cbcSMatt Macy {
258e92ffd9bSMartin Matuska 	(void) pri;
259eda14cbcSMatt Macy 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
260eda14cbcSMatt Macy 	int t;
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	if (flags & TASKQ_THREADS_CPU_PCT) {
263eda14cbcSMatt Macy 		int pct;
264eda14cbcSMatt Macy 		ASSERT3S(nthreads, >=, 0);
265eda14cbcSMatt Macy 		ASSERT3S(nthreads, <=, 100);
266eda14cbcSMatt Macy 		pct = MIN(nthreads, 100);
267eda14cbcSMatt Macy 		pct = MAX(pct, 0);
268eda14cbcSMatt Macy 
269eda14cbcSMatt Macy 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
270eda14cbcSMatt Macy 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
271eda14cbcSMatt Macy 	} else {
272eda14cbcSMatt Macy 		ASSERT3S(nthreads, >=, 1);
273eda14cbcSMatt Macy 	}
274eda14cbcSMatt Macy 
275eda14cbcSMatt Macy 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
276eda14cbcSMatt Macy 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
277eda14cbcSMatt Macy 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
278eda14cbcSMatt Macy 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
279eda14cbcSMatt Macy 	cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
280be181ee2SMartin Matuska 	(void) strlcpy(tq->tq_name, name, sizeof (tq->tq_name));
281eda14cbcSMatt Macy 	tq->tq_flags = flags | TASKQ_ACTIVE;
282eda14cbcSMatt Macy 	tq->tq_active = nthreads;
283eda14cbcSMatt Macy 	tq->tq_nthreads = nthreads;
284eda14cbcSMatt Macy 	tq->tq_minalloc = minalloc;
285eda14cbcSMatt Macy 	tq->tq_maxalloc = maxalloc;
286eda14cbcSMatt Macy 	tq->tq_task.tqent_next = &tq->tq_task;
287eda14cbcSMatt Macy 	tq->tq_task.tqent_prev = &tq->tq_task;
288eda14cbcSMatt Macy 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (kthread_t *),
289eda14cbcSMatt Macy 	    KM_SLEEP);
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 	if (flags & TASKQ_PREPOPULATE) {
292eda14cbcSMatt Macy 		mutex_enter(&tq->tq_lock);
293eda14cbcSMatt Macy 		while (minalloc-- > 0)
294eda14cbcSMatt Macy 			task_free(tq, task_alloc(tq, KM_SLEEP));
295eda14cbcSMatt Macy 		mutex_exit(&tq->tq_lock);
296eda14cbcSMatt Macy 	}
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	for (t = 0; t < nthreads; t++)
299b985c9caSMartin Matuska 		VERIFY((tq->tq_threadlist[t] = thread_create_named(tq->tq_name,
300b985c9caSMartin Matuska 		    NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL);
301eda14cbcSMatt Macy 
302eda14cbcSMatt Macy 	return (tq);
303eda14cbcSMatt Macy }
304eda14cbcSMatt Macy 
305eda14cbcSMatt Macy void
taskq_destroy(taskq_t * tq)306eda14cbcSMatt Macy taskq_destroy(taskq_t *tq)
307eda14cbcSMatt Macy {
308eda14cbcSMatt Macy 	int nthreads = tq->tq_nthreads;
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	taskq_wait(tq);
311eda14cbcSMatt Macy 
312eda14cbcSMatt Macy 	mutex_enter(&tq->tq_lock);
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy 	tq->tq_flags &= ~TASKQ_ACTIVE;
315eda14cbcSMatt Macy 	cv_broadcast(&tq->tq_dispatch_cv);
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy 	while (tq->tq_nthreads != 0)
318eda14cbcSMatt Macy 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
319eda14cbcSMatt Macy 
320eda14cbcSMatt Macy 	tq->tq_minalloc = 0;
321eda14cbcSMatt Macy 	while (tq->tq_nalloc != 0) {
322eda14cbcSMatt Macy 		ASSERT(tq->tq_freelist != NULL);
323be181ee2SMartin Matuska 		taskq_ent_t *tqent_nexttq = tq->tq_freelist->tqent_next;
324be181ee2SMartin Matuska 		task_free(tq, tq->tq_freelist);
325be181ee2SMartin Matuska 		tq->tq_freelist = tqent_nexttq;
326eda14cbcSMatt Macy 	}
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	mutex_exit(&tq->tq_lock);
329eda14cbcSMatt Macy 
330eda14cbcSMatt Macy 	kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
331eda14cbcSMatt Macy 
332eda14cbcSMatt Macy 	rw_destroy(&tq->tq_threadlock);
333eda14cbcSMatt Macy 	mutex_destroy(&tq->tq_lock);
334eda14cbcSMatt Macy 	cv_destroy(&tq->tq_dispatch_cv);
335eda14cbcSMatt Macy 	cv_destroy(&tq->tq_wait_cv);
336eda14cbcSMatt Macy 	cv_destroy(&tq->tq_maxalloc_cv);
337eda14cbcSMatt Macy 
338eda14cbcSMatt Macy 	kmem_free(tq, sizeof (taskq_t));
339eda14cbcSMatt Macy }
340eda14cbcSMatt Macy 
34114c2e0a0SMartin Matuska /*
34214c2e0a0SMartin Matuska  * Create a taskq with a specified number of pool threads. Allocate
34314c2e0a0SMartin Matuska  * and return an array of nthreads kthread_t pointers, one for each
34414c2e0a0SMartin Matuska  * thread in the pool. The array is not ordered and must be freed
34514c2e0a0SMartin Matuska  * by the caller.
34614c2e0a0SMartin Matuska  */
34714c2e0a0SMartin Matuska taskq_t *
taskq_create_synced(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags,kthread_t *** ktpp)34814c2e0a0SMartin Matuska taskq_create_synced(const char *name, int nthreads, pri_t pri,
34914c2e0a0SMartin Matuska     int minalloc, int maxalloc, uint_t flags, kthread_t ***ktpp)
35014c2e0a0SMartin Matuska {
35114c2e0a0SMartin Matuska 	taskq_t *tq;
35214c2e0a0SMartin Matuska 	kthread_t **kthreads = kmem_zalloc(sizeof (*kthreads) * nthreads,
35314c2e0a0SMartin Matuska 	    KM_SLEEP);
35414c2e0a0SMartin Matuska 
35514c2e0a0SMartin Matuska 	(void) pri; (void) minalloc; (void) maxalloc;
35614c2e0a0SMartin Matuska 
35714c2e0a0SMartin Matuska 	flags &= ~(TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT | TASKQ_DC_BATCH);
35814c2e0a0SMartin Matuska 
35914c2e0a0SMartin Matuska 	tq = taskq_create(name, nthreads, minclsyspri, nthreads, INT_MAX,
36014c2e0a0SMartin Matuska 	    flags | TASKQ_PREPOPULATE);
36114c2e0a0SMartin Matuska 	VERIFY(tq != NULL);
36214c2e0a0SMartin Matuska 	VERIFY(tq->tq_nthreads == nthreads);
36314c2e0a0SMartin Matuska 
36414c2e0a0SMartin Matuska 	for (int i = 0; i < nthreads; i++) {
36514c2e0a0SMartin Matuska 		kthreads[i] = tq->tq_threadlist[i];
36614c2e0a0SMartin Matuska 	}
36714c2e0a0SMartin Matuska 	*ktpp = kthreads;
36814c2e0a0SMartin Matuska 	return (tq);
36914c2e0a0SMartin Matuska }
37014c2e0a0SMartin Matuska 
371eda14cbcSMatt Macy int
taskq_member(taskq_t * tq,kthread_t * t)372eda14cbcSMatt Macy taskq_member(taskq_t *tq, kthread_t *t)
373eda14cbcSMatt Macy {
374eda14cbcSMatt Macy 	int i;
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 	if (taskq_now)
377eda14cbcSMatt Macy 		return (1);
378eda14cbcSMatt Macy 
379eda14cbcSMatt Macy 	for (i = 0; i < tq->tq_nthreads; i++)
380eda14cbcSMatt Macy 		if (tq->tq_threadlist[i] == t)
381eda14cbcSMatt Macy 			return (1);
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy 	return (0);
384eda14cbcSMatt Macy }
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy taskq_t *
taskq_of_curthread(void)387eda14cbcSMatt Macy taskq_of_curthread(void)
388eda14cbcSMatt Macy {
389eda14cbcSMatt Macy 	return (pthread_getspecific(taskq_tsd));
390eda14cbcSMatt Macy }
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy int
taskq_cancel_id(taskq_t * tq,taskqid_t id)393eda14cbcSMatt Macy taskq_cancel_id(taskq_t *tq, taskqid_t id)
394eda14cbcSMatt Macy {
395e92ffd9bSMartin Matuska 	(void) tq, (void) id;
396eda14cbcSMatt Macy 	return (ENOENT);
397eda14cbcSMatt Macy }
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy void
system_taskq_init(void)400eda14cbcSMatt Macy system_taskq_init(void)
401eda14cbcSMatt Macy {
402eda14cbcSMatt Macy 	VERIFY0(pthread_key_create(&taskq_tsd, NULL));
403eda14cbcSMatt Macy 	system_taskq = taskq_create("system_taskq", 64, maxclsyspri, 4, 512,
404eda14cbcSMatt Macy 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
405eda14cbcSMatt Macy 	system_delay_taskq = taskq_create("delay_taskq", 4, maxclsyspri, 4,
406eda14cbcSMatt Macy 	    512, TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
407eda14cbcSMatt Macy }
408eda14cbcSMatt Macy 
409eda14cbcSMatt Macy void
system_taskq_fini(void)410eda14cbcSMatt Macy system_taskq_fini(void)
411eda14cbcSMatt Macy {
412eda14cbcSMatt Macy 	taskq_destroy(system_taskq);
413eda14cbcSMatt Macy 	system_taskq = NULL; /* defensive */
414eda14cbcSMatt Macy 	taskq_destroy(system_delay_taskq);
415eda14cbcSMatt Macy 	system_delay_taskq = NULL;
416eda14cbcSMatt Macy 	VERIFY0(pthread_key_delete(taskq_tsd));
417eda14cbcSMatt Macy }
418