nginx-0.0.3-2004-04-21-22:54:33 import
diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c
index 4854762..52caaf0 100644
--- a/src/event/modules/ngx_devpoll_module.c
+++ b/src/event/modules/ngx_devpoll_module.c
@@ -313,7 +313,7 @@
 {
     int                 events;
     ngx_int_t           i;
-    ngx_uint_t          j, lock, expire;
+    ngx_uint_t          j, lock, accept_lock, expire;
     size_t              n;
     ngx_msec_t          timer;
     ngx_err_t           err;
@@ -346,21 +346,30 @@
         expire = 1;
     }
 
-    if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+    ngx_old_elapsed_msec = ngx_elapsed_msec;
+    accept_lock = 0;
 
-        if (ngx_accept_mutex_held == 0
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 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;
+                expire = 0;
+            }
         }
     }
 
-    ngx_old_elapsed_msec = ngx_elapsed_msec;
-
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
                    "devpoll timer: %d", timer);
 
@@ -493,30 +502,39 @@
             } else if (!c->read->accept) {
                 ngx_post_event(c->read);
 
-            } else {
+            } else if (ngx_accept_disabled <= 0) {
                 ngx_mutex_unlock(ngx_posted_events_mutex);
 
                 c->read->event_handler(c->read);
 
+                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) {
-                    ngx_accept_mutex_unlock();
+                    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_accept_mutex_unlock();
-
     if (expire && delta) {
         ngx_event_expire_timers((ngx_msec_t) delta);
     }
diff --git a/src/event/modules/ngx_epoll_module.c b/src/event/modules/ngx_epoll_module.c
index 5097f6c..a1d7084 100644
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -345,7 +345,7 @@
     int                events;
     size_t             n;
     ngx_int_t          instance, i;
-    ngx_uint_t         lock, expire;
+    ngx_uint_t         lock, accept_lock, expire;
     ngx_err_t          err;
     ngx_log_t         *log;
     ngx_msec_t         timer;
@@ -377,17 +377,26 @@
     }
 
     ngx_old_elapsed_msec = ngx_elapsed_msec;
+    accept_lock = 0;
 
     if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
 
-        if (ngx_accept_mutex_held == 0
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 0;
+        } 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;
+                expire = 0;
+            }
         }
     }
 
@@ -513,30 +522,40 @@
             } else if (!c->read->accept) {
                 ngx_post_event(c->read);
 
-            } else {
+            } else if (ngx_accept_disabled <= 0) {
+
                 ngx_mutex_unlock(ngx_posted_events_mutex);
 
                 c->read->event_handler(c->read);
 
+                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) {
-                    ngx_accept_mutex_unlock();
+                    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_accept_mutex_unlock();
-
     if (expire && delta) {
         ngx_event_expire_timers((ngx_msec_t) delta);
     }
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index 70944ea..71e6918 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -345,7 +345,7 @@
 {
     int                events;
     ngx_int_t          i, instance;
-    ngx_uint_t         lock, expire;
+    ngx_uint_t         lock, accept_lock, expire;
     ngx_err_t          err;
     ngx_msec_t         timer;
     ngx_event_t       *ev;
@@ -384,17 +384,26 @@
 
     ngx_old_elapsed_msec = ngx_elapsed_msec;
     expire = 1;
+    accept_lock = 0;
 
     if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
 
-        if (ngx_accept_mutex_held == 0
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 0;
+        } 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;
+                expire = 0;
+            }
         }
     }
 
@@ -539,27 +548,40 @@
             continue;
         }
 
+        if (ngx_accept_disabled > 0) {
+            continue;
+        }
+
         ngx_mutex_unlock(ngx_posted_events_mutex);
 
         ev->event_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) {
-            ngx_accept_mutex_unlock();
+            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_accept_mutex_unlock();
-
     /* TODO: wake up worker thread */
 
     if (expire && delta) {
diff --git a/src/event/modules/ngx_poll_module.c b/src/event/modules/ngx_poll_module.c
index e1ef123..3a2ca4c 100644
--- a/src/event/modules/ngx_poll_module.c
+++ b/src/event/modules/ngx_poll_module.c
@@ -304,23 +304,31 @@
     ngx_old_elapsed_msec = ngx_elapsed_msec; 
 
 #if (NGX_DEBUG0)
-    for (i = 0; i < nevents; i++) {
-        ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "poll: %d: fd:%d ev:%04X",
-                       i, event_list[i].fd, event_list[i].events);
+    if (cycle->log->log_level & NGX_LOG_DEBUG_ALL) {
+        for (i = 0; i < nevents; i++) {
+            ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                           "poll: %d: fd:%d ev:%04X",
+                           i, event_list[i].fd, event_list[i].events);
+        }
     }
 #endif
 
     if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
 
-        if (ngx_accept_mutex_held == 0 
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 0;
+        } 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;
+                expire = 0;
+            } 
         }
     }
 
