nginx-0.3.3-RELEASE import

    *) Change: the "bl" and "af" parameters of the "listen" directive was
       renamed to the "backlog" and "accept_filter".

    *) Feature: the "rcvbuf" and "sndbuf" parameters of the "listen"
       directive.

    *) Change: the "$msec" log parameter does not require now the
       additional the gettimeofday() system call.

    *) Feature: the -t switch now tests the "listen" directives.

    *) Bugfix: if the invalid address was specified in the "listen"
       directive, then after the -HUP signal nginx left an open socket in
       the CLOSED state.

    *) Bugfix: the mime type may be incorrectly set to default value for
       index file with variable in the name; the bug had appeared in 0.3.0.

    *) Feature: the "timer_resolution" directive.

    *) Feature: the millisecond "$upstream_response_time" log parameter.

    *) Bugfix: a temporary file with client request body now is removed
       just after the response header was transferred to a client.

    *) Bugfix: OpenSSL 0.9.6 compatibility.

    *) Bugfix: the SSL certificate and key file paths could not be relative.

    *) Bugfix: the "ssl_prefer_server_ciphers" directive did not work in
       the ngx_imap_ssl_module.

    *) Bugfix: the "ssl_protocols" directive allowed to specify the single
       protocol only.
diff --git a/src/event/modules/ngx_aio_module.c b/src/event/modules/ngx_aio_module.c
index bf78a8e..fb3c8c8 100644
--- a/src/event/modules/ngx_aio_module.c
+++ b/src/event/modules/ngx_aio_module.c
@@ -14,12 +14,13 @@
 #endif
 
 
-static ngx_int_t ngx_aio_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_aio_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_aio_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_aio_del_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_aio_del_connection(ngx_connection_t *c, u_int flags);
-static ngx_int_t ngx_aio_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 
 
 ngx_os_io_t ngx_os_aio = {
@@ -73,9 +74,9 @@
 #if (NGX_HAVE_KQUEUE)
 
 static ngx_int_t
-ngx_aio_init(ngx_cycle_t *cycle)
+ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
-    if (ngx_kqueue_module_ctx.actions.init(cycle) == NGX_ERROR) {
+    if (ngx_kqueue_module_ctx.actions.init(cycle, timer) == NGX_ERROR) {
         return NGX_ERROR;
     }
 
@@ -159,9 +160,9 @@
 
 
 static ngx_int_t
-ngx_aio_process_events(ngx_cycle_t *cycle)
+ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
 {
-    return ngx_kqueue_module_ctx.actions.process_events(cycle);
+    return ngx_kqueue_module_ctx.actions.process_events(cycle, timer, flags);
 }
 
 #endif /* NGX_HAVE_KQUEUE */
diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c
index 7ee8602..54a5956 100644
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -31,12 +31,13 @@
 } ngx_devpoll_conf_t;
 
 
-static ngx_int_t ngx_devpoll_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_devpoll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_devpoll_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_devpoll_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_devpoll_del_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_devpoll_set_event(ngx_event_t *ev, int event, u_int flags);
-static ngx_int_t ngx_devpoll_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_devpoll_process_events(ngx_cycle_t *cycle,
+    ngx_msec_t timer, ngx_uint_t flags);
 
 static void *ngx_devpoll_create_conf(ngx_cycle_t *cycle);
 static char *ngx_devpoll_init_conf(ngx_cycle_t *cycle, void *conf);
@@ -107,7 +108,7 @@
 
 
 static ngx_int_t
-ngx_devpoll_init(ngx_cycle_t *cycle)
+ngx_devpoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     size_t               n;
     ngx_devpoll_conf_t  *dpcf;
@@ -323,48 +324,21 @@
 
 
 ngx_int_t
-ngx_devpoll_process_events(ngx_cycle_t *cycle)
+ngx_devpoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags)
 {
     int                 events, revents;
     size_t              n;
     ngx_err_t           err;
     ngx_int_t           i;
-    ngx_uint_t          lock, accept_lock;
-    ngx_msec_t          timer, delta;
-#if 0
-    ngx_cycle_t       **old_cycle;
-#endif
-    ngx_event_t        *rev, *wev;
+    ngx_uint_t          level;
+    ngx_msec_t          delta;
+    ngx_event_t        *rev, *wev, **queue;
     ngx_connection_t   *c;
     struct dvpoll       dvp;
-    struct timeval      tv;
-
-    timer = ngx_event_find_timer();
 
     /* NGX_TIMER_INFINITE == INFTIM */
 
-    accept_lock = 0;
-
-    if (ngx_accept_mutex) {
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_disabled--;
-
-        } else {
-            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                return NGX_ERROR;
-            } 
-
-            if (ngx_accept_mutex_held) {
-                accept_lock = 1;
-
-            } else if (timer == NGX_TIMER_INFINITE
-                       || timer > ngx_accept_mutex_delay)
-            {
-                timer = ngx_accept_mutex_delay;
-            }
-        }
-    }
-
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "devpoll timer: %M", timer);
 
@@ -373,7 +347,6 @@
         if (write(dp, change_list, n) != (ssize_t) n) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                           "write(/dev/poll) failed");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
@@ -391,21 +364,32 @@
 
     nchanges = 0;
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
-
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    delta = ngx_current_msec;
+    
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     if (err) {
-        ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
-                      cycle->log, err, "ioctl(DP_POLL) failed");
-        ngx_accept_mutex_unlock();
+        if (err == NGX_EINTR) {
+
+            if (ngx_event_timer_alarm) {
+                ngx_event_timer_alarm = 0;
+                return NGX_OK;
+            }
+ 
+            level = NGX_LOG_INFO;
+ 
+        } else { 
+            level = NGX_LOG_ALERT;
+        }
+
+        ngx_log_error(level, cycle->log, err, "ioctl(DP_POLL) failed");
         return NGX_ERROR;
     }
 
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "devpoll timer: %M, delta: %M", timer, delta);
@@ -413,17 +397,15 @@
         if (events == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "ioctl(DP_POLL) returned no events without timeout");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
 
-    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-        ngx_accept_mutex_unlock();
-        return NGX_ERROR;
+    if (events == 0) {
+        return NGX_OK;
     }
 
-    lock = 1;
+    ngx_mutex_lock(ngx_posted_events_mutex);
 
     for (i = 0; i < events; i++) {
         c = ngx_cycle->files[event_list[i].fd];
@@ -468,74 +450,49 @@
             revents |= POLLIN|POLLOUT;
         }
 
-        wev = c->write;
-
-        if ((revents & POLLOUT) && wev->active) {
-            wev->ready = 1;
-
-            if (!ngx_threaded && !ngx_accept_mutex_held) {
-                wev->handler(wev);
-
-            } else {
-                ngx_post_event(wev);
-            }
-        }
-
-        /*
-         * POLLIN must be handled after POLLOUT because we use
-         * the optimization to avoid the unnecessary mutex locking/unlocking
-         * if the accept event is the last one.
-         */
-
         rev = c->read;
 
         if ((revents & POLLIN) && rev->active) {
-            rev->ready = 1;
 
-            if (!ngx_threaded && !ngx_accept_mutex_held) {
+            if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
+                rev->posted_ready = 1;
+
+            } else {
+                rev->ready = 1;
+            }
+
+            if (flags & NGX_POST_EVENTS) {
+                queue = (ngx_event_t **) (rev->accept ?
+                               &ngx_posted_accept_events : &ngx_posted_events);
+
+                ngx_locked_post_event(rev, queue);
+
+            } else {
                 rev->handler(rev);
+            }
+        }
 
-            } else if (!rev->accept) {
-                ngx_post_event(rev);
+        wev = c->write;
 
-            } else if (ngx_accept_disabled <= 0) {
-                ngx_mutex_unlock(ngx_posted_events_mutex);
+        if ((revents & POLLOUT) && wev->active) {
 
-                c->read->handler(rev);
+            if (flags & NGX_POST_THREAD_EVENTS) {
+                wev->posted_ready = 1;
 
-                if (ngx_accept_disabled > 0) { 
-                    ngx_accept_mutex_unlock();
-                    accept_lock = 0;
-                }
+            } else {
+                wev->ready = 1;
+            }
 
-                if (i + 1 == events) {
-                    lock = 0;
-                    break;
-                }
+            if (flags & NGX_POST_EVENTS) {
+                ngx_locked_post_event(wev, &ngx_posted_events);
 
-                if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                    if (accept_lock) {
-                        ngx_accept_mutex_unlock();
-                    }
-                    return NGX_ERROR;
-                }
+            } else {
+                wev->handler(wev);
             }
         }
     }
 
-    if (accept_lock) {
-        ngx_accept_mutex_unlock();
-    }
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-    }
-
-    ngx_event_expire_timers();
-
-    if (!ngx_threaded) {
-        ngx_event_process_posted(cycle);
-    }
+    ngx_mutex_unlock(ngx_posted_events_mutex);
 
     return NGX_OK;
 }
diff --git a/src/event/modules/ngx_epoll_module.c b/src/event/modules/ngx_epoll_module.c
index a53b8eb..14013dc 100644
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -70,13 +70,14 @@
 } ngx_epoll_conf_t;
 
 
-static ngx_int_t ngx_epoll_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_epoll_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_epoll_add_connection(ngx_connection_t *c);
 static ngx_int_t ngx_epoll_del_connection(ngx_connection_t *c, u_int flags);
-static ngx_int_t ngx_epoll_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 
 static void *ngx_epoll_create_conf(ngx_cycle_t *cycle);
 static char *ngx_epoll_init_conf(ngx_cycle_t *cycle, void *conf);
@@ -137,7 +138,7 @@
 
 
 static ngx_int_t
-ngx_epoll_init(ngx_cycle_t *cycle)
+ngx_epoll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     ngx_event_conf_t  *ecf;
     ngx_epoll_conf_t  *epcf;
@@ -382,57 +383,20 @@
 
 
 static ngx_int_t
-ngx_epoll_process_events(ngx_cycle_t *cycle)
+ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
 {
     int                events;
     uint32_t           revents;
     ngx_int_t          instance, i;
-    ngx_uint_t         lock, accept_lock;
+    ngx_uint_t         level;
     ngx_err_t          err;
     ngx_log_t         *log;
-    ngx_msec_t         timer, delta;
-    ngx_event_t       *rev, *wev;
-    struct timeval     tv;
+    ngx_msec_t         delta;
+    ngx_event_t       *rev, *wev, **queue;
     ngx_connection_t  *c;
 
-    timer = ngx_event_find_timer();
-
-#if (NGX_THREADS)
-
-    if (timer == NGX_TIMER_ERROR) {
-        return NGX_ERROR;
-    }
-
-    if (timer == NGX_TIMER_INFINITE || timer > 500) {
-        timer = 500;
-    }
-
-#endif
-
     /* NGX_TIMER_INFINITE == INFTIM */
 
-    accept_lock = 0;
-
-    if (ngx_accept_mutex) {
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_disabled--;
-
-        } else {
-            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            if (ngx_accept_mutex_held) {
-                accept_lock = 1;
-
-            } else if (timer == NGX_TIMER_INFINITE
-                       || timer > ngx_accept_mutex_delay)
-            {
-                timer = ngx_accept_mutex_delay;
-            }
-        }
-    }
-
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "epoll timer: %M", timer);
 
