1 /*
2  * Thread pool functions
3  *
4  * Copyright (C) 2012-2020, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _LIBCTHREADS_INTERNAL_THREAD_POOL_H )
23 #define _LIBCTHREADS_INTERNAL_THREAD_POOL_H
24 
25 #include <common.h>
26 #include <types.h>
27 
28 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
29 #include <Threadpoolapiset.h>
30 #endif
31 
32 #include "libcthreads_extern.h"
33 #include "libcthreads_libcerror.h"
34 #include "libcthreads_types.h"
35 
36 #if defined( __cplusplus )
37 extern "C" {
38 #endif
39 
40 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
41 
42 typedef struct libcthreads_internal_thread_pool libcthreads_internal_thread_pool_t;
43 
44 struct libcthreads_internal_thread_pool
45 {
46 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
47 	/* The thread pool
48 	 */
49 	TP_POOL *thread_pool;
50 
51 	/* The cleanup group.
52 	 */
53 	TP_CLEANUP_GROUP *cleanup_group;
54 
55 	/* The callback environment
56 	 */
57 	TP_CALLBACK_ENVIRON callback_environment;
58 
59 #else
60 	/* The number of threads in the pool
61 	 */
62 	int number_of_threads;
63 
64 #if defined( WINAPI )
65 	/* The thread handles array
66 	 */
67 	HANDLE *thread_handles_array;
68 
69 	/* The thread identifiers array
70 	 */
71 	DWORD *thread_identifiers_array;
72 
73 #elif defined( HAVE_PTHREAD_H )
74 	/* The threads array
75 	 */
76 	pthread_t *threads_array;
77 
78 #else
79 #error Missing thread type
80 #endif
81 
82 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
83 
84 	/* The callback function
85 	 */
86 	int (*callback_function)(
87 	       intptr_t *value,
88 	       void *arguments );
89 
90 	/* The value function arguments
91 	 */
92 	void *callback_function_arguments;
93 
94 	/* The (current) pop index
95 	 */
96 	int pop_index;
97 
98 	/* The (current) push index
99 	 */
100 	int push_index;
101 
102 	/* The number of values
103 	 */
104 	int number_of_values;
105 
106 	/* The allocated number of values
107 	 */
108 	int allocated_number_of_values;
109 
110 	/* The values array
111 	 */
112 	intptr_t **values_array;
113 
114 	/* The condition mutex
115 	 */
116 	libcthreads_mutex_t *condition_mutex;
117 
118 	/* The queue empty condition
119 	 */
120 	libcthreads_condition_t *empty_condition;
121 
122 	/* The queue full condition
123 	 */
124 	libcthreads_condition_t *full_condition;
125 
126 	/* The status
127 	 */
128 	uint8_t status;
129 };
130 
131 LIBCTHREADS_EXTERN \
132 int libcthreads_thread_pool_create(
133      libcthreads_thread_pool_t **thread_pool,
134      const libcthreads_thread_attributes_t *thread_attributes,
135      int number_of_threads,
136      int maximum_number_of_values,
137      int (*callback_function)(
138             intptr_t *value,
139             void *arguments ),
140      void *callback_function_arguments,
141      libcerror_error_t **error );
142 
143 int libcthreads_internal_thread_pool_pop(
144      libcthreads_internal_thread_pool_t *internal_thread_pool,
145      intptr_t **value,
146      libcerror_error_t **error );
147 
148 LIBCTHREADS_EXTERN \
149 int libcthreads_thread_pool_push(
150      libcthreads_thread_pool_t *thread_pool,
151      intptr_t *value,
152      libcerror_error_t **error );
153 
154 LIBCTHREADS_EXTERN \
155 int libcthreads_thread_pool_push_sorted(
156      libcthreads_thread_pool_t *thread_pool,
157      intptr_t *value,
158      int (*value_compare_function)(
159             intptr_t *first_value,
160             intptr_t *second_value,
161             libcerror_error_t **error ),
162      uint8_t sort_flags,
163      libcerror_error_t **error );
164 
165 LIBCTHREADS_EXTERN \
166 int libcthreads_thread_pool_join(
167      libcthreads_thread_pool_t **thread_pool,
168      libcerror_error_t **error );
169 
170 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
171 
172 #if defined( __cplusplus )
173 }
174 #endif
175 
176 #endif /* !defined( _LIBCTHREADS_INTERNAL_THREAD_POOL_H ) */
177 
178