xref: /netbsd-src/external/cddl/osnet/lib/libzfs/thread_pool_impl.h (revision ba2539a9805a0544ff82c0003cc02fe1eee5603d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * $FreeBSD: head/cddl/compat/opensolaris/misc/thread_pool_impl.h 265689 2014-05-08 16:59:36Z mav $
29  */
30 
31 #ifndef _THREAD_POOL_IMPL_H
32 #define	_THREAD_POOL_IMPL_H
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <thread_pool.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Thread pool implementation definitions.
44  * See <thread_pool.h> for interface declarations.
45  */
46 
47 /*
48  * FIFO queued job
49  */
50 typedef struct tpool_job tpool_job_t;
51 struct tpool_job {
52 	tpool_job_t	*tpj_next;		/* list of jobs */
53 	void		(*tpj_func)(void *);	/* function to call */
54 	void		*tpj_arg;		/* its argument */
55 };
56 
57 /*
58  * List of active threads, linked through their stacks.
59  */
60 typedef struct tpool_active tpool_active_t;
61 struct tpool_active {
62 	tpool_active_t	*tpa_next;	/* list of active threads */
63 	pthread_t	tpa_tid;	/* active thread id */
64 };
65 
66 /*
67  * The thread pool.
68  */
69 struct tpool {
70 	tpool_t		*tp_forw;	/* circular list of all thread pools */
71 	tpool_t		*tp_back;
72 	mutex_t		tp_mutex;	/* protects the pool data */
73 	cond_t		tp_busycv;	/* synchronization in tpool_dispatch */
74 	cond_t		tp_workcv;	/* synchronization with workers */
75 	cond_t		tp_waitcv;	/* synchronization in tpool_wait() */
76 	tpool_active_t	*tp_active;	/* threads performing work */
77 	tpool_job_t	*tp_head;	/* FIFO job queue */
78 	tpool_job_t	*tp_tail;
79 	pthread_attr_t	tp_attr;	/* attributes of the workers */
80 	int		tp_flags;	/* see below */
81 	uint_t		tp_linger;	/* seconds before idle workers exit */
82 	int		tp_njobs;	/* number of jobs in job queue */
83 	int		tp_minimum;	/* minimum number of worker threads */
84 	int		tp_maximum;	/* maximum number of worker threads */
85 	int		tp_current;	/* current number of worker threads */
86 	int		tp_idle;	/* number of idle workers */
87 };
88 
89 /* tp_flags */
90 #define	TP_WAIT		0x01		/* waiting in tpool_wait() */
91 #define	TP_SUSPEND	0x02		/* pool is being suspended */
92 #define	TP_DESTROY	0x04		/* pool is being destroyed */
93 #define	TP_ABANDON	0x08		/* pool is abandoned (auto-destroy) */
94 
95 #ifdef	__cplusplus
96 }
97 #endif
98 
99 #endif /* _THREAD_POOL_IMPL_H */
100