@@ -444,14 +408,14 @@
         err = 0;
     }
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
-
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    delta = ngx_current_msec;
+    
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "epoll timer: %M, delta: %M", timer, delta);
@@ -459,30 +423,34 @@
         if (events == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "epoll_wait() returned no events without timeout");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
 
     if (err) {
-        ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
-                      cycle->log, err, "epoll_wait() failed");
-        ngx_accept_mutex_unlock();
+        if (err == NGX_EINTR) {
+
+            if (ngx_event_timer_alarm) {
+                ngx_event_timer_alarm = 0;
+                return NGX_OK;
+            }
+ 
+            level = NGX_LOG_INFO;
+ 
+        } else { 
+            level = NGX_LOG_ALERT;
+        }
+
+        ngx_log_error(level, cycle->log, err, "epoll_wait() failed");
         return NGX_ERROR;
     }
 
-    if (events > 0) {
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            ngx_accept_mutex_unlock();
-            return NGX_ERROR;
-        } 
-
-        lock = 1;
-
-    } else {
-        lock =0;
+    if (events == 0) {
+        return NGX_OK;
     }
 
+    ngx_mutex_lock(ngx_posted_events_mutex);
+
     log = cycle->log;
 
     for (i = 0; i < events; i++) {
@@ -539,94 +507,47 @@
             revents |= EPOLLIN|EPOLLOUT;
         }
 
+        if ((revents & EPOLLIN) && rev->active) {
+
+            if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
+                rev->posted_ready = 1;
+
+            } else {
+                rev->ready = 1;
+            }
+
+            if (flags & NGX_POST_EVENTS) {
+                queue = (ngx_event_t **) (rev->accept ?
+                               &ngx_posted_accept_events : &ngx_posted_events);
+
+                ngx_locked_post_event(rev, queue);
+
+            } else {
+                rev->handler(rev);
+            }
+        }
+
         wev = c->write;
 
         if ((revents & EPOLLOUT) && wev->active) {
 
-            if (ngx_threaded) {
+            if (flags & NGX_POST_THREAD_EVENTS) {
                 wev->posted_ready = 1;
-                ngx_post_event(wev);
 
             } else {
                 wev->ready = 1;
-
-                if (!ngx_accept_mutex_held) {
-                    wev->handler(wev);
-
-                } else {
-                    ngx_post_event(wev);
-                }
-            }
-        }
-
-        /*
-         * EPOLLIN must be handled after EPOLLOUT because we use
-         * the optimization to avoid the unnecessary mutex locking/unlocking
-         * if the accept event is the last one.
-         */
-
-        if ((revents & EPOLLIN) && rev->active) {
-
-            if (ngx_threaded && !rev->accept) {
-                rev->posted_ready = 1;
-
-                ngx_post_event(rev);
-
-                continue;
             }
 
-            rev->ready = 1;
+            if (flags & NGX_POST_EVENTS) {
+                ngx_locked_post_event(wev, &ngx_posted_events);
 
-            if (!ngx_threaded && !ngx_accept_mutex_held) {
-                rev->handler(rev);
-
-            } else if (!rev->accept) {
-                ngx_post_event(rev);
-
-            } else if (ngx_accept_disabled <= 0) {
-
-                ngx_mutex_unlock(ngx_posted_events_mutex);
-
-                rev->handler(rev);
-
-                if (ngx_accept_disabled > 0) {
-                    ngx_accept_mutex_unlock();
-                    accept_lock = 0;
-                }
-
-                if (i + 1 == events) {
-                    lock = 0;
-                    break;
-                }
-
-                if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                    if (accept_lock) {
-                        ngx_accept_mutex_unlock();
-                    }
-                    return NGX_ERROR;
-                }
+            } else {
+                wev->handler(wev);
             }
         }
     }
 
-    if (accept_lock) {
-        ngx_accept_mutex_unlock();
-    }
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-    }
-
-    ngx_event_expire_timers();
-
-    if (ngx_posted_events) {
-        if (ngx_threaded) {
-            ngx_wakeup_worker_thread(cycle);
-
-        } else {
-            ngx_event_process_posted(cycle);
-        }
-    }
+    ngx_mutex_unlock(ngx_posted_events_mutex);
 
     return NGX_OK;
 }
diff --git a/src/event/modules/ngx_iocp_module.c b/src/event/modules/ngx_iocp_module.c
index 269d955..b7b1c62 100644
--- a/src/event/modules/ngx_iocp_module.c
+++ b/src/event/modules/ngx_iocp_module.c
@@ -10,11 +10,13 @@
 #include <ngx_iocp_module.h>
 
 
-static ngx_int_t ngx_iocp_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_iocp_init(ngx_cycle_t *cycle, ngx_msec_t timer);
+static ngx_thread_value_t __stdcall ngx_iocp_timer(void *data);
 static void ngx_iocp_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_iocp_add_event(ngx_event_t *ev, int event, u_int key);
 static ngx_int_t ngx_iocp_del_connection(ngx_connection_t *c, u_int flags);
-static ngx_int_t ngx_iocp_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_iocp_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 static void *ngx_iocp_create_conf(ngx_cycle_t *cycle);
 static char *ngx_iocp_init_conf(ngx_cycle_t *cycle, void *conf);
 
@@ -93,11 +95,13 @@
 };
 
 
-static HANDLE  iocp;
+static HANDLE      iocp;
+static ngx_tid_t   timer_thread;
+static ngx_msec_t  msec;
 
 
 static ngx_int_t
-ngx_iocp_init(ngx_cycle_t *cycle)
+ngx_iocp_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     ngx_iocp_conf_t  *cf;
 
@@ -109,7 +113,7 @@
     }
 
     if (iocp == NULL) {
-        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                       "CreateIoCompletionPort() failed");
         return NGX_ERROR;
     }
@@ -120,10 +124,55 @@
 
     ngx_event_flags = NGX_USE_AIO_EVENT|NGX_USE_IOCP_EVENT;
 
+    if (timer == 0) {
+        return NGX_OK;
+    }
+
+    /*
+     * The waitable timer could not be used, because
+     * GetQueuedCompletionStatus() does not set a thread to alertable state
+     */
+
+    if (timer_thread == NULL) {
+
+        msec = timer;
+
+        if (ngx_create_thread(&timer_thread, ngx_iocp_timer, &msec, cycle->log)
+            != 0)
+        {
+            return NGX_ERROR;
+        }
+    }
+
+    ngx_event_flags |= NGX_USE_TIMER_EVENT;
+
     return NGX_OK;
 }
 
 
+static ngx_thread_value_t __stdcall
+ngx_iocp_timer(void *data)
+{
+    ngx_msec_t  timer = *(ngx_msec_t *) data;
+
+    ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0,
+                   "THREAD %p %p", &msec, data);
+
+    for ( ;; ) {
+        Sleep(timer);
+
+        ngx_time_update(0, 0);
+#if 1
+        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, "timer");
+#endif
+    }
+
+#ifdef __WATCOMC__
+    return 0;
+#endif
+}
+
+
 static void
 ngx_iocp_done(ngx_cycle_t *cycle)
 {
@@ -178,19 +227,17 @@
 
 
 static
-ngx_int_t ngx_iocp_process_events(ngx_cycle_t *cycle)
+ngx_int_t ngx_iocp_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags)
 {
     int                rc;
     u_int              key;
     u_long             bytes;
     ngx_err_t          err;
-    ngx_msec_t         timer, delta;
+    ngx_msec_t         delta;
     ngx_event_t       *ev;
-    struct timeval     tv;
     ngx_event_ovlp_t  *ovlp;
 
-    timer = ngx_event_find_timer();
-
     if (timer == NGX_TIMER_INFINITE) {
         timer = INFINITE;
     }
@@ -206,17 +253,17 @@
         err = 0;
     }
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
+    delta = ngx_current_msec;
+    
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "iocp: %d b:%d k:%d ov:%p", rc, bytes, key, ovlp);
 
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
-
     if (timer != INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "iocp timer: %M, delta: %M", timer, delta);
@@ -231,8 +278,6 @@
                 return NGX_ERROR;
             }
 
-            ngx_event_expire_timers();
-
             return NGX_OK;
         }
 
@@ -263,8 +308,6 @@
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, err,
                        "iocp: aborted event %p", ev); 
 
-        ngx_event_expire_timers();
-
         return NGX_OK;
     }
 
@@ -297,8 +340,6 @@
 
     ev->handler(ev);
 
-    ngx_event_expire_timers();
-
     return NGX_OK;
 }
 
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index e80e1ab..1c6ab55 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -16,13 +16,14 @@
 } ngx_kqueue_conf_t;
 
 
-static ngx_int_t ngx_kqueue_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_kqueue_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_kqueue_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_kqueue_del_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags);
 static ngx_int_t ngx_kqueue_process_changes(ngx_cycle_t *cycle, ngx_uint_t try);
-static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 static ngx_inline void ngx_kqueue_dump_event(ngx_log_t *log,
     struct kevent *kev);
 
@@ -111,8 +112,9 @@
 
 
 static ngx_int_t
-ngx_kqueue_init(ngx_cycle_t *cycle)
+ngx_kqueue_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
+    struct kevent       kev;
     struct timespec     ts;
     ngx_kqueue_conf_t  *kcf;
 
@@ -191,25 +193,50 @@
         }
     }
 
