Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Header used to adapt pthread-based POSIX code to Windows API threads. |
| 3 | * |
| 4 | * Copyright (C) 2009 Andrzej K. Haczewski <ahaczewski@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef PTHREAD_H |
| 8 | #define PTHREAD_H |
| 9 | |
| 10 | #ifndef WIN32_LEAN_AND_MEAN |
| 11 | #define WIN32_LEAN_AND_MEAN |
| 12 | #endif |
| 13 | |
| 14 | #include <windows.h> |
| 15 | |
| 16 | /* |
| 17 | * Defines that adapt Windows API threads to pthreads API |
| 18 | */ |
| 19 | #define pthread_mutex_t CRITICAL_SECTION |
| 20 | |
Johannes Schindelin | 2921600 | 2016-01-15 14:24:45 +0100 | [diff] [blame] | 21 | static inline int return_0(int i) { |
| 22 | return 0; |
| 23 | } |
| 24 | #define pthread_mutex_init(a,b) return_0((InitializeCriticalSection((a)), 0)) |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 25 | #define pthread_mutex_destroy(a) DeleteCriticalSection((a)) |
| 26 | #define pthread_mutex_lock EnterCriticalSection |
| 27 | #define pthread_mutex_unlock LeaveCriticalSection |
| 28 | |
Johannes Sixt | 9374919 | 2010-04-08 09:15:39 +0200 | [diff] [blame] | 29 | typedef int pthread_mutexattr_t; |
| 30 | #define pthread_mutexattr_init(a) (*(a) = 0) |
| 31 | #define pthread_mutexattr_destroy(a) do {} while (0) |
| 32 | #define pthread_mutexattr_settype(a, t) 0 |
| 33 | #define PTHREAD_MUTEX_RECURSIVE 0 |
| 34 | |
Loo Rong Jie | c73b7ad | 2018-11-13 10:52:35 -0800 | [diff] [blame] | 35 | #define pthread_cond_t CONDITION_VARIABLE |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 36 | |
Loo Rong Jie | c73b7ad | 2018-11-13 10:52:35 -0800 | [diff] [blame] | 37 | #define pthread_cond_init(a,b) InitializeConditionVariable((a)) |
| 38 | #define pthread_cond_destroy(a) do {} while (0) |
| 39 | #define pthread_cond_wait(a,b) return_0(SleepConditionVariableCS((a), (b), INFINITE)) |
| 40 | #define pthread_cond_signal WakeConditionVariable |
| 41 | #define pthread_cond_broadcast WakeAllConditionVariable |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Simple thread creation implementation using pthread API |
| 45 | */ |
| 46 | typedef struct { |
| 47 | HANDLE handle; |
| 48 | void *(*start_routine)(void*); |
| 49 | void *arg; |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 50 | DWORD tid; |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 51 | } pthread_t; |
| 52 | |
Denton Liu | 7027f50 | 2019-09-04 04:09:48 -0700 | [diff] [blame] | 53 | int pthread_create(pthread_t *thread, const void *unused, |
Denton Liu | 9cad1c4 | 2019-10-10 12:37:04 -0700 | [diff] [blame] | 54 | void *(*start_routine)(void*), void *arg); |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * To avoid the need of copying a struct, we use small macro wrapper to pass |
| 58 | * pointer to win32_pthread_join instead. |
| 59 | */ |
| 60 | #define pthread_join(a, b) win32_pthread_join(&(a), (b)) |
| 61 | |
Denton Liu | 7027f50 | 2019-09-04 04:09:48 -0700 | [diff] [blame] | 62 | int win32_pthread_join(pthread_t *thread, void **value_ptr); |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 63 | |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 64 | #define pthread_equal(t1, t2) ((t1).tid == (t2).tid) |
Denton Liu | 7027f50 | 2019-09-04 04:09:48 -0700 | [diff] [blame] | 65 | pthread_t pthread_self(void); |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 66 | |
Johannes Schindelin | 296d673 | 2016-03-02 20:11:13 +0100 | [diff] [blame] | 67 | static inline void NORETURN pthread_exit(void *ret) |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 68 | { |
Johannes Schindelin | 83c90da | 2016-01-15 14:24:39 +0100 | [diff] [blame] | 69 | ExitThread((DWORD)(intptr_t)ret); |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | typedef DWORD pthread_key_t; |
| 73 | static inline int pthread_key_create(pthread_key_t *keyp, void (*destructor)(void *value)) |
| 74 | { |
| 75 | return (*keyp = TlsAlloc()) == TLS_OUT_OF_INDEXES ? EAGAIN : 0; |
| 76 | } |
| 77 | |
Ramsay Jones | 9ba604a | 2012-04-11 12:49:46 +0700 | [diff] [blame] | 78 | static inline int pthread_key_delete(pthread_key_t key) |
| 79 | { |
| 80 | return TlsFree(key) ? 0 : EINVAL; |
| 81 | } |
| 82 | |
Johannes Sixt | 912b263 | 2010-03-06 16:40:41 +0100 | [diff] [blame] | 83 | static inline int pthread_setspecific(pthread_key_t key, const void *value) |
| 84 | { |
| 85 | return TlsSetValue(key, (void *)value) ? 0 : EINVAL; |
| 86 | } |
| 87 | |
| 88 | static inline void *pthread_getspecific(pthread_key_t key) |
| 89 | { |
| 90 | return TlsGetValue(key); |
| 91 | } |
| 92 | |
Johannes Schindelin | ed84387 | 2016-05-11 17:10:30 +0200 | [diff] [blame] | 93 | #ifndef __MINGW64_VERSION_MAJOR |
Johannes Sixt | f924b52 | 2016-05-01 21:08:21 +0200 | [diff] [blame] | 94 | static inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) |
| 95 | { |
| 96 | return 0; |
| 97 | } |
Johannes Schindelin | ed84387 | 2016-05-11 17:10:30 +0200 | [diff] [blame] | 98 | #endif |
Johannes Sixt | f924b52 | 2016-05-01 21:08:21 +0200 | [diff] [blame] | 99 | |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 100 | #endif /* PTHREAD_H */ |