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