aboutsummaryrefslogtreecommitdiff
path: root/src/platform/windows
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-08-08 18:25:48 -0700
committerGarrett D'Amore <garrett@damore.org>2020-08-08 19:30:17 -0700
commit6c5070d9157ab0de667568655f0bbeb60780d701 (patch)
treeab1a03e16907c97e6d76f1d95d79aea0f4a875ce /src/platform/windows
parentddc0d044dd0fcf4aa1dc333fd5bda0de47850a64 (diff)
downloadnng-6c5070d9157ab0de667568655f0bbeb60780d701.tar.gz
nng-6c5070d9157ab0de667568655f0bbeb60780d701.tar.bz2
nng-6c5070d9157ab0de667568655f0bbeb60780d701.zip
fixes #960 NNG threads inherit application thread name
This also exposes an nng_thread_set_name() function for applications to use. All NNG thread names start with "nng:". Note that support is highly dependent on the operating system.
Diffstat (limited to 'src/platform/windows')
-rw-r--r--src/platform/windows/win_io.c3
-rw-r--r--src/platform/windows/win_ipcdial.c4
-rw-r--r--src/platform/windows/win_resolv.c1
-rw-r--r--src/platform/windows/win_thread.c53
4 files changed, 50 insertions, 11 deletions
diff --git a/src/platform/windows/win_io.c b/src/platform/windows/win_io.c
index 81214498..489dc01a 100644
--- a/src/platform/windows/win_io.c
+++ b/src/platform/windows/win_io.c
@@ -1,5 +1,5 @@
//
-// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
@@ -114,6 +114,7 @@ nni_win_io_sysinit(void)
if (rv != 0) {
goto fail;
}
+ nni_thr_set_name(&win_io_thrs[i], "nng:iocp");
}
for (i = 0; i < win_io_nthr; i++) {
nni_thr_run(&win_io_thrs[i]);
diff --git a/src/platform/windows/win_ipcdial.c b/src/platform/windows/win_ipcdial.c
index 5deebc01..65d1b544 100644
--- a/src/platform/windows/win_ipcdial.c
+++ b/src/platform/windows/win_ipcdial.c
@@ -1,5 +1,5 @@
//
-// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
@@ -269,7 +269,7 @@ nni_win_ipc_sysinit(void)
if (rv != 0) {
return (rv);
}
-
+ nni_thr_set_name(&worker->thr, "nng:ipc:dial");
nni_thr_run(&worker->thr);
return (0);
diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c
index d80b5ddf..d1cc058e 100644
--- a/src/platform/windows/win_resolv.c
+++ b/src/platform/windows/win_resolv.c
@@ -432,6 +432,7 @@ nni_win_resolv_sysinit(void)
nni_win_resolv_sysfini();
return (rv);
}
+ nni_thr_set_name(&resolv_thrs[i], "nng:resolver");
}
for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) {
nni_thr_run(&resolv_thrs[i]);
diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c
index 785376df..7cc87eb3 100644
--- a/src/platform/windows/win_thread.c
+++ b/src/platform/windows/win_thread.c
@@ -14,10 +14,14 @@
#ifdef NNG_PLATFORM_WINDOWS
+typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
+static HMODULE hKernel32;
+
+static pfnSetThreadDescription set_thread_desc;
+
// mingw does not define InterlockedAddNoFence64, use the mingw equivalent
#if defined(__MINGW32__) || defined(__MINGW64__)
-#define InterlockedAddNoFence(a, b) \
- __atomic_add_fetch(a, b, __ATOMIC_RELAXED)
+#define InterlockedAddNoFence(a, b) __atomic_add_fetch(a, b, __ATOMIC_RELAXED)
#define InterlockedAddNoFence64(a, b) \
__atomic_add_fetch(a, b, __ATOMIC_RELAXED)
#define InterlockedIncrementAcquire64(a) \
@@ -219,11 +223,10 @@ bool
nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t new)
{
uint64_t old;
- old = InterlockedCompareExchange64(&v->v, (LONG64)new, (LONG64)comp);
+ old = InterlockedCompareExchange64(&v->v, (LONG64) new, (LONG64) comp);
return (old == comp);
}
-
void
nni_atomic_add(nni_atomic_int *v, int bump)
{
@@ -278,11 +281,10 @@ bool
nni_atomic_cas(nni_atomic_int *v, int comp, int new)
{
int old;
- old = InterlockedCompareExchange(&v->v, (LONG)new, (LONG)comp);
+ old = InterlockedCompareExchange(&v->v, (LONG) new, (LONG) comp);
return (old == comp);
}
-
static unsigned int __stdcall nni_plat_thr_main(void *arg)
{
nni_plat_thr *thr = arg;
@@ -325,6 +327,30 @@ nni_plat_thr_is_self(nni_plat_thr *thr)
return (GetCurrentThreadId() == thr->id);
}
+void
+nni_plat_thr_set_name(nni_plat_thr *thr, const char *name)
+{
+ if (set_thread_desc != NULL) {
+ wchar_t *wcs;
+ size_t len;
+ HANDLE h;
+
+ if (thr == NULL) {
+ h = GetCurrentThread();
+ } else {
+ h = thr->handle;
+ }
+
+ len = strlen(name) + 1;
+ if ((wcs = nni_alloc(len * 2)) == NULL) {
+ return;
+ }
+ (void) MultiByteToWideChar(CP_UTF8, 0, name, len, wcs, len);
+ set_thread_desc(h, wcs);
+ nni_free(wcs, len * 2);
+ }
+}
+
static LONG plat_inited = 0;
int
@@ -346,10 +372,18 @@ nni_plat_init(int (*helper)(void))
return (0); // fast path
}
- AcquireSRWLockExclusive(&lock);
+
+ AcquireSRWLockExclusive(&lock);
if (!plat_inited) {
- if (((rv = nni_win_io_sysinit()) != 0) ||
+ // Let's look up the function to set thread descriptions.
+ hKernel32 = LoadLibrary(TEXT("kernel32.dll"));
+ if (hKernel32 != NULL) {
+ set_thread_desc = (pfnSetThreadDescription)
+ GetProcAddress(hKernel32, "SetThreadDescription");
+ }
+
+ if (((rv = nni_win_io_sysinit()) != 0) ||
((rv = nni_win_ipc_sysinit()) != 0) ||
((rv = nni_win_tcp_sysinit()) != 0) ||
((rv = nni_win_udp_sysinit()) != 0) ||
@@ -376,6 +410,9 @@ nni_plat_fini(void)
nni_win_tcp_sysfini();
nni_win_io_sysfini();
WSACleanup();
+ if (hKernel32 != NULL) {
+ FreeLibrary(hKernel32);
+ }
plat_inited = 0;
}