diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-08-08 18:25:48 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-08-08 19:30:17 -0700 |
| commit | 6c5070d9157ab0de667568655f0bbeb60780d701 (patch) | |
| tree | ab1a03e16907c97e6d76f1d95d79aea0f4a875ce /src/platform | |
| parent | ddc0d044dd0fcf4aa1dc333fd5bda0de47850a64 (diff) | |
| download | nng-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')
| -rw-r--r-- | src/platform/posix/posix_pollq_epoll.c | 1 | ||||
| -rw-r--r-- | src/platform/posix/posix_pollq_kqueue.c | 3 | ||||
| -rw-r--r-- | src/platform/posix/posix_pollq_poll.c | 3 | ||||
| -rw-r--r-- | src/platform/posix/posix_pollq_port.c | 1 | ||||
| -rw-r--r-- | src/platform/posix/posix_resolv_gai.c | 4 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c | 27 | ||||
| -rw-r--r-- | src/platform/windows/win_io.c | 3 | ||||
| -rw-r--r-- | src/platform/windows/win_ipcdial.c | 4 | ||||
| -rw-r--r-- | src/platform/windows/win_resolv.c | 1 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 53 |
10 files changed, 85 insertions, 15 deletions
diff --git a/src/platform/posix/posix_pollq_epoll.c b/src/platform/posix/posix_pollq_epoll.c index d7a2c70f..bbfa9f6e 100644 --- a/src/platform/posix/posix_pollq_epoll.c +++ b/src/platform/posix/posix_pollq_epoll.c @@ -377,6 +377,7 @@ nni_posix_pollq_create(nni_posix_pollq *pq) nni_mtx_fini(&pq->mtx); return (rv); } + nni_thr_set_name(&pq->thr, "nng:poll:epoll"); nni_thr_run(&pq->thr); return (0); } diff --git a/src/platform/posix/posix_pollq_kqueue.c b/src/platform/posix/posix_pollq_kqueue.c index 7d827d7d..dda81be4 100644 --- a/src/platform/posix/posix_pollq_kqueue.c +++ b/src/platform/posix/posix_pollq_kqueue.c @@ -229,6 +229,8 @@ nni_posix_poll_thr(void *arg) { nni_posix_pollq *pq = arg; + nni_thr_set_name(NULL, "nng:poll:kqueue"); + for (;;) { int n; struct kevent evs[NNI_MAX_KQUEUE_EVENTS]; @@ -335,7 +337,6 @@ nni_posix_pollq_create(nni_posix_pollq *pq) int nni_posix_pollq_sysinit(void) { - return (nni_posix_pollq_create(&nni_posix_global_pollq)); } diff --git a/src/platform/posix/posix_pollq_poll.c b/src/platform/posix/posix_pollq_poll.c index 12c5e541..f6f81703 100644 --- a/src/platform/posix/posix_pollq_poll.c +++ b/src/platform/posix/posix_pollq_poll.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> // // This software is supplied under the terms of the MIT License, a @@ -329,6 +329,7 @@ nni_posix_pollq_create(nni_posix_pollq *pq) nni_plat_pipe_close(pq->wakewfd, pq->wakerfd); return (rv); } + nni_thr_set_name(&pq->thr, "nng:poll:poll"); nni_mtx_init(&pq->mtx); nni_thr_run(&pq->thr); return (0); diff --git a/src/platform/posix/posix_pollq_port.c b/src/platform/posix/posix_pollq_port.c index bf628e47..7ce96f68 100644 --- a/src/platform/posix/posix_pollq_port.c +++ b/src/platform/posix/posix_pollq_port.c @@ -228,6 +228,7 @@ nni_posix_pollq_create(nni_posix_pollq *pq) nni_posix_pollq_destroy(pq); return (rv); } + nni_thr_set_name(&pq->thr, "nng:poll:port"); nni_thr_run(&pq->thr); return (0); diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c index 88313045..1799935e 100644 --- a/src/platform/posix/posix_resolv_gai.c +++ b/src/platform/posix/posix_resolv_gai.c @@ -318,7 +318,9 @@ resolv_worker(void *unused) NNI_ARG_UNUSED(unused); - nni_mtx_lock(&resolv_mtx); + nni_thr_set_name(NULL, "nng:resolver"); + + nni_mtx_lock(&resolv_mtx); for (;;) { nni_aio * aio; resolv_item *item; diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index b8fa814e..0178455f 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.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 @@ -254,6 +254,31 @@ nni_plat_thr_is_self(nni_plat_thr *thr) } void +nni_plat_thr_set_name(nni_plat_thr *thr, const char *name) +{ +#if defined(NNG_HAVE_PTHREAD_SET_NAME_NP) + if (thr == NULL) { + pthread_set_name_np(pthread_self(), name); + } else { + pthread_set_name_np(thr->tid, name); + } +#elif defined(NNG_HAVE_PTHREAD_SETNAME_NP) +#if defined(__APPLE__) + // Darwin is weird, it can only set the name of pthread_self. + if ((thr == NULL) || (pthread_self() == thr->tid)) { + pthread_setname_np(name); + } +#else + if (thr == NULL) { + pthread_setname_np(pthread_self(), name); + } else { + pthread_setname_np(thr->tid, name); + } +#endif +#endif +} + +void nni_atfork_child(void) { nni_plat_forked = 1; 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; } |
