Debian Codegraph @

libcthreads/libcthreads_thread_pool.c

raw symbols SCIP
1 /*
2 * Thread pool functions
3 *
4 * Copyright (C) 2012-2024, Joachim Metz <[email protected]>
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 #include <common.h>
23 #include <memory.h>
24 #include <types.h>
25
26 #include <errno.h>
27
28 #if defined( _MSC_VER ) && defined( WINAPI ) && ( WINVER >= 0x0602 )
29 #include <Threadpoolapiset.h>
30 #endif
31
32 #include "libcthreads_condition.h"
33 #include "libcthreads_definitions.h"
34 #include "libcthreads_libcerror.h"
35 #include "libcthreads_mutex.h"
36 #include "libcthreads_queue.h"
37 #include "libcthreads_thread.h"
38 #include "libcthreads_thread_attributes.h"
39 #include "libcthreads_thread_pool.h"
40 #include "libcthreads_types.h"
41 #include "libcthreads_unused.h"
42
43 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
44
45 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
46
47 void CALLBACK libcthreads_thread_pool_callback_function_helper(
48 TP_CALLBACK_INSTANCE *callback_instance,
49 void *arguments,
50 TP_WORK *thread_pool_work )
51 {
52 libcerror_error_t *error = NULL;
53 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
54 intptr_t *value = NULL;
55 int callback_function_result = 0;
56 int pop_result = 0;
57
58 LIBCTHREADS_UNREFERENCED_PARAMETER( callback_instance )
59 LIBCTHREADS_UNREFERENCED_PARAMETER( thread_pool_work )
60
61 if( arguments == NULL )
62 {
63 return;
64 }
65 internal_thread_pool = (libcthreads_internal_thread_pool_t *) arguments;
66
67 if( ( internal_thread_pool == NULL )
68 || ( internal_thread_pool->callback_function == NULL ) )
69 {
70 return;
71 }
72 pop_result = libcthreads_internal_thread_pool_pop(
73 internal_thread_pool,
74 &value,
75 &error );
76
77 if( pop_result == -1 )
78 {
79 #if defined( HAVE_VERBOSE_OUTPUT )
80 libcerror_error_backtrace_fprint(
81 error,
82 stdout );
83 #endif
84 libcerror_error_free(
85 &error );
86
87 return;
88 }
89 if( pop_result == 0 )
90 {
91 return;
92 }
93 callback_function_result = internal_thread_pool->callback_function(
94 value,
95 internal_thread_pool->callback_function_arguments );
96
97 if( callback_function_result != 1 )
98 {
99 #if defined( HAVE_VERBOSE_OUTPUT )
100 libcerror_error_backtrace_fprint(
101 error,
102 stdout );
103 #endif
104 libcerror_error_free(
105 &error );
106 }
107 }
108
109 #elif defined( WINAPI )
110
111 /* Start function helper function for WINAPI
112 * Returns 0 if successful or 1 on error
113 */
114 DWORD WINAPI libcthreads_thread_pool_callback_function_helper(
115 void *arguments )
116 {
117 libcerror_error_t *error = NULL;
118 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
119 intptr_t *value = NULL;
120 DWORD result = 1;
121 int callback_function_result = 0;
122 int pop_result = 0;
123
124 if( arguments != NULL )
125 {
126 internal_thread_pool = (libcthreads_internal_thread_pool_t *) arguments;
127
128 if( ( internal_thread_pool != NULL )
129 && ( internal_thread_pool->callback_function != NULL ) )
130 {
131 result = 0;
132
133 do
134 {
135 pop_result = libcthreads_internal_thread_pool_pop(
136 internal_thread_pool,
137 &value,
138 &error );
139
140 if( pop_result == -1 )
141 {
142 break;
143 }
144 else if( pop_result != 0 )
145 {
146 callback_function_result = internal_thread_pool->callback_function(
147 value,
148 internal_thread_pool->callback_function_arguments );
149
150 if( ( callback_function_result != 1 )
151 && ( result == 1 ) )
152 {
153 result = callback_function_result;
154 }
155 }
156 else if( internal_thread_pool->status == LIBCTHREADS_STATUS_EXIT )
157 {
158 break;
159 }
160 }
161 while( pop_result != -1 );
162
163 if( pop_result == -1 )
164 {
165 result = 1;
166 }
167 if( error != NULL )
168 {
169 #if defined( HAVE_VERBOSE_OUTPUT )
170 libcerror_error_backtrace_fprint(
171 error,
172 stdout );
173 #endif
174 libcerror_error_free(
175 &error );
176 }
177 }
178 }
179 ExitThread(
180 result );
181 }
182
183 #elif defined( HAVE_PTHREAD_H )
184
185 /* Start function helper function for pthread
186 * Returns a pointer to a newly allocated int containing 1 if successful or -1 on error
187 * NULL is return if the helper function was unable to run the callback
188 */
189 void *libcthreads_thread_pool_callback_function_helper(
190 void *arguments )
191 {
192 libcerror_error_t *error = NULL;
193 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
194 intptr_t *value = NULL;
195 int *result = NULL;
196 int callback_function_result = 0;
197 int pop_result = 0;
198
199 if( arguments != NULL )
200 {
201 internal_thread_pool = (libcthreads_internal_thread_pool_t *) arguments;
202
203 if( ( internal_thread_pool != NULL )
204 && ( internal_thread_pool->callback_function != NULL ) )
205 {
206 result = (int *) memory_allocate(
207 sizeof( int ) );
208
209 if( result != NULL )
210 {
211 *result = 1;
212
213 do
214 {
215 pop_result = libcthreads_internal_thread_pool_pop(
216 internal_thread_pool,
217 &value,
218 &error );
219
220 if( pop_result == -1 )
221 {
222 break;
223 }
224 else if( pop_result != 0 )
225 {
226 callback_function_result = internal_thread_pool->callback_function(
227 value,
228 internal_thread_pool->callback_function_arguments );
229
230 if( ( callback_function_result != 1 )
231 && ( *result == 1 ) )
232 {
233 *result = callback_function_result;
234 }
235 }
236 else if( internal_thread_pool->status == LIBCTHREADS_STATUS_EXIT )
237 {
238 break;
239 }
240 }
241 while( pop_result != -1 );
242
243 if( pop_result == -1 )
244 {
245 *result = -1;
246 }
247 if( error != NULL )
248 {
249 #if defined( HAVE_VERBOSE_OUTPUT )
250 libcerror_error_backtrace_fprint(
251 error,
252 stdout );
253 #endif
254 libcerror_error_free(
255 &error );
256 }
257 }
258 }
259 }
260 pthread_exit(
261 (void *) result );
262 }
263
264 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
265
266 /* Creates a thread pool
267 * Make sure the value thread_pool is referencing, is set to NULL
268 *
269 * The callback_function should return 1 if successful and -1 on error
270 * Returns 1 if successful or -1 on error
271 */
272 int libcthreads_thread_pool_create(
273 libcthreads_thread_pool_t **thread_pool,
274 const libcthreads_thread_attributes_t *thread_attributes,
275 int number_of_threads,
276 int maximum_number_of_values,
277 int (*callback_function)(
278 intptr_t *value,
279 void *arguments ),
280 void *callback_function_arguments,
281 libcerror_error_t **error )
282 {
283 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
284 static char *function = "libcthreads_thread_pool_create";
285 size_t array_size = 0;
286
287 #if !defined( WINAPI ) || ( WINVER < 0x0602 )
288 int thread_index = 0;
289 #endif
290
291 #if defined( WINAPI )
292 DWORD error_code = 0;
293 #endif
294
295 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
296 BOOL result = FALSE;
297
298 #elif defined( WINAPI )
299 SECURITY_ATTRIBUTES *security_attributes = NULL;
300 HANDLE thread_handle = NULL;
301
302 #elif defined( HAVE_PTHREAD_H )
303 pthread_attr_t *attributes = NULL;
304 int *thread_return_value = NULL;
305 int pthread_result = 0;
306 #endif
307
308 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
309 LIBCTHREADS_UNREFERENCED_PARAMETER( thread_attributes )
310 #endif
311
312 if( thread_pool == NULL )
313 {
314 libcerror_error_set(
315 error,
316 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
317 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
318 "%s: invalid thread pool.",
319 function );
320
321 return( -1 );
322 }
323 if( *thread_pool != NULL )
324 {
325 libcerror_error_set(
326 error,
327 LIBCERROR_ERROR_DOMAIN_RUNTIME,
328 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
329 "%s: invalid thread pool value already set.",
330 function );
331
332 return( -1 );
333 }
334 if( number_of_threads <= 0 )
335 {
336 libcerror_error_set(
337 error,
338 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
339 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
340 "%s: invalid number of threads value zero or less.",
341 function );
342
343 return( -1 );
344 }
345 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
346 #if SIZEOF_INT > 4
347 if( number_of_threads > (int) UINT32_MAX )
348 {
349 libcerror_error_set(
350 error,
351 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
352 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
353 "%s: invalid number of threads value exceeds maximum.",
354 function );
355
356 return( -1 );
357 }
358 #endif /* SIZEOF_INT > 4 */
359
360 #else
361 #if SIZEOF_INT <= SIZEOF_SIZE_T
362 if( (size_t) number_of_threads > (size_t) ( SSIZE_MAX / sizeof( libcthreads_thread_t * ) ) )
363 #else
364 if( number_of_threads > (int) ( SSIZE_MAX / sizeof( libcthreads_thread_t * ) ) )
365 #endif
366 {
367 libcerror_error_set(
368 error,
369 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
370 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
371 "%s: invalid number of threads value exceeds maximum.",
372 function );
373
374 return( -1 );
375 }
376 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
377
378 if( maximum_number_of_values <= 0 )
379 {
380 libcerror_error_set(
381 error,
382 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
383 LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS,
384 "%s: invalid maximum number of values value zero or less.",
385 function );
386
387 return( -1 );
388 }
389 #if SIZEOF_INT <= SIZEOF_SIZE_T
390 if( (size_t) maximum_number_of_values > (size_t) ( SSIZE_MAX / sizeof( intptr_t * ) ) )
391 #else
392 if( maximum_number_of_values > (int) ( SSIZE_MAX / sizeof( intptr_t * ) ) )
393 #endif
394 {
395 libcerror_error_set(
396 error,
397 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
398 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
399 "%s: invalid maximum number of values value exceeds maximum.",
400 function );
401
402 return( -1 );
403 }
404 if( callback_function == NULL )
405 {
406 libcerror_error_set(
407 error,
408 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
409 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
410 "%s: invalid callback function.",
411 function );
412
413 return( -1 );
414 }
415 internal_thread_pool = memory_allocate_structure(
416 libcthreads_internal_thread_pool_t );
417
418 if( internal_thread_pool == NULL )
419 {
420 libcerror_error_set(
421 error,
422 LIBCERROR_ERROR_DOMAIN_MEMORY,
423 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
424 "%s: unable to create thread pool.",
425 function );
426
427 goto on_error;
428 }
429 if( memory_set(
430 internal_thread_pool,
431 0,
432 sizeof( libcthreads_internal_thread_pool_t ) ) == NULL )
433 {
434 libcerror_error_set(
435 error,
436 LIBCERROR_ERROR_DOMAIN_MEMORY,
437 LIBCERROR_MEMORY_ERROR_SET_FAILED,
438 "%s: unable to clear thread pool.",
439 function );
440
441 memory_free(
442 internal_thread_pool );
443
444 return( -1 );
445 }
446 array_size = sizeof( intptr_t * ) * maximum_number_of_values;
447
448 if( array_size > (size_t) SSIZE_MAX )
449 {
450 libcerror_error_set(
451 error,
452 LIBCERROR_ERROR_DOMAIN_RUNTIME,
453 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
454 "%s: invalid values array size value exceeds maximum.",
455 function );
456
457 goto on_error;
458 }
459 internal_thread_pool->values_array = (intptr_t **) memory_allocate(
460 array_size );
461
462 if( internal_thread_pool->values_array == NULL )
463 {
464 libcerror_error_set(
465 error,
466 LIBCERROR_ERROR_DOMAIN_MEMORY,
467 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
468 "%s: unable to create values array.",
469 function );
470
471 goto on_error;
472 }
473 if( memory_set(
474 internal_thread_pool->values_array,
475 0,
476 array_size ) == NULL )
477 {
478 libcerror_error_set(
479 error,
480 LIBCERROR_ERROR_DOMAIN_MEMORY,
481 LIBCERROR_MEMORY_ERROR_SET_FAILED,
482 "%s: unable to clear values array.",
483 function );
484
485 goto on_error;
486 }
487 internal_thread_pool->allocated_number_of_values = maximum_number_of_values;
488
489 /* The callback must be set before the callback function helper threads are created */
490 internal_thread_pool->callback_function = callback_function;
491 internal_thread_pool->callback_function_arguments = callback_function_arguments;
492
493 if( libcthreads_mutex_initialize(
494 &( internal_thread_pool->condition_mutex ),
495 error ) != 1 )
496 {
497 libcerror_error_set(
498 error,
499 LIBCERROR_ERROR_DOMAIN_RUNTIME,
500 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
501 "%s: unable to create condition mutex.",
502 function );
503
504 goto on_error;
505 }
506 if( libcthreads_condition_initialize(
507 &( internal_thread_pool->empty_condition ),
508 error ) != 1 )
509 {
510 libcerror_error_set(
511 error,
512 LIBCERROR_ERROR_DOMAIN_RUNTIME,
513 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
514 "%s: unable to create empty condition.",
515 function );
516
517 goto on_error;
518 }
519 if( libcthreads_condition_initialize(
520 &( internal_thread_pool->full_condition ),
521 error ) != 1 )
522 {
523 libcerror_error_set(
524 error,
525 LIBCERROR_ERROR_DOMAIN_RUNTIME,
526 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
527 "%s: unable to create full condition.",
528 function );
529
530 goto on_error;
531 }
532 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
533 InitializeThreadpoolEnvironment(
534 &( internal_thread_pool->callback_environment ) );
535
536 internal_thread_pool->thread_pool = CreateThreadpool(
537 NULL );
538
539 if( internal_thread_pool->thread_pool == NULL )
540 {
541 error_code = GetLastError();
542
543 libcerror_system_set_error(
544 error,
545 error_code,
546 LIBCERROR_ERROR_DOMAIN_RUNTIME,
547 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
548 "%s: unable to create thread pool.",
549 function );
550
551 goto on_error;
552 }
553 internal_thread_pool->cleanup_group = CreateThreadpoolCleanupGroup();
554
555 result = SetThreadpoolThreadMinimum(
556 internal_thread_pool->thread_pool,
557 (DWORD) number_of_threads );
558
559 if( result != TRUE )
560 {
561 error_code = GetLastError();
562
563 libcerror_system_set_error(
564 error,
565 error_code,
566 LIBCERROR_ERROR_DOMAIN_RUNTIME,
567 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
568 "%s: unable to set minimum number of threads.",
569 function );
570
571 goto on_error;
572 }
573 SetThreadpoolThreadMaximum(
574 internal_thread_pool->thread_pool,
575 (DWORD) number_of_threads );
576
577 if( internal_thread_pool->cleanup_group == NULL )
578 {
579 error_code = GetLastError();
580
581 libcerror_system_set_error(
582 error,
583 error_code,
584 LIBCERROR_ERROR_DOMAIN_RUNTIME,
585 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
586 "%s: unable to create thread pool cleanup group.",
587 function );
588
589 goto on_error;
590 }
591 SetThreadpoolCallbackPool(
592 &( internal_thread_pool->callback_environment ),
593 internal_thread_pool->thread_pool );
594
595 SetThreadpoolCallbackCleanupGroup(
596 &( internal_thread_pool->callback_environment ),
597 internal_thread_pool->cleanup_group,
598 NULL );
599
600 #elif defined( WINAPI )
601 array_size = sizeof( HANDLE ) * number_of_threads;
602
603 if( array_size > (size_t) SSIZE_MAX )
604 {
605 libcerror_error_set(
606 error,
607 LIBCERROR_ERROR_DOMAIN_RUNTIME,
608 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
609 "%s: invalid thread handles array size value out of bounds.",
610 function );
611
612 goto on_error;
613 }
614 internal_thread_pool->thread_handles_array = (HANDLE *) memory_allocate(
615 array_size );
616
617 if( internal_thread_pool->thread_handles_array == NULL )
618 {
619 libcerror_error_set(
620 error,
621 LIBCERROR_ERROR_DOMAIN_MEMORY,
622 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
623 "%s: unable to create thread handles array.",
624 function );
625
626 goto on_error;
627 }
628 if( memory_set(
629 internal_thread_pool->thread_handles_array,
630 0,
631 array_size ) == NULL )
632 {
633 libcerror_error_set(
634 error,
635 LIBCERROR_ERROR_DOMAIN_MEMORY,
636 LIBCERROR_MEMORY_ERROR_SET_FAILED,
637 "%s: unable to clear thread handles array.",
638 function );
639
640 goto on_error;
641 }
642 internal_thread_pool->number_of_threads = number_of_threads;
643
644 array_size = sizeof( DWORD ) * number_of_threads;
645
646 if( array_size > (size_t) SSIZE_MAX )
647 {
648 libcerror_error_set(
649 error,
650 LIBCERROR_ERROR_DOMAIN_RUNTIME,
651 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
652 "%s: invalid thread identifiers array size value out of bounds.",
653 function );
654
655 goto on_error;
656 }
657 internal_thread_pool->thread_identifiers_array = (DWORD *) memory_allocate(
658 array_size );
659
660 if( internal_thread_pool->thread_identifiers_array == NULL )
661 {
662 libcerror_error_set(
663 error,
664 LIBCERROR_ERROR_DOMAIN_MEMORY,
665 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
666 "%s: unable to create thread identifiers array.",
667 function );
668
669 goto on_error;
670 }
671 if( memory_set(
672 internal_thread_pool->thread_identifiers_array,
673 0,
674 array_size ) == NULL )
675 {
676 libcerror_error_set(
677 error,
678 LIBCERROR_ERROR_DOMAIN_MEMORY,
679 LIBCERROR_MEMORY_ERROR_SET_FAILED,
680 "%s: unable to clear thread identifiers array.",
681 function );
682
683 goto on_error;
684 }
685 if( thread_attributes != NULL )
686 {
687 security_attributes = &( ( (libcthreads_internal_thread_attributes_t *) thread_attributes )->security_attributes );
688 }
689 for( thread_index = 0;
690 thread_index < number_of_threads;
691 thread_index++ )
692 {
693 thread_handle = CreateThread(
694 security_attributes,
695 0, /* stack size */
696 &libcthreads_thread_pool_callback_function_helper,
697 (void *) internal_thread_pool,
698 0, /* creation flags */
699 &( internal_thread_pool->thread_identifiers_array[ thread_index ] ) );
700
701 if( thread_handle == NULL )
702 {
703 error_code = GetLastError();
704
705 libcerror_system_set_error(
706 error,
707 error_code,
708 LIBCERROR_ERROR_DOMAIN_RUNTIME,
709 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
710 "%s: unable to create thread handle: %d.",
711 function,
712 thread_index );
713
714 goto on_error;
715 }
716 internal_thread_pool->thread_handles_array[ thread_index ] = thread_handle;
717 }
718
719 #elif defined( HAVE_PTHREAD_H )
720 array_size = sizeof( pthread_t ) * number_of_threads;
721
722 if( array_size > (size_t) SSIZE_MAX )
723 {
724 libcerror_error_set(
725 error,
726 LIBCERROR_ERROR_DOMAIN_RUNTIME,
727 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
728 "%s: invalid threads array size value out of bounds.",
729 function );
730
731 goto on_error;
732 }
733 internal_thread_pool->threads_array = (pthread_t *) memory_allocate(
734 array_size );
735
736 if( internal_thread_pool->threads_array == NULL )
737 {
738 libcerror_error_set(
739 error,
740 LIBCERROR_ERROR_DOMAIN_MEMORY,
741 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
742 "%s: unable to create threads array.",
743 function );
744
745 goto on_error;
746 }
747 if( memory_set(
748 internal_thread_pool->threads_array,
749 0,
750 array_size ) == NULL )
751 {
752 libcerror_error_set(
753 error,
754 LIBCERROR_ERROR_DOMAIN_MEMORY,
755 LIBCERROR_MEMORY_ERROR_SET_FAILED,
756 "%s: unable to clear threads array.",
757 function );
758
759 goto on_error;
760 }
761 internal_thread_pool->number_of_threads = number_of_threads;
762
763 if( thread_attributes != NULL )
764 {
765 attributes = &( ( (libcthreads_internal_thread_attributes_t *) thread_attributes )->attributes );
766 }
767 for( thread_index = 0;
768 thread_index < number_of_threads;
769 thread_index++ )
770 {
771 pthread_result = pthread_create(
772 &( internal_thread_pool->threads_array[ thread_index ] ),
773 attributes,
774 &libcthreads_thread_pool_callback_function_helper,
775 (void *) internal_thread_pool );
776
777 switch( pthread_result )
778 {
779 case 0:
780 break;
781
782 case EAGAIN:
783 libcerror_error_set(
784 error,
785 LIBCERROR_ERROR_DOMAIN_RUNTIME,
786 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
787 "%s: unable to create thread: %d with error: Insufficient resources.",
788 function,
789 thread_index );
790
791 goto on_error;
792
793 default:
794 libcerror_system_set_error(
795 error,
796 pthread_result,
797 LIBCERROR_ERROR_DOMAIN_RUNTIME,
798 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
799 "%s: unable to create thread: %d.",
800 function,
801 thread_index );
802
803 goto on_error;
804 }
805 }
806 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
807
808 *thread_pool = (libcthreads_thread_pool_t *) internal_thread_pool;
809
810 return( 1 );
811
812 on_error:
813 if( internal_thread_pool != NULL )
814 {
815 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
816 if( internal_thread_pool->cleanup_group != NULL )
817 {
818 CloseThreadpoolCleanupGroup(
819 internal_thread_pool->cleanup_group );
820
821 internal_thread_pool->cleanup_group = NULL;
822 }
823 if( internal_thread_pool->thread_pool != NULL )
824 {
825 CloseThreadpool(
826 internal_thread_pool->thread_pool );
827
828 internal_thread_pool->thread_pool = NULL;
829 }
830
831 #elif defined( WINAPI )
832 if( internal_thread_pool->thread_identifiers_array != NULL )
833 {
834 memory_free(
835 internal_thread_pool->thread_identifiers_array );
836 }
837 if( internal_thread_pool->thread_handles_array != NULL )
838 {
839 while( thread_index >= 0 )
840 {
841 WaitForSingleObject(
842 internal_thread_pool->thread_handles_array[ thread_index ],
843 INFINITE );
844
845 thread_index--;
846 }
847 memory_free(
848 internal_thread_pool->thread_handles_array );
849 }
850 #elif defined( HAVE_PTHREAD_H )
851 if( internal_thread_pool->threads_array != NULL )
852 {
853 while( thread_index >= 0 )
854 {
855 pthread_join(
856 internal_thread_pool->threads_array[ thread_index ],
857 (void **) &thread_return_value );
858
859 if( thread_return_value != NULL )
860 {
861 memory_free(
862 thread_return_value );
863
864 thread_return_value = NULL;
865 }
866 thread_index--;
867 }
868 memory_free(
869 internal_thread_pool->threads_array );
870 }
871 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
872
873 if( internal_thread_pool->full_condition != NULL )
874 {
875 libcthreads_condition_free(
876 &( internal_thread_pool->full_condition ),
877 NULL );
878 }
879 if( internal_thread_pool->empty_condition != NULL )
880 {
881 libcthreads_condition_free(
882 &( internal_thread_pool->empty_condition ),
883 NULL );
884 }
885 if( internal_thread_pool->condition_mutex != NULL )
886 {
887 libcthreads_mutex_free(
888 &( internal_thread_pool->condition_mutex ),
889 NULL );
890 }
891 if( internal_thread_pool->values_array != NULL )
892 {
893 memory_free(
894 internal_thread_pool->values_array );
895 }
896 memory_free(
897 internal_thread_pool );
898 }
899 return( -1 );
900 }
901
902 /* Pops a value off the queue of the thread pool
903 * Returns 1 if successful, 0 if no value available or -1 on error
904 */
905 int libcthreads_internal_thread_pool_pop(
906 libcthreads_internal_thread_pool_t *internal_thread_pool,
907 intptr_t **value,
908 libcerror_error_t **error )
909 {
910 static char *function = "libcthreads_internal_thread_pool_pop";
911 int result = 0;
912
913 if( internal_thread_pool == NULL )
914 {
915 libcerror_error_set(
916 error,
917 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
918 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
919 "%s: invalid thread pool.",
920 function );
921
922 return( -1 );
923 }
924 if( internal_thread_pool->values_array == NULL )
925 {
926 libcerror_error_set(
927 error,
928 LIBCERROR_ERROR_DOMAIN_RUNTIME,
929 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
930 "%s: invalid thread pool - missing values array.",
931 function );
932
933 return( -1 );
934 }
935 if( libcthreads_mutex_grab(
936 internal_thread_pool->condition_mutex,
937 error ) != 1 )
938 {
939 libcerror_error_set(
940 error,
941 LIBCERROR_ERROR_DOMAIN_RUNTIME,
942 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
943 "%s: unable to grab condition mutex.",
944 function );
945
946 return( -1 );
947 }
948 while( internal_thread_pool->number_of_values == 0 )
949 {
950 if( internal_thread_pool->status == LIBCTHREADS_STATUS_EXIT )
951 {
952 break;
953 }
954 if( libcthreads_condition_wait(
955 internal_thread_pool->empty_condition,
956 internal_thread_pool->condition_mutex,
957 error ) != 1 )
958 {
959 libcerror_error_set(
960 error,
961 LIBCERROR_ERROR_DOMAIN_RUNTIME,
962 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
963 "%s: unable to wait for empty condition.",
964 function );
965
966 goto on_error;
967 }
968 }
969 if( internal_thread_pool->number_of_values > 0 )
970 {
971 *value = internal_thread_pool->values_array[ internal_thread_pool->pop_index ];
972
973 internal_thread_pool->pop_index++;
974
975 if( internal_thread_pool->pop_index >= internal_thread_pool->allocated_number_of_values )
976 {
977 internal_thread_pool->pop_index = 0;
978 }
979 internal_thread_pool->number_of_values--;
980
981 result = 1;
982
983 if( libcthreads_condition_broadcast(
984 internal_thread_pool->full_condition,
985 error ) != 1 )
986 {
987 libcerror_error_set(
988 error,
989 LIBCERROR_ERROR_DOMAIN_RUNTIME,
990 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
991 "%s: unable to broadcast full condition.",
992 function );
993
994 goto on_error;
995 }
996 }
997 if( libcthreads_mutex_release(
998 internal_thread_pool->condition_mutex,
999 error ) != 1 )
1000 {
1001 libcerror_error_set(
1002 error,
1003 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1004 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1005 "%s: unable to release condition mutex.",
1006 function );
1007
1008 return( -1 );
1009 }
1010 return( result );
1011
1012 on_error:
1013 libcthreads_mutex_release(
1014 internal_thread_pool->condition_mutex,
1015 NULL );
1016
1017 return( -1 );
1018 }
1019
1020 /* Pushes a value onto the queue of the thread pool
1021 * Returns 1 if successful or -1 on error
1022 */
1023 int libcthreads_thread_pool_push(
1024 libcthreads_thread_pool_t *thread_pool,
1025 intptr_t *value,
1026 libcerror_error_t **error )
1027 {
1028 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
1029 static char *function = "libcthreads_thread_pool_push";
1030
1031 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
1032 TP_WORK *thread_pool_work = NULL;
1033 DWORD error_code = 0;
1034 #endif
1035
1036 if( thread_pool == NULL )
1037 {
1038 libcerror_error_set(
1039 error,
1040 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1041 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1042 "%s: invalid thread pool.",
1043 function );
1044
1045 return( -1 );
1046 }
1047 internal_thread_pool = (libcthreads_internal_thread_pool_t *) thread_pool;
1048
1049 if( internal_thread_pool->values_array == NULL )
1050 {
1051 libcerror_error_set(
1052 error,
1053 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1054 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1055 "%s: invalid thread pool - missing values array.",
1056 function );
1057
1058 return( -1 );
1059 }
1060 if( value == NULL )
1061 {
1062 libcerror_error_set(
1063 error,
1064 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1065 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1066 "%s: invalid value.",
1067 function );
1068
1069 return( -1 );
1070 }
1071 if( libcthreads_mutex_grab(
1072 internal_thread_pool->condition_mutex,
1073 error ) != 1 )
1074 {
1075 libcerror_error_set(
1076 error,
1077 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1078 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1079 "%s: unable to grab condition mutex.",
1080 function );
1081
1082 return( -1 );
1083 }
1084 while( internal_thread_pool->number_of_values == internal_thread_pool->allocated_number_of_values )
1085 {
1086 if( libcthreads_condition_wait(
1087 internal_thread_pool->full_condition,
1088 internal_thread_pool->condition_mutex,
1089 error ) != 1 )
1090 {
1091 libcerror_error_set(
1092 error,
1093 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1094 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1095 "%s: unable to wait for full condition.",
1096 function );
1097
1098 goto on_error;
1099 }
1100 }
1101 internal_thread_pool->values_array[ internal_thread_pool->push_index ] = value;
1102
1103 internal_thread_pool->push_index++;
1104
1105 if( internal_thread_pool->push_index >= internal_thread_pool->allocated_number_of_values )
1106 {
1107 internal_thread_pool->push_index = 0;
1108 }
1109 internal_thread_pool->number_of_values++;
1110
1111 /* The condition broadcast must be protected by the mutex for the WINAPI version
1112 */
1113 if( libcthreads_condition_broadcast(
1114 internal_thread_pool->empty_condition,
1115 error ) != 1 )
1116 {
1117 libcerror_error_set(
1118 error,
1119 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1120 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1121 "%s: unable to broadcast empty condition.",
1122 function );
1123
1124 goto on_error;
1125 }
1126 if( libcthreads_mutex_release(
1127 internal_thread_pool->condition_mutex,
1128 error ) != 1 )
1129 {
1130 libcerror_error_set(
1131 error,
1132 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1133 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1134 "%s: unable to release condition mutex.",
1135 function );
1136
1137 return( -1 );
1138 }
1139 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
1140 thread_pool_work = CreateThreadpoolWork(
1141 &libcthreads_thread_pool_callback_function_helper,
1142 (void *) internal_thread_pool,
1143 &( internal_thread_pool->callback_environment ) );
1144
1145 if( thread_pool_work == NULL )
1146 {
1147 error_code = GetLastError();
1148
1149 libcerror_system_set_error(
1150 error,
1151 error_code,
1152 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1153 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1154 "%s: unable to create thread pool work.",
1155 function );
1156
1157 return( -1 );
1158 }
1159 SubmitThreadpoolWork(
1160 thread_pool_work );
1161
1162 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
1163
1164 return( 1 );
1165
1166 on_error:
1167 libcthreads_mutex_release(
1168 internal_thread_pool->condition_mutex,
1169 NULL );
1170
1171 return( -1 );
1172 }
1173
1174 /* Pushes a value onto the thread pool in sorted order
1175 *
1176 * Uses the value_compare_function to determine the similarity of the values
1177 * The value_compare_function should return LIBCTHREADS_COMPARE_LESS,
1178 * LIBCTHREADS_COMPARE_EQUAL, LIBCTHREADS_COMPARE_GREATER if successful or -1 on error
1179 *
1180 * Returns 1 if successful, 0 if the value already exists or -1 on error
1181 */
1182 int libcthreads_thread_pool_push_sorted(
1183 libcthreads_thread_pool_t *thread_pool,
1184 intptr_t *value,
1185 int (*value_compare_function)(
1186 intptr_t *first_value,
1187 intptr_t *second_value,
1188 libcerror_error_t **error ),
1189 uint8_t sort_flags,
1190 libcerror_error_t **error )
1191 {
1192 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
1193 static char *function = "libcthreads_thread_pool_push_sorted";
1194 int compare_result = 0;
1195 int pop_index = 0;
1196 int previous_push_index = 0;
1197 int push_index = 0;
1198 int result = 1;
1199 int value_index = 0;
1200
1201 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
1202 TP_WORK *thread_pool_work = NULL;
1203 DWORD error_code = 0;
1204 #endif
1205
1206 if( thread_pool == NULL )
1207 {
1208 libcerror_error_set(
1209 error,
1210 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1211 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1212 "%s: invalid thread pool.",
1213 function );
1214
1215 return( -1 );
1216 }
1217 internal_thread_pool = (libcthreads_internal_thread_pool_t *) thread_pool;
1218
1219 if( internal_thread_pool->values_array == NULL )
1220 {
1221 libcerror_error_set(
1222 error,
1223 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1224 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1225 "%s: invalid queue - missing values array.",
1226 function );
1227
1228 return( -1 );
1229 }
1230 if( value == NULL )
1231 {
1232 libcerror_error_set(
1233 error,
1234 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1235 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1236 "%s: invalid value.",
1237 function );
1238
1239 return( -1 );
1240 }
1241 if( value_compare_function == NULL )
1242 {
1243 libcerror_error_set(
1244 error,
1245 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1246 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1247 "%s: invalid value compare function.",
1248 function );
1249
1250 return( -1 );
1251 }
1252 if( ( sort_flags & ~( LIBCTHREADS_SORT_FLAG_UNIQUE_VALUES ) ) != 0 )
1253 {
1254 libcerror_error_set(
1255 error,
1256 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1257 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1258 "%s: unsupported sort flags: 0x%02" PRIx8 ".",
1259 function,
1260 sort_flags );
1261
1262 return( -1 );
1263 }
1264 if( libcthreads_mutex_grab(
1265 internal_thread_pool->condition_mutex,
1266 error ) != 1 )
1267 {
1268 libcerror_error_set(
1269 error,
1270 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1271 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1272 "%s: unable to grab condition mutex.",
1273 function );
1274
1275 return( -1 );
1276 }
1277 while( internal_thread_pool->number_of_values == internal_thread_pool->allocated_number_of_values )
1278 {
1279 if( libcthreads_condition_wait(
1280 internal_thread_pool->full_condition,
1281 internal_thread_pool->condition_mutex,
1282 error ) != 1 )
1283 {
1284 libcerror_error_set(
1285 error,
1286 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1287 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1288 "%s: unable to wait for full condition.",
1289 function );
1290
1291 goto on_error;
1292 }
1293 }
1294 pop_index = internal_thread_pool->pop_index;
1295
1296 for( value_index = 0;
1297 value_index < internal_thread_pool->number_of_values;
1298 value_index++ )
1299 {
1300 compare_result = value_compare_function(
1301 value,
1302 internal_thread_pool->values_array[ pop_index ],
1303 error );
1304
1305 if( compare_result == -1 )
1306 {
1307 libcerror_error_set(
1308 error,
1309 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1310 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1311 "%s: unable to compare value: %d.",
1312 function,
1313 value_index );
1314
1315 goto on_error;
1316 }
1317 else if( compare_result == LIBCTHREADS_COMPARE_EQUAL )
1318 {
1319 if( ( sort_flags & LIBCTHREADS_SORT_FLAG_UNIQUE_VALUES ) != 0 )
1320 {
1321 result = 0;
1322
1323 break;
1324 }
1325 }
1326 else if( compare_result == LIBCTHREADS_COMPARE_LESS )
1327 {
1328 result = 1;
1329
1330 break;
1331 }
1332 else if( compare_result != LIBCTHREADS_COMPARE_GREATER )
1333 {
1334 libcerror_error_set(
1335 error,
1336 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1337 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1338 "%s: unsupported value compare function return value: %d.",
1339 function,
1340 compare_result );
1341
1342 goto on_error;
1343 }
1344 pop_index++;
1345
1346 if( pop_index >= internal_thread_pool->allocated_number_of_values )
1347 {
1348 pop_index = 0;
1349 }
1350 }
1351 if( result != 0 )
1352 {
1353 push_index = internal_thread_pool->push_index;
1354
1355 if( compare_result == LIBCTHREADS_COMPARE_LESS )
1356 {
1357 previous_push_index = push_index - 1;
1358
1359 while( push_index != pop_index )
1360 {
1361 if( previous_push_index < 0 )
1362 {
1363 previous_push_index = internal_thread_pool->allocated_number_of_values - 1;
1364 }
1365 internal_thread_pool->values_array[ push_index ] = internal_thread_pool->values_array[ previous_push_index ];
1366
1367 push_index = previous_push_index;
1368
1369 previous_push_index--;
1370 }
1371 }
1372 internal_thread_pool->values_array[ push_index ] = value;
1373
1374 internal_thread_pool->push_index++;
1375
1376 if( internal_thread_pool->push_index >= internal_thread_pool->allocated_number_of_values )
1377 {
1378 internal_thread_pool->push_index = 0;
1379 }
1380 internal_thread_pool->number_of_values++;
1381
1382 /* The condition broadcast must be protected by the mutex for the WINAPI version
1383 */
1384 if( libcthreads_condition_broadcast(
1385 internal_thread_pool->empty_condition,
1386 error ) != 1 )
1387 {
1388 libcerror_error_set(
1389 error,
1390 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1391 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1392 "%s: unable to broadcast empty condition.",
1393 function );
1394
1395 goto on_error;
1396 }
1397 }
1398 if( libcthreads_mutex_release(
1399 internal_thread_pool->condition_mutex,
1400 error ) != 1 )
1401 {
1402 libcerror_error_set(
1403 error,
1404 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1405 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1406 "%s: unable to release condition mutex.",
1407 function );
1408
1409 return( -1 );
1410 }
1411 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
1412 thread_pool_work = CreateThreadpoolWork(
1413 &libcthreads_thread_pool_callback_function_helper,
1414 (void *) internal_thread_pool,
1415 &( internal_thread_pool->callback_environment ) );
1416
1417 if( thread_pool_work == NULL )
1418 {
1419 error_code = GetLastError();
1420
1421 libcerror_system_set_error(
1422 error,
1423 error_code,
1424 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1425 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1426 "%s: unable to create thread pool work.",
1427 function );
1428
1429 return( -1 );
1430 }
1431 SubmitThreadpoolWork(
1432 thread_pool_work );
1433
1434 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
1435
1436 return( result );
1437
1438 on_error:
1439 libcthreads_mutex_release(
1440 internal_thread_pool->condition_mutex,
1441 NULL );
1442
1443 return( -1 );
1444 }
1445
1446 /* Joins the current with a specified thread pool
1447 * The the thread is freed after join
1448 * Returns 1 if successful or -1 on error
1449 */
1450 int libcthreads_thread_pool_join(
1451 libcthreads_thread_pool_t **thread_pool,
1452 libcerror_error_t **error )
1453 {
1454 libcthreads_internal_thread_pool_t *internal_thread_pool = NULL;
1455 static char *function = "libcthreads_thread_pool_join";
1456 int result = 1;
1457
1458 #if !defined( WINAPI ) || ( WINVER < 0x0602 )
1459 int thread_index = 0;
1460
1461 #if defined( WINAPI )
1462 DWORD error_code = 0;
1463 DWORD wait_status = 0;
1464
1465 #elif defined( HAVE_PTHREAD_H )
1466 int *thread_return_value = NULL;
1467 int pthread_result = 0;
1468 #endif
1469 #endif /* !defined( WINAPI ) || ( WINVER < 0x0602 ) */
1470
1471 if( thread_pool == NULL )
1472 {
1473 libcerror_error_set(
1474 error,
1475 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1476 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1477 "%s: invalid thread pool.",
1478 function );
1479
1480 return( -1 );
1481 }
1482 if( *thread_pool == NULL )
1483 {
1484 libcerror_error_set(
1485 error,
1486 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1487 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1488 "%s: missing thread pool value.",
1489 function );
1490
1491 return( -1 );
1492 }
1493 internal_thread_pool = (libcthreads_internal_thread_pool_t *) *thread_pool;
1494 *thread_pool = NULL;
1495
1496 if( libcthreads_mutex_grab(
1497 internal_thread_pool->condition_mutex,
1498 error ) != 1 )
1499 {
1500 libcerror_error_set(
1501 error,
1502 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1503 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1504 "%s: unable to grab condition mutex.",
1505 function );
1506
1507 return( -1 );
1508 }
1509 internal_thread_pool->status = LIBCTHREADS_STATUS_EXIT;
1510
1511 /* Wake up the worker threads
1512 */
1513 if( libcthreads_condition_broadcast(
1514 internal_thread_pool->empty_condition,
1515 error ) != 1 )
1516 {
1517 libcerror_error_set(
1518 error,
1519 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1520 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1521 "%s: unable to broadcast empty condition.",
1522 function );
1523
1524 result = -1;
1525 }
1526 while( internal_thread_pool->number_of_values != 0 )
1527 {
1528 /* Wait here to all the values in the queue have been processed
1529 */
1530 if( libcthreads_condition_wait(
1531 internal_thread_pool->full_condition,
1532 internal_thread_pool->condition_mutex,
1533 error ) != 1 )
1534 {
1535 libcerror_error_set(
1536 error,
1537 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1538 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1539 "%s: unable to wait for full condition.",
1540 function );
1541
1542 result = -1;
1543
1544 break;
1545 }
1546 }
1547 if( libcthreads_mutex_release(
1548 internal_thread_pool->condition_mutex,
1549 error ) != 1 )
1550 {
1551 libcerror_error_set(
1552 error,
1553 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1554 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1555 "%s: unable to release condition mutex.",
1556 function );
1557
1558 return( -1 );
1559 }
1560 #if defined( WINAPI ) && ( WINVER >= 0x0602 )
1561 CloseThreadpoolCleanupGroupMembers(
1562 internal_thread_pool->cleanup_group,
1563 FALSE,
1564 NULL );
1565
1566 CloseThreadpoolCleanupGroup(
1567 internal_thread_pool->cleanup_group );
1568
1569 internal_thread_pool->cleanup_group = NULL;
1570
1571 CloseThreadpool(
1572 internal_thread_pool->thread_pool );
1573
1574 internal_thread_pool->thread_pool = NULL;
1575
1576 #elif defined( WINAPI )
1577 for( thread_index = 0;
1578 thread_index < internal_thread_pool->number_of_threads;
1579 thread_index++ )
1580 {
1581 wait_status = WaitForSingleObject(
1582 internal_thread_pool->thread_handles_array[ thread_index ],
1583 INFINITE );
1584
1585 if( wait_status == WAIT_FAILED )
1586 {
1587 error_code = GetLastError();
1588
1589 libcerror_system_set_error(
1590 error,
1591 error_code,
1592 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1593 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1594 "%s: wait for thread: %d failed.",
1595 function,
1596 thread_index );
1597
1598 result = -1;
1599 }
1600 }
1601
1602 #elif defined( HAVE_PTHREAD_H )
1603 for( thread_index = 0;
1604 thread_index < internal_thread_pool->number_of_threads;
1605 thread_index++ )
1606 {
1607 pthread_result = pthread_join(
1608 internal_thread_pool->threads_array[ thread_index ],
1609 (void **) &thread_return_value );
1610
1611 if( pthread_result == EDEADLK )
1612 {
1613 libcerror_error_set(
1614 error,
1615 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1616 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1617 "%s: unable to join thread: %d with error: Deadlock condition detected.",
1618 function,
1619 thread_index );
1620
1621 result = -1;
1622 }
1623 else if( pthread_result != 0 )
1624 {
1625 libcerror_system_set_error(
1626 error,
1627 result,
1628 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1629 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1630 "%s: unable to join thread: %d.",
1631 function,
1632 thread_index );
1633
1634 result = -1;
1635 }
1636 /* If the thread returns NULL it never got around to launching the callback function
1637 */
1638 else if( ( thread_return_value != NULL )
1639 && ( *thread_return_value != 1 ) )
1640 {
1641 libcerror_error_set(
1642 error,
1643 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1644 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1645 "%s: thread: %d returned an error status of: %d.",
1646 function,
1647 thread_index,
1648 *thread_return_value );
1649
1650 result = -1;
1651 }
1652 if( thread_return_value != NULL )
1653 {
1654 memory_free(
1655 thread_return_value );
1656
1657 thread_return_value = NULL;
1658 }
1659 }
1660 #endif /* defined( WINAPI ) && ( WINVER >= 0x0602 ) */
1661 if( libcthreads_condition_free(
1662 &( internal_thread_pool->full_condition ),
1663 error ) != 1 )
1664 {
1665 libcerror_error_set(
1666 error,
1667 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1668 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1669 "%s: unable to free full condition.",
1670 function );
1671
1672 result = -1;
1673 }
1674 if( libcthreads_condition_free(
1675 &( internal_thread_pool->empty_condition ),
1676 error ) != 1 )
1677 {
1678 libcerror_error_set(
1679 error,
1680 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1681 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1682 "%s: unable to free empty condition.",
1683 function );
1684
1685 result = -1;
1686 }
1687 if( libcthreads_mutex_free(
1688 &( internal_thread_pool->condition_mutex ),
1689 error ) != 1 )
1690 {
1691 libcerror_error_set(
1692 error,
1693 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1694 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1695 "%s: unable to free condition mutex.",
1696 function );
1697
1698 result = -1;
1699 }
1700 #if defined( WINAPI ) && ( WINVER < 0x0602 )
1701 memory_free(
1702 internal_thread_pool->thread_identifiers_array );
1703
1704 memory_free(
1705 internal_thread_pool->thread_handles_array );
1706
1707 #elif defined( HAVE_PTHREAD_H )
1708 memory_free(
1709 internal_thread_pool->threads_array );
1710 #endif
1711
1712 memory_free(
1713 internal_thread_pool->values_array );
1714 memory_free(
1715 internal_thread_pool );
1716
1717 return( result );
1718 }
1719
1720 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
1721