diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index 4662e816d..792798ef8 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -175,6 +175,22 @@ int ngx_kqueue_process_events(ngx_log_t *log) struct timeval tv; struct timespec ts, *tp; + timer = ngx_event_find_timer(); + + if (timer) { + ts.tv_sec = timer / 1000; + ts.tv_nsec = (timer % 1000) * 1000000; + tp = &ts; + gettimeofday(&tv, NULL); + delta = tv.tv_sec * 1000 + tv.tv_usec / 1000; + + } else { + timer = 0; + delta = 0; + tp = NULL; + } + +#if 0 if (timer_queue.timer_next != &timer_queue) { timer = timer_queue.timer_next->timer_delta; ts.tv_sec = timer / 1000; @@ -188,6 +204,7 @@ int ngx_kqueue_process_events(ngx_log_t *log) delta = 0; tp = NULL; } +#endif #if (NGX_DEBUG_EVENT) ngx_log_debug(log, "kevent timer: %d" _ timer); @@ -267,6 +284,11 @@ int ngx_kqueue_process_events(ngx_log_t *log) } } + if (timer) { + ngx_event_expire_timers(delta); + } + +#if 0 if (timer && timer_queue.timer_next != &timer_queue) { if (delta >= timer_queue.timer_next->timer_delta) { for ( ;; ) { @@ -289,6 +311,7 @@ int ngx_kqueue_process_events(ngx_log_t *log) timer_queue.timer_next->timer_delta -= delta; } } +#endif return NGX_OK; } diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c index 4b5b83c83..5663f5481 100644 --- a/src/event/ngx_event_timer.c +++ b/src/event/ngx_event_timer.c @@ -51,6 +51,9 @@ void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) } n = timer % ngx_timer_hash_size; +#if (NGX_DEBUG_EVENT) + ngx_log_debug(ev->log, "timer slot: %d" _ n); +#endif for (e = ngx_timer_queue[n].timer_next; e != &ngx_timer_queue[n] && timer > e->timer_delta; @@ -67,3 +70,38 @@ void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) e->timer_prev->timer_next = ev; e->timer_prev = ev; } + + +void ngx_event_expire_timers(ngx_msec_t timer) +{ + int i; + ngx_msec_t delta; + ngx_event_t *ev; + + for (i = 0; i < ngx_timer_hash_size; i++) { + + delta = timer; + + for ( ;; ) { + ev = ngx_timer_queue[i].timer_next; + + if (ev == &ngx_timer_queue[i]) { + break; + } + + if (ev->timer_delta > delta) { + ev->timer_delta -= delta; + break; + } + + delta -= ev->timer_delta; + + ngx_del_timer(ev); + ev->timedout = 1; + + if (ev->event_handler(ev) == NGX_ERROR) { + ev->close_handler(ev); + } + } + } +} diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h index 27c301aab..24cb46c0a 100644 --- a/src/event/ngx_event_timer.h +++ b/src/event/ngx_event_timer.h @@ -16,7 +16,7 @@ extern ngx_event_t *ngx_timer_queue; extern int ngx_timer_hash_size; -ngx_inline static int ngx_event_get_timer() +ngx_inline static int ngx_event_find_timer() { int i; ngx_msec_t timer; @@ -24,10 +24,12 @@ ngx_inline static int ngx_event_get_timer() timer = NGX_MAX_MSEC; for (i = 0; i < ngx_timer_hash_size; i++) { - if (ngx_timer_queue[i].timer_next != &ngx_timer_queue[i]) { - if (timer > ngx_timer_queue[i].timer_next->timer_delta) { - timer = ngx_timer_queue[i].timer_next->timer_delta; - } + if (ngx_timer_queue[i].timer_next == &ngx_timer_queue[i]) { + continue; + } + + if (timer > ngx_timer_queue[i].timer_next->timer_delta) { + timer = ngx_timer_queue[i].timer_next->timer_delta; } }