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-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)
|
|
|
|
{
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
|
|
|
|
ngx_event_timer_sentinel.left = &ngx_event_timer_sentinel;
|
|
|
|
ngx_event_timer_sentinel.right = &ngx_event_timer_sentinel;
|
|
|
|
ngx_event_timer_sentinel.parent = &ngx_event_timer_sentinel;
|
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-06 01:07: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-06 01:07:27 +08:00
|
|
|
if (node == &ngx_event_timer_sentinel) {
|
2003-12-04 22:53:00 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
} else {
|
2003-12-05 15:11:46 +08:00
|
|
|
return (ngx_msec_t)
|
|
|
|
(node->key * NGX_TIMER_RESOLUTION - ngx_elapsed_msec);
|
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-06 01:07: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-06 01:07:27 +08:00
|
|
|
if (node == &ngx_event_timer_sentinel) {
|
2003-12-04 22:53:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
/* TODO: in multithreaded enviroment all timer operations must be
|
2003-07-21 05:15:59 +08:00
|
|
|
protected by the single mutex */
|
|
|
|
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
#if 0
|
2003-07-07 14:11:50 +08:00
|
|
|
static ngx_event_t *ngx_timer_queue, ngx_temp_timer_queue;
|
2003-11-26 04:44:56 +08:00
|
|
|
static int ngx_expire_timers;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static ngx_event_t *ngx_timer_queue;
|
|
|
|
static ngx_msec_t *ngx_timer_delta;
|
2003-01-27 05:08:14 +08:00
|
|
|
static int ngx_timer_cur_queue;
|
|
|
|
static int ngx_timer_queue_num;
|
2003-01-24 02:47:54 +08:00
|
|
|
|
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
int ngx_event_timer_init(ngx_cycle_t *cycle)
|
2003-01-24 02:47:54 +08:00
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_int_t i;
|
|
|
|
ngx_msec_t *new_delta;
|
2003-06-16 02:32:13 +08:00
|
|
|
ngx_event_t *new_queue;
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_event_conf_t *ecf;
|
2003-01-24 02:47:54 +08:00
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
|
2003-05-20 00:39:14 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
if (ngx_timer_queue_num < ecf->timer_queues) {
|
|
|
|
ngx_test_null(new_queue,
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_alloc(ecf->timer_queues * sizeof(ngx_event_t),
|
|
|
|
cycle->log),
|
2003-06-16 02:32:13 +08:00
|
|
|
NGX_ERROR);
|
2003-01-24 02:47:54 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
for (i = 0; i < ngx_timer_queue_num; i++) {
|
|
|
|
new_queue[i] = ngx_timer_queue[i];
|
|
|
|
}
|
2003-01-24 02:47:54 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
if (ngx_timer_queue) {
|
|
|
|
ngx_free(ngx_timer_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_timer_queue = new_queue;
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_test_null(new_delta,
|
|
|
|
ngx_calloc(ecf->timer_queues * sizeof(ngx_msec_t),
|
|
|
|
cycle->log),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
|
|
|
for (i = 0; i < ngx_timer_queue_num; i++) {
|
|
|
|
new_delta[i] = ngx_timer_delta[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_timer_delta) {
|
|
|
|
ngx_free(ngx_timer_delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_timer_delta = new_delta;
|
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
ngx_timer_queue_num = ecf->timer_queues;
|
|
|
|
ngx_timer_cur_queue = 0;
|
|
|
|
|
|
|
|
for (/* void */; i < ngx_timer_queue_num; i++) {
|
|
|
|
ngx_timer_queue[i].timer_prev = &ngx_timer_queue[i];
|
|
|
|
ngx_timer_queue[i].timer_next = &ngx_timer_queue[i];
|
|
|
|
}
|
2003-07-04 23:10:33 +08:00
|
|
|
|
|
|
|
} else if (ngx_timer_queue_num > ecf->timer_queues) {
|
|
|
|
/* STUB */
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "NOT READY: timer");
|
2003-07-04 23:10:33 +08:00
|
|
|
exit(1);
|
2003-01-24 02:47:54 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
#if 0
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_temp_timer_queue.timer_prev = &ngx_temp_timer_queue;
|
|
|
|
ngx_temp_timer_queue.timer_next = &ngx_temp_timer_queue;
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
return NGX_OK;;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
void ngx_event_timer_done(ngx_cycle_t *cycle)
|
2003-05-20 00:39:14 +08:00
|
|
|
{
|
|
|
|
ngx_free(ngx_timer_queue);
|
2003-06-16 02:32:13 +08:00
|
|
|
ngx_timer_queue = NULL;
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
ngx_free(ngx_timer_delta);
|
|
|
|
ngx_timer_delta = NULL;
|
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_timer_queue_num = 0;
|
2003-01-24 02:47:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
|
2003-01-22 01:36:01 +08:00
|
|
|
{
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_event_t *e, *queue;
|
2003-10-13 00:49:16 +08:00
|
|
|
#if (NGX_DEBUG_EVENT)
|
|
|
|
ngx_connection_t *c;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ev->timer_set) {
|
|
|
|
ngx_del_timer(ev);
|
|
|
|
}
|
2003-01-22 01:36:01 +08:00
|
|
|
|
|
|
|
#if (NGX_DEBUG_EVENT)
|
2003-10-13 00:49:16 +08:00
|
|
|
c = ev->data;
|
|
|
|
ngx_log_debug(ev->log, "set timer: %d:%d:%d, slot: %d" _
|
|
|
|
c->fd _ ev->write _ timer _ ngx_timer_cur_queue);
|
2003-01-22 01:36:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ev->timer_next || ev->timer_prev) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
queue = &ngx_timer_queue[ngx_timer_cur_queue];
|
|
|
|
timer += ngx_timer_delta[ngx_timer_cur_queue++];
|
|
|
|
|
|
|
|
if (ngx_timer_cur_queue >= ngx_timer_queue_num) {
|
|
|
|
ngx_timer_cur_queue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2003-07-07 14:11:50 +08:00
|
|
|
if (ngx_expire_timers) {
|
|
|
|
queue = &ngx_temp_timer_queue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
queue = &ngx_timer_queue[ngx_timer_cur_queue++];
|
|
|
|
|
|
|
|
if (ngx_timer_cur_queue >= ngx_timer_queue_num) {
|
|
|
|
ngx_timer_cur_queue = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-07-07 14:11:50 +08:00
|
|
|
|
|
|
|
for (e = queue->timer_next;
|
|
|
|
e != queue && timer > e->timer_delta;
|
2003-01-22 01:36:01 +08:00
|
|
|
e = e->timer_next)
|
|
|
|
{
|
|
|
|
timer -= e->timer_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev->timer_delta = timer;
|
|
|
|
|
|
|
|
ev->timer_next = e;
|
|
|
|
ev->timer_prev = e->timer_prev;
|
|
|
|
|
|
|
|
e->timer_prev->timer_next = ev;
|
|
|
|
e->timer_prev = ev;
|
2003-10-13 00:49:16 +08:00
|
|
|
|
|
|
|
ev->timer_set = 1;
|
|
|
|
|
|
|
|
return;
|
2003-01-22 01:36:01 +08:00
|
|
|
}
|
2003-01-24 14:20:47 +08:00
|
|
|
|
|
|
|
|
2003-01-27 05:08:14 +08:00
|
|
|
int ngx_event_find_timer(void)
|
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_int_t i;
|
2003-01-27 05:08:14 +08:00
|
|
|
ngx_msec_t timer;
|
|
|
|
|
|
|
|
timer = NGX_MAX_MSEC;
|
|
|
|
|
|
|
|
for (i = 0; i < ngx_timer_queue_num; i++) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer == NGX_MAX_MSEC) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-13 00:49:16 +08:00
|
|
|
|
|
|
|
return timer;
|
2003-01-27 05:08:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
void ngx_event_set_timer_delta(ngx_msec_t timer)
|
|
|
|
{
|
|
|
|
ngx_int_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ngx_timer_queue_num; i++) {
|
|
|
|
ngx_timer_delta[i] = timer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* void ngx_event_expire_timers() */
|
2003-01-24 14:20:47 +08:00
|
|
|
void ngx_event_expire_timers(ngx_msec_t timer)
|
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_int_t i;
|
|
|
|
#if 0
|
2003-01-24 14:20:47 +08:00
|
|
|
ngx_msec_t delta;
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-01-24 14:20:47 +08:00
|
|
|
ngx_event_t *ev;
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
#if 0
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_expire_timers = 1;
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2003-01-27 05:08:14 +08:00
|
|
|
for (i = 0; i < ngx_timer_queue_num; i++) {
|
2003-01-24 14:20:47 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
#if 0
|
2003-01-24 14:20:47 +08:00
|
|
|
delta = timer;
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-01-24 14:20:47 +08:00
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
ev = ngx_timer_queue[i].timer_next;
|
|
|
|
|
|
|
|
if (ev == &ngx_timer_queue[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (ev->timer_delta > ngx_timer_delta[i]) {
|
|
|
|
ev->timer_delta -= ngx_timer_delta[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_timer_delta[i] -= ev->timer_delta;
|
|
|
|
|
|
|
|
#if 0
|
2003-01-24 14:20:47 +08:00
|
|
|
if (ev->timer_delta > delta) {
|
|
|
|
ev->timer_delta -= delta;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
delta -= ev->timer_delta;
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-01-24 14:20:47 +08:00
|
|
|
|
|
|
|
ngx_del_timer(ev);
|
2003-05-20 00:39:14 +08:00
|
|
|
|
2003-04-18 01:59:35 +08:00
|
|
|
if (ev->delayed) {
|
|
|
|
ev->delayed = 0;
|
|
|
|
if (ev->ready == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ev->timedout = 1;
|
|
|
|
}
|
2003-01-24 14:20:47 +08:00
|
|
|
|
2003-05-14 00:02:32 +08:00
|
|
|
ev->event_handler(ev);
|
2003-01-24 14:20:47 +08:00
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
ngx_timer_delta[i] = 0;
|
2003-01-24 14:20:47 +08:00
|
|
|
}
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
#if 0
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_expire_timers = 0;
|
|
|
|
|
|
|
|
if (ngx_temp_timer_queue.timer_next == &ngx_temp_timer_queue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = 0;
|
|
|
|
|
|
|
|
while (ngx_temp_timer_queue.timer_next != &ngx_temp_timer_queue) {
|
|
|
|
timer += ngx_temp_timer_queue.timer_next->timer_delta;
|
|
|
|
ev = ngx_temp_timer_queue.timer_next;
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
#if (NGX_DEBUG_EVENT)
|
|
|
|
ngx_log_debug(ev->log, "process temp timer queue");
|
|
|
|
#endif
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_del_timer(ev);
|
|
|
|
ngx_add_timer(ev, timer);
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
#endif
|
2003-01-24 14:20:47 +08:00
|
|
|
}
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|