Debian Codegraph @

contrib/pthread-shim.c

raw symbols SCIP
1 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
2 * Author: Kuldeep Singh Dhaka <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "time-shim.h"
9 #include "pthread-shim.h"
10 #include <stdio.h>
11 #ifdef _WIN32
12 #if HAVE_PTHREADS
13 int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t ms)
14 {
15 timespec_t ts;
16 struct timeval tv;
17 gettimeofday(&tv, NULL);
18 uint64_t odd = (tv.tv_usec + (ms * 1000)) * 1000;
19 ts.tv_sec = tv.tv_sec + odd / 1000000000ULL;
20 ts.tv_nsec = odd % 1000000000ULL;
21 return pthread_cond_timedwait(cond, mutex, (const struct timespec *)&ts);
22 }
23 #else
24 #include <errno.h>
25
26 static BOOL WINAPI init_mutex_impl(PINIT_ONCE once_var, PVOID mutex, PVOID *param2) {
27 RIST_MARK_UNUSED(once_var);
28 RIST_MARK_UNUSED(param2);
29 if (pthread_mutex_init((pthread_mutex_t *)mutex, NULL) !=
30 0) {
31 return FALSE;
32 }
33 return TRUE;
34 }
35
36 int init_mutex_once(pthread_mutex_t *mutex, PINIT_ONCE once_var)
37 {
38 if (!InitOnceExecuteOnce(once_var, init_mutex_impl, mutex, NULL)) {
39 return -1;
40 }
41 return 0;
42 }
43
44 int pthread_create(pthread_t *thread, pthread_attr_t *attr, DWORD (__stdcall *start_routine)(LPVOID), void *arg)
45 {
46 (void)attr;
47 if (thread == NULL || start_routine == NULL) {
48 return 1;
49 }
50
51 *thread = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
52 if (*thread == NULL) {
53 return 1;
54 }
55
56 return 0;
57 }
58
59 int pthread_join(pthread_t thread, void **value_ptr)
60 {
61 (void)value_ptr;
62
63 WaitForSingleObject(thread, INFINITE);
64 CloseHandle(thread);
65 return 0;
66 }
67
68 void pthread_exit(void *retval)
69 {
70 ExitThread(retval ? (*((DWORD*)retval)) : 0);
71 }
72
73 int pthread_cancel(pthread_t thread)
74 {
75 return TerminateThread(thread, 0) ? 0 : -1;
76 }
77
78 int pthread_detach(pthread_t thread)
79 {
80 return CloseHandle(thread) ? 0 : -1;
81 }
82
83 int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
84 {
85 (void)attr;
86
87 if (mutex == NULL) {
88 return 1;
89 }
90
91 InitializeCriticalSection(mutex);
92 return 0;
93 }
94
95 int pthread_mutex_destroy(pthread_mutex_t *mutex)
96 {
97 if (mutex == NULL) {
98 return 1;
99 }
100
101 DeleteCriticalSection(mutex);
102 return 0;
103 }
104
105 int pthread_mutex_lock(pthread_mutex_t *mutex)
106 {
107 if (mutex == NULL) {
108 return 1;
109 }
110
111 EnterCriticalSection(mutex);
112 return 0;
113 }
114
115 int pthread_mutex_unlock(pthread_mutex_t *mutex)
116 {
117 if (mutex == NULL) {
118 return 1;
119 }
120
121 LeaveCriticalSection(mutex);
122 return 0;
123 }
124
125 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
126 {
127 (void)attr;
128
129 if (cond == NULL) {
130 return 1;
131 }
132
133 InitializeConditionVariable(cond);
134 return 0;
135 }
136
137 int pthread_cond_destroy(pthread_cond_t *cond)
138 {
139 /* Windows does not have a destroy for conditionals */
140 (void)cond;
141 return 0;
142 }
143
144 int pthread_cond_timedwait_broken(pthread_cond_t *cond, pthread_mutex_t *mutex,
145 const timespec_t *abstime)
146 {
147 DWORD ms = INFINITE;
148 int64_t duration = 0;
149 if (cond == NULL || mutex == NULL) {
150 return 1;
151 }
152
153 if (abstime != NULL) {
154 duration = ((abstime->tv_sec - time(NULL)) * 1000) + (abstime->tv_nsec / 1000000);
155 if (RIST_UNLIKELY(duration < 0)) {
156 duration = 1;
157 }
158 ms = (DWORD)duration;
159 }
160
161 if (!SleepConditionVariableCS(cond, mutex, ms)) {
162 return ETIMEDOUT;
163 }
164
165 return 0;
166 }
167
168 int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
169 {
170 if (cond == NULL || mutex == NULL) {
171 return 1;
172 }
173
174 return pthread_cond_timedwait_broken(cond, mutex, NULL);
175 }
176
177 int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t reltime_ms)
178 {
179 if (cond == NULL || mutex == NULL) {
180 return 1;
181 }
182
183 if (!SleepConditionVariableCS(cond, mutex, reltime_ms)) {
184 return ETIMEDOUT;
185 }
186
187 return 0;
188 }
189
190 int pthread_cond_signal(pthread_cond_t *cond)
191 {
192 if (cond == NULL) {
193 return 1;
194 }
195
196 WakeConditionVariable(cond);
197 return 0;
198 }
199
200 int pthread_cond_broadcast(pthread_cond_t *cond)
201 {
202 if (cond == NULL) {
203 return 1;
204 }
205
206 WakeAllConditionVariable(cond);
207 return 0;
208 }
209
210 int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
211 {
212 (void)attr;
213
214 if (rwlock == NULL) {
215 return 1;
216 }
217
218 InitializeSRWLock(&(rwlock->lock));
219 rwlock->exclusive = false;
220 return 0;
221 }
222
223 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
224 {
225 (void)rwlock;
226 return 0;
227 }
228
229 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
230 {
231 if (rwlock == NULL) {
232 return 1;
233 }
234
235 AcquireSRWLockShared(&(rwlock->lock));
236 return 0;
237 }
238
239 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
240 {
241 if (rwlock == NULL) {
242 return 1;
243 }
244
245 return !TryAcquireSRWLockShared(&(rwlock->lock));
246 }
247
248 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
249 {
250 if (rwlock == NULL) {
251 return 1;
252 }
253
254 AcquireSRWLockExclusive(&(rwlock->lock));
255 rwlock->exclusive = true;
256 return 0;
257 }
258
259 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
260 {
261 BOOLEAN ret;
262
263 if (rwlock == NULL) {
264 return 1;
265 }
266
267 ret = TryAcquireSRWLockExclusive(&(rwlock->lock));
268 if (ret) {
269 rwlock->exclusive = true;
270 }
271
272 return ret;
273 }
274
275 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
276 {
277 if (rwlock == NULL) {
278 return 1;
279 }
280
281 if (rwlock->exclusive) {
282 rwlock->exclusive = false;
283 ReleaseSRWLockExclusive(&(rwlock->lock));
284 } else {
285 ReleaseSRWLockShared(&(rwlock->lock));
286 }
287
288 return 0;
289 }
290
291 /* Semaphores */
292 #define MAX_SEM_COUNT 32
293
294 int sem_init(sem_t *sem, int pshared, unsigned value)
295 {
296 (void)pshared;
297 *sem = CreateSemaphore(
298 NULL, // default security attributes
299 value, // initial count
300 MAX_SEM_COUNT, // maximum count
301 NULL); // unnamed semaphore
302
303 if (*sem != NULL) {
304 return 0;
305 } else {
306 errno = ENOSPC;
307 return -1;
308 }
309 }
310
311 int sem_wait(sem_t *sem)
312 {
313 DWORD wait_result = WaitForSingleObject(*sem, INFINITE);
314 switch (wait_result) {
315 // The semaphore object was signaled.
316 case WAIT_ABANDONED:
317 case WAIT_OBJECT_0:
318 return 0;
319 break;
320 case WAIT_FAILED:
321 errno = EINVAL;
322 return -1;
323 break;
324 default:
325 break;
326 }
327 return -1;
328 }
329
330 int sem_post(sem_t *sem)
331 {
332 if (!ReleaseSemaphore(*sem, 1, NULL)) {
333 errno = EINVAL;
334 return -1;
335 } else {
336 return 0;
337 }
338 }
339 #endif
340 #else
341
342 #ifdef __APPLE__
343 #include <sys/time.h>
344 #include "time-shim.h"
345 #endif
346
347 // This is not part of the POSIX API
348 // Convience function around pthread
349 int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t ms)
350 {
351 timespec_t ts;
352 struct timeval tv;
353 gettimeofday(&tv, NULL);
354 uint64_t odd = (tv.tv_usec + (ms * 1000)) * 1000;
355 ts.tv_sec = tv.tv_sec + odd / 1000000000ULL;
356 ts.tv_nsec = odd % 1000000000ULL;
357 return pthread_cond_timedwait(cond, mutex, (const struct timespec*)&ts);
358 }
359
360 #endif