1 /*
2  * Condition 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_CONDITION_H )
23 #define _LIBCTHREADS_INTERNAL_CONDITION_H
24 
25 #include <common.h>
26 #include <types.h>
27 
28 #if 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_extern.h"
37 #include "libcthreads_libcerror.h"
38 #include "libcthreads_types.h"
39 
40 #if defined( __cplusplus )
41 extern "C" {
42 #endif
43 
44 #if !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT )
45 
46 typedef struct libcthreads_internal_condition libcthreads_internal_condition_t;
47 
48 struct libcthreads_internal_condition
49 {
50 
51 #if defined( WINAPI ) && ( WINVER >= 0x0600 )
52 	/* The condition variable
53 	 */
54 	CONDITION_VARIABLE condition_variable;
55 
56 #elif defined( WINAPI )
57 	/* The number of waiting threads
58 	 */
59 	int number_of_waiting_threads;
60 
61 	/* The wait critical section
62 	 */
63 	CRITICAL_SECTION wait_critical_section;
64 
65 	/* Semaphore handle used to signal the waiting threads
66 	 */
67 	HANDLE signal_semaphore_handle;
68 
69 	/* Event handle used to signal the waiting threads
70 	 */
71 	HANDLE signal_event_handle;
72 
73 	/* Value to indicate last signal was a broadcast
74 	 */
75 	uint8_t signal_is_broadcast;
76 
77 #elif defined( HAVE_PTHREAD_H )
78 	/* The condition
79 	 */
80 	pthread_cond_t condition;
81 
82 #else
83 #error Missing condition type
84 #endif
85 };
86 
87 LIBCTHREADS_EXTERN \
88 int libcthreads_condition_initialize(
89      libcthreads_condition_t **condition,
90      libcerror_error_t **error );
91 
92 LIBCTHREADS_EXTERN \
93 int libcthreads_condition_free(
94      libcthreads_condition_t **condition,
95      libcerror_error_t **error );
96 
97 LIBCTHREADS_EXTERN \
98 int libcthreads_condition_broadcast(
99      libcthreads_condition_t *condition,
100      libcerror_error_t **error );
101 
102 LIBCTHREADS_EXTERN \
103 int libcthreads_condition_signal(
104      libcthreads_condition_t *condition,
105      libcerror_error_t **error );
106 
107 LIBCTHREADS_EXTERN \
108 int libcthreads_condition_wait(
109      libcthreads_condition_t *condition,
110      libcthreads_mutex_t *mutex,
111      libcerror_error_t **error );
112 
113 #endif /* !defined( HAVE_LOCAL_LIBCTHREADS ) || defined( HAVE_MULTI_THREAD_SUPPORT ) */
114 
115 #if defined( __cplusplus )
116 }
117 #endif
118 
119 #endif /* !defined( _LIBCTHREADS_INTERNAL_CONDITION_H ) */
120 
121