2003-01-22 01:36:01 +08:00
|
|
|
|
2003-01-24 02:47:54 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_mutex_t *ngx_event_timer_mutex;
|
2004-02-24 04:57:12 +08:00
|
|
|
#endif
|
|
|
|
|
2003-12-15 04:10:27 +08:00
|
|
|
|
2004-03-02 23:40:59 +08:00
|
|
|
ngx_thread_volatile ngx_rbtree_t *ngx_event_timer_rbtree;
|
|
|
|
ngx_rbtree_t ngx_event_timer_sentinel;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_int_t ngx_event_timer_init(ngx_log_t *log)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
if (ngx_event_timer_rbtree) {
|
2004-03-01 05:03:02 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_event_timer_mutex->log = log;
|
2004-03-01 05:03:02 +08:00
|
|
|
#endif
|
2004-02-26 04:16:15 +08:00
|
|
|
return NGX_OK;
|
2003-12-09 23:08:11 +08:00
|
|
|
}
|
|
|
|
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
|
2004-02-26 04:16:15 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-02-26 04:16:15 +08:00
|
|
|
if (!(ngx_event_timer_mutex = ngx_mutex_init(log, 0))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-03-01 05:03:02 +08:00
|
|
|
#endif
|
2004-02-26 04:16:15 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-05 15:11:46 +08:00
|
|
|
ngx_msec_t ngx_event_find_timer(void)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2004-04-15 01:44:28 +08:00
|
|
|
ngx_msec_t timer;
|
2003-12-04 22:53:00 +08:00
|
|
|
ngx_rbtree_t *node;
|
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
2004-04-15 04:34:05 +08:00
|
|
|
return NGX_TIMER_INFINITE;
|
2003-12-19 20:45:27 +08:00
|
|
|
}
|
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
|
|
|
|
return NGX_TIMER_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree,
|
|
|
|
&ngx_event_timer_sentinel);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
ngx_mutex_unlock(ngx_event_timer_mutex);
|
|
|
|
|
2004-04-15 01:44:28 +08:00
|
|
|
timer = (ngx_msec_t)
|
2003-12-15 04:10:27 +08:00
|
|
|
(node->key * NGX_TIMER_RESOLUTION -
|
|
|
|
ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION);
|
|
|
|
#if 0
|
2003-12-05 15:11:46 +08:00
|
|
|
(node->key * NGX_TIMER_RESOLUTION - ngx_elapsed_msec);
|
2003-12-15 04:10:27 +08:00
|
|
|
#endif
|
2004-04-15 01:44:28 +08:00
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
return timer > 0 ? timer: 0 ;
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_event_expire_timers(ngx_msec_t timer)
|
|
|
|
{
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_rbtree_t *node;
|
|
|
|
|
2004-07-07 00:12:16 +08:00
|
|
|
if (timer < 0) {
|
|
|
|
/* avoid the endless loop if the time goes backward for some reason */
|
|
|
|
timer = 0;
|
|
|
|
}
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
2004-06-29 00:05:02 +08:00
|
|
|
return;
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|
|
|
|
|
2004-02-26 04:16:15 +08:00
|
|
|
if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree,
|
2003-12-19 20:45:27 +08:00
|
|
|
&ngx_event_timer_sentinel);
|
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
if (node->key <= (ngx_msec_t)
|
2003-12-06 01:07:27 +08:00
|
|
|
(ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
|
|
|
ev = (ngx_event_t *)
|
2003-12-05 15:11:46 +08:00
|
|
|
((char *) node - offsetof(ngx_event_t, rbtree_key));
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-07-02 23:54:34 +08:00
|
|
|
|
|
|
|
if (ngx_threaded && ngx_trylock(ev->lock) == 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can not change the timer of the event that is been
|
|
|
|
* handling by another thread. And we can not easy walk
|
|
|
|
* the rbtree to find a next expired timer so we exit the loop.
|
|
|
|
* However it should be rare case when the event that is
|
|
|
|
* been handling has expired timer.
|
|
|
|
*/
|
|
|
|
|
2004-07-07 00:12:16 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"event " PTR_FMT " is busy in expire timers",
|
|
|
|
ev);
|
2004-06-29 00:05:02 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-06-29 05:03:14 +08:00
|
|
|
#endif
|
2004-06-29 00:05:02 +08:00
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"event timer del: %d: %d",
|
|
|
|
ngx_event_ident(ev->data), ev->rbtree_key);
|
|
|
|
|
|
|
|
ngx_rbtree_delete((ngx_rbtree_t **) &ngx_event_timer_rbtree,
|
|
|
|
&ngx_event_timer_sentinel,
|
|
|
|
(ngx_rbtree_t *) &ev->rbtree_key);
|
|
|
|
|
|
|
|
ngx_mutex_unlock(ngx_event_timer_mutex);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2004-06-29 00:05:02 +08:00
|
|
|
#if (NGX_DEBUG)
|
|
|
|
ev->rbtree_left = NULL;
|
|
|
|
ev->rbtree_right = NULL;
|
|
|
|
ev->rbtree_parent = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ev->timer_set = 0;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2004-07-02 23:54:34 +08:00
|
|
|
#if (NGX_THREADS)
|
2004-04-05 04:32:09 +08:00
|
|
|
if (ngx_threaded) {
|
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-07-02 13:47:00 +08:00
|
|
|
ev->posted_timedout = 1;
|
2004-04-05 04:32:09 +08:00
|
|
|
ngx_post_event(ev);
|
|
|
|
|
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
2004-07-02 23:54:34 +08:00
|
|
|
|
|
|
|
ngx_unlock(ev->lock);
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
continue;
|
|
|
|
}
|
2004-07-02 23:54:34 +08:00
|
|
|
#endif
|
2004-02-26 04:16:15 +08:00
|
|
|
|
2004-07-02 13:47:00 +08:00
|
|
|
ev->timedout = 1;
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
ev->event_handler(ev);
|
2004-07-02 13:47:00 +08:00
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
2004-07-02 13:47:00 +08:00
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-06-29 00:05:02 +08:00
|
|
|
|
|
|
|
ngx_mutex_unlock(ngx_event_timer_mutex);
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|