@@ -543,6 +551,11 @@
 
         ev->event_handler(ev);
 
+        if (ngx_accept_disabled > 0) {
+            lock = 0;
+            break;
+        }
+
         ev = ev->next;
 
         if (ev == NULL) {
@@ -557,13 +570,13 @@
 
     }
 
+    ngx_accept_mutex_unlock();
+    accept_events = NULL;
+
     if (lock) {
         ngx_mutex_unlock(ngx_posted_events_mutex);
     }
 
-    ngx_accept_mutex_unlock();
-    accept_events = NULL;
-
     if (ready != 0) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "poll ready != events");
     }
diff --git a/src/event/modules/ngx_rtsig_module.c b/src/event/modules/ngx_rtsig_module.c
index 22243f5..e5f632a 100644
--- a/src/event/modules/ngx_rtsig_module.c
+++ b/src/event/modules/ngx_rtsig_module.c
@@ -221,15 +221,21 @@
     expire = 1;
 
     if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
 
-        if (ngx_accept_mutex_held == 0
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 0;
+        } 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;
+                expire = 0;
+            } 
         }
     }
 
@@ -299,7 +305,9 @@
                     c->read->event_handler(c->read);
 
                 } else if (c->read->accept) {
-                    c->read->event_handler(c->read);
+                    if (ngx_accept_disabled > 0) {
+                        c->read->event_handler(c->read);
+                    }
 
                 } else {
                     if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
@@ -367,7 +375,6 @@
         return NGX_ERROR;
     }
 
-
     ngx_accept_mutex_unlock();
 
     if (expire && delta) {
diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c
index 23566cf..39fb3f7 100644
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -284,15 +284,21 @@
 #if !(WIN32)
 
     if (ngx_accept_mutex) {
-        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
-            return NGX_ERROR;
-        }
+        if (ngx_accept_disabled > 0) {
+            ngx_accept_disabled--;
 
-        if (ngx_accept_mutex_held == 0
-            && (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
-        {
-            timer = ngx_accept_mutex_delay;
-            expire = 0;
+        } 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;
+                expire = 0;
+            }
         }
     }
 
@@ -311,11 +317,18 @@
 #endif
 
 #if (NGX_DEBUG)
-    for (i = 0; i < nevents; i++) {
-        ev = event_index[i];
-        c = ev->data;
-        ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                       "select event: fd:%d wr:%d", c->fd, ev->write);
+    if (cycle->log->log_level & NGX_LOG_DEBUG_ALL) {
+        for (i = 0; i < nevents; i++) {
+            ev = event_index[i];
+            c = ev->data;
+            ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                           "select event: fd:%d wr:%d", c->fd, ev->write);
+        }
+
+#if !(WIN32)
+        ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
+                       "max_fd: %d", max_fd);
+#endif
     }
 #endif
 
@@ -365,6 +378,11 @@
             ngx_gettimeofday(&tv);
             ngx_time_update(tv.tv_sec);
             deltas = tv.tv_usec / 1000;
+
+            ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000
+                                                              - ngx_start_msec;
+        } else {
+            ngx_elapsed_msec += delta;
         }
 
         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
@@ -375,6 +393,8 @@
         ngx_gettimeofday(&tv);
         ngx_time_update(tv.tv_sec);
 
+        ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000
+                                                              - ngx_start_msec;
         if (ready == 0) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                           "select() returned no events without timeout");
@@ -523,6 +543,11 @@
 
         ev->event_handler(ev);
 
+        if (ngx_accept_disabled > 0) {
+            lock = 0;
+            break;
+        }
+
         ev = ev->next;
 
         if (ev == NULL) {
@@ -534,16 +559,15 @@
             ngx_accept_mutex_unlock();
             return NGX_ERROR;
         }
-
-    }
-
-    if (lock) {
-        ngx_mutex_unlock(ngx_posted_events_mutex);
     }
 
     ngx_accept_mutex_unlock();
     accept_events = NULL;
 
+    if (lock) {
+        ngx_mutex_unlock(ngx_posted_events_mutex);
+    }
+
     if (ready != nready) {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "select ready != events");
     }