comparison src/event/ngx_event_timer.c @ 108:cf3d6edb3ad6 NGINX_0_3_1

nginx 0.3.1 *) Bugfix: the segmentation fault occurred when the signal queue overflowed if the "rtsig" method was used; bug appeared in 0.2.0. *) Change: correct handling of the "\\", "\"", "\'", and "\$" pairs in SSI.
author Igor Sysoev <http://sysoev.ru>
date Mon, 10 Oct 2005 00:00:00 +0400
parents 45f7329b4bd0
children dad2fe8ecf08
comparison
equal deleted inserted replaced
107:495d867e35e8 108:cf3d6edb3ad6
12 #if (NGX_THREADS) 12 #if (NGX_THREADS)
13 ngx_mutex_t *ngx_event_timer_mutex; 13 ngx_mutex_t *ngx_event_timer_mutex;
14 #endif 14 #endif
15 15
16 16
17 ngx_thread_volatile ngx_rbtree_t *ngx_event_timer_rbtree; 17 ngx_thread_volatile ngx_rbtree_t ngx_event_timer_rbtree;
18 ngx_rbtree_t ngx_event_timer_sentinel; 18 static ngx_rbtree_node_t ngx_event_timer_sentinel;
19 19
20 20
21 ngx_int_t 21 ngx_int_t
22 ngx_event_timer_init(ngx_log_t *log) 22 ngx_event_timer_init(ngx_log_t *log)
23 { 23 {
24 if (ngx_event_timer_rbtree) { 24 ngx_event_timer_rbtree.root = &ngx_event_timer_sentinel;
25 ngx_event_timer_rbtree.sentinel = &ngx_event_timer_sentinel;
26
25 #if (NGX_THREADS) 27 #if (NGX_THREADS)
28
29 if (ngx_event_timer_mutex) {
26 ngx_event_timer_mutex->log = log; 30 ngx_event_timer_mutex->log = log;
27 #endif
28 return NGX_OK; 31 return NGX_OK;
29 } 32 }
30 33
31 ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
32
33 #if (NGX_THREADS)
34 ngx_event_timer_mutex = ngx_mutex_init(log, 0); 34 ngx_event_timer_mutex = ngx_mutex_init(log, 0);
35 if (ngx_event_timer_mutex == NULL) { 35 if (ngx_event_timer_mutex == NULL) {
36 return NGX_ERROR; 36 return NGX_ERROR;
37 } 37 }
38
38 #endif 39 #endif
39 40
40 return NGX_OK; 41 return NGX_OK;
41 } 42 }
42 43
43 44
44 ngx_msec_t 45 ngx_msec_t
45 ngx_event_find_timer(void) 46 ngx_event_find_timer(void)
46 { 47 {
47 ngx_msec_t timer; 48 ngx_rbtree_key_int_t timer;
48 ngx_rbtree_t *node; 49 ngx_rbtree_node_t *node, *root, *sentinel;
49 50
50 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) { 51 if (ngx_event_timer_rbtree.root == &ngx_event_timer_sentinel) {
51 return NGX_TIMER_INFINITE; 52 return NGX_TIMER_INFINITE;
52 } 53 }
53 54
54 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { 55 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
55 return NGX_TIMER_ERROR; 56 return NGX_TIMER_ERROR;
56 } 57 }
57 58
58 node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree, 59 root = ngx_event_timer_rbtree.root;
59 &ngx_event_timer_sentinel); 60 sentinel = ngx_event_timer_rbtree.sentinel;
61
62 node = ngx_rbtree_min(root, sentinel);
60 63
61 ngx_mutex_unlock(ngx_event_timer_mutex); 64 ngx_mutex_unlock(ngx_event_timer_mutex);
62 65
63 timer = (ngx_msec_t) node->key - ngx_current_time; 66 timer = (ngx_rbtree_key_int_t) node->key
67 - (ngx_rbtree_key_int_t) ngx_current_time;
64 68
65 return timer > 0 ? timer : 0 ; 69 return (ngx_msec_t) (timer > 0 ? timer : 0);
66 } 70 }
67 71
68 72
69 void 73 void
70 ngx_event_expire_timers(void) 74 ngx_event_expire_timers(void)
71 { 75 {
72 ngx_event_t *ev; 76 ngx_event_t *ev;
73 ngx_rbtree_t *node; 77 ngx_rbtree_node_t *node, *root, *sentinel;
78
79 sentinel = ngx_event_timer_rbtree.sentinel;
74 80
75 for ( ;; ) { 81 for ( ;; ) {
76
77 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
78 return;
79 }
80 82
81 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) { 83 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
82 return; 84 return;
83 } 85 }
84 86
85 node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree, 87 root = ngx_event_timer_rbtree.root;
86 &ngx_event_timer_sentinel); 88
89 if (root == sentinel) {
90 return;
91 }
92
93 node = ngx_rbtree_min(root, sentinel);
87 94
88 /* node->key <= ngx_current_time */ 95 /* node->key <= ngx_current_time */
89 96
90 if ((ngx_rbtree_key_int_t) node->key 97 if ((ngx_rbtree_key_int_t) node->key
91 - (ngx_rbtree_key_int_t) ngx_current_time 98 - (ngx_rbtree_key_int_t) ngx_current_time
92 <= 0) 99 <= 0)
93 { 100 {
94 ev = (ngx_event_t *) 101 ev = (ngx_event_t *) ((char *) node - offsetof(ngx_event_t, timer));
95 ((char *) node - offsetof(ngx_event_t, rbtree_key));
96 102
97 #if (NGX_THREADS) 103 #if (NGX_THREADS)
98 104
99 if (ngx_threaded && ngx_trylock(ev->lock) == 0) { 105 if (ngx_threaded && ngx_trylock(ev->lock) == 0) {
100 106
112 } 118 }
113 #endif 119 #endif
114 120
115 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, 121 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
116 "event timer del: %d: %M", 122 "event timer del: %d: %M",
117 ngx_event_ident(ev->data), ev->rbtree_key); 123 ngx_event_ident(ev->data), ev->timer.key);
118 124
119 ngx_rbtree_delete((ngx_rbtree_t **) &ngx_event_timer_rbtree, 125 ngx_rbtree_delete(&ngx_event_timer_rbtree, &ev->timer);
120 &ngx_event_timer_sentinel,
121 (ngx_rbtree_t *) &ev->rbtree_key);
122 126
123 ngx_mutex_unlock(ngx_event_timer_mutex); 127 ngx_mutex_unlock(ngx_event_timer_mutex);
124 128
125 #if (NGX_DEBUG) 129 #if (NGX_DEBUG)
126 ev->rbtree_left = NULL; 130 ev->timer.left = NULL;
127 ev->rbtree_right = NULL; 131 ev->timer.right = NULL;
128 ev->rbtree_parent = NULL; 132 ev->timer.parent = NULL;
129 #endif 133 #endif
130 134
131 ev->timer_set = 0; 135 ev->timer_set = 0;
132 136
133 #if (NGX_THREADS) 137 #if (NGX_THREADS)