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) {
|
2003-12-04 22:53:00 +08:00
|
|
|
return 0;
|
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
|
|
|
|
|
|
|
return timer > 0 ? timer: -1 ;
|
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;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
2003-12-04 22:53:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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-02-26 04:16:15 +08:00
|
|
|
ngx_mutex_unlock(ngx_event_timer_mutex);
|
|
|
|
|
2003-12-05 15:11:46 +08:00
|
|
|
if ((ngx_msec_t) 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
|
|
|
|
|
|
|
ngx_del_timer(ev);
|
|
|
|
|
|
|
|
if (ev->delayed) {
|
|
|
|
ev->delayed = 0;
|
|
|
|
if (ev->ready == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ev->timedout = 1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (ngx_threaded) {
|
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_post_event(ev);
|
|
|
|
|
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
|
|
continue;
|
|
|
|
}
|
2004-02-26 04:16:15 +08:00
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
ev->event_handler(ev);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|