nginx-0.0.7-2004-07-06-20:12:16 import

This commit is contained in:
Igor Sysoev 2004-07-06 16:12:16 +00:00
parent 40e877165a
commit 81a432a185
13 changed files with 117 additions and 47 deletions

View File

@ -47,7 +47,7 @@ static const char *err_levels[] = {
}; };
static const char *debug_levels[] = { static const char *debug_levels[] = {
"debug_core", "debug_alloc", "debug_event", "debug_http" "debug_core", "debug_alloc", "debug_mutex", "debug_event", "debug_http"
}; };
@ -279,10 +279,6 @@ ngx_log_t *ngx_log_init_errlog()
ngx_log.file = &ngx_stderr; ngx_log.file = &ngx_stderr;
ngx_log.log_level = NGX_LOG_INFO; ngx_log.log_level = NGX_LOG_INFO;
#if 0
/* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
#endif
return &ngx_log; return &ngx_log;
} }
@ -303,10 +299,6 @@ ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args)
ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL); ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL);
ngx_test_null(log->file, ngx_conf_open_file(cycle, name), NULL); ngx_test_null(log->file, ngx_conf_open_file(cycle, name), NULL);
#if 0
/* STUB */ log->log_level = NGX_LOG_DEBUG | NGX_LOG_DEBUG_CORE | NGX_LOG_DEBUG_ALLOC | NGX_LOG_DEBUG_EVENT | NGX_LOG_DEBUG_HTTP;
#endif
return log; return log;
} }

View File

@ -16,10 +16,11 @@
#define NGX_LOG_INFO 7 #define NGX_LOG_INFO 7
#define NGX_LOG_DEBUG 8 #define NGX_LOG_DEBUG 8
#define NGX_LOG_DEBUG_CORE 0x10 #define NGX_LOG_DEBUG_CORE 0x010
#define NGX_LOG_DEBUG_ALLOC 0x20 #define NGX_LOG_DEBUG_ALLOC 0x020
#define NGX_LOG_DEBUG_EVENT 0x40 #define NGX_LOG_DEBUG_MUTEX 0x040
#define NGX_LOG_DEBUG_HTTP 0x80 #define NGX_LOG_DEBUG_EVENT 0x080
#define NGX_LOG_DEBUG_HTTP 0x100
#define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE #define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE
#define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_HTTP #define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_HTTP

View File

@ -369,7 +369,7 @@ static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle)
for ( ;; ) { for ( ;; ) {
timer = ngx_event_find_timer(); timer = ngx_event_find_timer();
#if (NGX_THREADS0) #if (NGX_THREADS)
if (timer == NGX_TIMER_ERROR) { if (timer == NGX_TIMER_ERROR) {
return NGX_ERROR; return NGX_ERROR;
} }

View File

@ -212,6 +212,8 @@ void ngx_event_accept(ngx_event_t *ev)
#if (NGX_THREADS) #if (NGX_THREADS)
if (*(rev->lock)) { if (*(rev->lock)) {
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"spinlock event " PTR_FMT " in accept", rev);
ngx_spinlock(rev->lock, 1000); ngx_spinlock(rev->lock, 1000);
ngx_unlock(rev->lock); ngx_unlock(rev->lock);
} }

View File

@ -183,6 +183,15 @@ int ngx_event_connect_peer(ngx_peer_connection_t *pc)
rinstance = rev->returned_instance; rinstance = rev->returned_instance;
winstance = wev->returned_instance; winstance = wev->returned_instance;
#if (NGX_THREADS)
if (*(rev->lock)) {
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0,
"spinlock event " PTR_FMT " in connect", rev);
ngx_spinlock(rev->lock, 1000);
ngx_unlock(rev->lock);
}
#endif
ngx_memzero(c, sizeof(ngx_connection_t)); ngx_memzero(c, sizeof(ngx_connection_t));
ngx_memzero(rev, sizeof(ngx_event_t)); ngx_memzero(rev, sizeof(ngx_event_t));
ngx_memzero(wev, sizeof(ngx_event_t)); ngx_memzero(wev, sizeof(ngx_event_t));

View File

@ -73,8 +73,11 @@ void ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle) ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
{ {
ngx_tls_t *tls;
ngx_event_t *ev; ngx_event_t *ev;
tls = ngx_thread_get_tls();
for ( ;; ) { for ( ;; ) {
ev = (ngx_event_t *) ngx_posted_events; ev = (ngx_event_t *) ngx_posted_events;
@ -121,6 +124,8 @@ ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
ngx_mutex_unlock(ngx_posted_events_mutex); ngx_mutex_unlock(ngx_posted_events_mutex);
tls->event = ev;
ev->event_handler(ev); ev->event_handler(ev);
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
@ -131,6 +136,9 @@ ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
ngx_unlock(ev->lock); ngx_unlock(ev->lock);
} }
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"posted event " PTR_FMT " is done", ev);
break; break;
} }
} }

View File

