diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2025-01-24 21:53:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-24 21:53:22 +0100 |
commit | 82351168b3e11bb02dba2529aa875dd001d03ff2 (patch) | |
tree | 52aa07b3a7f8ce3625cf32037c501587948f4120 /src | |
parent | bc19beadbd9fdc0f0ddbff30daa3ef1851a5ab47 (diff) | |
download | libuv-82351168b3e11bb02dba2529aa875dd001d03ff2.tar.gz libuv-82351168b3e11bb02dba2529aa875dd001d03ff2.zip |
win: lazy-load [GS]etThreadDescription symbols (#4679)
Said symbols are not by default available on Windows Server 2016 but
libuv can still use them when
api-ms-win-core-processthreads-l1-1-3.dll is present.
Fixes: https://github.com/libuv/libuv/issues/4677
Diffstat (limited to 'src')
-rw-r--r-- | src/win/thread.c | 28 | ||||
-rw-r--r-- | src/win/winapi.h | 9 |
2 files changed, 26 insertions, 11 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)); diff --git a/src/win/winapi.h b/src/win/winapi.h index 4e0ccc61..47c34622 100644 --- a/src/win/winapi.h +++ b/src/win/winapi.h @@ -4828,13 +4828,4 @@ typedef int (WINAPI *uv_sGetHostNameW) int); extern uv_sGetHostNameW pGetHostNameW; -/* processthreadsapi.h */ -#if defined(__MINGW32__) -WINBASEAPI -HRESULT WINAPI GetThreadDescription(HANDLE hThread, - PWSTR *ppszThreadDescription); -WINBASEAPI -HRESULT WINAPI SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription); -#endif - #endif /* UV_WIN_WINAPI_H_ */ |