From baee91a2364addf182ae7ae246c45d0b21b29c80 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sun, 20 Oct 2024 09:34:24 +0200 Subject: [PATCH] thread/win32: fix thrd_current and thrd_equal handling --- CMakeLists.txt | 2 +- include/re_thread.h | 8 ++++++-- src/thread/win32.c | 21 +++++++++++++-------- test/CMakeLists.txt | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb4d95b7a..ef52a5796 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,7 +74,7 @@ else() -Wnested-externs -Wno-strict-aliasing -Wold-style-definition - -Wshadow -Waggregate-return + -Wshadow -Wstrict-prototypes -Wuninitialized -Wvla diff --git a/include/re_thread.h b/include/re_thread.h index 774754ae3..2fc8c455a 100644 --- a/include/re_thread.h +++ b/include/re_thread.h @@ -22,14 +22,18 @@ #else #if defined(WIN32) - #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include + +struct thrd_win32 { + HANDLE hdl; + DWORD id; +}; #define ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT typedef INIT_ONCE once_flag; -typedef HANDLE thrd_t; +typedef struct thrd_win32 thrd_t; typedef CONDITION_VARIABLE cnd_t; typedef CRITICAL_SECTION mtx_t; typedef DWORD tss_t; diff --git a/src/thread/win32.c b/src/thread/win32.c index f177e6534..a5e791dc4 100644 --- a/src/thread/win32.c +++ b/src/thread/win32.c @@ -30,6 +30,7 @@ struct thread { void *arg; }; + static int dtor_register(tss_t key, tss_dtor_t dtor) { int i; @@ -107,7 +108,8 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) goto out; } - *thr = (thrd_t)handle; + thr->hdl = (HANDLE)handle; + thr->id = GetThreadId((HANDLE)handle); out: if (err) mem_deref(th); @@ -118,19 +120,22 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) int thrd_equal(thrd_t lhs, thrd_t rhs) { - return GetThreadId(lhs) == GetThreadId(rhs); + return lhs.id == rhs.id; } thrd_t thrd_current(void) { - return GetCurrentThread(); + /* GetCurrentThread() returns only a pseudo handle and can not used + * within other threads */ + thrd_t t = {.hdl = GetCurrentThread(), .id = GetCurrentThreadId()}; + return t; } int thrd_detach(thrd_t thr) { - CloseHandle(thr); + CloseHandle(thr.hdl); return thrd_success; } @@ -139,19 +144,19 @@ int thrd_join(thrd_t thr, int *res) { DWORD w, code; - w = WaitForSingleObject(thr, INFINITE); + w = WaitForSingleObject(thr.hdl, INFINITE); if (w != WAIT_OBJECT_0) return thrd_error; if (res) { - if (!GetExitCodeThread(thr, &code)) { - CloseHandle(thr); + if (!GetExitCodeThread(thr.hdl, &code)) { + CloseHandle(thr.hdl); return thrd_error; } *res = (int)code; } - CloseHandle(thr); + CloseHandle(thr.hdl); return thrd_success; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 649c6bc66..439094661 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -44,7 +44,7 @@ else() -Wnested-externs -Wno-strict-aliasing -Wold-style-definition - -Wshadow -Waggregate-return + -Wshadow -Wstrict-prototypes -Wuninitialized -Wvla