@ -68,6 +68,11 @@ void ngx_event_expire_timers(ngx_msec_t timer)
ngx_event_t *ev; ngx_event_t *ev;
ngx_rbtree_t *node; ngx_rbtree_t *node;
if (timer < 0) {
/* avoid the endless loop if the time goes backward for some reason */
timer = 0;
}
for ( ;; ) { for ( ;; ) {
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
@ -99,6 +104,9 @@ void ngx_event_expire_timers(ngx_msec_t timer)
* been handling has expired timer. * been handling has expired timer.
*/ */
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"event " PTR_FMT " is busy in expire timers",
ev);
break; break;
} }
#endif #endif

View File

@ -656,7 +656,7 @@ void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p)
* because another thread may reopen the same file descriptor * because another thread may reopen the same file descriptor
* before we clean the connection * before we clean the connection
*/ */
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) { if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
if (c->read->prev) { if (c->read->prev) {

View File

@ -30,18 +30,18 @@
*/ */
char *ngx_freebsd_kern_usrstack; char *ngx_freebsd_kern_usrstack;
size_t ngx_thread_stack_size; size_t ngx_thread_stack_size;
static size_t rz_size; static size_t rz_size;
static size_t usable_stack_size; static size_t usable_stack_size;
static char *last_stack; static char *last_stack;
static ngx_uint_t nthreads;
static ngx_uint_t max_threads;
static ngx_tid_t *tids; /* the threads tids array */
static ngx_uint_t nthreads;
static ngx_uint_t max_threads;
static ngx_tid_t *tids; /* the threads tids array */
void **ngx_tls; /* the threads tls's array */
/* the thread-safe libc errno */ /* the thread-safe libc errno */
@ -220,19 +220,26 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle)
"red zone address was changed"); "red zone address was changed");
} }
/* create the threads errno array */ /* create the threads errno's array */
if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) { if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
return NGX_ERROR; return NGX_ERROR;
} }
/* create the threads tid array */ /* create the threads tids array */
if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) { if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
return NGX_ERROR; return NGX_ERROR;
} }
tids[0] = ngx_pid; tids[0] = ngx_pid;
/* create the threads tls's array */
if (!(ngx_tls = ngx_calloc((n + 1) * sizeof(void *), cycle->log))) {
return NGX_ERROR;
}
nthreads = 1; nthreads = 1;
last_stack = zone + rz_size; last_stack = zone + rz_size;
@ -263,6 +270,14 @@ ngx_tid_t ngx_thread_self()
} }
ngx_int_t ngx_thread_set_tls(void *value)
{
ngx_tls[ngx_gettid()] = value;
return NGX_OK;
}
ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags) ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags)
{ {
ngx_mutex_t *m; ngx_mutex_t *m;
@ -326,10 +341,10 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
#if (NGX_DEBUG) #if (NGX_DEBUG)
if (try) { if (try) {
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"try lock mutex " PTR_FMT " lock:%X", m, m->lock); "try lock mutex " PTR_FMT " lock:%X", m, m->lock);
} else { } else {
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"lock mutex " PTR_FMT " lock:%X", m, m->lock); "lock mutex " PTR_FMT " lock:%X", m, m->lock);
} }
#endif #endif
@ -360,7 +375,7 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
continue; continue;
} }
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " lock:%X", m, m->lock); "mutex " PTR_FMT " lock:%X", m, m->lock);
/* /*
@ -380,7 +395,7 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
if (ngx_atomic_cmp_set(&m->lock, old, lock)) { if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"wait mutex " PTR_FMT " lock:%X", m, m->lock); "wait mutex " PTR_FMT " lock:%X", m, m->lock);
/* /*
@ -401,7 +416,7 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
return NGX_ERROR; return NGX_ERROR;
} }
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex waked up " PTR_FMT " lock:%X", "mutex waked up " PTR_FMT " lock:%X",
m, m->lock); m, m->lock);
@ -427,7 +442,7 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
if (tries++ > 1000) { if (tries++ > 1000) {
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is contested", m); "mutex " PTR_FMT " is contested", m);
/* the mutex is probably contested so we are giving up now */ /* the mutex is probably contested so we are giving up now */
@ -439,7 +454,7 @@ ngx_int_t ngx_mutex_dolock(ngx_mutex_t *m, ngx_int_t try)
} }
} }
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is locked, lock:%X", m, m->lock); "mutex " PTR_FMT " is locked, lock:%X", m, m->lock);
return NGX_OK; return NGX_OK;
@ -466,7 +481,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
/* free the mutex */ /* free the mutex */
#if 0 #if 0
ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"unlock mutex " PTR_FMT " lock:%X", m, old); "unlock mutex " PTR_FMT " lock:%X", m, old);
#endif #endif
@ -481,7 +496,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
} }
if (m->semid == -1) { if (m->semid == -1) {
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is unlocked", m); "mutex " PTR_FMT " is unlocked", m);
return NGX_OK; return NGX_OK;
@ -511,7 +526,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
/* wake up the thread that waits on semaphore */ /* wake up the thread that waits on semaphore */
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"wake up mutex " PTR_FMT "", m); "wake up mutex " PTR_FMT "", m);
op.sem_num = 0; op.sem_num = 0;
@ -531,7 +546,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
old = m->lock; old = m->lock;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is unlocked", m); "mutex " PTR_FMT " is unlocked", m);
return NGX_OK; return NGX_OK;

