diff options
author | Igor Sysoev <igor@sysoev.ru> | 2004-02-25 23:16:15 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2004-02-25 23:16:15 +0300 |
commit | f2334416e7485ff1c260a46ba47aeadffeff923a (patch) | |
tree | d4d562a3c0c4af27ed23e9602cad653f1d4fec00 | |
parent | 14dab4563898155799a06007fff9c30c4dac8b26 (diff) |
nginx-0.0.2-2004-02-25-23:16:15 import
-rw-r--r-- | auto/threads | 2 | ||||
-rw-r--r-- | src/core/nginx.c | 8 | ||||
-rw-r--r-- | src/core/ngx_times.c | 29 | ||||
-rw-r--r-- | src/core/ngx_times.h | 3 | ||||
-rw-r--r-- | src/event/modules/ngx_kqueue_module.c | 15 | ||||
-rw-r--r-- | src/event/ngx_event.c | 4 | ||||
-rw-r--r-- | src/event/ngx_event_timer.c | 42 | ||||
-rw-r--r-- | src/event/ngx_event_timer.h | 29 | ||||
-rw-r--r-- | src/os/unix/ngx_freebsd_rfork_thread.c | 15 | ||||
-rw-r--r-- | src/os/unix/ngx_thread.h | 4 |
10 files changed, 120 insertions, 31 deletions
diff --git a/auto/threads b/auto/threads index 0c677f1d6..0c5fd5699 100644 --- a/auto/threads +++ b/auto/threads @@ -2,7 +2,7 @@ if [ $USE_THREADS = "rfork" ]; then have=NGX_THREADS . auto/have - have=USE_RFORK . auto/have + have=NGX_USE_RFORK . auto/have CORE_DEPS="$CORE_DEPS $UNIX_THREADS_DEPS" CORE_SRCS="$CORE_SRCS $FREEBSD_RFORK_SRCS" diff --git a/src/core/nginx.c b/src/core/nginx.c index 592711522..21363d268 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -147,6 +147,12 @@ int main(int argc, char *const *argv, char **envp) ctx.argc = argc; ctx.argv = argv; +#if (NGX_THREADS) + if (ngx_time_mutex_init(log) == NGX_ERROR) { + return 1; + } +#endif + if (ngx_getopt(&ctx, &init_cycle) == NGX_ERROR) { return 1; } @@ -649,7 +655,7 @@ static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data) #if (NGX_THREADS) - if (ngx_init_threads(5, 128 * 1024 * 1024, cycle->log) == NGX_ERROR) { + if (ngx_init_threads(5, 128 * 1024 * 1024, cycle) == NGX_ERROR) { /* fatal */ exit(1); } diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c index 43c8c204a..235e7032d 100644 --- a/src/core/ngx_times.c +++ b/src/core/ngx_times.c @@ -51,15 +51,22 @@ void ngx_time_init() ngx_elapsed_msec = 0; ngx_time_update(tv.tv_sec); +} + + +#if (NGX_THREADS) -#if (NGX_THREADS0) - if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT); - return 0; +ngx_int_t ngx_time_mutex_init(ngx_log_t *log) +{ + if (!(ngx_time_mutex = ngx_mutex_init(log, NGX_MUTEX_LIGHT))) { + return NGX_ERROR; } -#endif + return NGX_OK; } +#endif + void ngx_time_update(time_t s) { @@ -69,9 +76,11 @@ void ngx_time_update(time_t s) return; } -#if (NGX_THREADS0) - if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { - return; +#if (NGX_THREADS) + if (ngx_time_mutex) { + if (ngx_mutex_trylock(ngx_time_mutex) != NGX_OK) { + return; + } } #endif @@ -109,8 +118,10 @@ void ngx_time_update(time_t s) tm.ngx_tm_min, tm.ngx_tm_sec); -#if (NGX_THREADS0) - ngx_mutex_unlock(ngx_time_mutex); +#if (NGX_THREADS) + if (ngx_time_mutex) { + ngx_mutex_unlock(ngx_time_mutex); + } #endif } diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h index 7823af0b8..50910b1b4 100644 --- a/src/core/ngx_times.h +++ b/src/core/ngx_times.h @@ -7,6 +7,9 @@ void ngx_time_init(); +#if (NGX_THREADS) +ngx_int_t ngx_time_mutex_init(ngx_log_t *log); +#endif void ngx_time_update(time_t s); size_t ngx_http_time(char *buf, time_t t); void ngx_gmtime(time_t t, ngx_tm_t *tp); diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index 6f0688428..e89eb9953 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -337,7 +337,7 @@ static int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags) } -static int ngx_kqueue_process_events(ngx_log_t *log) +static ngx_int_t ngx_kqueue_process_events(ngx_log_t *log) { int events; ngx_int_t instance, i; @@ -349,6 +349,19 @@ static int ngx_kqueue_process_events(ngx_log_t *log) struct timespec ts, *tp; timer = ngx_event_find_timer(); + +#if (NGX_THREADS) + if (timer == NGX_TIMER_ERROR) { + return NGX_ERROR; + } + + /* + * TODO: if timer is 0 and any worker thread is still busy + * then set 1 second timeout + */ + +#endif + ngx_old_elapsed_msec = ngx_elapsed_msec; if (timer) { diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c index 98b80b4be..80c65dce4 100644 --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -133,7 +133,9 @@ static int ngx_event_init(ngx_cycle_t *cycle) #endif - ngx_event_timer_init(); + if (ngx_event_timer_init(cycle->log) == NGX_ERROR) { + return NGX_ERROR; + } ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c index e002cb7eb..498fd9fa0 100644 --- a/src/event/ngx_event_timer.c +++ b/src/event/ngx_event_timer.c @@ -4,13 +4,8 @@ #include <ngx_event.h> -/* - * TODO: in multithreaded enviroment all timer operations must be - * protected by the single mutex - */ - #if (NGX_THREADS) -static ngx_mutex_t *ngx_event_timer_mutex; +ngx_mutex_t *ngx_event_timer_mutex; #endif @@ -18,13 +13,20 @@ ngx_rbtree_t *ngx_event_timer_rbtree; ngx_rbtree_t ngx_event_timer_sentinel; -void ngx_event_timer_init(void) +ngx_int_t ngx_event_timer_init(ngx_log_t *log) { if (ngx_event_timer_rbtree) { - return; + ngx_event_timer_mutex->log = log; + return NGX_OK; } ngx_event_timer_rbtree = &ngx_event_timer_sentinel; + + if (!(ngx_event_timer_mutex = ngx_mutex_init(log, 0))) { + return NGX_ERROR; + } + + return NGX_OK; } @@ -36,8 +38,18 @@ ngx_msec_t ngx_event_find_timer(void) return 0; } +#if (NGX_THREADS) + if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { + return NGX_TIMER_ERROR; + } +#endif + node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel); +#if (NGX_THREADS) + ngx_mutex_unlock(ngx_event_timer_mutex); +#endif + return (ngx_msec_t) (node->key * NGX_TIMER_RESOLUTION - ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION); @@ -58,9 +70,19 @@ void ngx_event_expire_timers(ngx_msec_t timer) break; } +#if (NGX_THREADS) + if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { + return; + } +#endif + node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel); +#if (NGX_THREADS) + ngx_mutex_unlock(ngx_event_timer_mutex); +#endif + if ((ngx_msec_t) node->key <= (ngx_msec_t) (ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION) { @@ -79,6 +101,10 @@ void ngx_event_expire_timers(ngx_msec_t timer) ev->timedout = 1; } +#if (NGX_THREADS) + /* STUB: post event */ +#endif + ev->event_handler(ev); continue; } diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h index ee920dc4e..300d79cb7 100644 --- a/src/event/ngx_event_timer.h +++ b/src/event/ngx_event_timer.h @@ -7,6 +7,8 @@ #include <ngx_event.h> +#define NGX_TIMER_ERROR (ngx_msec_t) -1 + /* * 32 bit timer key value resolution * @@ -19,11 +21,16 @@ #define NGX_TIMER_RESOLUTION 1 -void ngx_event_timer_init(void); +ngx_int_t ngx_event_timer_init(ngx_log_t *log); ngx_msec_t ngx_event_find_timer(void); void ngx_event_expire_timers(ngx_msec_t timer); +#if (NGX_THREADS) +extern ngx_mutex_t *ngx_event_timer_mutex; +#endif + + extern ngx_rbtree_t *ngx_event_timer_rbtree; extern ngx_rbtree_t ngx_event_timer_sentinel; @@ -34,9 +41,19 @@ ngx_inline static void ngx_event_del_timer(ngx_event_t *ev) "event timer del: %d: %d", ngx_event_ident(ev->data), ev->rbtree_key); +#if (NGX_THREADS) + if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { + return; + } +#endif + ngx_rbtree_delete(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel, (ngx_rbtree_t *) &ev->rbtree_key); +#if (NGX_THREADS) + ngx_mutex_unlock(ngx_event_timer_mutex); +#endif + #if (NGX_DEBUG) ev->rbtree_left = NULL; ev->rbtree_right = NULL; @@ -64,9 +81,19 @@ ngx_inline static void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) "event timer add: %d: %d", ngx_event_ident(ev->data), ev->rbtree_key); +#if (NGX_THREADS) + if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { + return; + } +#endif + ngx_rbtree_insert(&ngx_event_timer_rbtree, &ngx_event_timer_sentinel, (ngx_rbtree_t *) &ev->rbtree_key); +#if (NGX_THREADS) + ngx_mutex_unlock(ngx_event_timer_mutex); +#endif + ev->timer_set = 1; } diff --git a/src/os/unix/ngx_freebsd_rfork_thread.c b/src/os/unix/ngx_freebsd_rfork_thread.c index 7e28b0da5..4ea8b4c6c 100644 --- a/src/os/unix/ngx_freebsd_rfork_thread.c +++ b/src/os/unix/ngx_freebsd_rfork_thread.c @@ -147,7 +147,7 @@ int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg, } -ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log) +ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle) { size_t len; char *red_zone, *zone; @@ -156,7 +156,7 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log) len = sizeof(usrstack); if (sysctlbyname("kern.usrstack", &usrstack, &len, NULL, 0) == -1) { - ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "sysctlbyname(kern.usrstack) failed"); return NGX_ERROR; } @@ -164,13 +164,13 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log) /* the main thread stack red zone */ red_zone = usrstack - (size + rz_size); - ngx_log_debug2(NGX_LOG_DEBUG_CORE, log, 0, + ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0, "usrstack: " PTR_FMT " red zone: " PTR_FMT, usrstack, red_zone); zone = mmap(red_zone, rz_size, PROT_NONE, MAP_ANON, -1, 0); if (zone == MAP_FAILED) { - ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "mmap(" PTR_FMT ":" SIZE_T_FMT ", PROT_NONE, MAP_ANON) red zone failed", red_zone, rz_size); @@ -178,18 +178,19 @@ ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log) } if (zone != red_zone) { - ngx_log_error(NGX_LOG_ALERT, log, 0, "red zone address was changed"); + ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, + "red zone address was changed"); } /* create the threads errno array */ - if (!(errnos = ngx_calloc(n * sizeof(int), log))) { + if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) { return NGX_ERROR; } /* create the threads tid array */ - if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), log))) { + if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) { return NGX_ERROR; } diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h index 9637fda85..3ecc1e62a 100644 --- a/src/os/unix/ngx_thread.h +++ b/src/os/unix/ngx_thread.h @@ -7,7 +7,7 @@ #if (NGX_THREADS) -#if (USE_RFORK) +#if (NGX_USE_RFORK) #include <sys/ipc.h> #include <sys/sem.h> @@ -46,7 +46,7 @@ typedef pthread_t ngx_tid_t; #endif -ngx_int_t ngx_init_threads(int n, size_t size, ngx_log_t *log); +ngx_int_t ngx_init_threads(int n, size_t size, ngx_cycle_t *cycle); int ngx_create_thread(ngx_tid_t *tid, int (*func)(void *arg), void *arg, ngx_log_t *log); ngx_tid_t ngx_thread_self(); |