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>
|
|
|
|
|
|
|
|
|
2003-12-15 04:10:27 +08:00
|
|
|
/*
|
|
|
|
* TODO: in multithreaded enviroment all timer operations must be
|
|
|
|
* protected by the single mutex
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
ngx_rbtree_t *ngx_event_timer_rbtree;
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_rbtree_t ngx_event_timer_sentinel;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
int ngx_event_timer_init(ngx_cycle_t *cycle)
|
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
if (ngx_event_timer_rbtree) {
|
2003-12-09 23:08:11 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
|
2003-12-19 16:15:11 +08:00
|
|
|
|
|
|
|
#if 0
|
2003-12-19 20:45:27 +08:00
|
|
|
ngx_event_timer_sentinel.left = &ngx_event_timer_sentinel;
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_event_timer_sentinel.right = &ngx_event_timer_sentinel;
|
|
|
|
ngx_event_timer_sentinel.parent = &ngx_event_timer_sentinel;
|
2003-12-19 16:15:11 +08:00
|
|
|
#endif
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_event_timer_done(ngx_cycle_t *cycle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-05 15:11:46 +08:00
|
|
|
ngx_msec_t ngx_event_find_timer(void)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
node = ngx_rbtree_min(ngx_event_timer_rbtree, &ngx_event_timer_sentinel);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
return (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
|
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;
|
|
|
|
}
|
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
node = ngx_rbtree_min(ngx_event_timer_rbtree,
|
|
|
|
&ngx_event_timer_sentinel);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev->event_handler(ev);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|