Klaus Demo nginx / 81a432a
nginx-0.0.7-2004-07-06-20:12:16 import Igor Sysoev 18 years ago
13 changed file(s) with 120 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
4646 };
4747
4848 static const char *debug_levels[] = {
49 "debug_core", "debug_alloc", "debug_event", "debug_http"
49 "debug_core", "debug_alloc", "debug_mutex", "debug_event", "debug_http"
5050 };
5151
5252
278278 ngx_log.file = &ngx_stderr;
279279 ngx_log.log_level = NGX_LOG_INFO;
280280
281 #if 0
282 /* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
283 #endif
284
285281 return &ngx_log;
286282 }
287283
301297
302298 ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL);
303299 ngx_test_null(log->file, ngx_conf_open_file(cycle, name), NULL);
304
305 #if 0
306 /* STUB */ log->log_level = NGX_LOG_DEBUG | NGX_LOG_DEBUG_CORE | NGX_LOG_DEBUG_ALLOC | NGX_LOG_DEBUG_EVENT | NGX_LOG_DEBUG_HTTP;
307 #endif
308300
309301 return log;
310302 }
1515 #define NGX_LOG_INFO 7
1616 #define NGX_LOG_DEBUG 8
1717
18 #define NGX_LOG_DEBUG_CORE 0x10
19 #define NGX_LOG_DEBUG_ALLOC 0x20
20 #define NGX_LOG_DEBUG_EVENT 0x40
21 #define NGX_LOG_DEBUG_HTTP 0x80
18 #define NGX_LOG_DEBUG_CORE 0x010
19 #define NGX_LOG_DEBUG_ALLOC 0x020
20 #define NGX_LOG_DEBUG_MUTEX 0x040
21 #define NGX_LOG_DEBUG_EVENT 0x080
22 #define NGX_LOG_DEBUG_HTTP 0x100
2223
2324 #define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE
2425 #define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_HTTP
368368 for ( ;; ) {
369369 timer = ngx_event_find_timer();
370370
371 #if (NGX_THREADS0)
371 #if (NGX_THREADS)
372372 if (timer == NGX_TIMER_ERROR) {
373373 return NGX_ERROR;
374374 }
211211
212212 #if (NGX_THREADS)
213213 if (*(rev->lock)) {
214 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
215 "spinlock event " PTR_FMT " in accept", rev);
214216 ngx_spinlock(rev->lock, 1000);
215217 ngx_unlock(rev->lock);
216218 }
182182 rinstance = rev->returned_instance;
183183 winstance = wev->returned_instance;
184184
185 #if (NGX_THREADS)
186 if (*(rev->lock)) {
187 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0,
188 "spinlock event " PTR_FMT " in connect", rev);
189 ngx_spinlock(rev->lock, 1000);
190 ngx_unlock(rev->lock);
191 }
192 #endif
193
185194 ngx_memzero(c, sizeof(ngx_connection_t));
186195 ngx_memzero(rev, sizeof(ngx_event_t));
187196 ngx_memzero(wev, sizeof(ngx_event_t));
7272
7373 ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
7474 {
75 ngx_tls_t *tls;
7576 ngx_event_t *ev;
77
78 tls = ngx_thread_get_tls();
7679
7780 for ( ;; ) {
7881
120123
121124 ngx_mutex_unlock(ngx_posted_events_mutex);
122125
126 tls->event = ev;
127
123128 ev->event_handler(ev);
124129
125130 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
129134 if (ev->locked) {
130135 ngx_unlock(ev->lock);
131136 }
137
138 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
139 "posted event " PTR_FMT " is done", ev);
132140
133141 break;
134142 }
6767 ngx_event_t *ev;
6868 ngx_rbtree_t *node;
6969
70 if (timer < 0) {
71 /* avoid the endless loop if the time goes backward for some reason */
72 timer = 0;
73 }
74
7075 for ( ;; ) {
7176
7277 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
98103 * been handling has expired timer.
99104 */
100105
106 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
107 "event " PTR_FMT " is busy in expire timers",
108 ev);
101109 break;
102110 }
103111 #endif
655655 * because another thread may reopen the same file descriptor
656656 * before we clean the connection
657657 */
658
658
659659 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
660660
661661 if (c->read->prev) {
2929 */
3030
3131
32 char *ngx_freebsd_kern_usrstack;
33 size_t ngx_thread_stack_size;
34
35
36 static size_t rz_size;
37 static size_t usable_stack_size;
38 static char *last_stack;
39
40 static ngx_uint_t nthreads;
41 static ngx_uint_t max_threads;
42 static ngx_tid_t *tids; /* the threads tids array */
43
32 char *ngx_freebsd_kern_usrstack;
33 size_t ngx_thread_stack_size;
34
35
36 static size_t rz_size;
37 static size_t usable_stack_size;
38 static char *last_stack;
39
40 static ngx_uint_t nthreads;
41 static ngx_uint_t max_threads;
42 static ngx_tid_t *tids; /* the threads tids array */
43 void **ngx_tls; /* the threads tls's array */
4444
4545 /* the thread-safe libc errno */
4646
219219 "red zone address was changed");
220220 }
221221
222 /* create the threads errno array */
222 /* create the threads errno's array */
223223
224224 if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
225225 return NGX_ERROR;
226226 }
227227
228 /* create the threads tid array */
228 /* create the threads tids array */
229229
230230 if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
231231 return NGX_ERROR;
232232 }
233233
234234 tids[0] = ngx_pid;
235
236 /* create the threads tls's array */
237
238 if (!(ngx_tls = ngx_calloc((n + 1) * sizeof(void *), cycle->log))) {
239 return NGX_ERROR;
240 }
241
235242 nthreads = 1;
236243
237244 last_stack = zone + rz_size;
259266 }
260267
261268 return tids[tid];
269 }
270
271
272 ngx_int_t ngx_thread_set_tls(void *value)
273 {
274 ngx_tls[ngx_gettid()] = value;
275
276 return NGX_OK;
262277 }
263278
264279
325340
326341 #if (NGX_DEBUG)
327342 if (try) {
328 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
343 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
329344 "try lock mutex " PTR_FMT " lock:%X", m, m->lock);
330345 } else {
331 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
346 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
332347 "lock mutex " PTR_FMT " lock:%X", m, m->lock);
333348 }
334349 #endif
359374 continue;
360375 }
361376
362 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
377 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
363378 "mutex " PTR_FMT " lock:%X", m, m->lock);
364379
365380 /*
379394
380395 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
381396
382 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
397 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
383398 "wait mutex " PTR_FMT " lock:%X", m, m->lock);
384399
385400 /*
400415 return NGX_ERROR;
401416 }
402417
403 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
418 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
404419 "mutex waked up " PTR_FMT " lock:%X",
405420 m, m->lock);
406421
426441
427442 if (tries++ > 1000) {
428443
429 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
444 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
430445 "mutex " PTR_FMT " is contested", m);
431446
432447 /* the mutex is probably contested so we are giving up now */
438453 }
439454 }
440455
441 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
456 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
442457 "mutex " PTR_FMT " is locked, lock:%X", m, m->lock);
443458
444459 return NGX_OK;
465480 /* free the mutex */
466481
467482 #if 0
468 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0,
483 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
469484 "unlock mutex " PTR_FMT " lock:%X", m, old);
470485 #endif
471486
480495 }
481496
482497 if (m->semid == -1) {
483 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
498 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
484499 "mutex " PTR_FMT " is unlocked", m);
485500
486501 return NGX_OK;
510525
511526 /* wake up the thread that waits on semaphore */
512527
513 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
528 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
514529 "wake up mutex " PTR_FMT "", m);
515530
516531 op.sem_num = 0;
530545 old = m->lock;
531546 }
532547
533 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
548 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
534549 "mutex " PTR_FMT " is unlocked", m);
535550
536551 return NGX_OK;
1313
1414 #define TID_T_FMT PID_T_FMT
1515
16
17 extern void **ngx_tls;
18
19 #define ngx_thread_create_tls() 0
20 #define ngx_thread_create_tls_n ""
21 #define ngx_thread_get_tls() ngx_tls[ngx_gettid()]
22 ngx_int_t ngx_thread_set_tls(void *value);
23
1624
1725 #define NGX_MUTEX_LIGHT 1
1826
825825 {
826826 ngx_thread_t *thr = data;
827827
828 sigset_t set;
829 ngx_err_t err;
830 struct timeval tv;
828 sigset_t set;
829 ngx_err_t err;
830 ngx_tls_t *tls;
831 struct timeval tv;
831832
832833 thr->cv->tid = ngx_thread_self();
833834
848849
849850 ngx_setthrtitle("worker thread");
850851
852 if (!(tls = ngx_calloc(sizeof(ngx_tls_t), ngx_cycle->log))) {
853 return (void *) 1;
854 }
855
856 err = ngx_thread_create_tls();
857 if (err != 0) {
858 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
859 ngx_thread_create_tls_n " failed");
860 return (void *) 1;
861 }
862
863 ngx_thread_set_tls(tls);
864
851865 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
852866 return (void *) 1;
853867 }
115115 return NGX_OK;
116116 }
117117
118 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "lock mutex " PTR_FMT, m);
118 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "lock mutex " PTR_FMT, m);
119119
120120 err = pthread_mutex_lock(&m->mutex);
121121
125125 return NGX_ERROR;
126126 }
127127
128 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
128 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
129129 "mutex " PTR_FMT " is locked", m);
130130
131131 return NGX_OK;
140140 return NGX_OK;
141141 }
142142
143 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "try lock mutex " PTR_FMT, m);
143 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
144 "try lock mutex " PTR_FMT, m);
144145
145146 err = pthread_mutex_trylock(&m->mutex);
146147
150151 return NGX_ERROR;
151152 }
152153
153 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
154 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
154155 "mutex " PTR_FMT " is locked", m);
155156
156157 return NGX_OK;
165166 return NGX_OK;
166167 }
167168
168 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "unlock mutex " PTR_FMT, m);
169 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "unlock mutex " PTR_FMT, m);
169170
170171 err = pthread_mutex_unlock(&m->mutex);
171172
175176 return NGX_ERROR;
176177 }
177178
178 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
179 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
179180 "mutex " PTR_FMT " is unlocked", m);
180181
181182 return NGX_OK;
238239 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
239240 "cv " PTR_FMT " is waked up", cv);
240241
241 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0,
242 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
242243 "mutex " PTR_FMT " is locked", m);
243244
244245 return NGX_OK;
2424 #define ngx_log_tid (int) ngx_thread_self()
2525
2626 #define TID_T_FMT PTR_FMT
27
28
29 #define ngx_thread_create_tls() pthread_key_create(0, NULL)
30 #define ngx_thread_create_tls_n "pthread_key_create(0, NULL)"
31 #define ngx_thread_get_tls() pthread_getspecific(0)
32 #define ngx_thread_set_tls(v) pthread_setspecific(0, v)
2733
2834
2935 #define NGX_MUTEX_LIGHT 0
105111 #endif
106112
107113
114 typedef struct {
115 ngx_event_t *event;
116 } ngx_tls_t;
117
118
119
108120 #endif /* _NGX_THREAD_H_INCLUDED_ */