Skip to content

Commit 7f23ec0

Browse files
committed
Fix Windows threading for MinGW builds
- Fix pthread_create macro: cast thread_func to LPTHREAD_START_ROUTINE and properly check for CreateThread failure (returns NULL) - Fix pthread_mutex_trylock macro: use 0 instead of NULL for timeout - Extend N2N_THREAD_RETURN_DATATYPE and N2N_THREAD_PARAMETER_DATATYPE definitions to also apply to __MINGW32__, not just _MSC_VER Made-with: Cursor
1 parent dac4e66 commit 7f23ec0

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

include/n2n_define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ enum skip_add {SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2};
196196
#define N2N_IFNAMSIZ 16 /* 15 chars * NULL */
197197
#endif
198198

199-
#ifdef _MSC_VER
199+
#if defined(_MSC_VER) || defined(__MINGW32__)
200200
#define N2N_THREAD_RETURN_DATATYPE DWORD WINAPI
201201
#define N2N_THREAD_PARAMETER_DATATYPE LPVOID
202202
#else

win32/n2n_win32.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ typedef struct tuntap_dev {
105105
#define pthread_mutex_t HANDLE
106106

107107
#define pthread_create(p_thread_handle, attr, thread_func, p_param) \
108-
(*p_thread_handle = CreateThread(0 /* default security flags */, 0 /*default stack*/, \
109-
thread_func, p_param, 0 /* default creation flags */, \
110-
NULL) == 0)
108+
((*p_thread_handle = CreateThread(0 /* default security flags */, 0 /*default stack*/, \
109+
(LPTHREAD_START_ROUTINE)thread_func, p_param, 0 /* default creation flags */, \
110+
NULL)) == NULL)
111111

112112
#define pthread_cancel(p_thread_handle) \
113113
TerminateThread(p_thread_handle, 0)
@@ -120,7 +120,7 @@ typedef struct tuntap_dev {
120120
WaitForSingleObject(*mutex, INFINITE)
121121

122122
#define pthread_mutex_trylock(mutex) \
123-
WaitForSingleObject(*mutex, NULL)
123+
WaitForSingleObject(*mutex, 0)
124124

125125
#define pthread_mutex_unlock(mutex) \
126126
ReleaseMutex(*mutex)

0 commit comments

Comments
 (0)