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