Debian Codegraph @

libcthreads/libcthreads_condition.c

raw symbols SCIP
1 /*
2 * Condition 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 <Synchapi.h>
30 #endif
31
32 #if defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
33 #include <pthread.h>
34 #endif
35
36 #include "libcthreads_condition.h"
37 #include "libcthreads_libcerror.h"
38 #include "libcthreads_mutex.h"
39 #include "libcthreads_types.h"
40
41 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
42
43 /* Creates a condition
44 * Make sure the value condition is referencing, is set to NULL
45 * Returns 1 if successful or -1 on error
46 */
47 int libcthreads_condition_initialize(
48 libcthreads_condition_t **condition,
49 libcerror_error_t **error )
50 {
51 libcthreads_internal_condition_t *internal_condition = NULL;
52 static char *function = "libcthreads_condition_initialize";
53
54 #if defined( WINAPI ) && ( WINVER < 0x0600 )
55 DWORD error_code = 0;
56
57 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
58 int pthread_result = 0;
59 #endif
60
61 if( condition == NULL )
62 {
63 libcerror_error_set(
64 error,
65 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
66 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
67 "%s: invalid condition.",
68 function );
69
70 return( -1 );
71 }
72 if( *condition != NULL )
73 {
74 libcerror_error_set(
75 error,
76 LIBCERROR_ERROR_DOMAIN_RUNTIME,
77 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
78 "%s: invalid condition value already set.",
79 function );
80
81 return( -1 );
82 }
83 internal_condition = memory_allocate_structure(
84 libcthreads_internal_condition_t );
85
86 if( internal_condition == NULL )
87 {
88 libcerror_error_set(
89 error,
90 LIBCERROR_ERROR_DOMAIN_MEMORY,
91 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
92 "%s: unable to create condition.",
93 function );
94
95 goto on_error;
96 }
97 if( memory_set(
98 internal_condition,
99 0,
100 sizeof( libcthreads_internal_condition_t ) ) == NULL )
101 {
102 libcerror_error_set(
103 error,
104 LIBCERROR_ERROR_DOMAIN_MEMORY,
105 LIBCERROR_MEMORY_ERROR_SET_FAILED,
106 "%s: unable to clear condition.",
107 function );
108
109 memory_free(
110 internal_condition );
111
112 return( -1 );
113 }
114 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
115 InitializeConditionVariable(
116 &( internal_condition->condition_variable ) );
117
118 #elif defined( WINAPI )
119 InitializeCriticalSection(
120 &( internal_condition->wait_critical_section ) );
121
122 internal_condition->signal_semaphore_handle = CreateSemaphore (
123 NULL,
124 0,
125 INT_MAX,
126 NULL );
127
128 if( internal_condition->signal_semaphore_handle == NULL )
129 {
130 error_code = GetLastError();
131
132 libcerror_system_set_error(
133 error,
134 error_code,
135 LIBCERROR_ERROR_DOMAIN_RUNTIME,
136 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
137 "%s: unable to initialize signal semaphore handle.",
138 function );
139
140 goto on_error;
141 }
142 internal_condition->signal_event_handle = CreateEvent (
143 NULL,
144 FALSE,
145 FALSE,
146 NULL );
147
148 if( internal_condition->signal_event_handle == NULL )
149 {
150 error_code = GetLastError();
151
152 libcerror_system_set_error(
153 error,
154 error_code,
155 LIBCERROR_ERROR_DOMAIN_RUNTIME,
156 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
157 "%s: unable to initialize signal event handle.",
158 function );
159
160 goto on_error;
161 }
162
163 #elif defined( HAVE_PTHREAD_H )
164 pthread_result = pthread_cond_init(
165 &( internal_condition->condition ),
166 NULL );
167
168 switch( pthread_result )
169 {
170 case 0:
171 break;
172
173 case EAGAIN:
174 libcerror_error_set(
175 error,
176 LIBCERROR_ERROR_DOMAIN_RUNTIME,
177 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
178 "%s: unable to initialize condition with error: Insufficient resources.",
179 function );
180
181 goto on_error;
182
183 default:
184 libcerror_system_set_error(
185 error,
186 pthread_result,
187 LIBCERROR_ERROR_DOMAIN_RUNTIME,
188 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
189 "%s: unable to initialize condition.",
190 function );
191
192 goto on_error;
193 }
194 #endif
195 *condition = (libcthreads_condition_t *) internal_condition;
196
197 return( 1 );
198
199 on_error:
200 if( internal_condition != NULL )
201 {
202 #if defined( WINAPI ) && ( WINVER < 0x0600 )
203 if( internal_condition->signal_semaphore_handle != NULL )
204 {
205 CloseHandle(
206 internal_condition->signal_semaphore_handle );
207 }
208 DeleteCriticalSection(
209 &( internal_condition->wait_critical_section ) );
210 #endif
211 memory_free(
212 internal_condition );
213 }
214 return( -1 );
215 }
216
217 /* Frees a condition
218 * Returns 1 if successful or -1 on error
219 */
220 int libcthreads_condition_free(
221 libcthreads_condition_t **condition,
222 libcerror_error_t **error )
223 {
224 libcthreads_internal_condition_t *internal_condition = NULL;
225 static char *function = "libcthreads_condition_free";
226 int result = 1;
227
228 #if defined( WINAPI ) && ( WINVER < 0x0600 )
229 DWORD error_code = 0;
230
231 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
232 int pthread_result = 0;
233 #endif
234
235 if( condition == NULL )
236 {
237 libcerror_error_set(
238 error,
239 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241 "%s: invalid condition.",
242 function );
243
244 return( -1 );
245 }
246 if( *condition != NULL )
247 {
248 internal_condition = (libcthreads_internal_condition_t *) *condition;
249 *condition = NULL;
250
251 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
252
253 #elif defined( WINAPI )
254 if( CloseHandle(
255 internal_condition->signal_event_handle ) == 0 )
256 {
257 error_code = GetLastError();
258
259 libcerror_system_set_error(
260 error,
261 error_code,
262 LIBCERROR_ERROR_DOMAIN_RUNTIME,
263 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
264 "%s: unable to free signal event handle.",
265 function );
266
267 result = -1;
268 }
269 if( CloseHandle(
270 internal_condition->signal_semaphore_handle ) == 0 )
271 {
272 error_code = GetLastError();
273
274 libcerror_system_set_error(
275 error,
276 error_code,
277 LIBCERROR_ERROR_DOMAIN_RUNTIME,
278 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
279 "%s: unable to free signal semaphore handle.",
280 function );
281
282 result = -1;
283 }
284 DeleteCriticalSection(
285 &( internal_condition->wait_critical_section ) );
286
287 #elif defined( HAVE_PTHREAD_H )
288 pthread_result = pthread_cond_destroy(
289 &( internal_condition->condition ) );
290
291 switch( pthread_result )
292 {
293 case 0:
294 break;
295
296 case EAGAIN:
297 libcerror_error_set(
298 error,
299 LIBCERROR_ERROR_DOMAIN_RUNTIME,
300 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
301 "%s: unable to destroy condition with error: Insufficient resources.",
302 function );
303
304 result = -1;
305 break;
306
307 case EBUSY:
308 libcerror_error_set(
309 error,
310 LIBCERROR_ERROR_DOMAIN_RUNTIME,
311 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
312 "%s: unable to destroy condition with error: Resource busy.",
313 function );
314
315 result = -1;
316 break;
317
318 default:
319 libcerror_system_set_error(
320 error,
321 pthread_result,
322 LIBCERROR_ERROR_DOMAIN_RUNTIME,
323 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
324 "%s: unable to destroy condition.",
325 function );
326
327 result = -1;
328 break;
329 }
330 #endif
331 memory_free(
332 internal_condition );
333 }
334 return( result );
335 }
336
337 /* Broadcasts a condition
338 * The of this function must be locked by the same mutex as used to wait
339 * This is necessary for the WINAPI pre Vista (0x0600) implementation
340 * Returns 1 if successful or -1 on error
341 */
342 int libcthreads_condition_broadcast(
343 libcthreads_condition_t *condition,
344 libcerror_error_t **error )
345 {
346 libcthreads_internal_condition_t *internal_condition = NULL;
347 static char *function = "libcthreads_condition_broadcast";
348
349 #if defined( WINAPI ) && ( WINVER < 0x0600 )
350 DWORD error_code = 0;
351 DWORD wait_status = 0;
352 BOOL result = 1;
353 int number_of_waiting_threads = 0;
354
355 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
356 int pthread_result = 0;
357 #endif
358
359 if( condition == NULL )
360 {
361 libcerror_error_set(
362 error,
363 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
364 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
365 "%s: invalid condition.",
366 function );
367
368 return( -1 );
369 }
370 internal_condition = (libcthreads_internal_condition_t *) condition;
371
372 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
373 WakeAllConditionVariable(
374 &( internal_condition->condition_variable ) );
375
376 #elif defined( WINAPI )
377 EnterCriticalSection(
378 &( internal_condition->wait_critical_section ) );
379
380 number_of_waiting_threads = internal_condition->number_of_waiting_threads;
381
382 if( number_of_waiting_threads > 0 )
383 {
384 internal_condition->signal_is_broadcast = 1;
385
386 result = ReleaseSemaphore(
387 internal_condition->signal_semaphore_handle,
388 number_of_waiting_threads,
389 0 );
390
391 if( result == 0 )
392 {
393 error_code = GetLastError();
394
395 internal_condition->signal_is_broadcast = 0;
396 }
397 }
398 LeaveCriticalSection(
399 &( internal_condition->wait_critical_section ) );
400
401 if( result == 0 )
402 {
403 libcerror_system_set_error(
404 error,
405 error_code,
406 LIBCERROR_ERROR_DOMAIN_RUNTIME,
407 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
408 "%s: unable to release signal semaphore handle.",
409 function );
410
411 return( -1 );
412 }
413 if( number_of_waiting_threads > 0 )
414 {
415 wait_status = WaitForSingleObject(
416 internal_condition->signal_event_handle,
417 INFINITE );
418
419 if( wait_status == WAIT_FAILED )
420 {
421 error_code = GetLastError();
422
423 internal_condition->signal_is_broadcast = 0;
424
425 libcerror_system_set_error(
426 error,
427 error_code,
428 LIBCERROR_ERROR_DOMAIN_RUNTIME,
429 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
430 "%s: wait for no read event handle failed.",
431 function );
432
433 return( -1 );
434 }
435 internal_condition->signal_is_broadcast = 0;
436 }
437
438 #elif defined( HAVE_PTHREAD_H )
439 pthread_result = pthread_cond_broadcast(
440 &( internal_condition->condition ) );
441
442 if( pthread_result != 0 )
443 {
444 libcerror_system_set_error(
445 error,
446 pthread_result,
447 LIBCERROR_ERROR_DOMAIN_RUNTIME,
448 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
449 "%s: unable to broadcast condition.",
450 function );
451
452 return( -1 );
453 }
454 #endif
455 return( 1 );
456 }
457
458 /* Signals a condition
459 * The of this function must be locked by the same mutex as used to wait
460 * This is necessary for the WINAPI pre Vista (0x0600) implementation
461 * Returns 1 if successful or -1 on error
462 */
463 int libcthreads_condition_signal(
464 libcthreads_condition_t *condition,
465 libcerror_error_t **error )
466 {
467 libcthreads_internal_condition_t *internal_condition = NULL;
468 static char *function = "libcthreads_condition_signal";
469
470 #if defined( WINAPI ) && ( WINVER < 0x0600 )
471 DWORD error_code = 0;
472 BOOL result = 1;
473 int number_of_waiting_threads = 0;
474
475 #elif defined( HAVE_PTHREAD_H ) && !defined( WINAPI )
476 int pthread_result = 0;
477 #endif
478
479 if( condition == NULL )
480 {
481 libcerror_error_set(
482 error,
483 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
484 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
485 "%s: invalid condition.",
486 function );
487
488 return( -1 );
489 }
490 internal_condition = (libcthreads_internal_condition_t *) condition;
491
492 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
493 WakeConditionVariable(
494 &( internal_condition->condition_variable ) );
495
496 #elif defined( WINAPI )
497 EnterCriticalSection(
498 &( internal_condition->wait_critical_section ) );
499
500 number_of_waiting_threads = internal_condition->number_of_waiting_threads;
501
502 LeaveCriticalSection(
503 &( internal_condition->wait_critical_section ) );
504
505 if( number_of_waiting_threads > 0 )
506 {
507 result = ReleaseSemaphore(
508 internal_condition->signal_semaphore_handle,
509 1,
510 0 );
511
512 if( result == 0 )
513 {
514 error_code = GetLastError();
515
516 libcerror_system_set_error(
517 error,
518 error_code,
519 LIBCERROR_ERROR_DOMAIN_RUNTIME,
520 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
521 "%s: unable to release signal semaphore handle.",
522 function );
523
524 return( -1 );
525 }
526 }
527
528 #elif defined( HAVE_PTHREAD_H )
529 pthread_result = pthread_cond_signal(
530 &( internal_condition->condition ) );
531
532 if( pthread_result != 0 )
533 {
534 libcerror_system_set_error(
535 error,
536 pthread_result,
537 LIBCERROR_ERROR_DOMAIN_RUNTIME,
538 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
539 "%s: unable to signal condition.",
540 function );
541
542 return( -1 );
543 }
544 #endif
545 return( 1 );
546 }
547
548 /* Waits for a condition
549 * Returns 1 if successful or -1 on error
550 */
551 int libcthreads_condition_wait(
552 libcthreads_condition_t *condition,
553 libcthreads_mutex_t *mutex,
554 libcerror_error_t **error )
555 {
556 libcthreads_internal_condition_t *internal_condition = NULL;
557 libcthreads_internal_mutex_t *internal_mutex = NULL;
558 static char *function = "libcthreads_condition_wait";
559
560 #if defined( WINAPI )
561 DWORD error_code = 0;
562
563 #if ( WINVER >= 0x0600 )
564 BOOL result = 0;
565
566 #elif ( WINVER >= 0x0400 )
567 DWORD wait_status = 0;
568 int is_last_waiting_thread = 0;
569 #endif
570
571 #elif defined( HAVE_PTHREAD_H )
572 int pthread_result = 0;
573 #endif
574
575 if( condition == NULL )
576 {
577 libcerror_error_set(
578 error,
579 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
580 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
581 "%s: invalid condition.",
582 function );
583
584 return( -1 );
585 }
586 internal_condition = (libcthreads_internal_condition_t *) condition;
587
588 if( mutex == NULL )
589 {
590 libcerror_error_set(
591 error,
592 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
593 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
594 "%s: invalid mutex.",
595 function );
596
597 return( -1 );
598 }
599 internal_mutex = (libcthreads_internal_mutex_t *) mutex;
600
601 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
602 result = SleepConditionVariableCS(
603 &( internal_condition->condition_variable ),
604 &( internal_mutex->critical_section ),
605 INFINITE );
606
607 if( result == 0 )
608 {
609 error_code = GetLastError();
610
611 libcerror_system_set_error(
612 error,
613 error_code,
614 LIBCERROR_ERROR_DOMAIN_RUNTIME,
615 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
616 "%s: unable to sleep on condition variable.",
617 function );
618
619 return( -1 );
620 }
621
622 #elif defined( WINAPI ) && ( WINVER >= 0x0400 )
623 EnterCriticalSection(
624 &( internal_condition->wait_critical_section ) );
625
626 internal_condition->number_of_waiting_threads++;
627
628 LeaveCriticalSection(
629 &( internal_condition->wait_critical_section ) );
630
631 wait_status = SignalObjectAndWait(
632 internal_mutex->mutex_handle,
633 internal_condition->signal_semaphore_handle,
634 INFINITE,
635 FALSE );
636
637 if( wait_status == WAIT_FAILED )
638 {
639 error_code = GetLastError();
640
641 libcerror_system_set_error(
642 error,
643 error_code,
644 LIBCERROR_ERROR_DOMAIN_RUNTIME,
645 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
646 "%s: unable signal mutex handle and wait for signal semaphore handle.",
647 function );
648
649 return( -1 );
650 }
651 EnterCriticalSection(
652 &( internal_condition->wait_critical_section ) );
653
654 internal_condition->number_of_waiting_threads--;
655
656 if( ( internal_condition->number_of_waiting_threads == 0 )
657 && ( internal_condition->signal_is_broadcast != 0 ) )
658 {
659 is_last_waiting_thread = 1;
660 }
661 LeaveCriticalSection(
662 &( internal_condition->wait_critical_section ) );
663
664 if( is_last_waiting_thread != 0 )
665 {
666 wait_status = SignalObjectAndWait(
667 internal_condition->signal_event_handle,
668 internal_mutex->mutex_handle,
669 INFINITE,
670 FALSE );
671
672 if( wait_status == WAIT_FAILED )
673 {
674 error_code = GetLastError();
675
676 libcerror_system_set_error(
677 error,
678 error_code,
679 LIBCERROR_ERROR_DOMAIN_RUNTIME,
680 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
681 "%s: unable to signal signal event handle and wait for mutex handle.",
682 function );
683
684 return( -1 );
685 }
686 }
687 else
688 {
689 wait_status = WaitForSingleObject(
690 internal_mutex->mutex_handle,
691 INFINITE );
692
693 if( wait_status == WAIT_FAILED )
694 {
695 error_code = GetLastError();
696
697 libcerror_system_set_error(
698 error,
699 error_code,
700 LIBCERROR_ERROR_DOMAIN_RUNTIME,
701 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
702 "%s: wait for mutex handle failed.",
703 function );
704
705 return( -1 );
706 }
707 }
708
709 #elif defined( WINAPI )
710
711 #error libcthreads_condition_wait for Windows earlier than NT4 not implemented
712
713 #elif defined( HAVE_PTHREAD_H )
714 pthread_result = pthread_cond_wait(
715 &( internal_condition->condition ),
716 &( internal_mutex->mutex ) );
717
718 if( pthread_result != 0 )
719 {
720 libcerror_system_set_error(
721 error,
722 pthread_result,
723 LIBCERROR_ERROR_DOMAIN_RUNTIME,
724 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
725 "%s: unable to wait for condition.",
726 function );
727
728 return( -1 );
729 }
730 #endif
731 return( 1 );
732 }
733
734 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
735