Skip to content

Commit

Permalink
thread/win32: fix thrd_current and thrd_equal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Oct 21, 2024
1 parent d93b0fd commit baee91a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ else()
-Wnested-externs
-Wno-strict-aliasing
-Wold-style-definition
-Wshadow -Waggregate-return
-Wshadow
-Wstrict-prototypes
-Wuninitialized
-Wvla
Expand Down
8 changes: 6 additions & 2 deletions include/re_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
#else

#if defined(WIN32)

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>

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;
Expand Down
21 changes: 13 additions & 8 deletions src/thread/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct thread {
void *arg;
};


static int dtor_register(tss_t key, tss_dtor_t dtor)
{
int i;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ else()
-Wnested-externs
-Wno-strict-aliasing
-Wold-style-definition
-Wshadow -Waggregate-return
-Wshadow
-Wstrict-prototypes
-Wuninitialized
-Wvla
Expand Down

0 comments on commit baee91a

Please sign in to comment.