+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * include/compat/win32/pthread.h - Win32 emulation of POSIX threads using avx_platform.h |
| 7 | */ |
| 8 | |
| 9 | #ifndef OPENAVX_COMPAT_WIN32_PTHREAD_H |
| 10 | #define OPENAVX_COMPAT_WIN32_PTHREAD_H |
| 11 | |
| 12 | #if defined(__MINGW32__) || defined(__MINGW64__) |
| 13 | #include_next <pthread.h> |
| 14 | #elif defined(_MSC_VER) || defined(_WIN32) |
| 15 | |
| 16 | #ifndef WIN32_LEAN_AND_MEAN |
| 17 | #define WIN32_LEAN_AND_MEAN |
| 18 | #endif |
| 19 | #include <windows.h> |
| 20 | #include <process.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | typedef HANDLE pthread_t; |
| 24 | typedef void* pthread_attr_t; |
| 25 | |
| 26 | typedef struct { |
| 27 | void *(*start_routine)(void *); |
| 28 | void *arg; |
| 29 | } avx_pthread_wrapper_t; |
| 30 | |
| 31 | static inline unsigned __stdcall avx_pthread_trampoline(void *p) { |
| 32 | avx_pthread_wrapper_t *w = (avx_pthread_wrapper_t*)p; |
| 33 | void *(*fn)(void *) = w->start_routine; |
| 34 | void *arg = w->arg; |
| 35 | free(w); |
| 36 | fn(arg); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { |
| 41 | (void)attr; |
| 42 | avx_pthread_wrapper_t *w = (avx_pthread_wrapper_t*)malloc(sizeof(avx_pthread_wrapper_t)); |
| 43 | if (!w) return -1; |
| 44 | w->start_routine = start_routine; |
| 45 | w->arg = arg; |
| 46 | HANDLE h = (HANDLE)_beginthreadex(NULL, 0, avx_pthread_trampoline, w, 0, NULL); |
| 47 | if (!h) { |
| 48 | free(w); |
| 49 | return -1; |
| 50 | } |
| 51 | if (thread) *thread = h; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static inline int pthread_join(pthread_t thread, void **retval) { |
| 56 | (void)retval; |
| 57 | if (!thread) return 0; |
| 58 | WaitForSingleObject(thread, INFINITE); |
| 59 | CloseHandle(thread); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static inline int pthread_detach(pthread_t thread) { |
| 64 | if (thread) { |
| 65 | CloseHandle(thread); |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | typedef SRWLOCK pthread_mutex_t; |
| 71 | typedef void* pthread_mutexattr_t; |
| 72 | |
| 73 | #define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT |
| 74 | |
| 75 | static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { |
| 76 | (void)attr; |
| 77 | InitializeSRWLock(mutex); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static inline int pthread_mutex_lock(pthread_mutex_t *mutex) { |
| 82 | AcquireSRWLockExclusive(mutex); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) { |
| 87 | ReleaseSRWLockExclusive(mutex); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) { |
| 92 | (void)mutex; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | #endif /* __MINGW32__ vs _MSC_VER */ |
| 97 | |
| 98 | #endif /* OPENAVX_COMPAT_WIN32_PTHREAD_H */ |