diff options
Diffstat (limited to 'src/win/thread.c')
-rw-r--r-- | src/win/thread.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/win/thread.c b/src/win/thread.c index 436846a7..753cb6a3 100644 --- a/src/win/thread.c +++ b/src/win/thread.c @@ -57,6 +57,9 @@ STATIC_ASSERT(sizeof(uv_thread_t) <= sizeof(void*)); static uv_key_t uv__current_thread_key; static uv_once_t uv__current_thread_init_guard = UV_ONCE_INIT; +static uv_once_t uv__thread_name_once = UV_ONCE_INIT; +HRESULT (WINAPI *pGetThreadDescription)(HANDLE, PWSTR*); +HRESULT (WINAPI *pSetThreadDescription)(HANDLE, PCWSTR); static void uv__init_current_thread_key(void) { @@ -278,12 +281,28 @@ int uv_thread_equal(const uv_thread_t* t1, const uv_thread_t* t2) { } +static void uv__thread_name_init_once(void) { + HMODULE m; + + m = GetModuleHandleA("api-ms-win-core-processthreads-l1-1-3.dll"); + if (m != NULL) { + pGetThreadDescription = (void*) GetProcAddress(m, "GetThreadDescription"); + pSetThreadDescription = (void*) GetProcAddress(m, "SetThreadDescription"); + } +} + + int uv_thread_setname(const char* name) { HRESULT hr; WCHAR* namew; int err; char namebuf[UV_PTHREAD_MAX_NAMELEN_NP]; + uv_once(&uv__thread_name_once, uv__thread_name_init_once); + + if (pSetThreadDescription == NULL) + return UV_ENOSYS; + if (name == NULL) return UV_EINVAL; @@ -295,7 +314,7 @@ int uv_thread_setname(const char* name) { if (err) return err; - hr = SetThreadDescription(GetCurrentThread(), namew); + hr = pSetThreadDescription(GetCurrentThread(), namew); uv__free(namew); if (FAILED(hr)) return uv_translate_sys_error(HRESULT_CODE(hr)); @@ -312,6 +331,11 @@ int uv_thread_getname(uv_thread_t* tid, char* name, size_t size) { int r; DWORD exit_code; + uv_once(&uv__thread_name_once, uv__thread_name_init_once); + + if (pGetThreadDescription == NULL) + return UV_ENOSYS; + if (name == NULL || size == 0) return UV_EINVAL; @@ -324,7 +348,7 @@ int uv_thread_getname(uv_thread_t* tid, char* name, size_t size) { namew = NULL; thread_name = NULL; - hr = GetThreadDescription(*tid, &namew); + hr = pGetThreadDescription(*tid, &namew); if (FAILED(hr)) return uv_translate_sys_error(HRESULT_CODE(hr)); |