[nginx] Events: introduced cancelable timers.

Valentin Bartenev vbart at nginx.com
Thu Oct 30 22:24:45 UTC 2014


details:   http://hg.nginx.org/nginx/rev/3efdd7788bb0
branches:  
changeset: 5896:3efdd7788bb0
user:      Valentin Bartenev <vbart at nginx.com>
date:      Wed Aug 13 22:45:04 2014 +0400
description:
Events: introduced cancelable timers.

diffstat:

 src/event/ngx_event.h            |   2 +
 src/event/ngx_event_timer.c      |  42 ++++++++++++++++++++++++++++++++++++++++
 src/event/ngx_event_timer.h      |   1 +
 src/os/unix/ngx_process_cycle.c  |   2 +
 src/os/win32/ngx_process_cycle.c |   2 +
 5 files changed, 49 insertions(+), 0 deletions(-)

diffs (96 lines):

diff -r 0f53e5fb7205 -r 3efdd7788bb0 src/event/ngx_event.h
--- a/src/event/ngx_event.h	Mon Aug 25 13:37:06 2014 +0400
+++ b/src/event/ngx_event.h	Wed Aug 13 22:45:04 2014 +0400
@@ -136,6 +136,8 @@ struct ngx_event_s {
     unsigned         channel:1;
     unsigned         resolver:1;
 
+    unsigned         cancelable:1;
+
 
 #if 0
 
diff -r 0f53e5fb7205 -r 3efdd7788bb0 src/event/ngx_event_timer.c
--- a/src/event/ngx_event_timer.c	Mon Aug 25 13:37:06 2014 +0400
+++ b/src/event/ngx_event_timer.c	Wed Aug 13 22:45:04 2014 +0400
@@ -94,3 +94,45 @@ ngx_event_expire_timers(void)
         ev->handler(ev);
     }
 }
+
+
+void
+ngx_event_cancel_timers(void)
+{
+    ngx_event_t        *ev;
+    ngx_rbtree_node_t  *node, *root, *sentinel;
+
+    sentinel = ngx_event_timer_rbtree.sentinel;
+
+    for ( ;; ) {
+        root = ngx_event_timer_rbtree.root;
+
+        if (root == sentinel) {
+            return;
+        }
+
+        node = ngx_rbtree_min(root, sentinel);
+
+        ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
+
+        if (!ev->cancelable) {
+            return;
+        }
+
+        ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
+                       "event timer cancel: %d: %M",
+                       ngx_event_ident(ev->data), ev->timer.key);
+
+        ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
+
+#if (NGX_DEBUG)
+        ev->timer.left = NULL;
+        ev->timer.right = NULL;
+        ev->timer.parent = NULL;
+#endif
+
+        ev->timer_set = 0;
+
+        ev->handler(ev);
+    }
+}
diff -r 0f53e5fb7205 -r 3efdd7788bb0 src/event/ngx_event_timer.h
--- a/src/event/ngx_event_timer.h	Mon Aug 25 13:37:06 2014 +0400
+++ b/src/event/ngx_event_timer.h	Wed Aug 13 22:45:04 2014 +0400
@@ -22,6 +22,7 @@
 ngx_int_t ngx_event_timer_init(ngx_log_t *log);
 ngx_msec_t ngx_event_find_timer(void);
 void ngx_event_expire_timers(void);
+void ngx_event_cancel_timers(void);
 
 
 extern ngx_rbtree_t  ngx_event_timer_rbtree;
diff -r 0f53e5fb7205 -r 3efdd7788bb0 src/os/unix/ngx_process_cycle.c
--- a/src/os/unix/ngx_process_cycle.c	Mon Aug 25 13:37:06 2014 +0400
+++ b/src/os/unix/ngx_process_cycle.c	Wed Aug 13 22:45:04 2014 +0400
@@ -809,6 +809,8 @@ ngx_worker_process_cycle(ngx_cycle_t *cy
                 }
             }
 
+            ngx_event_cancel_timers();
+
             if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
             {
                 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
diff -r 0f53e5fb7205 -r 3efdd7788bb0 src/os/win32/ngx_process_cycle.c
--- a/src/os/win32/ngx_process_cycle.c	Mon Aug 25 13:37:06 2014 +0400
+++ b/src/os/win32/ngx_process_cycle.c	Wed Aug 13 22:45:04 2014 +0400
@@ -821,6 +821,8 @@ ngx_worker_thread(void *data)
                 }
             }
 
+            ngx_event_cancel_timers();
+
             if (ngx_event_timer_rbtree.root
                 == ngx_event_timer_rbtree.sentinel)
             {



More information about the nginx-devel mailing list