nxt_thread_id.h (223:bf98efe2c55c) nxt_thread_id.h (735:3f7538f0681a)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#ifndef _NXT_UNIX_THREAD_ID_H_INCLUDED_
8#define _NXT_UNIX_THREAD_ID_H_INCLUDED_
9
10
11#if (NXT_LINUX)
12
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7#ifndef _NXT_UNIX_THREAD_ID_H_INCLUDED_
8#define _NXT_UNIX_THREAD_ID_H_INCLUDED_
9
10
11#if (NXT_LINUX)
12
13typedef pid_t nxt_tid_t;
13/*
14 * Linux thread id is a pid of thread created by clone(2),
15 * glibc does not provide a wrapper for gettid().
16 */
14
17
18typedef pid_t nxt_tid_t;
19
20nxt_inline nxt_tid_t
21nxt_thread_get_tid(void)
22{
23 return syscall(SYS_gettid);
24}
25
15#elif (NXT_FREEBSD)
16
26#elif (NXT_FREEBSD)
27
17typedef uint32_t nxt_tid_t;
28/*
29 * FreeBSD 9.0 provides pthread_getthreadid_np(), here is its
30 * emulation. Kernel thread id is the first field of struct pthread.
31 * Although kernel exports a thread id as long type, lwpid_t is 32bit.
32 * Thread id is a number above 100,000.
33 */
18
34
35typedef uint32_t nxt_tid_t;
36
37nxt_inline nxt_tid_t
38nxt_thread_get_tid(void)
39{
40 return (uint32_t) (*(long *) pthread_self());
41}
42
19#elif (NXT_SOLARIS)
20
43#elif (NXT_SOLARIS)
44
45/* Solaris pthread_t are numbers starting with 1. */
46
21typedef pthread_t nxt_tid_t;
22
47typedef pthread_t nxt_tid_t;
48
49nxt_inline nxt_tid_t
50nxt_thread_get_tid(void)
51{
52 return pthread_self();
53}
54
23#elif (NXT_MACOSX)
24
55#elif (NXT_MACOSX)
56
25typedef uint64_t nxt_tid_t;
57/*
58 * MacOSX thread has two thread ids:
59 *
60 * 1) MacOSX 10.6 (Snow Leoprad) has pthread_threadid_np() returning
61 * an uint64_t value, which is obtained using the __thread_selfid()
62 * syscall. It is a number above 300,000.
63 */
26
64
65typedef uint64_t nxt_tid_t;
66
67nxt_inline nxt_tid_t
68nxt_thread_get_tid(void)
69{
70 uint64_t tid;
71
72 (void) pthread_threadid_np(NULL, &tid);
73 return tid;
74}
75
76/*
77 * 2) Kernel thread mach_port_t returned by pthread_mach_thread_np().
78 * It is a number in range 100-100,000.
79 *
80 * return pthread_mach_thread_np(pthread_self());
81 */
82
27#elif (NXT_AIX)
28
83#elif (NXT_AIX)
84
29typedef tid_t nxt_tid_t;
85/*
86 * pthread_self() in main thread returns 1.
87 * pthread_self() in other threads returns 258, 515, etc.
88 *
89 * pthread_getthrds_np(PTHRDSINFO_QUERY_TID) returns kernel tid
90 * shown in "ps -ef -m -o THREAD" output.
91 */
30
92
93typedef tid_t nxt_tid_t;
94
95nxt_inline nxt_tid_t
96nxt_thread_get_tid(void)
97{
98 int err, size;
99 pthread_t pt;
100 struct __pthrdsinfo ti;
101
102 size = 0;
103 pt = pthread_self();
104
105 err = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &ti,
106 sizeof(struct __pthrdsinfo), NULL, size);
107
108 if (nxt_fast_path(err == 0)) {
109 return ti.__pi_tid;
110 }
111
112 nxt_main_log_alert("pthread_getthrds_np(PTHRDSINFO_QUERY_TID) failed %E",
113 err);
114 return 0;
115}
116
117/*
118 * AIX pthread_getunique_np() returns thread unique number starting with 1.
119 * OS/400 and i5/OS have pthread_getthreadid_np(), but AIX lacks their
120 * counterpart.
121 *
122 *
123 * int tid;
124 * pthread_t pt;
125 *
126 * pt = pthread_self();
127 * pthread_getunique_np(&pt, &tid);
128 * return tid;
129 */
130
31#elif (NXT_HPUX)
32
131#elif (NXT_HPUX)
132
133/* HP-UX pthread_t are numbers starting with 1. */
134
33typedef pthread_t nxt_tid_t;
34
135typedef pthread_t nxt_tid_t;
136
137nxt_inline nxt_tid_t
138nxt_thread_get_tid(void)
139{
140 return pthread_self();
141}
142
35#else
36
37typedef pthread_t nxt_tid_t;
38
143#else
144
145typedef pthread_t nxt_tid_t;
146
147nxt_inline nxt_tid_t
148nxt_thread_get_tid(void)
149{
150 return pthread_self();
151}
152
39#endif
40
41
42NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr);
43
44
45/*
46 * On Linux pthread_t is unsigned long integer.

--- 19 unchanged lines hidden ---
153#endif
154
155
156NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr);
157
158
159/*
160 * On Linux pthread_t is unsigned long integer.

--- 19 unchanged lines hidden ---