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 /src/event | |
parent | 14dab4563898155799a06007fff9c30c4dac8b26 (diff) |
nginx-0.0.2-2004-02-25-23:16:15 import
Diffstat (limited to 'src/event')
-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 |
4 files changed, 79 insertions, 11 deletions
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; } |