summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_thread_id.h
blob: 3263a47a8246ae779f0d42f86f5581b42f9e39dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NXT_UNIX_THREAD_ID_H_INCLUDED_
#define _NXT_UNIX_THREAD_ID_H_INCLUDED_


#if (NXT_LINUX)

/*
 * 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)

/*
 * 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)

/*
 * 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_OPENBSD)

typedef pid_t  nxt_tid_t;

/* OpenBSD 3.9 getthrid(). */

nxt_inline nxt_tid_t
nxt_thread_get_tid(void)
{
    return getthrid();
}

#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.
 */

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


NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr);


/*
 * On Linux pthread_t is unsigned long integer.
 * On FreeBSD, MacOSX, NetBSD, and OpenBSD pthread_t is pointer to a struct.
 * On Solaris and AIX pthread_t is unsigned integer.
 * On HP-UX pthread_t is int.
 * On Cygwin pthread_t is pointer to void.
 * On z/OS pthread_t is "struct { char __[0x08]; }".
 */
typedef pthread_t  nxt_thread_handle_t;


#define                                                                       \
nxt_thread_handle_clear(th)                                                   \
    th = (pthread_t) 0

#define                                                                       \
nxt_thread_handle_equal(th0, th1)                                             \
    pthread_equal(th0, th1)


#endif /* _NXT_UNIX_THREAD_ID_H_INCLUDED_ */