+    ngx_event_flags = 0;
+
+#if (NGX_HAVE_TIMER_EVENT)
+
+    if (timer) {
+        kev.ident = 0;
+        kev.filter = EVFILT_TIMER;
+        kev.flags = EV_ADD|EV_ENABLE;
+        kev.fflags = 0;
+        kev.data = timer;
+        kev.udata = 0;
+
+        ts.tv_sec = 0;
+        ts.tv_nsec = 0;
+
+        if (kevent(ngx_kqueue, &kev, 1, NULL, 0, &ts) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                          "kevent(EVFILT_TIMER) failed");
+            return NGX_ERROR;
+        }
+
+        ngx_event_flags |= NGX_USE_TIMER_EVENT;
+    }
+
+#endif
+
+    ngx_event_flags |= NGX_USE_ONESHOT_EVENT|NGX_USE_KQUEUE_EVENT;
+
+#if (NGX_HAVE_CLEAR_EVENT)
+    ngx_event_flags |= NGX_USE_CLEAR_EVENT;
+#else
+    ngx_event_flags |= NGX_USE_LEVEL_EVENT;
+#endif
+
+#if (NGX_HAVE_LOWAT_EVENT)
+    ngx_event_flags |= NGX_USE_LOWAT_EVENT;
+#endif
+
     nevents = kcf->events;
 
     ngx_io = ngx_os_io;
 
     ngx_event_actions = ngx_kqueue_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_ONESHOT_EVENT
-#if 1
-#if (NGX_HAVE_CLEAR_EVENT)
-                     |NGX_USE_CLEAR_EVENT
-#else
-                     |NGX_USE_LEVEL_EVENT
-#endif
-#endif
-#if (NGX_HAVE_LOWAT_EVENT)
-                     |NGX_USE_LOWAT_EVENT
-#endif
-                     |NGX_USE_KQUEUE_EVENT;
-
     return NGX_OK;
 }
 
@@ -254,9 +281,7 @@
     ev->disabled = 0;
     ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
 
-    if (ngx_mutex_lock(list_mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(list_mutex);
 
 #if 1
 
@@ -317,9 +342,7 @@
     ev->active = 0;
     ev->disabled = 0;
 
-    if (ngx_mutex_lock(list_mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(list_mutex);
 
 #if 1
 
@@ -441,56 +464,19 @@
 
 
 static ngx_int_t
-ngx_kqueue_process_events(ngx_cycle_t *cycle)
+ngx_kqueue_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags)
 {
-    int                events, n;
-    ngx_int_t          i, instance;
-    ngx_uint_t         lock, accept_lock;
-    ngx_err_t          err;
-    ngx_msec_t         timer, delta;
-    ngx_event_t       *ev;
-    struct timeval     tv;
-    struct timespec    ts, *tp;
-
-    timer = ngx_event_find_timer();
-
-#if (NGX_THREADS)
-
-    if (timer == NGX_TIMER_ERROR) {
-        return NGX_ERROR;
-    }
-
-    if (timer == NGX_TIMER_INFINITE || timer > 500) {
-        timer = 500;
-    }
-
-#endif
-
-    accept_lock = 0;
-
-    if (ngx_accept_mutex) {
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_disabled--;
-
-        } else {
-            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            if (ngx_accept_mutex_held) {
-                accept_lock = 1;
-
-            } else if (timer == NGX_TIMER_INFINITE
-                       || timer > ngx_accept_mutex_delay)
-            {
-                timer = ngx_accept_mutex_delay;
-            }
-        }
-    }
+    int               events, n;
+    ngx_int_t         i, instance;
+    ngx_uint_t        level;
+    ngx_err_t         err;
+    ngx_msec_t        delta;
+    ngx_event_t      *ev, **queue;
+    struct timespec   ts, *tp;
 
     if (ngx_threaded) {
         if (ngx_kqueue_process_changes(cycle, 0) == NGX_ERROR) {
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
 
@@ -521,24 +507,35 @@
         err = 0;
     }
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
+    delta = ngx_current_msec;
+
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "kevent events: %d", events);
 
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
-
     if (err) {
-        ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
-                      cycle->log, err, "kevent() failed");
-        ngx_accept_mutex_unlock();
+        if (err == NGX_EINTR) {
+
+            if (ngx_event_timer_alarm) {
+                ngx_event_timer_alarm = 0;
+                return NGX_OK;
+            }
+
+            level = NGX_LOG_INFO;
+
+        } else {
+            level = NGX_LOG_ALERT;
+        }
+
+        ngx_log_error(level, cycle->log, err, "kevent() failed");
         return NGX_ERROR;
     }
 
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "kevent timer: %M, delta: %M", timer, delta);
@@ -547,23 +544,16 @@
         if (events == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "kevent() returned no events without timeout");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
 
-    if (events > 0) {
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            ngx_accept_mutex_unlock();
-            return NGX_ERROR;
-        }
-
-        lock = 1;
-
-    } else {
-        lock =0;
+    if (events == 0) {
+        return NGX_OK;
     }
 
+    ngx_mutex_lock(ngx_posted_events_mutex);
+
     for (i = 0; i < events; i++) {
 
         ngx_kqueue_dump_event(cycle->log, &event_list[i]);
@@ -574,6 +564,15 @@
             continue;
         }
 
+#if (NGX_HAVE_TIMER_EVENT)
+
+        if (event_list[i].filter == EVFILT_TIMER) {
+            ngx_time_update(0, 0);
+            continue;
+        }
+
+#endif
+
         ev = (ngx_event_t *) event_list[i].udata;
 
         switch (event_list[i].filter) {
@@ -606,7 +605,7 @@
 
 #if (NGX_THREADS)
 
-            if (ngx_threaded && !ev->accept) {
+            if ((flags & NGX_POST_THREAD_EVENTS) && !ev->accept) {
                 ev->posted_ready = 1;
                 ev->posted_available = event_list[i].data;
 
@@ -615,7 +614,7 @@
                     ev->posted_errno = event_list[i].fflags;
                 }
 
-                ngx_post_event(ev);
+                ngx_locked_post_event(ev, &ngx_posted_events);
 
                 continue;
             }
@@ -651,60 +650,18 @@
             continue;
         }
 
-        if (!ngx_threaded && !ngx_accept_mutex_held) {
-            ev->handler(ev);
+        if (flags & NGX_POST_EVENTS) {
+            queue = (ngx_event_t **) (ev->accept ? &ngx_posted_accept_events:
+                                                   &ngx_posted_events);
+            ngx_locked_post_event(ev, queue);
+
             continue;
         }
 
-        if (!ev->accept) {
-            ngx_post_event(ev);
-            continue;
-        }
-
-        if (ngx_accept_disabled > 0) {
-            continue;
-        }
-
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-
         ev->handler(ev);
-
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_mutex_unlock();
-            accept_lock = 0;
-        }
-
-        if (i + 1 == events) {
-            lock = 0;
-            break;
-        }
-
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            if (accept_lock) {
-                ngx_accept_mutex_unlock();
-            }
-            return NGX_ERROR;
-        }
     }
 
-    if (accept_lock) {
-        ngx_accept_mutex_unlock();
-    }
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-    }
-
-    ngx_event_expire_timers();
-
-    if (ngx_posted_events) {
-        if (ngx_threaded) {
-            ngx_wakeup_worker_thread(cycle);
-
-        } else {
-            ngx_event_process_posted(cycle);
-        }
-    }
+    ngx_mutex_unlock(ngx_posted_events_mutex);
 
     return NGX_OK;
 }
@@ -719,14 +676,9 @@
     struct timespec   ts;
     struct kevent    *changes;
 
-    if (ngx_mutex_lock(kevent_mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(kevent_mutex);
 
-    if (ngx_mutex_lock(list_mutex) == NGX_ERROR) {
-        ngx_mutex_unlock(kevent_mutex);
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(list_mutex);
 
     if (nchanges == 0) {
         ngx_mutex_unlock(list_mutex);
diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c
index 29b375a..61d6179 100644
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -9,23 +9,18 @@
 #include <ngx_event.h>
 
 
-static ngx_int_t ngx_poll_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_poll_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags);
-static ngx_int_t ngx_poll_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 static char *ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf);
 
 
 static struct pollfd  *event_list;
 static int             nevents;
 
-#if 0
-static ngx_event_t   **ready_index;
-#endif
-
-static ngx_event_t    *accept_events;
-
 
 static ngx_str_t    poll_name = ngx_string("poll");
 
@@ -67,7 +62,7 @@
 
 
 static ngx_int_t
-ngx_poll_init(ngx_cycle_t *cycle)
+ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     struct pollfd   *list;
 
@@ -91,27 +86,13 @@
         }
 
         event_list = list;
-
-#if 0
-        if (ready_index) {
-            ngx_free(ready_index);
-        }
-
-        ready_index = ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
-                                cycle->log);
-        if (ready_index == NULL) {
-            return NGX_ERROR;
-        }
-#endif
     }
 
     ngx_io = ngx_os_io;
 
     ngx_event_actions = ngx_poll_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_LEVEL_EVENT
-                      |NGX_USE_ONESHOT_EVENT
-                      |NGX_USE_FD_EVENT;
+    ngx_event_flags = NGX_USE_LEVEL_EVENT|NGX_USE_FD_EVENT;
 
     return NGX_OK;
 }
@@ -121,14 +102,8 @@
 ngx_poll_done(ngx_cycle_t *cycle)
 {
     ngx_free(event_list);
-#if 0
-    ngx_free(ready_index);
-#endif
 
     event_list = NULL;
-#if 0
-    ready_index = NULL;
-#endif
 }
 
 
@@ -189,10 +164,8 @@
 static ngx_int_t
 ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags)
 {
-    ngx_uint_t          i;
-    ngx_cycle_t       **cycle;
-    ngx_event_t        *e;
-    ngx_connection_t   *c;
+    ngx_event_t       *e;
+    ngx_connection_t  *c;
 
     c = ev->data;
 
@@ -234,19 +207,6 @@
             c = ngx_cycle->files[event_list[nevents].fd];
 
             if (c->fd == -1) {
-                cycle = ngx_old_cycles.elts;
-                for (i = 0; i < ngx_old_cycles.nelts; i++) {
-                    if (cycle[i] == NULL) {
-                        continue;
-                    }
-                    c = cycle[i]->files[event_list[nevents].fd];
-                    if (c->fd != -1) {
-                        break;
-                    }
-                }
-            }
-
-            if (c->fd == -1) {
                 ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
                               "unexpected last event");
 
@@ -275,19 +235,15 @@
 
 
 static ngx_int_t
-ngx_poll_process_events(ngx_cycle_t *cycle)
+ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
 {
     int                 ready, revents;
     ngx_err_t           err;
     ngx_int_t           i, nready;
-    ngx_uint_t          n, found, lock;
-    ngx_msec_t          timer, delta;
-    ngx_cycle_t       **old_cycle;
-    ngx_event_t        *ev;
+    ngx_uint_t          found, level;
+    ngx_msec_t          delta;
+    ngx_event_t        *ev, **queue;
     ngx_connection_t   *c;
-    struct timeval      tv;
-
-    timer = ngx_event_find_timer();
 
     /* NGX_TIMER_INFINITE == INFTIM */
 
@@ -301,24 +257,6 @@
     }
 #endif
 
-    if (ngx_accept_mutex) {
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_disabled--;
-
-        } else {
-            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            if (ngx_accept_mutex_held == 0
-                && (timer == NGX_TIMER_INFINITE
-                    || timer > ngx_accept_mutex_delay))
-            { 
-                timer = ngx_accept_mutex_delay;
-            } 
-        }
-    }
-
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "poll timer: %M", timer);
 
     ready = poll(event_list, (u_int) nevents, (int) timer);