View File

@ -15,6 +15,14 @@ typedef pid_t ngx_tid_t;
#define TID_T_FMT PID_T_FMT #define TID_T_FMT PID_T_FMT
extern void **ngx_tls;
#define ngx_thread_create_tls() 0
#define ngx_thread_create_tls_n ""
#define ngx_thread_get_tls() ngx_tls[ngx_gettid()]
ngx_int_t ngx_thread_set_tls(void *value);
#define NGX_MUTEX_LIGHT 1 #define NGX_MUTEX_LIGHT 1
#define NGX_MUTEX_LOCK_BUSY 0x80000000 #define NGX_MUTEX_LOCK_BUSY 0x80000000

View File

@ -826,9 +826,10 @@ static void* ngx_worker_thread_cycle(void *data)
{ {
ngx_thread_t *thr = data; ngx_thread_t *thr = data;
sigset_t set; sigset_t set;
ngx_err_t err; ngx_err_t err;
struct timeval tv; ngx_tls_t *tls;
struct timeval tv;
thr->cv->tid = ngx_thread_self(); thr->cv->tid = ngx_thread_self();
@ -849,6 +850,19 @@ static void* ngx_worker_thread_cycle(void *data)
ngx_setthrtitle("worker thread"); ngx_setthrtitle("worker thread");
if (!(tls = ngx_calloc(sizeof(ngx_tls_t), ngx_cycle->log))) {
return (void *) 1;
}
err = ngx_thread_create_tls();
if (err != 0) {
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
ngx_thread_create_tls_n " failed");
return (void *) 1;
}
ngx_thread_set_tls(tls);
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
return (void *) 1; return (void *) 1;
} }

View File

@ -116,7 +116,7 @@ ngx_int_t ngx_mutex_lock(ngx_mutex_t *m)
return NGX_OK; return NGX_OK;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "lock mutex " PTR_FMT, m); ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "lock mutex " PTR_FMT, m);
err = pthread_mutex_lock(&m->mutex); err = pthread_mutex_lock(&m->mutex);
@ -126,7 +126,7 @@ ngx_int_t ngx_mutex_lock(ngx_mutex_t *m)
return NGX_ERROR; return NGX_ERROR;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is locked", m); "mutex " PTR_FMT " is locked", m);
return NGX_OK; return NGX_OK;
@ -141,7 +141,8 @@ ngx_int_t ngx_mutex_trylock(ngx_mutex_t *m)
return NGX_OK; return NGX_OK;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "try lock mutex " PTR_FMT, m); ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"try lock mutex " PTR_FMT, m);
err = pthread_mutex_trylock(&m->mutex); err = pthread_mutex_trylock(&m->mutex);
@ -151,7 +152,7 @@ ngx_int_t ngx_mutex_trylock(ngx_mutex_t *m)
return NGX_ERROR; return NGX_ERROR;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is locked", m); "mutex " PTR_FMT " is locked", m);
return NGX_OK; return NGX_OK;
@ -166,7 +167,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
return NGX_OK; return NGX_OK;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, "unlock mutex " PTR_FMT, m); ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0, "unlock mutex " PTR_FMT, m);
err = pthread_mutex_unlock(&m->mutex); err = pthread_mutex_unlock(&m->mutex);
@ -176,7 +177,7 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m)
return NGX_ERROR; return NGX_ERROR;
} }
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is unlocked", m); "mutex " PTR_FMT " is unlocked", m);
return NGX_OK; return NGX_OK;
@ -239,7 +240,7 @@ ngx_int_t ngx_cond_wait(ngx_cond_t *cv, ngx_mutex_t *m)
ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_CORE, cv->log, 0,
"cv " PTR_FMT " is waked up", cv); "cv " PTR_FMT " is waked up", cv);
ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
"mutex " PTR_FMT " is locked", m); "mutex " PTR_FMT " is locked", m);
return NGX_OK; return NGX_OK;

View File

@ -27,6 +27,12 @@ typedef pthread_t ngx_tid_t;
#define TID_T_FMT PTR_FMT #define TID_T_FMT PTR_FMT
#define ngx_thread_create_tls() pthread_key_create(0, NULL)
#define ngx_thread_create_tls_n "pthread_key_create(0, NULL)"
#define ngx_thread_get_tls() pthread_getspecific(0)
#define ngx_thread_set_tls(v) pthread_setspecific(0, v)
#define NGX_MUTEX_LIGHT 0 #define NGX_MUTEX_LIGHT 0
typedef struct { typedef struct {
@ -106,4 +112,10 @@ ngx_int_t ngx_cond_signal(ngx_cond_t *cv);
#endif #endif
typedef struct {
ngx_event_t *event;
} ngx_tls_t;
#endif /* _NGX_THREAD_H_INCLUDED_ */ #endif /* _NGX_THREAD_H_INCLUDED_ */