nginx-0.3.2-RELEASE import

    *) Feature: the Sun Studio 10 C compiler support.

    *) Feature: the "proxy_upstream_max_fails",
       "proxy_upstream_fail_timeout", "fastcgi_upstream_max_fails", and
       "fastcgi_upstream_fail_timeout" directives.
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index 99342ab..e80e1ab 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -511,7 +511,7 @@
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
-                   "kevent timer: %d, changes: %d", timer, n);
+                   "kevent timer: %M, changes: %d", timer, n);
 
     events = kevent(ngx_kqueue, change_list, n, event_list, nevents, tp);
 
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index eaca467..2e2a065 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -40,7 +40,7 @@
 ngx_event_actions_t   ngx_event_actions;
 
 
-ngx_atomic_t          connection_counter;
+ngx_atomic_t          connection_counter = 1;
 ngx_atomic_t         *ngx_connection_counter = &connection_counter;
 
 
@@ -49,6 +49,7 @@
 ngx_uint_t            ngx_accept_mutex_held;
 ngx_msec_t            ngx_accept_mutex_delay;
 ngx_int_t             ngx_accept_disabled;
+ngx_file_t            ngx_accept_mutex_lock_file;
 
 
 #if (NGX_STAT_STUB)
@@ -349,7 +350,7 @@
 
     ecf = (*cf)[ngx_event_core_module.ctx_index];
 
-    ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
+    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
                   "using the \"%s\" event method", ecf->name);
 
 #if !(NGX_WIN32)
@@ -501,21 +502,21 @@
 
 #endif
 
-    cycle->connections0 = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
+    cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections,
                                    cycle->log);
-    if (cycle->connections0 == NULL) {
+    if (cycle->connections == NULL) {
         return NGX_ERROR;
     }
 
-    c = cycle->connections0;
+    c = cycle->connections;
 
-    cycle->read_events0 = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
+    cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
                                    cycle->log);
-    if (cycle->read_events0 == NULL) {
+    if (cycle->read_events == NULL) {
         return NGX_ERROR;
     }
 
-    rev = cycle->read_events0;
+    rev = cycle->read_events;
     for (i = 0; i < cycle->connection_n; i++) {
         rev[i].closed = 1;
         rev[i].instance = 1;
@@ -525,13 +526,13 @@
 #endif
     }
 
-    cycle->write_events0 = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
+    cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections,
                                     cycle->log);
-    if (cycle->write_events0 == NULL) {
+    if (cycle->write_events == NULL) {
         return NGX_ERROR;
     }
 
-    wev = cycle->write_events0;
+    wev = cycle->write_events;
     for (i = 0; i < cycle->connection_n; i++) {
         wev[i].closed = 1;
 #if (NGX_THREADS)
@@ -547,8 +548,8 @@
         i--;
 
         c[i].data = next;
-        c[i].read = &cycle->read_events0[i];
-        c[i].write = &cycle->write_events0[i];
+        c[i].read = &cycle->read_events[i];
+        c[i].write = &cycle->write_events[i];
         c[i].fd = (ngx_socket_t) -1;
 
         next = &c[i];
@@ -572,14 +573,6 @@
             return NGX_ERROR;
         }
 
-        rev = c->read;
-        wev = c->write;
-
-        ngx_memzero(c, sizeof(ngx_connection_t));
-
-        c->read = rev;
-        c->write = wev;
-        c->fd = ls[i].fd;
         c->log = &ls[i].log;
 
         c->listening = &ls[i];
@@ -588,18 +581,9 @@
         c->ctx = ls[i].ctx;
         c->servers = ls[i].servers;
 
-        ngx_memzero(rev, sizeof(ngx_event_t));
-        ngx_memzero(wev, sizeof(ngx_event_t));
-
-        /* required by poll */
-        wev->index = NGX_INVALID_INDEX;
+        rev = c->read;
 
         rev->log = c->log;
-        rev->data = c;
-        rev->index = NGX_INVALID_INDEX;
-
-        rev->available = 0;
-
         rev->accept = 1;
 
 #if (NGX_HAVE_DEFERRED_ACCEPT)
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
index 4df9dc6..238a494 100644
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -22,7 +22,6 @@
     socklen_t          sl;
     ngx_err_t          err;
     ngx_log_t         *log;
-    ngx_uint_t         instance;
     ngx_socket_t       s;
     ngx_event_t       *rev, *wev;
     ngx_listening_t   *ls;
@@ -76,8 +75,8 @@
         }
 
 #if (NGX_STAT_STUB)
