diff options
author | Chris Robinson <[email protected]> | 2014-04-17 21:01:54 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-04-17 21:01:54 -0700 |
commit | 5abefaed0a21b1c97decdf6a40d582a0ac41a3ed (patch) | |
tree | ed5aac10457e4f13d29b6d6ef3ca62aa73803d0b | |
parent | 36df67f546c3162e06964ca6a2466023647c7485 (diff) |
Use the thread ID for althrd_t on Windows
-rw-r--r-- | Alc/threads.c | 42 | ||||
-rw-r--r-- | OpenAL32/Include/threads.h | 10 |
2 files changed, 34 insertions, 18 deletions
diff --git a/Alc/threads.c b/Alc/threads.c index 29e177ea..d0aea6e8 100644 --- a/Alc/threads.c +++ b/Alc/threads.c @@ -65,7 +65,7 @@ void althrd_setname(althrd_t thr, const char *name) #pragma pack(pop) info.dwType = 0x1000; info.szName = name; - info.dwThreadID = ((thr == GetCurrentThread()) ? -1 : GetThreadId(thr)); + info.dwThreadID = thr; info.dwFlags = 0; __try { @@ -75,11 +75,28 @@ void althrd_setname(althrd_t thr, const char *name) } #undef MS_VC_EXCEPTION #else - TRACE("Can't set thread %04lx name to \"%s\"\n", GetThreadId(thr), name); + TRACE("Can't set thread %04lx name to \"%s\"\n", thr, name); #endif } +static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE; + +static void NTAPI althrd_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved)) +{ + if(reason == DLL_PROCESS_DETACH) + ResetUIntMap(&ThrdIdHandle); +} +#ifdef _MSC_VER +#pragma section(".CRT$XLC",read) +__declspec(allocate(".CRT$XLC")) PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback; +#elif defined(__GNUC__) +PIMAGE_TLS_CALLBACK althrd_callback_ __attribute__((section(".CRT$XLC"))) = althrd_callback; +#else +PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback; +#endif + + typedef struct thread_cntr { althrd_start_t func; void *arg; @@ -98,7 +115,7 @@ static DWORD WINAPI althrd_starter(void *arg) int althrd_create(althrd_t *thr, althrd_start_t func, void *arg) { thread_cntr *cntr; - DWORD dummy; + DWORD thrid; HANDLE hdl; cntr = malloc(sizeof(*cntr)); @@ -107,22 +124,24 @@ int althrd_create(althrd_t *thr, althrd_start_t func, void *arg) cntr->func = func; cntr->arg = arg; - hdl = CreateThread(NULL, THREAD_STACK_SIZE, althrd_starter, cntr, 0, &dummy); + hdl = CreateThread(NULL, THREAD_STACK_SIZE, althrd_starter, cntr, 0, &thrid); if(!hdl) { free(cntr); return althrd_error; } + InsertUIntMapEntry(&ThrdIdHandle, thrid, hdl); - *thr = hdl; + *thr = thrid; return althrd_success; } int althrd_detach(althrd_t thr) { - if(!thr) return althrd_error; - CloseHandle(thr); + HANDLE hdl = RemoveUIntMapKey(&ThrdIdHandle, thr); + if(!hdl) return althrd_error; + CloseHandle(hdl); return althrd_success; } @@ -130,11 +149,12 @@ int althrd_join(althrd_t thr, int *res) { DWORD code; - if(!thr) return althrd_error; + HANDLE hdl = RemoveUIntMapKey(&ThrdIdHandle, thr); + if(!hdl) return althrd_error; - WaitForSingleObject(thr, INFINITE); - GetExitCodeThread(thr, &code); - CloseHandle(thr); + WaitForSingleObject(hdl, INFINITE); + GetExitCodeThread(hdl, &code); + CloseHandle(hdl); *res = (int)code; return althrd_success; diff --git a/OpenAL32/Include/threads.h b/OpenAL32/Include/threads.h index 1d9f7640..eade2406 100644 --- a/OpenAL32/Include/threads.h +++ b/OpenAL32/Include/threads.h @@ -29,7 +29,7 @@ typedef void (*altss_dtor_t)(void*); #define WIN32_LEAN_AND_MEAN #include <windows.h> -typedef HANDLE althrd_t; +typedef DWORD althrd_t; typedef CRITICAL_SECTION almtx_t; typedef DWORD altss_t; @@ -46,16 +46,12 @@ int althrd_sleep(const struct timespec *ts, struct timespec *rem); inline althrd_t althrd_current(void) { - /* This is wrong. GetCurrentThread() returns a psuedo-handle of -1 which - * various functions will interpret as the calling thread. There is no - * apparent way to retrieve the same handle that was returned by - * CreateThread. */ - return GetCurrentThread(); + return GetCurrentThreadId(); } inline int althrd_equal(althrd_t thr0, althrd_t thr1) { - return GetThreadId(thr0) == GetThreadId(thr1); + return thr0 == thr1; } inline void althrd_exit(int res) |