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 20, 2024
1 parent ff496dc commit 3f4cd77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/re_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
#else

#if defined(WIN32)
struct thrd_win32;

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#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
25 changes: 17 additions & 8 deletions src/thread/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct thread {
void *arg;
};

struct thrd_win32 {
HANDLE hdl;
uint32_t id;
};

static int dtor_register(tss_t key, tss_dtor_t dtor)
{
int i;
Expand Down Expand Up @@ -107,7 +112,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);
out:
if (err)
mem_deref(th);
Expand All @@ -118,19 +124,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 +148,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

0 comments on commit 3f4cd77

Please sign in to comment.