nginx/src/event/ngx_event_timer.c

145 lines
3.1 KiB
C
Raw Normal View History

2003-01-22 01:36:01 +08:00
2003-01-24 02:47:54 +08:00
#include <ngx_config.h>
2003-01-22 01:36:01 +08:00
2003-01-24 02:47:54 +08:00
#include <ngx_core.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
#include <ngx_connection.h>
#include <ngx_event.h>
#include <ngx_event_timer.h>
/* STUB */
2003-01-27 05:08:14 +08:00
#define NGX_TIMER_QUEUE_NUM 5
2003-01-24 02:47:54 +08:00
2003-01-27 05:08:14 +08:00
/* should be per-thread */
static ngx_event_t *ngx_timer_queue;
static int ngx_timer_cur_queue;
/* */
static int ngx_timer_queue_num;
2003-01-24 02:47:54 +08:00
2003-01-27 05:08:14 +08:00
ngx_event_t *ngx_event_init_timer(ngx_log_t *log)
2003-01-24 02:47:54 +08:00
{
int i;
2003-01-27 05:08:14 +08:00
ngx_timer_queue_num = NGX_TIMER_QUEUE_NUM;
2003-01-25 00:09:40 +08:00
ngx_timer_cur_queue = 0;
2003-01-24 02:47:54 +08:00
ngx_test_null(ngx_timer_queue,
2003-01-27 05:08:14 +08:00
ngx_alloc(ngx_timer_queue_num * sizeof(ngx_event_t), log),
NULL);
2003-01-24 02:47:54 +08:00
2003-01-27 05:08:14 +08:00
for (i = 0; i < ngx_timer_queue_num; i++) {
2003-01-24 02:47:54 +08:00
ngx_timer_queue[i].timer_prev = &ngx_timer_queue[i];
ngx_timer_queue[i].timer_next = &ngx_timer_queue[i];
}
2003-01-27 05:08:14 +08:00
return ngx_timer_queue;
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
{
ngx_event_t *e;
#if (NGX_DEBUG_EVENT)
ngx_connection_t *c = (ngx_connection_t *) ev->data;
2003-05-12 23:52:24 +08:00
ngx_log_debug(ev->log, "set timer: %d:%d, slot: %d" _
c->fd _ 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-01-25 00:09:40 +08:00
for (e = ngx_timer_queue[ngx_timer_cur_queue].timer_next;
e != &ngx_timer_queue[ngx_timer_cur_queue] && timer > e->timer_delta;
2003-01-22 01:36:01 +08:00
e = e->timer_next)
{
timer -= e->timer_delta;
}
2003-01-25 00:09:40 +08:00
ngx_timer_cur_queue++;
2003-01-27 05:08:14 +08:00
if (ngx_timer_cur_queue >= ngx_timer_queue_num) {
2003-01-25 00:09:40 +08:00
ngx_timer_cur_queue = 0;
}
2003-01-22 01:36:01 +08:00
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-01-24 14:20:47 +08:00
2003-01-27 05:08:14 +08:00
int ngx_event_find_timer(void)
{
int i;
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;
} else {
return timer;
}
}
2003-01-24 14:20:47 +08:00
void ngx_event_expire_timers(ngx_msec_t timer)
{
int i;
ngx_msec_t delta;
ngx_event_t *ev;
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
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);
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
if (ev->event_handler(ev) == NGX_ERROR) {
ev->close_handler(ev);
}
}
}
}