aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/src/threading.rst4
-rw-r--r--src/win/thread.c28
-rw-r--r--src/win/winapi.h9
3 files changed, 30 insertions, 11 deletions
diff --git a/docs/src/threading.rst b/docs/src/threading.rst
index f40cf0a3..2edf3a89 100644
--- a/docs/src/threading.rst
+++ b/docs/src/threading.rst
@@ -146,6 +146,8 @@ Threads
a thread name can be: Linux, IBM i (16), macOS (64), Windows (32767), and NetBSD (32), etc. `uv_thread_setname()`
will truncate it in case `name` is larger than the limit of the platform.
+ Not supported on Windows Server 2016, returns `UV_ENOSYS`.
+
.. versionadded:: 1.50.0
.. c:function:: int uv_thread_getname(uv_thread_t* tid, char* name, size_t* size)
@@ -155,6 +157,8 @@ Threads
The buffer should be large enough to hold the name of the thread plus the trailing NUL, or it will be truncated to fit
with the trailing NUL.
+ Not supported on Windows Server 2016, returns `UV_ENOSYS`.
+
.. versionadded:: 1.50.0
.. c:function:: int uv_thread_setpriority(uv_thread_t tid, int priority)
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_ */