@@ -329,24 +267,35 @@
         err = 0;
     }
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
+    delta = ngx_current_msec;
 
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "poll ready %d of %d", ready, nevents);
 
     if (err) {
-        ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
-                      cycle->log, err, "poll() failed");
-        ngx_accept_mutex_unlock();
+        if (err == NGX_EINTR) {
+
+            if (ngx_event_timer_alarm) {
+                ngx_event_timer_alarm = 0;
+                return NGX_OK;
+            }
+ 
+            level = NGX_LOG_INFO;
+ 
+        } else { 
+            level = NGX_LOG_ALERT;
+        }
+
+        ngx_log_error(level, cycle->log, err, "poll() failed");
         return NGX_ERROR;
     }
 
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "poll timer: %M, delta: %M", timer, delta);
@@ -354,24 +303,23 @@
         if (ready == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "poll() returned no events without timeout");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
 
-    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-        ngx_accept_mutex_unlock();
-        return NGX_ERROR;
+    if (ready == 0) {
+        return NGX_OK;
     }
 
-    lock = 1;
+    ngx_mutex_lock(ngx_posted_events_mutex);
+
     nready = 0;
 
     for (i = 0; i < nevents && ready; i++) {
 
         revents = event_list[i].revents;
 
-#if 0
+#if 1
         ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "poll: %d: fd:%d ev:%04Xd rev:%04Xd",
                        i, event_list[i].fd, event_list[i].events, revents);
@@ -406,19 +354,6 @@
         c = ngx_cycle->files[event_list[i].fd];
 
         if (c->fd == -1) {
-            old_cycle = ngx_old_cycles.elts;
-            for (n = 0; n < ngx_old_cycles.nelts; n++) {
-                if (old_cycle[n] == NULL) {
-                    continue;
-                }
-                c = old_cycle[n]->files[event_list[i].fd];
-                if (c->fd != -1) {
-                    break;
-                }
-            }
-        }
-
-        if (c->fd == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "unexpected event");
 
             /*
@@ -453,43 +388,31 @@
             found = 1;
 
             ev = c->read;
-            ev->ready = 1;
 
-            if (ev->oneshot) {
-                if (ev->timer_set) {
-                    ngx_del_timer(ev);
-                }
-                ngx_poll_del_event(ev, NGX_READ_EVENT, 0);
-            }
+            if ((flags & NGX_POST_THREAD_EVENTS) && !ev->accept) {
+                ev->posted_ready = 1;
 
-            if (ev->accept) {
-                ev->next = accept_events;
-                accept_events = ev;
             } else {
-                ngx_post_event(ev);
+                ev->ready = 1;
             }
 
-#if 0
-            ready_index[nready++] = c->read;
-#endif
+            queue = (ngx_event_t **) (ev->accept ? &ngx_posted_accept_events:
+                                                   &ngx_posted_events);
+            ngx_locked_post_event(ev, queue); 
         }
 
         if (revents & POLLOUT) {
             found = 1;
             ev = c->write;
-            ev->ready = 1;
 
-            if (ev->oneshot) {
-                if (ev->timer_set) {
-                    ngx_del_timer(ev);
-                }
-                ngx_poll_del_event(ev, NGX_WRITE_EVENT, 0);
+            if (flags & NGX_POST_THREAD_EVENTS) {
+                ev->posted_ready = 1;
+
+            } else {
+                ev->ready = 1;
             }
 
-            ngx_post_event(ev);
-#if 0
-            ready_index[nready++] = c->write;
-#endif
+            ngx_locked_post_event(ev, &ngx_posted_events);
         }
 
         if (found) {
@@ -498,83 +421,12 @@
         }
     }
 
-#if 0
-    for (i = 0; i < nready; i++) {
-        ev = ready_index[i];
-
-        if (!ev->active) {
-            continue;
-        }
-
-        ev->ready = 1;
-
-        if (ev->oneshot) {
-            if (ev->timer_set) {
-                ngx_del_timer(ev);
-            }
-
-            if (ev->write) {
-                ngx_poll_del_event(ev, NGX_WRITE_EVENT, 0);
-            } else {
-                ngx_poll_del_event(ev, NGX_READ_EVENT, 0);
-            }
-        }
-
-        ev->handler(ev);
-    }
-#endif
-
-    ev = accept_events;
-
-    for ( ;; ) {
-
-        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                      "accept event %p", ev);
-
-        if (ev == NULL) {
-            break;
-        }
-
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-
-        ev->handler(ev);
-
-        if (ngx_accept_disabled > 0) {
-            lock = 0;
-            break;
-        }
-
-        ev = ev->next;
-
-        if (ev == NULL) {
-            lock = 0;
-            break;
-        }
-
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            ngx_accept_mutex_unlock();
-            return NGX_ERROR;
-        }
-
-    }
-
-    ngx_accept_mutex_unlock();
-    accept_events = NULL;
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-    }
+    ngx_mutex_unlock(ngx_posted_events_mutex);
 
     if (ready != 0) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "poll ready != events");
     }
 
-    ngx_event_expire_timers();
-
-    if (!ngx_threaded) {
-        ngx_event_process_posted(cycle);
-    }
-
     return nready;
 }
 
diff --git a/src/event/modules/ngx_rtsig_module.c b/src/event/modules/ngx_rtsig_module.c
index f88dd5d..450943c 100644
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -38,12 +38,14 @@
 
 extern ngx_event_module_t  ngx_poll_module_ctx;
 
-static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_rtsig_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_rtsig_add_connection(ngx_connection_t *c);
 static ngx_int_t ngx_rtsig_del_connection(ngx_connection_t *c, u_int flags);
-static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle);
-static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle);
+static ngx_int_t ngx_rtsig_process_events(ngx_cycle_t *cycle,
+    ngx_msec_t timer, ngx_uint_t flags);
+static ngx_int_t ngx_rtsig_process_overflow(ngx_cycle_t *cycle,
+    ngx_msec_t timer, ngx_uint_t flags);
 
 static void *ngx_rtsig_create_conf(ngx_cycle_t *cycle);
 static char *ngx_rtsig_init_conf(ngx_cycle_t *cycle, void *conf);
@@ -134,7 +136,7 @@
 
 
 static ngx_int_t
-ngx_rtsig_init(ngx_cycle_t *cycle)
+ngx_rtsig_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     ngx_rtsig_conf_t  *rtscf;
 
@@ -144,6 +146,7 @@
     sigaddset(&set, rtscf->signo);
     sigaddset(&set, rtscf->signo + 1);
     sigaddset(&set, SIGIO);
+    sigaddset(&set, SIGALRM);
 
     if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
         ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
@@ -279,59 +282,19 @@
 
 
 static ngx_int_t
-ngx_rtsig_process_events(ngx_cycle_t *cycle)
+ngx_rtsig_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
 {
     int                 signo;
     ngx_int_t           instance;
-    ngx_msec_t          timer, delta;
+    ngx_msec_t          delta;
     ngx_err_t           err;
     siginfo_t           si;
     ngx_event_t        *rev, *wev;
-    struct timeval      tv;
     struct timespec     ts, *tp;
     struct sigaction    sa;
     ngx_connection_t   *c;
     ngx_rtsig_conf_t   *rtscf;
 
-    if (overflow) {
-        timer = 0;
-
-    } else {
-        timer = ngx_event_find_timer();
-
-#if (NGX_THREADS)
-
-        if (timer == NGX_TIMER_ERROR) {
-            return NGX_ERROR;
-        }
-
-        if (timer == NGX_TIMER_INFINITE || timer > 500) {
-            timer = 500;
-        }
-
-#endif
-
-        if (ngx_accept_mutex) {
-            if (ngx_accept_disabled > 0) {
-                ngx_accept_disabled--;
-
-            } else {
-                ngx_accept_mutex_held = 0;
-
-                if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                    return NGX_ERROR;
-                }
-
-                if (ngx_accept_mutex_held == 0
-                    && (timer == NGX_TIMER_INFINITE
-                        || timer > ngx_accept_mutex_delay))
-                {
-                    timer = ngx_accept_mutex_delay;
-                } 
-            }
-        }
-    }
-
     if (timer == NGX_TIMER_INFINITE) {
         tp = NULL;
 
@@ -357,7 +320,6 @@
         if (err == NGX_EAGAIN) {
 
             if (timer == NGX_TIMER_INFINITE) {
-                ngx_accept_mutex_unlock();
                 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
                               "sigtimedwait() returned EAGAIN without timeout");
                 return NGX_ERROR;
@@ -373,21 +335,20 @@
                        signo, si.si_fd, si.si_band);
     }
 
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
-
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
+    delta = ngx_current_msec;
+    
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
+    }
 
     if (err) {
-        ngx_accept_mutex_unlock();
         ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
                       cycle->log, err, "sigtimedwait() failed");
         return NGX_ERROR;
     }
 
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "rtsig timer: %M, delta: %M", timer, delta);
@@ -404,9 +365,8 @@
         c = ngx_cycle->files[si.si_fd];
 
         if (c == NULL) {
-            /* the stale event */
 
-            ngx_accept_mutex_unlock();
+            /* the stale event */
 
             return NGX_OK;
         }
@@ -415,84 +375,36 @@
 
         rev = c->read;
 
-        if (c->read->instance != instance) {
+        if (rev->instance != instance) {
 
             /*
              * the stale event from a file descriptor
              * that was just closed in this iteration
              */
 
-            ngx_accept_mutex_unlock();
-
             ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                            "rtsig: stale event %p", c);
 
             return NGX_OK;
         }
 
-        if (si.si_band & (POLLIN|POLLHUP|POLLERR)) {
-            if (rev->active) {
-
-                if (ngx_threaded && !rev->accept) {
-                    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                        ngx_accept_mutex_unlock();
-                        return NGX_ERROR;
-                    }
-
-                    rev->posted_ready = 1;
-                    ngx_post_event(rev);
-
-                    ngx_mutex_unlock(ngx_posted_events_mutex);
-
-                } else {
-                    rev->ready = 1;
-
-                    if (!ngx_threaded && !ngx_accept_mutex_held) {
-                        rev->handler(rev);
-
-                    } else if (rev->accept) {
-                        if (ngx_accept_disabled <= 0) {
-                            rev->handler(rev);
-                        }
-
-                    } else {
-                        ngx_post_event(rev); 
-                    }
-                }
-            }
+        if ((si.si_band & (POLLIN|POLLHUP|POLLERR)) && rev->active) {
+            rev->ready = 1;
+            rev->handler(rev);
         }
 
         wev = c->write;
 
