Use the thread ID for althrd_t on Windows

This commit is contained in:
Chris Robinson
2014-04-17 21:01:54 -07:00
parent 36df67f546
commit 5abefaed0a
2 changed files with 34 additions and 18 deletions
+31 -11
View File
@@ -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;
+3 -7
View File
@@ -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)