0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
|
12 #if (NGX_THREADS)
|
|
13 ngx_mutex_t *ngx_event_timer_mutex;
|
|
14 #endif
|
|
15
|
|
16
|
|
17 ngx_thread_volatile ngx_rbtree_t *ngx_event_timer_rbtree;
|
|
18 ngx_rbtree_t ngx_event_timer_sentinel;
|
|
19
|
|
20
|
42
|
21 ngx_int_t
|
|
22 ngx_event_timer_init(ngx_log_t *log)
|
0
|
23 {
|
|
24 if (ngx_event_timer_rbtree) {
|
|
25 #if (NGX_THREADS)
|
|
26 ngx_event_timer_mutex->log = log;
|
|
27 #endif
|
|
28 return NGX_OK;
|
|
29 }
|
|
30
|
|
31 ngx_event_timer_rbtree = &ngx_event_timer_sentinel;
|
|
32
|
|
33 #if (NGX_THREADS)
|
|
34 if (!(ngx_event_timer_mutex = ngx_mutex_init(log, 0))) {
|
|
35 return NGX_ERROR;
|
|
36 }
|
|
37 #endif
|
|
38
|
|
39 return NGX_OK;
|
|
40 }
|
|
41
|
|
42
|
42
|
43 ngx_msec_t
|
|
44 ngx_event_find_timer(void)
|
0
|
45 {
|
|
46 ngx_msec_t timer;
|
|
47 ngx_rbtree_t *node;
|
|
48
|
|
49 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
|
50 return NGX_TIMER_INFINITE;
|
|
51 }
|
|
52
|
|
53 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
|
|
54 return NGX_TIMER_ERROR;
|
|
55 }
|
|
56
|
|
57 node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree,
|
|
58 &ngx_event_timer_sentinel);
|
|
59
|
|
60 ngx_mutex_unlock(ngx_event_timer_mutex);
|
|
61
|
|
62 timer = (ngx_msec_t)
|
|
63 (node->key * NGX_TIMER_RESOLUTION -
|
|
64 ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION);
|
|
65 #if 0
|
|
66 (node->key * NGX_TIMER_RESOLUTION - ngx_elapsed_msec);
|
|
67 #endif
|
|
68
|
|
69 return timer > 0 ? timer: 0 ;
|
|
70 }
|
|
71
|
|
72
|
42
|
73 void
|
|
74 ngx_event_expire_timers(ngx_msec_t timer)
|
0
|
75 {
|
|
76 ngx_event_t *ev;
|
|
77 ngx_rbtree_t *node;
|
|
78
|
|
79 if (timer < 0) {
|
|
80 /* avoid the endless loop if the time goes backward for some reason */
|
|
81 timer = 0;
|
|
82 }
|
|
83
|
|
84 for ( ;; ) {
|
|
85
|
|
86 if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
|
|
87 return;
|
|
88 }
|
|
89
|
|
90 if (ngx_mutex_lock(ngx_event_timer_mutex) == NGX_ERROR) {
|
|
91 return;
|
|
92 }
|
|
93
|
|
94 node = ngx_rbtree_min((ngx_rbtree_t *) ngx_event_timer_rbtree,
|
|
95 &ngx_event_timer_sentinel);
|
|
96
|
|
97 if (node->key <= (ngx_msec_t)
|
|
98 (ngx_old_elapsed_msec + timer) / NGX_TIMER_RESOLUTION)
|
|
99 {
|
|
100 ev = (ngx_event_t *)
|
|
101 ((char *) node - offsetof(ngx_event_t, rbtree_key));
|
|
102
|
|
103 #if (NGX_THREADS)
|
|
104
|
|
105 if (ngx_threaded && ngx_trylock(ev->lock) == 0) {
|
|
106
|
|
107 /*
|
|
108 * We can not change the timer of the event that is been
|
|
109 * handling by another thread. And we can not easy walk
|
|
110 * the rbtree to find a next expired timer so we exit the loop.
|
|
111 * However it should be rare case when the event that is
|
|
112 * been handling has expired timer.
|
|
113 */
|
|
114
|
|
115 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
10
|
116 "event %p is busy in expire timers", ev);
|
0
|
117 break;
|
|
118 }
|
|
119 #endif
|
|
120
|
|
121 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
10
|
122 "event timer del: %d: %i",
|
0
|
123 ngx_event_ident(ev->data), ev->rbtree_key);
|
|
124
|
|
125 ngx_rbtree_delete((ngx_rbtree_t **) &ngx_event_timer_rbtree,
|
|
126 &ngx_event_timer_sentinel,
|
|
127 (ngx_rbtree_t *) &ev->rbtree_key);
|
|
128
|
|
129 ngx_mutex_unlock(ngx_event_timer_mutex);
|
|
130
|
|
131 #if (NGX_DEBUG)
|
|
132 ev->rbtree_left = NULL;
|
|
133 ev->rbtree_right = NULL;
|
|
134 ev->rbtree_parent = NULL;
|
|
135 #endif
|
|
136
|
|
137 ev->timer_set = 0;
|
|
138
|
|
139 #if (NGX_THREADS)
|
|
140 if (ngx_threaded) {
|
|
141 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
142 return;
|
|
143 }
|
|
144
|
|
145 ev->posted_timedout = 1;
|
|
146 ngx_post_event(ev);
|
|
147
|
|
148 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
149
|
|
150 ngx_unlock(ev->lock);
|
|
151
|
|
152 continue;
|
|
153 }
|
|
154 #endif
|
|
155
|
|
156 ev->timedout = 1;
|
|
157
|
|
158 ev->event_handler(ev);
|
|
159
|
|
160 continue;
|
|
161 }
|
|
162
|
|
163 break;
|
|
164 }
|
|
165
|
|
166 ngx_mutex_unlock(ngx_event_timer_mutex);
|
|
167 }
|