-        if (si.si_band & (POLLOUT|POLLHUP|POLLERR)) {
-            if (wev->active) {
-
-                if (ngx_threaded) {
-                    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                        ngx_accept_mutex_unlock();
-                        return NGX_ERROR;
-                    }
-
-                    wev->posted_ready = 1;
-                    ngx_post_event(wev);
-
-                    ngx_mutex_unlock(ngx_posted_events_mutex);
-
-                } else {
-                    wev->ready = 1;
-
-                    if (!ngx_threaded && !ngx_accept_mutex_held) {
-                        wev->handler(wev);
-
-                    } else {
-                        ngx_post_event(wev);
-                    }
-                }
-            }
+        if ((si.si_band & (POLLOUT|POLLHUP|POLLERR)) && wev->active) {
+            wev->ready = 1;
+            wev->handler(wev);
         }
 
+    } else if (signo == SIGALRM) {
+
+        return NGX_OK;
+
     } else if (signo == SIGIO) {
-        ngx_accept_mutex_unlock();
 
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                       "rt signal queue overflowed");
@@ -520,48 +432,35 @@
         return NGX_ERROR;
 
     } else if (signo != -1) {
-        ngx_accept_mutex_unlock();
-
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                       "sigtimedwait() returned unexpected signal: %d", signo);
 
         return NGX_ERROR;
     }
 
-    ngx_accept_mutex_unlock();
-
-    ngx_event_expire_timers();
-
-    if (ngx_posted_events) {
-        if (ngx_threaded) {
-            ngx_wakeup_worker_thread(cycle);
-
-        } else {
-            ngx_event_process_posted(cycle);
-        }
-    }
-
-    if (signo == -1) {
-        return NGX_AGAIN;
-    } else {
+    if (signo != -1) {
         return NGX_OK;
     }
+
+    return NGX_AGAIN;
 }
 
 
-/* TODO: old cylces */
-
 static ngx_int_t
-ngx_rtsig_process_overflow(ngx_cycle_t *cycle)
+ngx_rtsig_process_overflow(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags)
 {
     int                name[2], rtsig_max, rtsig_nr, events, ready;
     size_t             len;
     ngx_int_t          tested, n, i;
     ngx_err_t          err;
-    ngx_event_t       *rev, *wev;
+    ngx_event_t       *rev, *wev, **queue;
     ngx_connection_t  *c;
     ngx_rtsig_conf_t  *rtscf;
 
+    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                   "rtsig process overflow");
+
     rtscf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_rtsig_module);
 
     tested = 0;
@@ -608,6 +507,9 @@
         for ( ;; ) {
             ready = poll(overflow_list, n, 0);
 
+            ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                           "rtsig overflow poll:%d", ready);
+
             if (ready == -1) {
                 err = ngx_errno;
                 ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
@@ -626,9 +528,7 @@
             continue;
         }
 
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        ngx_mutex_lock(ngx_posted_events_mutex);
 
         for (i = 0; i < n; i++) {
             c = cycle->files[overflow_list[i].fd];
@@ -647,13 +547,21 @@
             {
                 tested++;
 
-                if (ngx_threaded) {
+                if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {
                     rev->posted_ready = 1;
-                    ngx_post_event(rev);
 
                 } else {
                     rev->ready = 1;
-                    rev->handler(rev); 
+                }
+
+                if (flags & NGX_POST_EVENTS) {
+                    queue = (ngx_event_t **) (rev->accept ?
+                               &ngx_posted_accept_events : &ngx_posted_events);
+
+                    ngx_locked_post_event(rev, queue);
+
+                } else {
+                    rev->handler(rev);
                 }
             }
 
@@ -667,13 +575,18 @@
             {
                 tested++;
 
-                if (ngx_threaded) {
+                if (flags & NGX_POST_THREAD_EVENTS) {
                     wev->posted_ready = 1;
-                    ngx_post_event(wev);
 
                 } else {
                     wev->ready = 1;
-                    wev->handler(wev); 
+                }
+
+                if (flags & NGX_POST_EVENTS) {
+                    ngx_locked_post_event(wev, &ngx_posted_events);
+
+                } else {
+                    wev->handler(wev);
                 }
             }
         }
@@ -688,8 +601,8 @@
                  * Check the current rt queue length to prevent
                  * the new overflow.
                  *
-                 * Learn the /proc/sys/kernel/rtsig-max value because
-                 * it can be changed since the last checking.
+                 * learn the "/proc/sys/kernel/rtsig-max" value because
+                 * it can be changed since the last checking
                  */
 
                 name[0] = CTL_KERN;
@@ -713,16 +626,17 @@
                 }
 
                 /*
-                 * drain the rt signal queue if the /proc/sys/kernel/rtsig-nr
+                 * drain the rt signal queue if the /"proc/sys/kernel/rtsig-nr"
                  * is bigger than
-                 *    /proc/sys/kernel/rtsig-max / rtsig_overflow_threshold
+                 *    "/proc/sys/kernel/rtsig-max" / "rtsig_overflow_threshold"
                  */
 
                 if (rtsig_max / rtscf->overflow_threshold < rtsig_nr) {
                     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                                    "rtsig queue state: %d/%d",
                                    rtsig_nr, rtsig_max);
-                    while (ngx_rtsig_process_events(cycle) == NGX_OK) {
+                    while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK)
+                    {
                         /* void */
                     }
                 }
@@ -734,20 +648,17 @@
                  * so drain the rt signal queue unconditionally
                  */
 
-                while (ngx_rtsig_process_events(cycle) == NGX_OK) { /* void */ }
+                while (ngx_rtsig_process_events(cycle, 0, flags) == NGX_OK) {
+                    /* void */
+                }
             }
 
             tested = 0;
         }
     }
 
-    if (ngx_posted_events) {
-        if (ngx_threaded) {
-            ngx_wakeup_worker_thread(cycle);
-
-        } else {
-            ngx_event_process_posted(cycle);
-        }
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
     }
 
     ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 5be3afa..da4303d 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -10,11 +10,12 @@
 
 
 
-static ngx_int_t ngx_select_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_select_init(ngx_cycle_t *cycle, ngx_msec_t timer);
 static void ngx_select_done(ngx_cycle_t *cycle);
 static ngx_int_t ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
 static ngx_int_t ngx_select_del_event(ngx_event_t *ev, int event, u_int flags);
-static ngx_int_t ngx_select_process_events(ngx_cycle_t *cycle);
+static ngx_int_t ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags);
 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf);
 
 
@@ -33,11 +34,6 @@
 static ngx_uint_t     nevents;
 
 static ngx_event_t  **event_index;
-#if 0
-static ngx_event_t  **ready_index;
-#endif
-
-static ngx_event_t   *accept_events;
 
 
 static ngx_str_t    select_name = ngx_string("select");
@@ -79,7 +75,7 @@
 
 
 static ngx_int_t
-ngx_select_init(ngx_cycle_t *cycle)
+ngx_select_init(ngx_cycle_t *cycle, ngx_msec_t timer)
 {
     ngx_event_t  **index;
 
@@ -103,26 +99,15 @@
             ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
             ngx_free(event_index);
         }
+
         event_index = index;
-
-#if 0
-        if (ready_index) {
-            ngx_free(ready_index);
-        }
-
-        ready_index = ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
-                                cycle->log);
-        if (ready_index == NULL) {
-            return NGX_ERROR;
-        }
-#endif
     }
 
     ngx_io = ngx_os_io;
 
     ngx_event_actions = ngx_select_module_ctx.actions;
 
-    ngx_event_flags = NGX_USE_LEVEL_EVENT|NGX_USE_ONESHOT_EVENT;
+    ngx_event_flags = NGX_USE_LEVEL_EVENT;
 
 #if (NGX_WIN32)
     max_read = max_write = 0;
@@ -138,9 +123,6 @@
 ngx_select_done(ngx_cycle_t *cycle)
 {
     ngx_free(event_index);
-#if 0
-    ngx_free(ready_index);
-#endif
 
     event_index = NULL;
 }
@@ -262,41 +244,22 @@
 
 
 static ngx_int_t
-ngx_select_process_events(ngx_cycle_t *cycle)
+ngx_select_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
+    ngx_uint_t flags)
 {
-    int                       ready, nready;
-    ngx_uint_t                i, found, lock;
-    ngx_err_t                 err;
-    ngx_msec_t                timer, delta;
-    ngx_event_t              *ev;
-    ngx_connection_t         *c;
-    struct timeval            tv, *tp;
-#if (NGX_HAVE_SELECT_CHANGE_TIMEOUT)
-    static ngx_msec_t         deltas = 0;
+    int                 ready, nready;
+    ngx_uint_t          i, found;
+    ngx_err_t           err;
+    ngx_msec_t          delta;
+    ngx_event_t        *ev, **queue;
+    ngx_connection_t   *c;
+    struct timeval      tv, *tp;
+#if !(NGX_WIN32)
+    ngx_uint_t          level;
 #endif
 
-    timer = ngx_event_find_timer();
-
 #if !(NGX_WIN32)
 
-    if (ngx_accept_mutex) {
-        if (ngx_accept_disabled > 0) {
-            ngx_accept_disabled--;
-
-        } else {
-            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            if (ngx_accept_mutex_held == 0
-                && (timer == NGX_TIMER_INFINITE
-                    || timer > ngx_accept_mutex_delay))
-            {
-                timer = ngx_accept_mutex_delay;
-            }
-        }
-    }
-
     if (max_fd == -1) {
         for (i = 0; i < nevents; i++) {
             c = event_index[i]->data;
@@ -353,9 +316,13 @@
 #endif
 
 #if (NGX_WIN32)
+
     ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp);
+
 #else
+
     ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp);
+
 #endif
 
     if (ready == -1) {
@@ -364,58 +331,14 @@
         err = 0;
     }
 
-#if (NGX_HAVE_SELECT_CHANGE_TIMEOUT)
+    delta = ngx_current_msec;
 
-    if (timer != NGX_TIMER_INFINITE) {
-        delta = timer - ((ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000);
-
-        /*
-         * learn the real time and update the cached time
-         * if the sum of the last deltas overcomes 1 second
-         */
-
-        deltas += delta;
-        if (deltas > 1000) {
-            ngx_gettimeofday(&tv);
-            ngx_time_update(tv.tv_sec);
-            deltas = tv.tv_usec / 1000;
-
-            ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000
-                                                           + tv.tv_usec / 1000;
-        } else {
-            ngx_current_time += delta;
-        }
-
-        ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "select timer: %M, delta: %M", timer, delta);
-
-    } else {
-        deltas = 0;
-
-        ngx_gettimeofday(&tv);
-        ngx_time_update(tv.tv_sec);
-
-        delta = ngx_current_time;
-        ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
-
-        if (ready == 0) {
-            ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
-                          "select() returned no events without timeout");
-            ngx_accept_mutex_unlock();
-            return NGX_ERROR;
-        }
+    if (flags & NGX_UPDATE_TIME) {
+        ngx_time_update(0, 0);
     }
 
-#else /* !(NGX_HAVE_SELECT_CHANGE_TIMEOUT) */
-
-    ngx_gettimeofday(&tv);
-    ngx_time_update(tv.tv_sec);
-
-    delta = ngx_current_time;
-    ngx_current_time = (ngx_msec_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
-
     if (timer != NGX_TIMER_INFINITE) {
-        delta = ngx_current_time - delta;
+        delta = ngx_current_msec - delta;
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                        "select timer: %M, delta: %M", timer, delta);
@@ -424,34 +347,48 @@
         if (ready == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "select() returned no events without timeout");
-            ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
     }
 
-#endif /* NGX_HAVE_SELECT_CHANGE_TIMEOUT */
-
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "select ready %d", ready);
 
