-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_thread.c
executable file
·339 lines (267 loc) · 8.55 KB
/
ngx_thread.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "ngx_atomic.h"
#include "ngx_thread.h"
#include "flog.h"
/*
* All modern pthread mutex implementations try to acquire a lock
* atomically in userland before going to sleep in kernel. Some
* spins before the sleeping.
*
* In Solaris since version 8 all mutex types spin before sleeping.
* The default spin count is 1000. It can be overridden using
* _THREAD_ADAPTIVE_SPIN=100 environment variable.
*
* In MacOSX all mutex types spin to acquire a lock protecting a mutex's
* internals. If the mutex is busy, thread calls Mach semaphore_wait().
*
*
* PTHREAD_MUTEX_NORMAL lacks deadlock detection and is the fastest
* mutex type.
*
* Linux: No spinning. The internal name PTHREAD_MUTEX_TIMED_NP
* remains from the times when pthread_mutex_timedlock() was
* non-standard extension. Alias name: PTHREAD_MUTEX_FAST_NP.
* FreeBSD: No spinning.
*
*
* PTHREAD_MUTEX_ERRORCHECK is usually as fast as PTHREAD_MUTEX_NORMAL
* yet has lightweight deadlock detection.
*
* Linux: No spinning. The internal name: PTHREAD_MUTEX_ERRORCHECK_NP.
* FreeBSD: No spinning.
*
*
* PTHREAD_MUTEX_RECURSIVE allows recursive locking.
*
* Linux: No spinning. The internal name: PTHREAD_MUTEX_RECURSIVE_NP.
* FreeBSD: No spinning.
*
*
* PTHREAD_MUTEX_ADAPTIVE_NP spins on SMP systems before sleeping.
*
* Linux: No deadlock detection. Dynamically changes a spin count
* for each mutex from 10 to 100 based on spin count taken
* previously.
* FreeBSD: Deadlock detection. The default spin count is 2000.
* It can be overriden using LIBPTHREAD_SPINLOOPS environment
* variable or by pthread_mutex_setspinloops_np(). If a lock
* is still busy, sched_yield() can be called on both UP and
* SMP systems. The default yield loop count is zero, but
* it can be set by LIBPTHREAD_YIELDLOOPS environment
* variable or by pthread_mutex_setyieldloops_np().
* Solaris: No PTHREAD_MUTEX_ADAPTIVE_NP.
* MacOSX: No PTHREAD_MUTEX_ADAPTIVE_NP.
*
*
* PTHREAD_MUTEX_ELISION_NP is a Linux extension to elide locks using
* Intel Restricted Transactional Memory. It is the most suitable for
* rwlock pattern access because it allows simultaneous reads without lock.
* Supported since glibc 2.18.
*
*
* PTHREAD_MUTEX_DEFAULT is default mutex type.
*
* Linux: PTHREAD_MUTEX_NORMAL.
* FreeBSD: PTHREAD_MUTEX_ERRORCHECK.
* Solaris: PTHREAD_MUTEX_NORMAL.
* MacOSX: PTHREAD_MUTEX_NORMAL.
*/
ngx_int_t
ngx_thread_mutex_create(ngx_thread_mutex_t *mtx)
{
ngx_err_t err;
pthread_mutexattr_t attr;
err = pthread_mutexattr_init(&attr);
if (err != 0) {
//ngx_log_error(NGX_LOG_EMERG, log, err,
// "pthread_mutexattr_init() failed");
LOG_ERROR("pthread_mutexattr_init() failed");
return NGX_ERROR;
}
err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (err != 0) {
//ngx_log_error(NGX_LOG_EMERG, log, err,
// "pthread_mutexattr_settype"
// "(PTHREAD_MUTEX_ERRORCHECK) failed");
LOG_ERROR("pthread_mutexattr_settype() failed");
return NGX_ERROR;
}
err = pthread_mutex_init(mtx, &attr);
if (err != 0) {
//ngx_log_error(NGX_LOG_EMERG, log, err,
// "pthread_mutex_init() failed");
LOG_ERROR("pthread_mutex_init() failed");
return NGX_ERROR;
}
err = pthread_mutexattr_destroy(&attr);
if (err != 0) {
//ngx_log_error(NGX_LOG_ALERT, log, err,
// "pthread_mutexattr_destroy() failed");
LOG_ERROR("pthread_mutexattr_destroy() failed");
}
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_mutex_init(%p)", mtx);
LOG_DEBUG("pthread_mutex_init(%p)", mtx);
return NGX_OK;
}
ngx_int_t
ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx)
{
ngx_err_t err;
err = pthread_mutex_destroy(mtx);
if (err != 0) {
//ngx_log_error(NGX_LOG_ALERT, log, err,
// "pthread_mutex_destroy() failed");
LOG_ERROR("pthread_mutex_destroy() failed");
return NGX_ERROR;
}
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_mutex_destroy(%p)", mtx);
LOG_DEBUG("pthread_mutex_destroy(%p)", mtx);
return NGX_OK;
}
ngx_int_t
ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx)
{
ngx_err_t err;
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_mutex_lock(%p) enter", mtx);
LOG_DEBUG("pthread_mutex_lock(%p)", mtx);
err = pthread_mutex_lock(mtx);
if (err == 0) {
return NGX_OK;
}
//ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_lock() failed");
LOG_ERROR("pthread_mutex_lock() failed");
return NGX_ERROR;
}
ngx_int_t
ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx)
{
ngx_err_t err;
err = pthread_mutex_unlock(mtx);
#if 0
ngx_time_update();
#endif
if (err == 0) {
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_mutex_unlock(%p) exit", mtx);
LOG_DEBUG("pthread_mutex_lock(%p)", mtx);
return NGX_OK;
}
//ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_unlock() failed");
LOG_ERROR("pthread_mutex_unlock() failed");
return NGX_ERROR;
}
//-----thread_cond-------
ngx_int_t
ngx_thread_cond_create(ngx_thread_cond_t *cond)
{
ngx_err_t err;
err = pthread_cond_init(cond, NULL);
if (err == 0) {
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_cond_init(%p)", cond);
LOG_DEBUG("pthread_mutex_lock(%p)", cond);
return NGX_OK;
}
//ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_init() failed");
LOG_ERROR("pthread_cond_init() failed");
return NGX_ERROR;
}
ngx_int_t
ngx_thread_cond_destroy(ngx_thread_cond_t *cond)
{
ngx_err_t err;
err = pthread_cond_destroy(cond);
if (err == 0) {
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_cond_destroy(%p)", cond);
LOG_DEBUG("pthread_cond_destroy(%p)", cond);
return NGX_OK;
}
//ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_destroy() failed");
LOG_ERROR("pthread_cond_destroy() failed");
return NGX_ERROR;
}
ngx_int_t
ngx_thread_cond_signal(ngx_thread_cond_t *cond)
{
ngx_err_t err;
err = pthread_cond_signal(cond);
if (err == 0) {
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_cond_signal(%p)", cond);
LOG_DEBUG("pthread_cond_signal(%p)", cond);
return NGX_OK;
}
//ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_signal() failed");
LOG_ERROR("pthread_cond_signal() failed");
return NGX_ERROR;
}
ngx_int_t
ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx)
{
ngx_err_t err;
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_cond_wait(%p) enter", cond);
LOG_DEBUG("pthread_cond_wait(%p) enter", cond);
err = pthread_cond_wait(cond, mtx);
#if 0
ngx_time_update();
#endif
if (err == 0) {
//ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
// "pthread_cond_wait(%p) exit", cond);
LOG_DEBUG("pthread_cond_wait(%p) exit", cond);
return NGX_OK;
}
//ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_cond_wait() failed");
LOG_ERROR("pthread_cond_wait() failed");
return NGX_ERROR;
}
//--------------ngx_thread_tid-----------
#if (NGX_LINUX)
/*
* Linux thread id is a pid of thread created by clone(2),
* glibc does not provide a wrapper for gettid().
*/
ngx_tid_t
ngx_thread_tid(void)
{
return syscall(SYS_gettid);
}
#else
ngx_tid_t
ngx_thread_tid(void)
{
return (uint64_t) (uintptr_t) pthread_self();
}
#endif
//spinlock
void
ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin)
{
#if (NGX_HAVE_ATOMIC_OPS)
ngx_uint_t i, n;
for ( ;; ) {
if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
return;
}
if (ngx_ncpu > 1) {
for (n = 1; n < spin; n <<= 1) {
for (i = 0; i < n; i++) {
ngx_cpu_pause();
}
if (*lock == 0 && ngx_atomic_cmp_set(lock, 0, value)) {
return;
}
}
}
ngx_sched_yield();
}
#else
//#if (NGX_THREADS)
#error ngx_spinlock() or ngx_atomic_cmp_set() are not defined !
//#endif
#endif
}