diff src/event/modules/ngx_epoll_module.c @ 112:408f195b3482 NGINX_0_3_3

nginx 0.3.3 *) 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; bug 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.
author Igor Sysoev <http://sysoev.ru>
date Wed, 19 Oct 2005 00:00:00 +0400
parents cf3d6edb3ad6
children e38f51cd0905
line wrap: on
line diff
--- a/src/event/modules/ngx_epoll_module.c
+++ b/src/event/modules/ngx_epoll_module.c
@@ -70,13 +70,14 @@ typedef struct {
 } 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 @@ ngx_module_t  ngx_epoll_module = {
 
 
 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 @@ ngx_epoll_del_connection(ngx_connection_
 
 
 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 @@ ngx_epoll_process_events(ngx_cycle_t *cy
         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,29 +423,33 @@ ngx_epoll_process_events(ngx_cycle_t *cy
         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;
-        } 
+    if (events == 0) {
+        return NGX_OK;
+    }
 
-        lock = 1;
-
-    } else {
-        lock =0;
-    }
+    ngx_mutex_lock(ngx_posted_events_mutex);
 
     log = cycle->log;
 
@@ -539,94 +507,47 @@ ngx_epoll_process_events(ngx_cycle_t *cy
             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 (!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 (flags & NGX_POST_EVENTS) {
+                ngx_locked_post_event(wev, &ngx_posted_events);
 
-                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;
 }