diff options
author | Igor Sysoev <igor@sysoev.ru> | 2018-07-27 16:53:26 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2018-07-27 16:53:26 +0300 |
commit | 7e41f9d1081ff0c5c335da4ae21f0d76c7d8edc9 (patch) | |
tree | 48ced5cd8efe8bf61bf961c468a2e6fa5d88956e /src | |
parent | de885e10cb895751d9994e8ab99bdb56da891d3d (diff) | |
download | unit-7e41f9d1081ff0c5c335da4ae21f0d76c7d8edc9.tar.gz unit-7e41f9d1081ff0c5c335da4ae21f0d76c7d8edc9.tar.bz2 |
Refactored thread ID functions.
nxt_thread_tid() was moved to src/nxt_thread.c
nxt_thread_get_tid() was moved to src/nxt_thread_id.h.
src/nxt_thread_id.c was removed.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_thread.c | 32 | ||||
-rw-r--r-- | src/nxt_thread_id.c | 170 | ||||
-rw-r--r-- | src/nxt_thread_id.h | 122 |
3 files changed, 150 insertions, 174 deletions
diff --git a/src/nxt_thread.c b/src/nxt_thread.c index a4ef416f..72429f4d 100644 --- a/src/nxt_thread.c +++ b/src/nxt_thread.c @@ -237,3 +237,35 @@ nxt_thread_wait(nxt_thread_handle_t handle) nxt_main_log_alert("pthread_join(%PH) failed %E", handle, err); } } + + +nxt_tid_t +nxt_thread_tid(nxt_thread_t *thr) +{ + if (thr == NULL) { + thr = nxt_thread(); + } + +#if (NXT_HAVE_THREAD_STORAGE_CLASS) + + if (nxt_slow_path(thr->tid == 0)) { + thr->tid = nxt_thread_get_tid(); + } + + return thr->tid; + +#else + + if (nxt_fast_path(thr != NULL)) { + + if (nxt_slow_path(thr->tid == 0)) { + thr->tid = nxt_thread_get_tid(); + } + + return thr->tid; + } + + return nxt_thread_get_tid(); + +#endif +} diff --git a/src/nxt_thread_id.c b/src/nxt_thread_id.c deleted file mode 100644 index 44f10d25..00000000 --- a/src/nxt_thread_id.c +++ /dev/null @@ -1,170 +0,0 @@ - -/* - * Copyright (C) Igor Sysoev - * Copyright (C) NGINX, Inc. - */ - -#include <nxt_main.h> - - -#if (NXT_LINUX) - -/* - * Linux thread id is a pid of thread created by clone(2), - * glibc does not provide a wrapper for gettid(). - */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - return syscall(SYS_gettid); -} - -#elif (NXT_FREEBSD) - -/* - * FreeBSD 9.0 provides pthread_getthreadid_np(), here is its - * emulation. Kernel thread id is the first field of struct pthread. - * Although kernel exports a thread id as long type, lwpid_t is 32bit. - * Thread id is a number above 100,000. - */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - return (uint32_t) (*(long *) pthread_self()); -} - -#elif (NXT_SOLARIS) - -/* Solaris pthread_t are numbers starting with 1 */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - return pthread_self(); -} - -#elif (NXT_MACOSX) - -/* - * MacOSX thread has two thread ids: - * - * 1) MacOSX 10.6 (Snow Leoprad) has pthread_threadid_np() returning - * an uint64_t value, which is obtained using the __thread_selfid() - * syscall. It is a number above 300,000. - */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - uint64_t tid; - - (void) pthread_threadid_np(NULL, &tid); - return tid; -} - -/* - * 2) Kernel thread mach_port_t returned by pthread_mach_thread_np(). - * It is a number in range 100-100,000. - * - * return pthread_mach_thread_np(pthread_self()); - */ - -#elif (NXT_AIX) - -/* - * pthread_self() in main thread returns 1. - * pthread_self() in other threads returns 258, 515, etc. - * - * pthread_getthrds_np(PTHRDSINFO_QUERY_TID) returns kernel tid - * shown in "ps -ef -m -o THREAD" output. - */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - int err, size; - pthread_t pt; - struct __pthrdsinfo ti; - - size = 0; - pt = pthread_self(); - - err = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &ti, - sizeof(struct __pthrdsinfo), NULL, size); - - if (nxt_fast_path(err == 0)) { - return ti.__pi_tid; - } - - nxt_main_log_alert("pthread_getthrds_np(PTHRDSINFO_QUERY_TID) failed %E", - err); - return 0; -} - -/* - * AIX pthread_getunique_np() returns thread unique number starting with 1. - * OS/400 and i5/OS have pthread_getthreadid_np(), but AIX lacks their - * counterpart. - * - * - * int tid; - * pthread_t pt; - * - * pt = pthread_self(); - * pthread_getunique_np(&pt, &tid); - * return tid; - */ - -#elif (NXT_HPUX) - -/* HP-UX pthread_t are numbers starting with 1 */ - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - return pthread_self(); -} - -#else - -nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - return pthread_self(); -} - -#endif - - -nxt_tid_t -nxt_thread_tid(nxt_thread_t *thr) -{ - if (thr == NULL) { - thr = nxt_thread(); - } - -#if (NXT_HAVE_THREAD_STORAGE_CLASS) - - if (nxt_slow_path(thr->tid == 0)) { - thr->tid = nxt_thread_get_tid(); - } - - return thr->tid; - -#else - - if (nxt_fast_path(thr != NULL)) { - - if (nxt_slow_path(thr->tid == 0)) { - thr->tid = nxt_thread_get_tid(); - } - - return thr->tid; - } - - return nxt_thread_get_tid(); - -#endif -} diff --git a/src/nxt_thread_id.h b/src/nxt_thread_id.h index f8a52533..eba191ea 100644 --- a/src/nxt_thread_id.h +++ b/src/nxt_thread_id.h @@ -10,32 +10,146 @@ #if (NXT_LINUX) -typedef pid_t nxt_tid_t; +/* + * Linux thread id is a pid of thread created by clone(2), + * glibc does not provide a wrapper for gettid(). + */ + +typedef pid_t nxt_tid_t; + +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + return syscall(SYS_gettid); +} #elif (NXT_FREEBSD) -typedef uint32_t nxt_tid_t; +/* + * FreeBSD 9.0 provides pthread_getthreadid_np(), here is its + * emulation. Kernel thread id is the first field of struct pthread. + * Although kernel exports a thread id as long type, lwpid_t is 32bit. + * Thread id is a number above 100,000. + */ + +typedef uint32_t nxt_tid_t; + +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + return (uint32_t) (*(long *) pthread_self()); +} #elif (NXT_SOLARIS) +/* Solaris pthread_t are numbers starting with 1. */ + typedef pthread_t nxt_tid_t; +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + return pthread_self(); +} + #elif (NXT_MACOSX) -typedef uint64_t nxt_tid_t; +/* + * MacOSX thread has two thread ids: + * + * 1) MacOSX 10.6 (Snow Leoprad) has pthread_threadid_np() returning + * an uint64_t value, which is obtained using the __thread_selfid() + * syscall. It is a number above 300,000. + */ + +typedef uint64_t nxt_tid_t; + +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + uint64_t tid; + + (void) pthread_threadid_np(NULL, &tid); + return tid; +} + +/* + * 2) Kernel thread mach_port_t returned by pthread_mach_thread_np(). + * It is a number in range 100-100,000. + * + * return pthread_mach_thread_np(pthread_self()); + */ #elif (NXT_AIX) -typedef tid_t nxt_tid_t; +/* + * pthread_self() in main thread returns 1. + * pthread_self() in other threads returns 258, 515, etc. + * + * pthread_getthrds_np(PTHRDSINFO_QUERY_TID) returns kernel tid + * shown in "ps -ef -m -o THREAD" output. + */ + +typedef tid_t nxt_tid_t; + +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + int err, size; + pthread_t pt; + struct __pthrdsinfo ti; + + size = 0; + pt = pthread_self(); + + err = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &ti, + sizeof(struct __pthrdsinfo), NULL, size); + + if (nxt_fast_path(err == 0)) { + return ti.__pi_tid; + } + + nxt_main_log_alert("pthread_getthrds_np(PTHRDSINFO_QUERY_TID) failed %E", + err); + return 0; +} + +/* + * AIX pthread_getunique_np() returns thread unique number starting with 1. + * OS/400 and i5/OS have pthread_getthreadid_np(), but AIX lacks their + * counterpart. + * + * + * int tid; + * pthread_t pt; + * + * pt = pthread_self(); + * pthread_getunique_np(&pt, &tid); + * return tid; + */ #elif (NXT_HPUX) +/* HP-UX pthread_t are numbers starting with 1. */ + typedef pthread_t nxt_tid_t; +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + return pthread_self(); +} + #else typedef pthread_t nxt_tid_t; +nxt_inline nxt_tid_t +nxt_thread_get_tid(void) +{ + return pthread_self(); +} + #endif |