-        ngx_atomic_inc(ngx_stat_accepted);
-        ngx_atomic_inc(ngx_stat_active);
+        ngx_atomic_fetch_add(ngx_stat_accepted, 1);
+        ngx_atomic_fetch_add(ngx_stat_active, 1);
 #endif
 
         ngx_accept_disabled = NGX_ACCEPT_THRESHOLD
@@ -94,16 +93,6 @@
             return;
         }
 
-        rev = c->read;
-        wev = c->write;
-
-        ngx_memzero(c, sizeof(ngx_connection_t));
-
-        c->read = rev;
-        c->write = wev;
-        c->fd = s;
-        c->log = ev->log;
-
         c->pool = ngx_create_pool(ls->pool_size, ev->log);
         if (c->pool == NULL) {
             ngx_close_accepted_connection(c);
@@ -164,21 +153,9 @@
         c->ctx = lc->ctx;
         c->servers = lc->servers;
 
-        instance = rev->instance;
+        rev = c->read;
+        wev = c->write;
 
-        ngx_memzero(rev, sizeof(ngx_event_t));
-        ngx_memzero(wev, sizeof(ngx_event_t));
-
-        rev->instance = !instance;
-        wev->instance = !instance;
-
-        rev->index = NGX_INVALID_INDEX;
-        wev->index = NGX_INVALID_INDEX;
-
-        rev->data = c;
-        wev->data = c;
-
-        wev->write = 1;
         wev->ready = 1;
 
         if (ngx_event_flags & (NGX_USE_AIO_EVENT|NGX_USE_RTSIG_EVENT)) {
@@ -197,18 +174,18 @@
         wev->log = log;
 
         /*
-         * TODO: MT: - atomic increment (x86: lock xadd)
+         * TODO: MT: - ngx_atomic_fetch_add()
          *             or protection by critical section or light mutex
          *
          * TODO: MP: - allocated in a shared memory
-         *           - atomic increment (x86: lock xadd)
+         *           - ngx_atomic_fetch_add()
          *             or protection by critical section or light mutex
          */
 
-        c->number = ngx_atomic_inc(ngx_connection_counter);
+        c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
 
 #if (NGX_STAT_STUB)
-        ngx_atomic_inc(ngx_stat_handled);
+        ngx_atomic_fetch_add(ngx_stat_handled, 1);
 #endif
 
 #if (NGX_THREADS)
@@ -391,7 +368,7 @@
     }
 
 #if (NGX_STAT_STUB)
-    ngx_atomic_dec(ngx_stat_active);
+    ngx_atomic_fetch_add(ngx_stat_active, -1);
 #endif
 }
 
diff --git a/src/event/ngx_event_acceptex.c b/src/event/ngx_event_acceptex.c
index 9619714..f269844 100644
--- a/src/event/ngx_event_acceptex.c
+++ b/src/event/ngx_event_acceptex.c
@@ -75,7 +75,7 @@
 
     ngx_event_post_acceptex(c->listening, 1);
 
-    c->number = ngx_atomic_inc(ngx_connection_counter);
+    c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
 
     c->listening->handler(c);
 
@@ -117,16 +117,6 @@
             return NGX_ERROR;
         }
 
