From 898446c8bacb5b0e1c223dfec4643ebee8d07f8e Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Thu, 26 Feb 2004 17:10:01 +0000 Subject: [PATCH] nginx-0.0.2-2004-02-26-20:10:01 import --- src/core/ngx_times.c | 10 +-- src/event/modules/ngx_kqueue_module.c | 88 ++++++++++++++++++++++++-- src/event/ngx_event.c | 1 + src/event/ngx_event.h | 5 +- src/os/unix/ngx_freebsd_rfork_thread.c | 12 ++++ src/os/unix/ngx_thread.h | 3 + 6 files changed, 103 insertions(+), 16 deletions(-) diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c index 235e7032d..a9ddb82dc 100644 --- a/src/core/ngx_times.c +++ b/src/core/ngx_times.c @@ -77,10 +77,8 @@ void ngx_time_update(time_t s) } #if (NGX_THREADS) - if (ngx_time_mutex) { - if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { - return; - } + if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { + return; } #endif @@ -119,9 +117,7 @@ void ngx_time_update(time_t s) tm.ngx_tm_sec); #if (NGX_THREADS) - if (ngx_time_mutex) { - ngx_mutex_unlock(ngx_time_mutex); - } + ngx_mutex_unlock(ngx_time_mutex); #endif } diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index e89eb9953..bfdd98bb2 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -413,6 +413,12 @@ static ngx_int_t ngx_kqueue_process_events(ngx_log_t *log) } } +#if (NGX_THREADS0) + if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { + return NGX_ERROR; + } +#endif + for (i = 0; i < events; i++) { ngx_log_debug6(NGX_LOG_DEBUG_EVENT, log, 0, @@ -468,41 +474,109 @@ static ngx_int_t ngx_kqueue_process_events(ngx_log_t *log) ev->ready = 1; - ev->event_handler(ev); - break; case EVFILT_VNODE: ev->kq_vnode = 1; - ev->event_handler(ev); - break; case EVFILT_AIO: ev->complete = 1; ev->ready = 1; - ev->event_handler(ev); - break; - default: ngx_log_error(NGX_LOG_ALERT, log, 0, "unexpected kevent() filter %d", event_list[i].filter); + continue; } + +#if (NGX_THREADS0) + + if (ngx_threaded) { + + if (ev->light) { + + /* the accept event */ + + ngx_mutex_unlock(ngx_posted_events_mutex); + + ev->event_handler(ev); + + if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { + return NGX_ERROR; + } + + } else { + ev->next = ngx_posted_events; + ngx_posted_events = ev; + } + + continue; + } + +#endif + + ev->event_handler(ev); } +#if (NGX_THREADS0) + ngx_mutex_unlock(ngx_posted_events_mutex); +#endif + if (timer && delta) { ngx_event_expire_timers((ngx_msec_t) delta); } +#if (NGX_THREADS0) + if (!ngx_threaded) { + } +#endif + + /* TODO: non-thread mode only */ + + ev = ngx_posted_events; + ngx_posted_events = NULL; + + while (ev) { + ev->event_handler(ev); + ev = ev->next; + } + return NGX_OK; } +#if (NGX_THREADS) + +static void ngx_kqueue_thread_handler(ngx_event_t *ev) +{ + ngx_int_t instance; + + instance = (uintptr_t) ev & 1; + ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1); + + if (ev->active == 0 || ev->instance != instance) { + + /* + * the stale event from a file descriptor + * that was just closed in this iteration + */ + + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, + "kevent: stale event " PTR_FMT, ev); + return; + } + + ev->event_handler(ev); +} + +#endif + + static void *ngx_kqueue_create_conf(ngx_cycle_t *cycle) { ngx_kqueue_conf_t *kcf; diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c index 80c65dce4..820a5f51b 100644 --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -45,6 +45,7 @@ ngx_event_actions_t ngx_event_actions; static int ngx_event_max_module; +ngx_event_t *ngx_posted_events; static ngx_str_t events_name = ngx_string("events"); diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h index 83ce81436..899db18aa 100644 --- a/src/event/ngx_event.h +++ b/src/event/ngx_event.h @@ -27,8 +27,7 @@ struct ngx_event_s { u_int index; - /* queue in mutex(), aio_read(), aio_write() */ - ngx_event_t *prev; + /* the link of the posted queue or the event mutecies queues */ ngx_event_t *next; ngx_log_t *log; @@ -374,6 +373,8 @@ typedef struct { +extern ngx_event_t *ngx_posted_events; + extern int ngx_event_flags; extern ngx_module_t ngx_events_module; extern ngx_module_t ngx_event_core_module; diff --git a/src/os/unix/ngx_freebsd_rfork_thread.c b/src/os/unix/ngx_freebsd_rfork_thread.c index 4ea8b4c6c..dd18168a2 100644 --- a/src/os/unix/ngx_freebsd_rfork_thread.c +++ b/src/os/unix/ngx_freebsd_rfork_thread.c @@ -21,6 +21,8 @@ */ +ngx_int_t ngx_threaded; + static inline int ngx_gettid(); @@ -204,6 +206,8 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle) /* allow the spinlock in libc malloc() */ __isthreaded = 1; + ngx_threaded = 1; + return NGX_OK; } @@ -315,6 +319,10 @@ ngx_int_t ngx_mutex_do_lock(ngx_mutex_t *m, ngx_int_t try) ngx_uint_t tries; struct sembuf op; + if (!ngx_threaded) { + return NGX_OK; + } + #if (NGX_DEBUG) if (try) { ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, @@ -438,6 +446,10 @@ ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m) uint32_t lock, new, old; struct sembuf op; + if (!ngx_threaded) { + return NGX_OK; + } + old = m->lock; if (!(old & NGX_MUTEX_LOCK_BUSY)) { diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h index 3ecc1e62a..3961f328f 100644 --- a/src/os/unix/ngx_thread.h +++ b/src/os/unix/ngx_thread.h @@ -60,6 +60,9 @@ ngx_int_t ngx_mutex_do_lock(ngx_mutex_t *m, ngx_int_t try); ngx_int_t ngx_mutex_unlock(ngx_mutex_t *m); +extern ngx_int_t ngx_threaded; + + #else /* !NGX_THREADS */ #define ngx_log_tid 0