xref: /freebsd/sys/contrib/openzfs/lib/libtpool/thread_pool.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 /*
24eda14cbcSMatt Macy  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25eda14cbcSMatt Macy  * Use is subject to license terms.
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy 
28eda14cbcSMatt Macy #include <stdlib.h>
29eda14cbcSMatt Macy #include <signal.h>
30eda14cbcSMatt Macy #include <errno.h>
31eda14cbcSMatt Macy #include <assert.h>
32bb2d13b6SMartin Matuska #include <limits.h>
33eda14cbcSMatt Macy #include "thread_pool_impl.h"
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy static pthread_mutex_t thread_pool_lock = PTHREAD_MUTEX_INITIALIZER;
36eda14cbcSMatt Macy static tpool_t *thread_pools = NULL;
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy static void
delete_pool(tpool_t * tpool)39eda14cbcSMatt Macy delete_pool(tpool_t *tpool)
40eda14cbcSMatt Macy {
41eda14cbcSMatt Macy 	tpool_job_t *job;
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy 	ASSERT(tpool->tp_current == 0 && tpool->tp_active == NULL);
44eda14cbcSMatt Macy 
45eda14cbcSMatt Macy 	/*
46eda14cbcSMatt Macy 	 * Unlink the pool from the global list of all pools.
47eda14cbcSMatt Macy 	 */
48eda14cbcSMatt Macy 	(void) pthread_mutex_lock(&thread_pool_lock);
49eda14cbcSMatt Macy 	if (thread_pools == tpool)
50eda14cbcSMatt Macy 		thread_pools = tpool->tp_forw;
51eda14cbcSMatt Macy 	if (thread_pools == tpool)
52eda14cbcSMatt Macy 		thread_pools = NULL;
53eda14cbcSMatt Macy 	else {
54eda14cbcSMatt Macy 		tpool->tp_back->tp_forw = tpool->tp_forw;
55eda14cbcSMatt Macy 		tpool->tp_forw->tp_back = tpool->tp_back;
56eda14cbcSMatt Macy 	}
57eda14cbcSMatt Macy 	pthread_mutex_unlock(&thread_pool_lock);
58eda14cbcSMatt Macy 
59eda14cbcSMatt Macy 	/*
60eda14cbcSMatt Macy 	 * There should be no pending jobs, but just in case...
61eda14cbcSMatt Macy 	 */
62eda14cbcSMatt Macy 	for (job = tpool->tp_head; job != NULL; job = tpool->tp_head) {
63eda14cbcSMatt Macy 		tpool->tp_head = job->tpj_next;
64eda14cbcSMatt Macy 		free(job);
65eda14cbcSMatt Macy 	}
66eda14cbcSMatt Macy 	(void) pthread_attr_destroy(&tpool->tp_attr);
67eda14cbcSMatt Macy 	free(tpool);
68eda14cbcSMatt Macy }
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy /*
71eda14cbcSMatt Macy  * Worker thread is terminating.
72eda14cbcSMatt Macy  */
73eda14cbcSMatt Macy static void
worker_cleanup(void * arg)74eda14cbcSMatt Macy worker_cleanup(void *arg)
75eda14cbcSMatt Macy {
76eda14cbcSMatt Macy 	tpool_t *tpool = (tpool_t *)arg;
77eda14cbcSMatt Macy 
78eda14cbcSMatt Macy 	if (--tpool->tp_current == 0 &&
79eda14cbcSMatt Macy 	    (tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) {
80eda14cbcSMatt Macy 		if (tpool->tp_flags & TP_ABANDON) {
81eda14cbcSMatt Macy 			pthread_mutex_unlock(&tpool->tp_mutex);
82eda14cbcSMatt Macy 			delete_pool(tpool);
83eda14cbcSMatt Macy 			return;
84eda14cbcSMatt Macy 		}
85eda14cbcSMatt Macy 		if (tpool->tp_flags & TP_DESTROY)
86eda14cbcSMatt Macy 			(void) pthread_cond_broadcast(&tpool->tp_busycv);
87eda14cbcSMatt Macy 	}
88eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
89eda14cbcSMatt Macy }
90eda14cbcSMatt Macy 
91eda14cbcSMatt Macy static void
notify_waiters(tpool_t * tpool)92eda14cbcSMatt Macy notify_waiters(tpool_t *tpool)
93eda14cbcSMatt Macy {
94eda14cbcSMatt Macy 	if (tpool->tp_head == NULL && tpool->tp_active == NULL) {
95eda14cbcSMatt Macy 		tpool->tp_flags &= ~TP_WAIT;
96eda14cbcSMatt Macy 		(void) pthread_cond_broadcast(&tpool->tp_waitcv);
97eda14cbcSMatt Macy 	}
98eda14cbcSMatt Macy }
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy /*
101eda14cbcSMatt Macy  * Called by a worker thread on return from a tpool_dispatch()d job.
102eda14cbcSMatt Macy  */
103eda14cbcSMatt Macy static void
job_cleanup(void * arg)104eda14cbcSMatt Macy job_cleanup(void *arg)
105eda14cbcSMatt Macy {
106eda14cbcSMatt Macy 	tpool_t *tpool = (tpool_t *)arg;
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy 	pthread_t my_tid = pthread_self();
109eda14cbcSMatt Macy 	tpool_active_t *activep;
110eda14cbcSMatt Macy 	tpool_active_t **activepp;
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
113eda14cbcSMatt Macy 	for (activepp = &tpool->tp_active; ; activepp = &activep->tpa_next) {
114eda14cbcSMatt Macy 		activep = *activepp;
115eda14cbcSMatt Macy 		if (activep->tpa_tid == my_tid) {
116eda14cbcSMatt Macy 			*activepp = activep->tpa_next;
117eda14cbcSMatt Macy 			break;
118eda14cbcSMatt Macy 		}
119eda14cbcSMatt Macy 	}
120eda14cbcSMatt Macy 	if (tpool->tp_flags & TP_WAIT)
121eda14cbcSMatt Macy 		notify_waiters(tpool);
122eda14cbcSMatt Macy }
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy static void *
tpool_worker(void * arg)125eda14cbcSMatt Macy tpool_worker(void *arg)
126eda14cbcSMatt Macy {
127eda14cbcSMatt Macy 	tpool_t *tpool = (tpool_t *)arg;
128eda14cbcSMatt Macy 	int elapsed;
129eda14cbcSMatt Macy 	tpool_job_t *job;
130eda14cbcSMatt Macy 	void (*func)(void *);
131eda14cbcSMatt Macy 	tpool_active_t active;
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
134eda14cbcSMatt Macy 	pthread_cleanup_push(worker_cleanup, tpool);
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 	/*
137eda14cbcSMatt Macy 	 * This is the worker's main loop.
138eda14cbcSMatt Macy 	 * It will only be left if a timeout or an error has occurred.
139eda14cbcSMatt Macy 	 */
140eda14cbcSMatt Macy 	active.tpa_tid = pthread_self();
141eda14cbcSMatt Macy 	for (;;) {
142eda14cbcSMatt Macy 		elapsed = 0;
143eda14cbcSMatt Macy 		tpool->tp_idle++;
144eda14cbcSMatt Macy 		if (tpool->tp_flags & TP_WAIT)
145eda14cbcSMatt Macy 			notify_waiters(tpool);
146eda14cbcSMatt Macy 		while ((tpool->tp_head == NULL ||
147eda14cbcSMatt Macy 		    (tpool->tp_flags & TP_SUSPEND)) &&
148eda14cbcSMatt Macy 		    !(tpool->tp_flags & (TP_DESTROY | TP_ABANDON))) {
149eda14cbcSMatt Macy 			if (tpool->tp_current <= tpool->tp_minimum ||
150eda14cbcSMatt Macy 			    tpool->tp_linger == 0) {
151eda14cbcSMatt Macy 				(void) pthread_cond_wait(&tpool->tp_workcv,
152eda14cbcSMatt Macy 				    &tpool->tp_mutex);
153eda14cbcSMatt Macy 			} else {
154eda14cbcSMatt Macy 				struct timespec ts;
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 				clock_gettime(CLOCK_REALTIME, &ts);
157eda14cbcSMatt Macy 				ts.tv_sec += tpool->tp_linger;
158eda14cbcSMatt Macy 
159eda14cbcSMatt Macy 				if (pthread_cond_timedwait(&tpool->tp_workcv,
160eda14cbcSMatt Macy 				    &tpool->tp_mutex, &ts) != 0) {
161eda14cbcSMatt Macy 					elapsed = 1;
162eda14cbcSMatt Macy 					break;
163eda14cbcSMatt Macy 				}
164eda14cbcSMatt Macy 			}
165eda14cbcSMatt Macy 		}
166eda14cbcSMatt Macy 		tpool->tp_idle--;
167eda14cbcSMatt Macy 		if (tpool->tp_flags & TP_DESTROY)
168eda14cbcSMatt Macy 			break;
169eda14cbcSMatt Macy 		if (tpool->tp_flags & TP_ABANDON) {
170eda14cbcSMatt Macy 			/* can't abandon a suspended pool */
171eda14cbcSMatt Macy 			if (tpool->tp_flags & TP_SUSPEND) {
172eda14cbcSMatt Macy 				tpool->tp_flags &= ~TP_SUSPEND;
173eda14cbcSMatt Macy 				(void) pthread_cond_broadcast(
174eda14cbcSMatt Macy 				    &tpool->tp_workcv);
175eda14cbcSMatt Macy 			}
176eda14cbcSMatt Macy 			if (tpool->tp_head == NULL)
177eda14cbcSMatt Macy 				break;
178eda14cbcSMatt Macy 		}
179eda14cbcSMatt Macy 		if ((job = tpool->tp_head) != NULL &&
180eda14cbcSMatt Macy 		    !(tpool->tp_flags & TP_SUSPEND)) {
181eda14cbcSMatt Macy 			elapsed = 0;
182eda14cbcSMatt Macy 			func = job->tpj_func;
183eda14cbcSMatt Macy 			arg = job->tpj_arg;
184eda14cbcSMatt Macy 			tpool->tp_head = job->tpj_next;
185eda14cbcSMatt Macy 			if (job == tpool->tp_tail)
186eda14cbcSMatt Macy 				tpool->tp_tail = NULL;
187eda14cbcSMatt Macy 			tpool->tp_njobs--;
188eda14cbcSMatt Macy 			active.tpa_next = tpool->tp_active;
189eda14cbcSMatt Macy 			tpool->tp_active = &active;
190eda14cbcSMatt Macy 			pthread_mutex_unlock(&tpool->tp_mutex);
191eda14cbcSMatt Macy 			pthread_cleanup_push(job_cleanup, tpool);
192eda14cbcSMatt Macy 			free(job);
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 			sigset_t maskset;
195eda14cbcSMatt Macy 			(void) pthread_sigmask(SIG_SETMASK, NULL, &maskset);
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy 			/*
198eda14cbcSMatt Macy 			 * Call the specified function.
199eda14cbcSMatt Macy 			 */
200eda14cbcSMatt Macy 			func(arg);
201eda14cbcSMatt Macy 			/*
202eda14cbcSMatt Macy 			 * We don't know what this thread has been doing,
203eda14cbcSMatt Macy 			 * so we reset its signal mask and cancellation
204eda14cbcSMatt Macy 			 * state back to the values prior to calling func().
205eda14cbcSMatt Macy 			 */
206eda14cbcSMatt Macy 			(void) pthread_sigmask(SIG_SETMASK, &maskset, NULL);
207eda14cbcSMatt Macy 			(void) pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,
208eda14cbcSMatt Macy 			    NULL);
209eda14cbcSMatt Macy 			(void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,
210eda14cbcSMatt Macy 			    NULL);
211eda14cbcSMatt Macy 			pthread_cleanup_pop(1);
212eda14cbcSMatt Macy 		}
213eda14cbcSMatt Macy 		if (elapsed && tpool->tp_current > tpool->tp_minimum) {
214eda14cbcSMatt Macy 			/*
215eda14cbcSMatt Macy 			 * We timed out and there is no work to be done
216eda14cbcSMatt Macy 			 * and the number of workers exceeds the minimum.
217eda14cbcSMatt Macy 			 * Exit now to reduce the size of the pool.
218eda14cbcSMatt Macy 			 */
219eda14cbcSMatt Macy 			break;
220eda14cbcSMatt Macy 		}
221eda14cbcSMatt Macy 	}
222eda14cbcSMatt Macy 	pthread_cleanup_pop(1);
223eda14cbcSMatt Macy 	return (arg);
224eda14cbcSMatt Macy }
225eda14cbcSMatt Macy 
226eda14cbcSMatt Macy /*
227eda14cbcSMatt Macy  * Create a worker thread, with default signals blocked.
228eda14cbcSMatt Macy  */
229eda14cbcSMatt Macy static int
create_worker(tpool_t * tpool)230eda14cbcSMatt Macy create_worker(tpool_t *tpool)
231eda14cbcSMatt Macy {
232eda14cbcSMatt Macy 	pthread_t thread;
233eda14cbcSMatt Macy 	sigset_t oset;
234eda14cbcSMatt Macy 	int error;
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy 	(void) pthread_sigmask(SIG_SETMASK, NULL, &oset);
237eda14cbcSMatt Macy 	error = pthread_create(&thread, &tpool->tp_attr, tpool_worker, tpool);
238eda14cbcSMatt Macy 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
239eda14cbcSMatt Macy 	return (error);
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy 
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy /*
244eda14cbcSMatt Macy  * pthread_attr_clone: make a copy of a pthread_attr_t.  When old_attr
245eda14cbcSMatt Macy  * is NULL initialize the cloned attr using default values.
246eda14cbcSMatt Macy  */
247eda14cbcSMatt Macy static int
pthread_attr_clone(pthread_attr_t * attr,const pthread_attr_t * old_attr)248eda14cbcSMatt Macy pthread_attr_clone(pthread_attr_t *attr, const pthread_attr_t *old_attr)
249eda14cbcSMatt Macy {
250eda14cbcSMatt Macy 	int error;
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy 	error = pthread_attr_init(attr);
253eda14cbcSMatt Macy 	if (error || (old_attr == NULL))
254eda14cbcSMatt Macy 		return (error);
255eda14cbcSMatt Macy 
256eda14cbcSMatt Macy #ifdef __GLIBC__
257eda14cbcSMatt Macy 	cpu_set_t cpuset;
258eda14cbcSMatt Macy 	size_t cpusetsize = sizeof (cpuset);
259eda14cbcSMatt Macy 	error = pthread_attr_getaffinity_np(old_attr, cpusetsize, &cpuset);
260eda14cbcSMatt Macy 	if (error == 0)
261eda14cbcSMatt Macy 		error = pthread_attr_setaffinity_np(attr, cpusetsize, &cpuset);
262eda14cbcSMatt Macy 	if (error)
263eda14cbcSMatt Macy 		goto error;
264eda14cbcSMatt Macy #endif /* __GLIBC__ */
265eda14cbcSMatt Macy 
266eda14cbcSMatt Macy 	int detachstate;
267eda14cbcSMatt Macy 	error = pthread_attr_getdetachstate(old_attr, &detachstate);
268eda14cbcSMatt Macy 	if (error == 0)
269eda14cbcSMatt Macy 		error = pthread_attr_setdetachstate(attr, detachstate);
270eda14cbcSMatt Macy 	if (error)
271eda14cbcSMatt Macy 		goto error;
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	size_t guardsize;
274eda14cbcSMatt Macy 	error = pthread_attr_getguardsize(old_attr, &guardsize);
275eda14cbcSMatt Macy 	if (error == 0)
276eda14cbcSMatt Macy 		error = pthread_attr_setguardsize(attr, guardsize);
277eda14cbcSMatt Macy 	if (error)
278eda14cbcSMatt Macy 		goto error;
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 	int inheritsched;
281eda14cbcSMatt Macy 	error = pthread_attr_getinheritsched(old_attr, &inheritsched);
282eda14cbcSMatt Macy 	if (error == 0)
283eda14cbcSMatt Macy 		error = pthread_attr_setinheritsched(attr, inheritsched);
284eda14cbcSMatt Macy 	if (error)
285eda14cbcSMatt Macy 		goto error;
286eda14cbcSMatt Macy 
287eda14cbcSMatt Macy 	struct sched_param param;
288eda14cbcSMatt Macy 	error = pthread_attr_getschedparam(old_attr, &param);
289eda14cbcSMatt Macy 	if (error == 0)
290eda14cbcSMatt Macy 		error = pthread_attr_setschedparam(attr, &param);
291eda14cbcSMatt Macy 	if (error)
292eda14cbcSMatt Macy 		goto error;
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	int policy;
295eda14cbcSMatt Macy 	error = pthread_attr_getschedpolicy(old_attr, &policy);
296eda14cbcSMatt Macy 	if (error == 0)
297eda14cbcSMatt Macy 		error = pthread_attr_setschedpolicy(attr, policy);
298eda14cbcSMatt Macy 	if (error)
299eda14cbcSMatt Macy 		goto error;
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	int scope;
302eda14cbcSMatt Macy 	error = pthread_attr_getscope(old_attr, &scope);
303eda14cbcSMatt Macy 	if (error == 0)
304eda14cbcSMatt Macy 		error = pthread_attr_setscope(attr, scope);
305eda14cbcSMatt Macy 	if (error)
306eda14cbcSMatt Macy 		goto error;
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 	void *stackaddr;
309eda14cbcSMatt Macy 	size_t stacksize;
310eda14cbcSMatt Macy 	error = pthread_attr_getstack(old_attr, &stackaddr, &stacksize);
311eda14cbcSMatt Macy 	if (error == 0)
312eda14cbcSMatt Macy 		error = pthread_attr_setstack(attr, stackaddr, stacksize);
313eda14cbcSMatt Macy 	if (error)
314eda14cbcSMatt Macy 		goto error;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	return (0);
317eda14cbcSMatt Macy error:
318eda14cbcSMatt Macy 	pthread_attr_destroy(attr);
319eda14cbcSMatt Macy 	return (error);
320eda14cbcSMatt Macy }
321eda14cbcSMatt Macy 
322eda14cbcSMatt Macy tpool_t	*
tpool_create(uint_t min_threads,uint_t max_threads,uint_t linger,pthread_attr_t * attr)323eda14cbcSMatt Macy tpool_create(uint_t min_threads, uint_t max_threads, uint_t linger,
324eda14cbcSMatt Macy     pthread_attr_t *attr)
325eda14cbcSMatt Macy {
326eda14cbcSMatt Macy 	tpool_t	*tpool;
327eda14cbcSMatt Macy 	void *stackaddr;
328eda14cbcSMatt Macy 	size_t stacksize;
329eda14cbcSMatt Macy 	size_t minstack;
330eda14cbcSMatt Macy 	int error;
331eda14cbcSMatt Macy 
332eda14cbcSMatt Macy 	if (min_threads > max_threads || max_threads < 1) {
333eda14cbcSMatt Macy 		errno = EINVAL;
334eda14cbcSMatt Macy 		return (NULL);
335eda14cbcSMatt Macy 	}
336eda14cbcSMatt Macy 	if (attr != NULL) {
337eda14cbcSMatt Macy 		if (pthread_attr_getstack(attr, &stackaddr, &stacksize) != 0) {
338eda14cbcSMatt Macy 			errno = EINVAL;
339eda14cbcSMatt Macy 			return (NULL);
340eda14cbcSMatt Macy 		}
341eda14cbcSMatt Macy 		/*
342eda14cbcSMatt Macy 		 * Allow only one thread in the pool with a specified stack.
343eda14cbcSMatt Macy 		 * Require threads to have at least the minimum stack size.
344eda14cbcSMatt Macy 		 */
345eda14cbcSMatt Macy 		minstack = PTHREAD_STACK_MIN;
346eda14cbcSMatt Macy 		if (stackaddr != NULL) {
347eda14cbcSMatt Macy 			if (stacksize < minstack || max_threads != 1) {
348eda14cbcSMatt Macy 				errno = EINVAL;
349eda14cbcSMatt Macy 				return (NULL);
350eda14cbcSMatt Macy 			}
351eda14cbcSMatt Macy 		} else if (stacksize != 0 && stacksize < minstack) {
352eda14cbcSMatt Macy 			errno = EINVAL;
353eda14cbcSMatt Macy 			return (NULL);
354eda14cbcSMatt Macy 		}
355eda14cbcSMatt Macy 	}
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy 	tpool = calloc(1, sizeof (*tpool));
358eda14cbcSMatt Macy 	if (tpool == NULL) {
359eda14cbcSMatt Macy 		errno = ENOMEM;
360eda14cbcSMatt Macy 		return (NULL);
361eda14cbcSMatt Macy 	}
362eda14cbcSMatt Macy 	(void) pthread_mutex_init(&tpool->tp_mutex, NULL);
363eda14cbcSMatt Macy 	(void) pthread_cond_init(&tpool->tp_busycv, NULL);
364eda14cbcSMatt Macy 	(void) pthread_cond_init(&tpool->tp_workcv, NULL);
365eda14cbcSMatt Macy 	(void) pthread_cond_init(&tpool->tp_waitcv, NULL);
366eda14cbcSMatt Macy 	tpool->tp_minimum = min_threads;
367eda14cbcSMatt Macy 	tpool->tp_maximum = max_threads;
368eda14cbcSMatt Macy 	tpool->tp_linger = linger;
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	/*
371eda14cbcSMatt Macy 	 * We cannot just copy the attribute pointer.
372eda14cbcSMatt Macy 	 * We need to initialize a new pthread_attr_t structure
373eda14cbcSMatt Macy 	 * with the values from the user-supplied pthread_attr_t.
374eda14cbcSMatt Macy 	 * If the attribute pointer is NULL, we need to initialize
375eda14cbcSMatt Macy 	 * the new pthread_attr_t structure with default values.
376eda14cbcSMatt Macy 	 */
377eda14cbcSMatt Macy 	error = pthread_attr_clone(&tpool->tp_attr, attr);
378eda14cbcSMatt Macy 	if (error) {
379eda14cbcSMatt Macy 		free(tpool);
380eda14cbcSMatt Macy 		errno = error;
381eda14cbcSMatt Macy 		return (NULL);
382eda14cbcSMatt Macy 	}
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 	/* make all pool threads be detached daemon threads */
385eda14cbcSMatt Macy 	(void) pthread_attr_setdetachstate(&tpool->tp_attr,
386eda14cbcSMatt Macy 	    PTHREAD_CREATE_DETACHED);
387eda14cbcSMatt Macy 
388eda14cbcSMatt Macy 	/* insert into the global list of all thread pools */
389eda14cbcSMatt Macy 	pthread_mutex_lock(&thread_pool_lock);
390eda14cbcSMatt Macy 	if (thread_pools == NULL) {
391eda14cbcSMatt Macy 		tpool->tp_forw = tpool;
392eda14cbcSMatt Macy 		tpool->tp_back = tpool;
393eda14cbcSMatt Macy 		thread_pools = tpool;
394eda14cbcSMatt Macy 	} else {
395eda14cbcSMatt Macy 		thread_pools->tp_back->tp_forw = tpool;
396eda14cbcSMatt Macy 		tpool->tp_forw = thread_pools;
397eda14cbcSMatt Macy 		tpool->tp_back = thread_pools->tp_back;
398eda14cbcSMatt Macy 		thread_pools->tp_back = tpool;
399eda14cbcSMatt Macy 	}
400eda14cbcSMatt Macy 	pthread_mutex_unlock(&thread_pool_lock);
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy 	return (tpool);
403eda14cbcSMatt Macy }
404eda14cbcSMatt Macy 
405eda14cbcSMatt Macy /*
406eda14cbcSMatt Macy  * Dispatch a work request to the thread pool.
407eda14cbcSMatt Macy  * If there are idle workers, awaken one.
408eda14cbcSMatt Macy  * Else, if the maximum number of workers has
409eda14cbcSMatt Macy  * not been reached, spawn a new worker thread.
410eda14cbcSMatt Macy  * Else just return with the job added to the queue.
411eda14cbcSMatt Macy  */
412eda14cbcSMatt Macy int
tpool_dispatch(tpool_t * tpool,void (* func)(void *),void * arg)413eda14cbcSMatt Macy tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg)
414eda14cbcSMatt Macy {
415eda14cbcSMatt Macy 	tpool_job_t *job;
416eda14cbcSMatt Macy 
417eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy 	if ((job = calloc(1, sizeof (*job))) == NULL)
420eda14cbcSMatt Macy 		return (-1);
421eda14cbcSMatt Macy 	job->tpj_next = NULL;
422eda14cbcSMatt Macy 	job->tpj_func = func;
423eda14cbcSMatt Macy 	job->tpj_arg = arg;
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
426eda14cbcSMatt Macy 
427aca928a5SMartin Matuska 	if (!(tpool->tp_flags & TP_SUSPEND)) {
428aca928a5SMartin Matuska 		if (tpool->tp_idle > 0)
429aca928a5SMartin Matuska 			(void) pthread_cond_signal(&tpool->tp_workcv);
430aca928a5SMartin Matuska 		else if (tpool->tp_current >= tpool->tp_maximum) {
431aca928a5SMartin Matuska 			/* At worker limit.  Leave task on queue */
432aca928a5SMartin Matuska 		} else {
433aca928a5SMartin Matuska 			if (create_worker(tpool) == 0) {
434aca928a5SMartin Matuska 				/* Started a new worker thread */
435aca928a5SMartin Matuska 				tpool->tp_current++;
436aca928a5SMartin Matuska 			} else if (tpool->tp_current > 0) {
437aca928a5SMartin Matuska 				/* Leave task on queue */
438aca928a5SMartin Matuska 			} else {
439aca928a5SMartin Matuska 				/* Cannot start a single worker! */
440aca928a5SMartin Matuska 				pthread_mutex_unlock(&tpool->tp_mutex);
441aca928a5SMartin Matuska 				free(job);
442aca928a5SMartin Matuska 				return (-1);
443aca928a5SMartin Matuska 			}
444aca928a5SMartin Matuska 		}
445aca928a5SMartin Matuska 	}
446aca928a5SMartin Matuska 
447eda14cbcSMatt Macy 	if (tpool->tp_head == NULL)
448eda14cbcSMatt Macy 		tpool->tp_head = job;
449eda14cbcSMatt Macy 	else
450eda14cbcSMatt Macy 		tpool->tp_tail->tpj_next = job;
451eda14cbcSMatt Macy 	tpool->tp_tail = job;
452eda14cbcSMatt Macy 	tpool->tp_njobs++;
453eda14cbcSMatt Macy 
454eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
455eda14cbcSMatt Macy 	return (0);
456eda14cbcSMatt Macy }
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy static void
tpool_cleanup(void * arg)459eda14cbcSMatt Macy tpool_cleanup(void *arg)
460eda14cbcSMatt Macy {
461eda14cbcSMatt Macy 	tpool_t *tpool = (tpool_t *)arg;
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
464eda14cbcSMatt Macy }
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy /*
467eda14cbcSMatt Macy  * Assumes: by the time tpool_destroy() is called no one will use this
468eda14cbcSMatt Macy  * thread pool in any way and no one will try to dispatch entries to it.
469eda14cbcSMatt Macy  * Calling tpool_destroy() from a job in the pool will cause deadlock.
470eda14cbcSMatt Macy  */
471eda14cbcSMatt Macy void
tpool_destroy(tpool_t * tpool)472eda14cbcSMatt Macy tpool_destroy(tpool_t *tpool)
473eda14cbcSMatt Macy {
474eda14cbcSMatt Macy 	tpool_active_t *activep;
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	ASSERT(!tpool_member(tpool));
477eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
480eda14cbcSMatt Macy 	pthread_cleanup_push(tpool_cleanup, tpool);
481eda14cbcSMatt Macy 
482eda14cbcSMatt Macy 	/* mark the pool as being destroyed; wakeup idle workers */
483eda14cbcSMatt Macy 	tpool->tp_flags |= TP_DESTROY;
484eda14cbcSMatt Macy 	tpool->tp_flags &= ~TP_SUSPEND;
485eda14cbcSMatt Macy 	(void) pthread_cond_broadcast(&tpool->tp_workcv);
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy 	/* cancel all active workers */
488eda14cbcSMatt Macy 	for (activep = tpool->tp_active; activep; activep = activep->tpa_next)
489eda14cbcSMatt Macy 		(void) pthread_cancel(activep->tpa_tid);
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	/* wait for all active workers to finish */
492eda14cbcSMatt Macy 	while (tpool->tp_active != NULL) {
493eda14cbcSMatt Macy 		tpool->tp_flags |= TP_WAIT;
494eda14cbcSMatt Macy 		(void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex);
495eda14cbcSMatt Macy 	}
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	/* the last worker to terminate will wake us up */
498eda14cbcSMatt Macy 	while (tpool->tp_current != 0)
499eda14cbcSMatt Macy 		(void) pthread_cond_wait(&tpool->tp_busycv, &tpool->tp_mutex);
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy 	pthread_cleanup_pop(1);	/* pthread_mutex_unlock(&tpool->tp_mutex); */
502eda14cbcSMatt Macy 	delete_pool(tpool);
503eda14cbcSMatt Macy }
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy /*
506eda14cbcSMatt Macy  * Like tpool_destroy(), but don't cancel workers or wait for them to finish.
507eda14cbcSMatt Macy  * The last worker to terminate will delete the pool.
508eda14cbcSMatt Macy  */
509eda14cbcSMatt Macy void
tpool_abandon(tpool_t * tpool)510eda14cbcSMatt Macy tpool_abandon(tpool_t *tpool)
511eda14cbcSMatt Macy {
512eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
515eda14cbcSMatt Macy 	if (tpool->tp_current == 0) {
516eda14cbcSMatt Macy 		/* no workers, just delete the pool */
517eda14cbcSMatt Macy 		pthread_mutex_unlock(&tpool->tp_mutex);
518eda14cbcSMatt Macy 		delete_pool(tpool);
519eda14cbcSMatt Macy 	} else {
520eda14cbcSMatt Macy 		/* wake up all workers, last one will delete the pool */
521eda14cbcSMatt Macy 		tpool->tp_flags |= TP_ABANDON;
522eda14cbcSMatt Macy 		tpool->tp_flags &= ~TP_SUSPEND;
523eda14cbcSMatt Macy 		(void) pthread_cond_broadcast(&tpool->tp_workcv);
524eda14cbcSMatt Macy 		pthread_mutex_unlock(&tpool->tp_mutex);
525eda14cbcSMatt Macy 	}
526eda14cbcSMatt Macy }
527eda14cbcSMatt Macy 
528eda14cbcSMatt Macy /*
529eda14cbcSMatt Macy  * Wait for all jobs to complete.
530eda14cbcSMatt Macy  * Calling tpool_wait() from a job in the pool will cause deadlock.
531eda14cbcSMatt Macy  */
532eda14cbcSMatt Macy void
tpool_wait(tpool_t * tpool)533eda14cbcSMatt Macy tpool_wait(tpool_t *tpool)
534eda14cbcSMatt Macy {
535eda14cbcSMatt Macy 	ASSERT(!tpool_member(tpool));
536eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
539eda14cbcSMatt Macy 	pthread_cleanup_push(tpool_cleanup, tpool);
540eda14cbcSMatt Macy 	while (tpool->tp_head != NULL || tpool->tp_active != NULL) {
541eda14cbcSMatt Macy 		tpool->tp_flags |= TP_WAIT;
542eda14cbcSMatt Macy 		(void) pthread_cond_wait(&tpool->tp_waitcv, &tpool->tp_mutex);
543eda14cbcSMatt Macy 		ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
544eda14cbcSMatt Macy 	}
545eda14cbcSMatt Macy 	pthread_cleanup_pop(1);	/* pthread_mutex_unlock(&tpool->tp_mutex); */
546eda14cbcSMatt Macy }
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy void
tpool_suspend(tpool_t * tpool)549eda14cbcSMatt Macy tpool_suspend(tpool_t *tpool)
550eda14cbcSMatt Macy {
551eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
554eda14cbcSMatt Macy 	tpool->tp_flags |= TP_SUSPEND;
555eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
556eda14cbcSMatt Macy }
557eda14cbcSMatt Macy 
558eda14cbcSMatt Macy int
tpool_suspended(tpool_t * tpool)559eda14cbcSMatt Macy tpool_suspended(tpool_t *tpool)
560eda14cbcSMatt Macy {
561eda14cbcSMatt Macy 	int suspended;
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
564eda14cbcSMatt Macy 
565eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
566eda14cbcSMatt Macy 	suspended = (tpool->tp_flags & TP_SUSPEND) != 0;
567eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy 	return (suspended);
570eda14cbcSMatt Macy }
571eda14cbcSMatt Macy 
572eda14cbcSMatt Macy void
tpool_resume(tpool_t * tpool)573eda14cbcSMatt Macy tpool_resume(tpool_t *tpool)
574eda14cbcSMatt Macy {
575eda14cbcSMatt Macy 	int excess;
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
580eda14cbcSMatt Macy 	if (!(tpool->tp_flags & TP_SUSPEND)) {
581eda14cbcSMatt Macy 		pthread_mutex_unlock(&tpool->tp_mutex);
582eda14cbcSMatt Macy 		return;
583eda14cbcSMatt Macy 	}
584eda14cbcSMatt Macy 	tpool->tp_flags &= ~TP_SUSPEND;
585eda14cbcSMatt Macy 	(void) pthread_cond_broadcast(&tpool->tp_workcv);
586eda14cbcSMatt Macy 	excess = tpool->tp_njobs - tpool->tp_idle;
587eda14cbcSMatt Macy 	while (excess-- > 0 && tpool->tp_current < tpool->tp_maximum) {
588eda14cbcSMatt Macy 		if (create_worker(tpool) != 0)
589eda14cbcSMatt Macy 			break;		/* pthread_create() failed */
590eda14cbcSMatt Macy 		tpool->tp_current++;
591eda14cbcSMatt Macy 	}
592eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
593eda14cbcSMatt Macy }
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy int
tpool_member(tpool_t * tpool)596eda14cbcSMatt Macy tpool_member(tpool_t *tpool)
597eda14cbcSMatt Macy {
598eda14cbcSMatt Macy 	pthread_t my_tid = pthread_self();
599eda14cbcSMatt Macy 	tpool_active_t *activep;
600eda14cbcSMatt Macy 
601eda14cbcSMatt Macy 	ASSERT(!(tpool->tp_flags & (TP_DESTROY | TP_ABANDON)));
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy 	pthread_mutex_lock(&tpool->tp_mutex);
604eda14cbcSMatt Macy 	for (activep = tpool->tp_active; activep; activep = activep->tpa_next) {
605eda14cbcSMatt Macy 		if (activep->tpa_tid == my_tid) {
606eda14cbcSMatt Macy 			pthread_mutex_unlock(&tpool->tp_mutex);
607eda14cbcSMatt Macy 			return (1);
608eda14cbcSMatt Macy 		}
609eda14cbcSMatt Macy 	}
610eda14cbcSMatt Macy 	pthread_mutex_unlock(&tpool->tp_mutex);
611eda14cbcSMatt Macy 	return (0);
612eda14cbcSMatt Macy }
613