-    if (err) {
 #if (NGX_WIN32)
+
+    if (err) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, err, "select() failed");
+        return NGX_ERROR;
+    }
+
 #else
-        ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
-                      cycle->log, err, "select() failed");
+
+    if (err) {
+        if (err == NGX_EINTR) {
+
+            if (ngx_event_timer_alarm) {
+                ngx_event_timer_alarm = 0;
+                return NGX_OK;
+            }
+ 
+            level = NGX_LOG_INFO;
+ 
+        } else { 
+            level = NGX_LOG_ALERT;
+        }
+
+        ngx_log_error(level, cycle->log, err, "select() failed");
+        return NGX_ERROR;
+    }
+
 #endif
-        ngx_accept_mutex_unlock();
-        return NGX_ERROR;
+
+    if (nevents == 0) {
+        return NGX_OK;
     }
 
+    ngx_mutex_lock(ngx_posted_events_mutex);
 
-    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-        ngx_accept_mutex_unlock();
-        return NGX_ERROR;
-    }
-
-    lock = 1;
     nready = 0;
 
     for (i = 0; i < nevents; i++) {
@@ -477,110 +414,20 @@
         if (found) {
             ev->ready = 1;
 
-            if (ev->oneshot) {
-                if (ev->timer_set) {
-                    ngx_del_timer(ev);
-                }
-
-                if (ev->write) {
-                    ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
-                } else {
-                    ngx_select_del_event(ev, NGX_READ_EVENT, 0);
-                }
-            }
-
-            if (ev->accept) {
-                ev->next = accept_events;
-                accept_events = ev;
-            } else {
-                ngx_post_event(ev);
-            }
+            queue = (ngx_event_t **) (ev->accept ? &ngx_posted_accept_events:
+                                                   &ngx_posted_events);
+            ngx_locked_post_event(ev, queue); 
 
             nready++;
-
-#if 0
-            ready_index[nready++] = ev;
-#endif
         }
     }
 
-#if 0
-    for (i = 0; i < nready; i++) {
-        ev = ready_index[i];
-        ready--;
-
-        if (!ev->active) {
-            continue;
-        }
-
-        ev->ready = 1;
-
-        if (ev->oneshot) {
-            if (ev->timer_set) {
-                ngx_del_timer(ev);
-            }
-
-            if (ev->write) {
-                ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
-            } else {
-                ngx_select_del_event(ev, NGX_READ_EVENT, 0);
-            }
-        }
-
-        ev->handler(ev);
-    }
-#endif
-
-    ev = accept_events;
-
-    for ( ;; ) {
-
-        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                      "accept event %p", ev);
-
-        if (ev == NULL) {
-            break;
-        }
-
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-
-        ev->handler(ev);
-
-        if (ngx_accept_disabled > 0) {
-            lock = 0;
-            break;
-        }
-
-        ev = ev->next;
-
-        if (ev == NULL) {
-            lock = 0;
-            break;
-        }
-
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            ngx_accept_mutex_unlock();
-            return NGX_ERROR;
-        }
-    }
-
-    ngx_accept_mutex_unlock();
-    accept_events = NULL;
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
-    }
+    ngx_mutex_unlock(ngx_posted_events_mutex);
 
     if (ready != nready) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "select ready != events");
     }
 
-    ngx_event_expire_timers();
-
-    if (!ngx_threaded) {
-        ngx_event_process_posted(cycle);
-    }
-
     return NGX_OK;
 }
 
@@ -599,19 +446,25 @@
     /* disable warning: the default FD_SETSIZE is 1024U in FreeBSD 5.x */
 
 #if !(NGX_WIN32)
+
     if ((unsigned) ecf->connections > FD_SETSIZE) {
         ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                       "the maximum number of files "
                       "supported by select() is " ngx_value(FD_SETSIZE));
         return NGX_CONF_ERROR;
     }
+
 #endif
 
 #if (NGX_THREADS) && !(NGX_WIN32)
+
     ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
                   "select() is not supported in the threaded mode");
     return NGX_CONF_ERROR;
+
 #else
+
     return NGX_CONF_OK;
+
 #endif
 }
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 2e2a065..6dc6e3c 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -34,6 +34,9 @@
 static char *ngx_accept_mutex_check(ngx_conf_t *cf, void *post, void *data);
 
 
+static ngx_uint_t     ngx_timer_resolution;
+sig_atomic_t          ngx_event_timer_alarm;
+
 static ngx_uint_t     ngx_event_max_module;
 
 ngx_uint_t            ngx_event_flags;
@@ -192,6 +195,77 @@
 };
 
 
+void
+ngx_process_events_and_timers(ngx_cycle_t *cycle)
+{
+    ngx_uint_t  flags;
+    ngx_msec_t  timer;
+
+    if (ngx_timer_resolution) {
+        timer = NGX_TIMER_INFINITE;
+        flags = 0;
+
+    } else {
+        timer = ngx_event_find_timer();
+        flags = NGX_UPDATE_TIME;
+
+#if (NGX_THREADS)
+
+        if (timer == NGX_TIMER_INFINITE || timer > 500) {
+            timer = 500;
+        }
+
+#endif
+    }
+
+    if (ngx_accept_mutex) {
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
+
+        } else {
+            if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
+                return;
+            }
+
+            if (ngx_accept_mutex_held) {
+                flags |= NGX_POST_EVENTS;
+
+            } else {
+                if (timer == NGX_TIMER_INFINITE
+                    || timer > ngx_accept_mutex_delay)
+                {
+                    timer = ngx_accept_mutex_delay;
+                }
+            }
+        }
+    }
+
+    (void) ngx_process_events(cycle, timer, flags);
+
+    ngx_event_expire_timers();
+
+    if (ngx_posted_accept_events) {
+        ngx_event_process_posted(cycle, &ngx_posted_accept_events);
+    }
+
+    if (ngx_accept_mutex_held) {
+        ngx_accept_mutex = 0;
+    }
+
+    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                   "posted events %p", ngx_posted_events);
+
+    if (ngx_posted_events) {
+        if (ngx_threaded) {
+            ngx_wakeup_worker_thread(cycle);
+
+        } else {
+            ngx_event_process_posted(cycle, &ngx_posted_events);
+        }
+    }
+}
+
+
 ngx_int_t
 ngx_handle_read_event(ngx_event_t *rev, u_int flags)
 {
@@ -332,12 +406,12 @@
 {
     void              ***cf;
     ngx_event_conf_t    *ecf;
+    ngx_core_conf_t     *ccf;
 #if !(NGX_WIN32)
     char                *shared;
     size_t               size;
     ngx_int_t            limit;
     struct rlimit        rlmt;
-    ngx_core_conf_t     *ccf;
 #endif
 
     cf = ngx_get_conf(cycle->conf_ctx, ngx_events_module);
@@ -353,10 +427,12 @@
     ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
                   "using the \"%s\" event method", ecf->name);
 
-#if !(NGX_WIN32)
-
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
+    ngx_timer_resolution = ccf->timer_resolution;
+
+#if !(NGX_WIN32)
+
     if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                       "getrlimit(RLIMIT_NOFILE) failed, ignored");
@@ -417,6 +493,8 @@
 
 #endif
 
+    *ngx_connection_counter = 1;
+
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "counter: %p, %d",
                    ngx_connection_counter, *ngx_connection_counter);
@@ -427,6 +505,23 @@
 }
 
 
+#if !(NGX_WIN32)
+
+void
+ngx_timer_signal_handler(int signo)
+{
+    ngx_event_timer_alarm = 1;
+
+    ngx_time_update(0, 0);
+
+#if 1
+    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ngx_cycle->log, 0, "timer signal");
+#endif
+}
+
+#endif
+
+
 static ngx_int_t
 ngx_event_process_init(ngx_cycle_t *cycle)
 {
@@ -441,6 +536,8 @@
     ngx_iocp_conf_t     *iocpcf;
 #else
     struct rlimit        rlmt;
+    struct sigaction     sa;
+    struct itimerval     itv;
 #endif
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
@@ -473,7 +570,8 @@
 
         if (ngx_modules[m]->ctx_index == ecf->use) {
             module = ngx_modules[m]->ctx;
-            if (module->actions.init(cycle) == NGX_ERROR) {
+            if (module->actions.init(cycle, ngx_timer_resolution) == NGX_ERROR)
+            {
                 /* fatal */
                 exit(2);
             }
@@ -483,6 +581,29 @@
 
 #if !(NGX_WIN32)
 
+    if (ngx_timer_resolution && !(ngx_event_flags & NGX_USE_TIMER_EVENT)) {
+
+        ngx_memzero(&sa, sizeof(struct sigaction));
+        sa.sa_handler = ngx_timer_signal_handler;
+        sigemptyset(&sa.sa_mask);
+
+        if (sigaction(SIGALRM, &sa, NULL) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                          "sigaction(SIGALRM) failed");
+            return NGX_ERROR;
+        }
+
+        itv.it_interval.tv_sec = ngx_timer_resolution / 1000;
+        itv.it_interval.tv_usec = (ngx_timer_resolution % 1000) * 1000;
+        itv.it_value.tv_sec = ngx_timer_resolution / 1000;
+        itv.it_value.tv_usec = (ngx_timer_resolution % 1000 ) * 1000;
+
+        if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
+            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
+                          "setitimer() failed");
+        } 
+    }
+
     if (ngx_event_flags & NGX_USE_FD_EVENT) {
 
         if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 9d917ea..af3e4a4 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -134,6 +134,9 @@
 
     unsigned         closed:1;
 
+    /* to test on worker exit */
+    unsigned         channel:1;
+
 #if (NGX_THREADS)
 
     unsigned         locked:1;
@@ -196,9 +199,10 @@
     ngx_int_t  (*del_conn)(ngx_connection_t *c, u_int flags);
 
     ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
-    ngx_int_t  (*process_events)(ngx_cycle_t *cycle);
+    ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
+                   ngx_uint_t flags);
 
