Debian Codegraph @

contrib/pthread-shim.h

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 #ifndef __PTHREAD_SHIM_H
9 #define __PTHREAD_SHIM_H
10
11 #include "common/attributes.h"
12 #include "config.h"
13
14 #ifdef _WIN32
15 /* librist uses WSAPoll/inet_pton/inet_ntop which mingw-w64 gates on
16 * _WIN32_WINNT >= 0x0600. Force a Vista minimum before <winsock2.h>
17 * is pulled in, so downstream consumers that set a lower baseline
18 * (e.g. VLC 3.0 contribs pin it to XP SP2 via 0x0502) still compile. */
19 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
20 # undef _WIN32_WINNT
21 # define _WIN32_WINNT 0x0600
22 #endif
23 # include <stdbool.h>
24 # include <stdint.h>
25 #include <winsock2.h> //<-- not used here, but included because otherwise our order is broken.
26 #define _WINSOCKAPI_
27 # include <windows.h>
28 #if HAVE_PTHREADS
29 #include <pthread.h>
30 # define PTHREAD_START_FUNC(fname,aname) void *fname(void *aname)
31 typedef void *(*pthread_start_func_t)(void *aname);
32 RIST_PRIV int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t ms);
33 /* Sub-millisecond variant. Rounded up to a millisecond on Windows builds
34 * without pthreads, where the underlying wait cannot express less. */
35 RIST_PRIV int pthread_cond_timedwait_us(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t us);
36 #else
37 typedef CRITICAL_SECTION pthread_mutex_t;
38 typedef void pthread_mutexattr_t;
39 typedef void pthread_condattr_t;
40 typedef void pthread_rwlockattr_t;
41 typedef HANDLE pthread_t;
42 typedef int pthread_attr_t;
43 typedef CONDITION_VARIABLE pthread_cond_t;
44 typedef LPVOID thread_arg_t;
45 typedef DWORD thread_return_t;
46
47 typedef struct {
48 SRWLOCK lock;
49 bool exclusive;
50 } pthread_rwlock_t;
51
52 # define PTHREAD_START_FUNC(fname,aname) DWORD __stdcall fname(LPVOID aname)
53 typedef DWORD (__stdcall *pthread_start_func_t)(LPVOID aname);
54 RIST_PRIV int init_mutex_once(pthread_mutex_t *mutex, PINIT_ONCE once_var);
55
56 RIST_PRIV int pthread_create(pthread_t *thread, pthread_attr_t *attr, DWORD (__stdcall *start_routine)(LPVOID), void *arg);
57 RIST_PRIV int pthread_join(pthread_t thread, void **value_ptr);
58 RIST_PRIV int pthread_detach(pthread_t);
59
60 RIST_PRIV int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
61 RIST_PRIV int pthread_mutex_destroy(pthread_mutex_t *mutex);
62 RIST_PRIV int pthread_mutex_lock(pthread_mutex_t *mutex);
63 RIST_PRIV int pthread_mutex_unlock(pthread_mutex_t *mutex);
64
65 RIST_PRIV int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
66 RIST_PRIV int pthread_cond_destroy(pthread_cond_t *cond);
67 RIST_PRIV int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
68 RIST_PRIV int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t reltime_ms);
69 /* Sub-millisecond variant; see the note on the implementation. */
70 RIST_PRIV int pthread_cond_timedwait_us(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t us);
71
72 RIST_PRIV int pthread_cond_signal(pthread_cond_t *cond);
73 RIST_PRIV int pthread_cond_broadcast(pthread_cond_t *cond);
74
75 RIST_PRIV int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
76 RIST_PRIV int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
77 RIST_PRIV int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
78 RIST_PRIV int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
79 RIST_PRIV int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
80 RIST_PRIV int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
81 RIST_PRIV int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
82
83 /* Semaphores */
84 typedef HANDLE sem_t;
85 RIST_PRIV int sem_init(sem_t *sem, int pshared, unsigned value);
86 RIST_PRIV int sem_post(sem_t *sem);
87 RIST_PRIV int sem_wait(sem_t *sem);
88 #endif
89 #else
90 # include <pthread.h>
91 # include <sys/types.h>
92 # include <semaphore.h>
93 # include <stdint.h>
94 # define PTHREAD_START_FUNC(fname,aname) void *fname(void *aname)
95 typedef void *(*pthread_start_func_t)(void *aname);
96 RIST_PRIV int pthread_cond_timedwait_ms(pthread_cond_t *cond, pthread_mutex_t *mutex, uint32_t ms);
97 /* Sub-millisecond variant. Rounded up to a millisecond on Windows builds
98 * without pthreads, where the underlying wait cannot express less. */
99 RIST_PRIV int pthread_cond_timedwait_us(pthread_cond_t *cond, pthread_mutex_t *mutex, uint64_t us);
100 #endif
101
102 #endif /* __PTHREAD_SHIM_H */