-        rev = c->read;
-        wev = c->write;
-
-        ngx_memzero(c, sizeof(ngx_connection_t));
-
-        c->read = rev;
-        c->write = wev;
-        c->fd = s;
-        c->log = &ls->log;
-
         c->pool = ngx_create_pool(ls->pool_size, &ls->log);
         if (c->pool == NULL) {
             ngx_close_posted_connection(c);
@@ -172,21 +162,14 @@
 
         c->listening = ls;
 
-        ngx_memzero(rev, sizeof(ngx_event_t));
-        ngx_memzero(wev, sizeof(ngx_event_t));
-
-        rev->data = c;
-        wev->data = c;
-
-        rev->index = NGX_INVALID_INDEX;
-        wev->index = NGX_INVALID_INDEX;
+        rev = c->read;
+        wev = c->write;
 
         rev->ovlp.event = rev;
         wev->ovlp.event = wev;
         rev->handler = ngx_event_acceptex;
 
         rev->ready = 1;
-        wev->write = 1;
         wev->ready = 1;
 
         rev->log = c->log;
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
index 804226f..6182ef0 100644
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -14,7 +14,7 @@
 ngx_event_connect_peer(ngx_peer_connection_t *pc)
 {
     int                rc;
-    ngx_uint_t         instance, level, i;
+    ngx_uint_t         level, i;
     u_int              event;
     time_t             now;
     ngx_err_t          err;
@@ -81,7 +81,7 @@
         for ( ;; ) {
             peer = &pc->peers->peer[pc->cur_peer];
 
-            if (peer->fails <= peer->max_fails) {
+            if (peer->max_fails == 0 || peer->fails <= peer->max_fails) {
                 break;
             }
 
@@ -139,16 +139,6 @@
         return NGX_ERROR;
     }
 
-    rev = c->read;
-    wev = c->write;
-
-    ngx_memzero(c, sizeof(ngx_connection_t));
-
-    c->read = rev;
-    c->write = wev;
-    c->fd = s;
-    c->log = pc->log;
-
     if (pc->rcvbuf) {
         if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
                        (const void *) &pc->rcvbuf, sizeof(int)) == -1) {
@@ -191,21 +181,8 @@
         c->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;
     }
 
-    instance = rev->instance;
-
-    ngx_memzero(rev, sizeof(ngx_event_t));
-    ngx_memzero(wev, sizeof(ngx_event_t));
-
-    rev->instance = !instance;
-    wev->instance = !instance;
-
-    rev->index = NGX_INVALID_INDEX;
-    wev->index = NGX_INVALID_INDEX;
-
-    rev->data = c;
-    wev->data = c;
-
-    wev->write = 1;
+    rev = c->read;
+    wev = c->write;
 
     rev->log = pc->log;
     wev->log = pc->log;
@@ -213,15 +190,15 @@
     pc->connection = c;
 
     /*
-     * TODO: MT: - atomic increment (x86: lock xadd)
+     * TODO: MT: - ngx_atomic_fetch_add()
      *             or protection by critical section or mutex
      *
      * TODO: MP: - allocated in a shared memory
-     *           - atomic increment (x86: lock xadd)
+     *           - ngx_atomic_fetch_add()
      *             or protection by critical section or mutex
      */
 
-    c->number = ngx_atomic_inc(ngx_connection_counter);
+    c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
 
 #if (NGX_THREADS)
     rev->lock = pc->lock;
diff --git a/src/event/ngx_event_timer.c b/src/event/ngx_event_timer.c
index 656f62b..174cc65 100644
--- a/src/event/ngx_event_timer.c
+++ b/src/event/ngx_event_timer.c
@@ -45,8 +45,8 @@
 ngx_msec_t
 ngx_event_find_timer(void)
 {
-    ngx_rbtree_key_int_t   timer;
-    ngx_rbtree_node_t     *node, *root, *sentinel;
+    ngx_msec_int_t      timer;
+    ngx_rbtree_node_t  *node, *root, *sentinel;
 
     if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) {
         return NGX_TIMER_INFINITE;
@@ -63,8 +63,7 @@
 
     ngx_mutex_unlock(ngx_event_timer_mutex);
 
-    timer = (ngx_rbtree_key_int_t) node->key
-                                     - (ngx_rbtree_key_int_t) ngx_current_time;
+    timer = (ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_time;
 
     return (ngx_msec_t) (timer > 0 ? timer : 0);
 }
@@ -94,9 +93,7 @@
 
         /* node->key <= ngx_current_time */
 
-        if ((ngx_rbtree_key_int_t) node->key
-                                      - (ngx_rbtree_key_int_t) ngx_current_time
-            <= 0)
+        if ((ngx_msec_int_t) node->key - (ngx_msec_int_t) ngx_current_time <= 0)
         {
             ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
 
diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h
index 13b5c0a..f980234 100644
--- a/src/event/ngx_event_timer.h
+++ b/src/event/ngx_event_timer.h
@@ -60,8 +60,8 @@
 static ngx_inline void
 ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
 {
-    ngx_rbtree_key_t      key;
-    ngx_rbtree_key_int_t  diff;
+    ngx_msec_t      key;
+    ngx_msec_int_t  diff;
 
     key = ngx_current_time + timer;
 
@@ -73,7 +73,7 @@
          * the rbtree operations for the fast connections.
          */
 
-        diff = (ngx_rbtree_key_int_t) (key - ev->timer.key);
+        diff = (ngx_msec_int_t) (key - ev->timer.key);
 
         if (ngx_abs(diff) < NGX_TIMER_LAZY_DELAY) {
             ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,