nginx-0.0.1-2003-01-27-00:08:14 import
diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c
index ad44743..13971ad 100644
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -29,7 +29,7 @@
 
 static ngx_event_t    **change_index;
 
-static ngx_event_t      timer_queue;
+static ngx_event_t     *timer_queue;
 /* */
 
 
@@ -55,13 +55,15 @@
                   ngx_alloc(sizeof(ngx_event_t *) * DEVPOLL_NCHANGES, log),
                   NGX_ERROR);
 
-    timer_queue.timer_prev = &timer_queue;
-    timer_queue.timer_next = &timer_queue;
+    timer_queue = ngx_event_init_timer(log);
+    if (timer_queue == NULL) {
+        return NGX_ERROR;
+    }
 
 #if !(USE_DEVPOLL)
     ngx_event_actions.add = ngx_devpoll_add_event;
     ngx_event_actions.del = ngx_devpoll_del_event;
-    ngx_event_actions.timer = ngx_devpoll_add_timer;
+    ngx_event_actions.timer = ngx_event_add_timer;
     ngx_event_actions.process = ngx_devpoll_process_events;
 #endif
 
@@ -116,6 +118,8 @@
         return NGX_OK;
     }
 
+    /* we need to restore second event if it exists */
+
     if (event == NGX_READ_EVENT) {
         e = c->write;
         event = POLLOUT;
@@ -186,15 +190,16 @@
 int ngx_devpoll_process_events(ngx_log_t *log)
 {
     int                events, n, i;
-    u_int              timer, delta;
+    ngx_msec_t         timer, delta;
     ngx_err_t          err;
     ngx_event_t       *ev;
     ngx_connection_t  *c;
     struct dvpoll      dvp;
     struct timeval     tv;
 
-    if (timer_queue.timer_next != &timer_queue) {
-        timer = timer_queue.timer_next->timer_delta;
+    timer = ngx_event_find_timer();
+
+    if (timer) {
         gettimeofday(&tv, NULL);
         delta = tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
@@ -300,59 +305,9 @@
         }
     }
 
-    if (timer != INFTIM && timer_queue.timer_next != &timer_queue) {
-        if (delta >= timer_queue.timer_next->timer_delta) {
-            for ( ;; ) {
-                ev = timer_queue.timer_next;
-
-                if (ev == &timer_queue || delta < ev->timer_delta) {
-                    break;
-                }
-
-                delta -= ev->timer_delta;
-
-                ngx_del_timer(ev);
-                ev->timedout = 1;
-                if (ev->event_handler(ev) == NGX_ERROR) {
-                    ev->close_handler(ev);
-                }
-            }
-
-        } else {
-           timer_queue.timer_next->timer_delta -= delta;
-        }
+    if (timer != INFTIM) {
+        ngx_event_expire_timers(delta);
     }
 
     return NGX_OK;
 }
-
-
-void ngx_devpoll_add_timer(ngx_event_t *ev, ngx_msec_t timer)
-{
-    ngx_event_t *e;
-
-#if (NGX_DEBUG_EVENT)
-    ngx_connection_t *c = (ngx_connection_t *) ev->data;
-    ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer);
-#endif
-
-    if (ev->timer_next || ev->timer_prev) {
-        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
-        return;
-    }
-
-    for (e = timer_queue.timer_next;
-         e != &timer_queue && timer > e->timer_delta;
-         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;
-}
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index 792798e..abf3b6e 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -28,7 +28,7 @@
 static unsigned int     nchanges;
 static int              nevents;
 
-static ngx_event_t      timer_queue;
+static ngx_event_t     *timer_queue;
 /* */
 
 
@@ -51,20 +51,19 @@
     ngx_test_null(change_list, ngx_alloc(change_size, log), NGX_ERROR);
     ngx_test_null(event_list, ngx_alloc(event_size, log), NGX_ERROR);
 
-    if (ngx_event_init_timer(log) == NGX_ERROR) {
+    timer_queue = ngx_event_init_timer(log);
+    if (timer_queue == NULL) {
         return NGX_ERROR;
     }
 
-#if 0
-    timer_queue.timer_prev = &timer_queue;
-    timer_queue.timer_next = &timer_queue;
-#endif
-
 #if !(USE_KQUEUE)
     ngx_event_actions.add = ngx_kqueue_add_event;
     ngx_event_actions.del = ngx_kqueue_del_event;
-    ngx_event_actions.timer = ngx_kqueue_add_timer;
+    ngx_event_actions.timer = ngx_event_add_timer;
     ngx_event_actions.process = ngx_kqueue_process_events;
+
+    ngx_event_flags = NGX_HAVE_LEVEL_EVENT
+                      |NGX_HAVE_ONESHOT_EVENT|NGX_HAVE_CLEAR_EVENT;
 #endif
 
     return NGX_OK;
@@ -170,7 +169,7 @@
 int ngx_kqueue_process_events(ngx_log_t *log)
 {
     int              events, i;
-    u_int            timer, delta;
+    ngx_msec_t       timer, delta;
     ngx_event_t      *ev;
     struct timeval   tv;
     struct timespec  ts, *tp;
@@ -190,22 +189,6 @@
         tp = NULL;
     }
 
-#if 0
-    if (timer_queue.timer_next != &timer_queue) {
-        timer = timer_queue.timer_next->timer_delta;
-        ts.tv_sec = timer / 1000;
-        ts.tv_nsec = (timer % 1000) * 1000000;
-        tp = &ts;
-        gettimeofday(&tv, NULL);
-        delta = tv.tv_sec * 1000 + tv.tv_usec / 1000;
-
-    } else {
-        timer = 0;
-        delta = 0;
-        tp = NULL;
-    }
-#endif
-
 #if (NGX_DEBUG_EVENT)
     ngx_log_debug(log, "kevent timer: %d" _ timer);
 #endif
@@ -288,61 +271,5 @@
         ngx_event_expire_timers(delta);
     }
 
-#if 0
-    if (timer && timer_queue.timer_next != &timer_queue) {
-        if (delta >= timer_queue.timer_next->timer_delta) {
-            for ( ;; ) {
-                ev = timer_queue.timer_next;
-
-                if (ev == &timer_queue || delta < ev->timer_delta) {
-                    break;
-                }
-
-                delta -= ev->timer_delta;
-
-                ngx_del_timer(ev);
-                ev->timedout = 1;
-                if (ev->event_handler(ev) == NGX_ERROR) {
-                    ev->close_handler(ev);
-                }
-            }
-
-        } else {
-           timer_queue.timer_next->timer_delta -= delta;
-        }
-    }
-#endif
-
     return NGX_OK;
 }
-
-
-void ngx_kqueue_add_timer(ngx_event_t *ev, ngx_msec_t timer)
-{
-    ngx_event_t  *e;
-
-#if (NGX_DEBUG_EVENT)
-    ngx_connection_t *c = (ngx_connection_t *) ev->data;
-    ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer);
-#endif
-
-    if (ev->timer_next || ev->timer_prev) {
-        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
-        return;
-    }
-
-    for (e = timer_queue.timer_next;
-         e != &timer_queue && timer > e->timer_delta;
-         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;
-}
diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c
index 4b2434a..e27031c 100644
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -17,7 +17,7 @@
 
 static ngx_event_t   **event_index;
 static ngx_event_t   **ready_index;
-static ngx_event_t     timer_queue;
+static ngx_event_t    *timer_queue;
 /* */
 
 int ngx_poll_init(int max_connections, ngx_log_t *log)
@@ -36,12 +36,14 @@
 
     nevents = 0;
 
-    timer_queue.timer_prev = &timer_queue;
-    timer_queue.timer_next = &timer_queue;
+    timer_queue = ngx_event_init_timer(log);
+    if (timer_queue == NULL) {
+        return NGX_ERROR;
+    }
 
     ngx_event_actions.add = ngx_poll_add_event;
     ngx_event_actions.del = ngx_poll_del_event;
-    ngx_event_actions.timer = ngx_poll_add_timer;
+    ngx_event_actions.timer = ngx_event_add_timer;
     ngx_event_actions.process = ngx_poll_process_events;
 
     return NGX_OK;
@@ -139,13 +141,14 @@
 int ngx_poll_process_events(ngx_log_t *log)
 {
     int                i, ready, nready, found;
-    u_int              timer, delta;
+    ngx_msec_t         timer, delta;
     ngx_err_t          err;
     ngx_event_t       *ev;
     ngx_connection_t  *c;
 
-    if (timer_queue.timer_next != &timer_queue) {
-        timer = timer_queue.timer_next->timer_delta;
+    timer = ngx_event_find_timer();
+
+    if (timer) {
         delta = ngx_msec();
 
     } else {
@@ -253,58 +256,9 @@
         ngx_log_error(NGX_LOG_ALERT, log, 0, "poll ready != events");
     }
 
-    if (timer != INFTIM && timer_queue.timer_next != &timer_queue) {
-        if (delta >= timer_queue.timer_next->timer_delta) {
-            for ( ;; ) {
-                ev = timer_queue.timer_next;
-
-                if (ev == &timer_queue || delta < ev->timer_delta) {
-                    break;
-                }
-
-                delta -= ev->timer_delta;
-
-                ngx_del_timer(ev);
-                ev->timedout = 1;
-                if (ev->event_handler(ev) == NGX_ERROR) {
-                    ev->close_handler(ev);
-                }
-            }
-
-        } else {
-           timer_queue.timer_next->timer_delta -= delta;
-        }
+    if (timer != INFTIM) {
+        ngx_event_expire_timers(delta);
     }
 
     return NGX_OK;
 }
-
-void ngx_poll_add_timer(ngx_event_t *ev, ngx_msec_t timer)
-{
-    ngx_event_t *e;
-
-#if (NGX_DEBUG_EVENT)
-    ngx_connection_t *c = (ngx_connection_t *) ev->data;
-    ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer);
-#endif
-
-    if (ev->timer_next || ev->timer_prev) {
-        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
-        return;
-    }
-
-    for (e = timer_queue.timer_next;
-         e != &timer_queue && timer > e->timer_delta;
-         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;
-}
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 5b62dfd..fe461c3 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -23,11 +23,11 @@
 static int            max_fd;
 #endif
 
-static u_int          nevents;
+static int            nevents;
 
 static ngx_event_t  **event_index;
 static ngx_event_t  **ready_index;
-static ngx_event_t    timer_queue;
+static ngx_event_t   *timer_queue;
 /* */
 
 int ngx_select_init(int max_connections, ngx_log_t *log)
@@ -57,12 +57,14 @@
 
     nevents = 0;
 
-    timer_queue.timer_prev = &timer_queue;
-    timer_queue.timer_next = &timer_queue;
+    timer_queue = ngx_event_init_timer(log);
+    if (timer_queue == NULL) {
+        return NGX_ERROR;
+    }
 
     ngx_event_actions.add = ngx_select_add_event;
     ngx_event_actions.del = ngx_select_del_event;
-    ngx_event_actions.timer = ngx_select_add_timer;
+    ngx_event_actions.timer = ngx_event_add_timer;
     ngx_event_actions.process = ngx_select_process_events;
 
 #if (WIN32)
@@ -176,7 +178,8 @@
 int ngx_select_process_events(ngx_log_t *log)
 {
     int                ready, found, nready;
-    u_int              i, timer, delta;
+    u_int              i;
+    ngx_msec_t         timer, delta;
     ngx_event_t       *ev;
     ngx_connection_t  *c;
     struct timeval     tv, *tp;
@@ -184,12 +187,12 @@
     work_read_fd_set = master_read_fd_set;
     work_write_fd_set = master_write_fd_set;
 
-    if (timer_queue.timer_next != &timer_queue) {
-        timer = timer_queue.timer_next->timer_delta;
+    timer = ngx_event_find_timer();
+
+    if (timer) {
         tv.tv_sec = timer / 1000;
         tv.tv_usec = (timer % 1000) * 1000;
         tp = &tv;
-
         delta = ngx_msec();
 
     } else {
@@ -309,58 +312,9 @@
         ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events");
     }
 
-    if (timer && timer_queue.timer_next != &timer_queue) {
-        if (delta >= timer_queue.timer_next->timer_delta) {
-            for ( ;; ) {
-                ev = timer_queue.timer_next;
-
-                if (ev == &timer_queue || delta < ev->timer_delta) {
-                    break;
-                }
-
-                delta -= ev->timer_delta;
-
-                ngx_del_timer(ev);
-                ev->timedout = 1;
-                if (ev->event_handler(ev) == NGX_ERROR) {
-                    ev->close_handler(ev);
-                }
-            }
-
-        } else {
-           timer_queue.timer_next->timer_delta -= delta;
-        }
+    if (timer) {
+        ngx_event_expire_timers(delta);
     }
 
     return NGX_OK;
 }
-
-void ngx_select_add_timer(ngx_event_t *ev, ngx_msec_t timer)
-{
-    ngx_event_t *e;
-
-#if (NGX_DEBUG_EVENT)
-    ngx_connection_t *c = (ngx_connection_t *) ev->data;
-    ngx_log_debug(ev->log, "set timer: %d:%d" _ c->fd _ timer);
-#endif
-
-    if (ev->timer_next || ev->timer_prev) {
-        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "timer already set");
-        return;
-    }
-
-    for (e = timer_queue.timer_next;
-         e != &timer_queue && timer > e->timer_delta;
-         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;
-}
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 9a5b730..a6addd5 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -32,7 +32,7 @@
 
 #if 0
 ngx_event_type_e     ngx_event_type = NGX_SELECT_EVENT;
-#elif 0
+#elif 1
 ngx_event_type_e     ngx_event_type = NGX_POLL_EVENT;
 #else
 ngx_event_type_e     ngx_event_type = NGX_KQUEUE_EVENT;
@@ -54,6 +54,8 @@
 
 #endif
 
+int ngx_event_flags;
+
 ngx_event_actions_t  ngx_event_actions;
 
 /* ngx_event_type_e order */
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 1b0ac06..76e3167 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -56,6 +56,7 @@
     unsigned         ready:1;
     unsigned         timedout:1;
     unsigned         blocked:1;
+    unsigned         timer_set:1;
 
     unsigned         process:1;
     unsigned         read_discarded:1;
@@ -69,6 +70,17 @@
     unsigned         eof:1;
     int              error;
 #endif
+
+#if 0
+    void            *thr_ctx;   /* event thread context if $(CC) doesn't
+                                   understand __thread declaration
+                                   and pthread_getspecific() is too costly */
+
+#if (NGX_EVENT_T_PADDING)
+    int              padding[NGX_EVENT_T_PADDING];  /* event should not cross
+                                                       cache line in SMP */
+#endif
+#endif
 };
 
 typedef enum {
@@ -96,20 +108,24 @@
 } ngx_event_actions_t;
 
 
-/*
-NGX_LEVEL_EVENT (default)  select, poll, /dev/poll, kqueue
-                                requires to read whole data
-NGX_ONESHOT_EVENT          select, poll, /dev/poll(*), kqueue, epoll(*)
-                           (*) - additional syscall
-NGX_CLEAR_EVENT            kqueue, epoll
-NGX_AIO_EVENT              overlapped, aio_read, aioread
-                                no need to add or delete events
+/* Event filter requires to read/write the whole data -
+   select, poll, /dev/poll, kqueue. */
+#define NGX_HAVE_LEVEL_EVENT    1
+/* Event filter deleted after notification - select, poll, kqueue.
+   /dev/poll, epoll implemetned with additional syscall */
+#define NGX_HAVE_ONESHOT_EVENT  2
+/* Event filter notify only changes - kqueue, epoll */
+#define NGX_HAVE_CLEAR_EVENT    4
+/* No nedd to add or delete event filters - overlapped, aio_read, aioread */
+#define NGX_HAVE_AIO_EVENT      8
 
-NGX_CLOSE_EVENT            kqueue: kqueue deletes events for file that closed
-                           /dev/poll: need to flush events before closing
-*/
+/* Event filter is deleted before closing file. Has no meaning for select, poll.
+   kqueue:     kqueue deletes event filters for file that closed
+               so we need only to delete filters in user-level batch array
+   /dev/poll:  we need to flush POLLREMOVE event before closing file
+   epoll:      ??? */
+#define NGX_CLOSE_EVENT         1
 
-#define NGX_CLOSE_EVENT    1
 
 #if (HAVE_KQUEUE)
 
@@ -209,6 +225,7 @@
 #if !(USE_KQUEUE)
 extern ngx_event_actions_t   ngx_event_actions;
 extern ngx_event_type_e      ngx_event_type;
+extern int                   ngx_event_flags;
 #endif
 
 
diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c
index 8b26161..e20839d 100644
--- a/src/event/ngx_event_timer.c
+++ b/src/event/ngx_event_timer.c
@@ -10,30 +10,32 @@
 #include <ngx_event_timer.h>
 
 /* STUB */
-#define NGX_TIMER_HASH_SIZE  5
+#define NGX_TIMER_QUEUE_NUM  5
 
-ngx_event_t  *ngx_timer_queue;
-int           ngx_timer_hash_size;
-static int    ngx_timer_cur_queue;
+/* should be per-thread */
+static ngx_event_t  *ngx_timer_queue;
+static int           ngx_timer_cur_queue;
+/* */
+static int           ngx_timer_queue_num;
 
 
-int ngx_event_init_timer(ngx_log_t *log)
+ngx_event_t *ngx_event_init_timer(ngx_log_t *log)
 {
     int  i;
 
-    ngx_timer_hash_size = NGX_TIMER_HASH_SIZE;
+    ngx_timer_queue_num = NGX_TIMER_QUEUE_NUM;
     ngx_timer_cur_queue = 0;
 
     ngx_test_null(ngx_timer_queue,
-                  ngx_alloc(ngx_timer_hash_size * sizeof(ngx_event_t), log),
-                  NGX_ERROR);
+                  ngx_alloc(ngx_timer_queue_num * sizeof(ngx_event_t), log),
+                  NULL);
 
-    for (i = 0; i < ngx_timer_hash_size; i++) {
+    for (i = 0; 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];
     }
 
-    return NGX_OK;
+    return ngx_timer_queue;
 }
 
 
@@ -63,7 +65,7 @@
     }
 
     ngx_timer_cur_queue++;
-    if (ngx_timer_cur_queue >= ngx_timer_hash_size) {
+    if (ngx_timer_cur_queue >= ngx_timer_queue_num) {
         ngx_timer_cur_queue = 0;
     }
 
@@ -77,13 +79,38 @@
 }
 
 
+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;
+    }
+}
+
+
 void ngx_event_expire_timers(ngx_msec_t timer)
 {
     int           i;
     ngx_msec_t    delta;
     ngx_event_t  *ev;
 
-    for (i = 0; i < ngx_timer_hash_size; i++) {
+    for (i = 0; i < ngx_timer_queue_num; i++) {
 
         delta = timer;
 
diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h
index 24cb46c..7f1816a 100644
--- a/src/event/ngx_event_timer.h
+++ b/src/event/ngx_event_timer.h
@@ -9,36 +9,13 @@
 #include <ngx_event.h>
 
 
-int ngx_event_init_timer(ngx_log_t *log);
+ngx_event_t *ngx_event_init_timer(ngx_log_t *log);
 void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer);
+int ngx_event_find_timer(void);
+void ngx_event_expire_timers(ngx_msec_t timer);
+
 
 extern ngx_event_t  *ngx_timer_queue;
-extern int           ngx_timer_hash_size;
-
-
-ngx_inline static int ngx_event_find_timer()
-{
-    int         i;
-    ngx_msec_t  timer;
-
-    timer = NGX_MAX_MSEC;
-
-    for (i = 0; i < ngx_timer_hash_size; 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;
-    }
-}
 
 
 ngx_inline static void ngx_event_del_timer(ngx_event_t *ev)