-    ngx_int_t  (*init)(ngx_cycle_t *cycle);
+    ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
     void       (*done)(ngx_cycle_t *cycle);
 } ngx_event_actions_t;
 
@@ -214,7 +218,7 @@
 
 /*
  * The event filter is deleted after a notification without an additional
- * syscall: select, poll, kqueue, epoll, Solaris 10's event ports.
+ * syscall: kqueue, epoll, Solaris 10's event ports.
  */
 #define NGX_USE_ONESHOT_EVENT    0x00000002
 
@@ -270,6 +274,12 @@
  */
 #define NGX_USE_FD_EVENT         0x00000400
 
+/*
+ * The event module handles periodic or absolute timer event by itself:
+ * kqueue in FreeBSD 4.4 and NetBSD 2.0, Solaris 10's event ports.
+ */
+#define NGX_USE_TIMER_EVENT      0x00000800
+
 
 
 /*
@@ -446,13 +456,12 @@
 #endif
 
 
-
-#define ngx_accept_mutex_unlock()                                             \
-           if (ngx_accept_mutex_held) {                                       \
-               *ngx_accept_mutex = 0;                                         \
-           }
+#define NGX_UPDATE_TIME         1
+#define NGX_POST_EVENTS         2
+#define NGX_POST_THREAD_EVENTS  4
 
 
+extern sig_atomic_t           ngx_event_timer_alarm;
 extern ngx_uint_t             ngx_event_flags;
 extern ngx_module_t           ngx_events_module;
 extern ngx_module_t           ngx_event_core_module;
@@ -470,6 +479,7 @@
 u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);
 
 
+void ngx_process_events_and_timers(ngx_cycle_t *cycle);
 ngx_int_t ngx_handle_read_event(ngx_event_t *rev, u_int flags);
 ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);
 
diff --git a/src/event/ngx_event_acceptex.c b/src/event/ngx_event_acceptex.c
index f269844..956a3f1 100644
--- a/src/event/ngx_event_acceptex.c
+++ b/src/event/ngx_event_acceptex.c
@@ -188,7 +188,7 @@
             err = ngx_socket_errno;
             if (err != WSA_IO_PENDING) {
                 ngx_log_error(NGX_LOG_ALERT, &ls->log, err,
-                              "AcceptEx() %s falied", ls->addr_text.data);
+                              "AcceptEx() %V falied", &ls->addr_text);
 
                 ngx_close_posted_connection(c);
                 return NGX_ERROR;
diff --git a/src/event/ngx_event_busy_lock.c b/src/event/ngx_event_busy_lock.c
index 4d499a3..1b09ad9 100644
--- a/src/event/ngx_event_busy_lock.c
+++ b/src/event/ngx_event_busy_lock.c
@@ -10,7 +10,7 @@
 
 
 static int ngx_event_busy_lock_look_cachable(ngx_event_busy_lock_t *bl,
-                                             ngx_event_busy_lock_ctx_t *ctx);
+    ngx_event_busy_lock_ctx_t *ctx);
 static void ngx_event_busy_lock_handler(ngx_event_t *ev);
 static void ngx_event_busy_lock_posted_handler(ngx_event_t *ev);
 
@@ -23,14 +23,12 @@
  * NGX_ERROR:  an error occured while the mutex locking
  */
 
-ngx_int_t ngx_event_busy_lock(ngx_event_busy_lock_t *bl,
-                              ngx_event_busy_lock_ctx_t *ctx)
+ngx_int_t
+ngx_event_busy_lock(ngx_event_busy_lock_t *bl, ngx_event_busy_lock_ctx_t *ctx)
 {
     ngx_int_t  rc;
 
-    if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(bl->mutex);
 
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ctx->event->log, 0,
                    "event busy lock: b:%d mb:%d",
@@ -66,14 +64,13 @@
 }
 
 
-ngx_int_t ngx_event_busy_lock_cachable(ngx_event_busy_lock_t *bl,
-                                       ngx_event_busy_lock_ctx_t *ctx)
+ngx_int_t
+ngx_event_busy_lock_cachable(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx)
 {
     ngx_int_t  rc;
 
-    if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(bl->mutex);
 
     rc = ngx_event_busy_lock_look_cachable(bl, ctx);
 
@@ -112,15 +109,14 @@
 }
 
 
-ngx_int_t ngx_event_busy_unlock(ngx_event_busy_lock_t *bl,
-                                ngx_event_busy_lock_ctx_t *ctx)
+void
+ngx_event_busy_unlock(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx)
 {
     ngx_event_t                *ev;
     ngx_event_busy_lock_ctx_t  *wakeup;
 
-    if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(bl->mutex);
 
     if (bl->events) {
         wakeup = bl->events;
@@ -138,7 +134,7 @@
 
     if (wakeup == NULL) {
         ngx_mutex_unlock(bl->mutex);
-        return NGX_OK;
+        return;
     }
 
     if (ctx->md5) {
@@ -152,13 +148,7 @@
 
             ev = wakeup->event;
 
-            if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            ngx_post_event(ev);
-
-            ngx_mutex_unlock(ngx_posted_events_mutex);
+            ngx_post_event(ev, &ngx_posted_events);
         }
 
         ngx_mutex_unlock(bl->mutex);
@@ -177,27 +167,18 @@
             ngx_del_timer(ev);
         }
 
-        if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
-
-        ngx_post_event(ev);
-
-        ngx_mutex_unlock(ngx_posted_events_mutex);
+        ngx_post_event(ev, &ngx_posted_events);
     }
-
-    return NGX_OK;
 }
 
 
-ngx_int_t ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl,
-                                     ngx_event_busy_lock_ctx_t *ctx)
+void
+ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx)
 {
     ngx_event_busy_lock_ctx_t  *c, *p;
 
-    if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
-        return NGX_ERROR;
-    }
+    ngx_mutex_lock(bl->mutex);
 
     bl->waiting--;
 
@@ -216,13 +197,12 @@
     }
 
     ngx_mutex_unlock(bl->mutex);
-
-    return NGX_OK;
 }
 
 
-static int ngx_event_busy_lock_look_cachable(ngx_event_busy_lock_t *bl,
-                                             ngx_event_busy_lock_ctx_t *ctx)
+static ngx_int_t
+ngx_event_busy_lock_look_cachable(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx)
 {
     ngx_int_t    free;
     ngx_uint_t   i, bit, cachable, mask;
@@ -286,21 +266,17 @@
 }
 
 
-static void ngx_event_busy_lock_handler(ngx_event_t *ev)
+static void
+ngx_event_busy_lock_handler(ngx_event_t *ev)
 {
-    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-        return;
-    }
-
-    ngx_post_event(ev);
-
-    ngx_mutex_unlock(ngx_posted_events_mutex);
-
     ev->handler = ngx_event_busy_lock_posted_handler;
+
+    ngx_post_event(ev, &ngx_posted_events);
 }
 
 
-static void ngx_event_busy_lock_posted_handler(ngx_event_t *ev)
+static void
+ngx_event_busy_lock_posted_handler(ngx_event_t *ev)
 {
     ngx_event_busy_lock_ctx_t  *ctx;
 
diff --git a/src/event/ngx_event_busy_lock.h b/src/event/ngx_event_busy_lock.h
index ce60002..5b50be6 100644
--- a/src/event/ngx_event_busy_lock.h
+++ b/src/event/ngx_event_busy_lock.h
@@ -52,13 +52,13 @@
 
 
 ngx_int_t ngx_event_busy_lock(ngx_event_busy_lock_t *bl,
-                              ngx_event_busy_lock_ctx_t *ctx);
+    ngx_event_busy_lock_ctx_t *ctx);
 ngx_int_t ngx_event_busy_lock_cachable(ngx_event_busy_lock_t *bl,
-                                       ngx_event_busy_lock_ctx_t *ctx);
-ngx_int_t ngx_event_busy_unlock(ngx_event_busy_lock_t *bl,
-                                ngx_event_busy_lock_ctx_t *ctx);
-ngx_int_t ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl,
-                                     ngx_event_busy_lock_ctx_t *ctx);
+    ngx_event_busy_lock_ctx_t *ctx);
+void ngx_event_busy_unlock(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx);
+void ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl,
+    ngx_event_busy_lock_ctx_t *ctx);
 
 
 #endif /* _NGX_EVENT_BUSY_LOCK_H_INCLUDED_ */
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
index 6182ef0..b20d16d 100644
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -141,7 +141,8 @@
 
     if (pc->rcvbuf) {
         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
-                       (const void *) &pc->rcvbuf, sizeof(int)) == -1) {
+                       (const void *) &pc->rcvbuf, sizeof(int)) == -1)
+        {
             ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
                           "setsockopt(SO_RCVBUF) failed");
 
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
index 48a03a1..5e8e747 100644
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -105,7 +105,27 @@
         return NGX_ERROR;
     }
 
-    SSL_CTX_set_options(ssl->ctx, SSL_OP_ALL);
+    /*
+     * these options are needed on client side only:
+     *    SSL_OP_MICROSOFT_SESS_ID_BUG
+     *    SSL_OP_NETSCAPE_CHALLENGE_BUG
+     *    SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
+     */
+
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
+
+    /* this option allow a potential SSL 2.0 rollback (CAN-2005-2969) */
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING);
+
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_D5_BUG);
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_TLS_BLOCK_PADDING_BUG);
+
+#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
+    SSL_CTX_set_options(ssl->ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
+#endif
+
 
     if (ngx_ssl_protocols[protocols >> 1] != 0) {
         SSL_CTX_set_options(ssl->ctx, ngx_ssl_protocols[protocols >> 1]);
@@ -120,20 +140,31 @@
 
 
 ngx_int_t
-ngx_ssl_certificate(ngx_ssl_t *ssl, u_char *cert, u_char *key)
+ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *cert,
+    ngx_str_t *key)
 {
-    if (SSL_CTX_use_certificate_chain_file(ssl->ctx, (char *) cert) == 0) {
-        ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
-                      "SSL_CTX_use_certificate_chain_file(\"%s\") failed",
-                      cert);
+    if (ngx_conf_full_name(cf->cycle, cert) == NGX_ERROR) {
         return NGX_ERROR;
     }
 
-    if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key, SSL_FILETYPE_PEM)
+    if (SSL_CTX_use_certificate_chain_file(ssl->ctx, (char *) cert->data)
         == 0)
     {
         ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
-                      "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key);
+                      "SSL_CTX_use_certificate_chain_file(\"%s\") failed",
+                      cert->data);
+        return NGX_ERROR;
+    }
+
+    if (ngx_conf_full_name(cf->cycle, key) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
+
+    if (SSL_CTX_use_PrivateKey_file(ssl->ctx, (char *) key->data,
+                                    SSL_FILETYPE_PEM) == 0)
+    {
+        ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0,
+                      "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->data);
         return NGX_ERROR;
     }
 
@@ -402,13 +433,7 @@
                 return NGX_ERROR;
             }
 
