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 
13 /*
14  * Linux thread id is a pid of thread created by clone(2),
15  * glibc does not provide a wrapper for gettid().
16  */
17 
18 typedef pid_t  nxt_tid_t;
19 
20 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)21 nxt_thread_get_tid(void)
22 {
23     return syscall(SYS_gettid);
24 }
25 
26 #elif (NXT_FREEBSD)
27 
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  */
34 
35 typedef uint32_t  nxt_tid_t;
36 
37 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)38 nxt_thread_get_tid(void)
39 {
40     return (uint32_t) (*(long *) pthread_self());
41 }
42 
43 #elif (NXT_SOLARIS)
44 
45 /* Solaris pthread_t are numbers starting with 1. */
46 
47 typedef pthread_t  nxt_tid_t;
48 
49 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)50 nxt_thread_get_tid(void)
51 {
52     return pthread_self();
53 }
54 
55 #elif (NXT_MACOSX)
56 
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  */
64 
65 typedef uint64_t  nxt_tid_t;
66 
67 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)68 nxt_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 
83 #elif (NXT_OPENBSD)
84 
85 typedef pid_t  nxt_tid_t;
86 
87 /* OpenBSD 3.9 getthrid(). */
88 
89 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)90 nxt_thread_get_tid(void)
91 {
92     return getthrid();
93 }
94 
95 #elif (NXT_AIX)
96 
97 /*
98  * pthread_self() in main thread returns 1.
99  * pthread_self() in other threads returns 258, 515, etc.
100  *
101  * pthread_getthrds_np(PTHRDSINFO_QUERY_TID) returns kernel tid
102  * shown in "ps -ef -m -o THREAD" output.
103  */
104 
105 typedef tid_t  nxt_tid_t;
106 
107 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)108 nxt_thread_get_tid(void)
109 {
110     int                  err, size;
111     pthread_t            pt;
112     struct __pthrdsinfo  ti;
113 
114     size = 0;
115     pt = pthread_self();
116 
117     err = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &ti,
118                             sizeof(struct __pthrdsinfo), NULL, size);
119 
120     if (nxt_fast_path(err == 0)) {
121         return ti.__pi_tid;
122     }
123 
124     nxt_main_log_alert("pthread_getthrds_np(PTHRDSINFO_QUERY_TID) failed %E",
125                        err);
126     return 0;
127 }
128 
129 /*
130  * AIX pthread_getunique_np() returns thread unique number starting with 1.
131  * OS/400 and i5/OS have pthread_getthreadid_np(), but AIX lacks their
132  * counterpart.
133  *
134  *
135  * int        tid;
136  * pthread_t  pt;
137  *
138  * pt = pthread_self();
139  * pthread_getunique_np(&pt, &tid);
140  * return tid;
141  */
142 
143 #elif (NXT_HPUX)
144 
145 /* HP-UX pthread_t are numbers starting with 1. */
146 
147 typedef pthread_t  nxt_tid_t;
148 
149 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)150 nxt_thread_get_tid(void)
151 {
152     return pthread_self();
153 }
154 
155 #else
156 
157 typedef pthread_t  nxt_tid_t;
158 
159 nxt_inline nxt_tid_t
nxt_thread_get_tid(void)160 nxt_thread_get_tid(void)
161 {
162     return pthread_self();
163 }
164 
165 #endif
166 
167 
168 NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr);
169 
170 
171 /*
172  * On Linux pthread_t is unsigned long integer.
173  * On FreeBSD, MacOSX, NetBSD, and OpenBSD pthread_t is pointer to a struct.
174  * On Solaris and AIX pthread_t is unsigned integer.
175  * On HP-UX pthread_t is int.
176  * On Cygwin pthread_t is pointer to void.
177  * On z/OS pthread_t is "struct { char __[0x08]; }".
178  */
179 typedef pthread_t  nxt_thread_handle_t;
180 
181 
182 #define                                                                       \
183 nxt_thread_handle_clear(th)                                                   \
184     th = (pthread_t) 0
185 
186 #define                                                                       \
187 nxt_thread_handle_equal(th0, th1)                                             \
188     pthread_equal(th0, th1)
189 
190 
191 #endif /* _NXT_UNIX_THREAD_ID_H_INCLUDED_ */
192