-            if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            ngx_post_event(c->write);
-
-            ngx_mutex_unlock(ngx_posted_events_mutex);
+            ngx_post_event(c->write, &ngx_posted_events);
         }
 
         return NGX_OK;
@@ -632,13 +657,7 @@
                 return NGX_ERROR;
             }
 
-            if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            ngx_post_event(c->read);
-
-            ngx_mutex_unlock(ngx_posted_events_mutex);
+            ngx_post_event(c->read, &ngx_posted_events);
         }
 
         return n;
@@ -925,8 +944,8 @@
 ngx_openssl_noengine(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                       "\"ssl_engine\" is not supported: " NGX_SSL_NAME
-                       " library does not support crypto accelerators");
+                       "\"ssl_engine\" directive is available only in "
+                       "OpenSSL 0.9.7 and higher,");
 
     return NGX_CONF_ERROR;
 }
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
index cd3898c..6e81cdf 100644
--- a/src/event/ngx_event_openssl.h
+++ b/src/event/ngx_event_openssl.h
@@ -58,7 +58,8 @@
 
 ngx_int_t ngx_ssl_init(ngx_log_t *log);
 ngx_int_t ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols);
-ngx_int_t ngx_ssl_certificate(ngx_ssl_t *ssl, u_char *cert, u_char *key);
+ngx_int_t ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
+    ngx_str_t *cert, ngx_str_t *key);
 ngx_int_t ngx_ssl_generate_rsa512_key(ngx_ssl_t *ssl);
 ngx_int_t ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c,
     ngx_uint_t flags);
diff --git a/src/event/ngx_event_posted.c b/src/event/ngx_event_posted.c
index 181ab4c..a7b09fd 100644
--- a/src/event/ngx_event_posted.c
+++ b/src/event/ngx_event_posted.c
@@ -9,6 +9,7 @@
 #include <ngx_event.h>
 
 
+ngx_thread_volatile ngx_event_t  *ngx_posted_accept_events;
 ngx_thread_volatile ngx_event_t  *ngx_posted_events;
 
 #if (NGX_THREADS)
@@ -16,13 +17,15 @@
 #endif
 
 
-void ngx_event_process_posted(ngx_cycle_t *cycle)
+void
+ngx_event_process_posted(ngx_cycle_t *cycle,
+    ngx_thread_volatile ngx_event_t **posted)
 {
     ngx_event_t  *ev;
 
     for ( ;; ) {
 
-        ev = (ngx_event_t *) ngx_posted_events;
+        ev = (ngx_event_t *) *posted;
 
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                       "posted event %p", ev);
@@ -40,7 +43,8 @@
 
 #if (NGX_THREADS) && !(NGX_WIN32)
 
-void ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
+void
+ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
 {
     ngx_int_t     i;
 #if 0
@@ -76,7 +80,8 @@
 }
 
 
-ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle)
+ngx_int_t
+ngx_event_thread_process_posted(ngx_cycle_t *cycle)
 {
     ngx_event_t  *ev;
 
@@ -139,9 +144,7 @@
 
             ev->handler(ev);
 
-            if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                return NGX_ERROR;
-            }
+            ngx_mutex_lock(ngx_posted_events_mutex);
 
             if (ev->locked) {
                 ngx_unlock(ev->lock);
@@ -161,7 +164,8 @@
 
 #else
 
-void ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
+void
+ngx_wakeup_worker_thread(ngx_cycle_t *cycle)
 {
 }
 
diff --git a/src/event/ngx_event_posted.h b/src/event/ngx_event_posted.h
index 86100af..f9c55e5 100644
--- a/src/event/ngx_event_posted.h
+++ b/src/event/ngx_event_posted.h
@@ -13,43 +13,62 @@
 #include <ngx_event.h>
 
 
-#define ngx_post_event(ev)                                                    \
-            if (ev->prev == NULL) {                                           \
-                ev->next = (ngx_event_t *) ngx_posted_events;                 \
-                ev->prev = (ngx_event_t **) &ngx_posted_events;               \
-                ngx_posted_events = ev;                                       \
-                if (ev->next) {                                               \
-                    ev->next->prev = &ev->next;                               \
-                }                                                             \
-                ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                \
-                               "post event %p", ev);                          \
-            } else  {                                                         \
-                ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                \
-                               "update posted event %p", ev);                 \
-            }
+#if (NGX_THREADS)
+extern ngx_mutex_t  *ngx_posted_events_mutex;
+#endif
+
+
+#define ngx_locked_post_event(ev, queue)                                      \
+                                                                              \
+    if (ev->prev == NULL) {                                                   \
+        ev->next = (ngx_event_t *) *queue;                                    \
+        ev->prev = (ngx_event_t **) queue;                                    \
+        *queue = ev;                                                          \
+                                                                              \
+        if (ev->next) {                                                       \
+            ev->next->prev = &ev->next;                                       \
+        }                                                                     \
+                                                                              \
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "post event %p", ev);  \
+                                                                              \
+    } else  {                                                                 \
+        ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                        \
+                       "update posted event %p", ev);                         \
+    }
+
+
+#define ngx_post_event(ev, queue)                                             \
+                                                                              \
+    ngx_mutex_lock(ngx_posted_events_mutex);                                  \
+    ngx_locked_post_event(ev, queue);                                         \
+    ngx_mutex_unlock(ngx_posted_events_mutex);
+
 
 #define ngx_delete_posted_event(ev)                                           \
-        *(ev->prev) = ev->next;                                               \
-        if (ev->next) {                                                       \
-            ev->next->prev = ev->prev;                                        \
-        }                                                                     \
-        ev->prev = NULL;                                                      \
-        ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                        \
-                       "delete posted event %p", ev);
+                                                                              \
+    *(ev->prev) = ev->next;                                                   \
+                                                                              \
+    if (ev->next) {                                                           \
+        ev->next->prev = ev->prev;                                            \
+    }                                                                         \
+                                                                              \
+    ev->prev = NULL;                                                          \
+    ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,                            \
+                   "delete posted event %p", ev);
 
 
 
-void ngx_event_process_posted(ngx_cycle_t *cycle);
+void ngx_event_process_posted(ngx_cycle_t *cycle,
+    ngx_thread_volatile ngx_event_t **posted);
 void ngx_wakeup_worker_thread(ngx_cycle_t *cycle);
 
-extern ngx_thread_volatile ngx_event_t  *ngx_posted_events;
-
-
 #if (NGX_THREADS)
 ngx_int_t ngx_event_thread_process_posted(ngx_cycle_t *cycle);
-
-extern ngx_mutex_t                      *ngx_posted_events_mutex;
 #endif
 
 
+extern ngx_thread_volatile ngx_event_t  *ngx_posted_accept_events;
+extern ngx_thread_volatile ngx_event_t  *ngx_posted_events;
+
+
 #endif /* _NGX_EVENT_POSTED_H_INCLUDED_ */
diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c
index 174cc65..3eb23f6 100644
--- a/src/event/ngx_event_timer.c
+++ b/src/event/ngx_event_timer.c
@@ -52,9 +52,7 @@
         return NGX_TIMER_INFINITE;
     }
 
-    if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
-        return NGX_TIMER_ERROR;
-    }
+    ngx_mutex_lock(ngx_event_timer_mutex);
 
     root = ngx_event_timer_rbtree.root;
     sentinel = ngx_event_timer_rbtree.sentinel;
@@ -63,7 +61,7 @@
 
     ngx_mutex_unlock(ngx_event_timer_mutex);
 
-    timer = (ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_time;
+    timer = (ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec;
 
     return (ngx_msec_t) (timer > 0 ? timer : 0);
 }
@@ -79,9 +77,7 @@
 
     for ( ;; ) {
 
-        if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
-            return;
-        }
+        ngx_mutex_lock(ngx_event_timer_mutex);
 
         root = ngx_event_timer_rbtree.root;
 
@@ -93,7 +89,7 @@
 
         /* node->key <= ngx_current_time */
 
-        if ((ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_time <= 0)
+        if ((ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_msec <= 0)
         {
             ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
 
@@ -133,14 +129,9 @@
 
 #if (NGX_THREADS)
             if (ngx_threaded) {
-                if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
-                    return;
-                }
-
                 ev->posted_timedout = 1;
-                ngx_post_event(ev);
 
-                ngx_mutex_unlock(ngx_posted_events_mutex);
+                ngx_post_event(ev, &ngx_posted_events);
 
                 ngx_unlock(ev->lock);
 
diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h
index f980234..ce32ce9 100644
--- a/src/event/ngx_event_timer.h
+++ b/src/event/ngx_event_timer.h
@@ -14,7 +14,6 @@
 
 
 #define NGX_TIMER_INFINITE  (ngx_msec_t) -1
-#define NGX_TIMER_ERROR     (ngx_msec_t) -2
 
 #define NGX_TIMER_LAZY_DELAY  300
 
@@ -39,9 +38,7 @@
                    "event timer del: %d: %M",
                     ngx_event_ident(ev->data), ev->timer.key);
 
-    if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
-        return;
-    }
+    ngx_mutex_lock(ngx_event_timer_mutex);
 
     ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
 
@@ -63,7 +60,7 @@
     ngx_msec_t      key;
     ngx_msec_int_t  diff;
 
-    key = ngx_current_time + timer;
+    key = ngx_current_msec + timer;
 
     if (ev->timer_set) {
 
@@ -91,9 +88,7 @@
                    "event timer add: %d: %M:%M",
                     ngx_event_ident(ev->data), timer, ev->timer.key);
 
-    if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
-        return;
-    }
+    ngx_mutex_lock(ngx_event_timer_mutex);
 
     ngx_rbtree_insert(&ngx_event_timer_rbtree, &ev->timer);