comparison src/event/ngx_event_busy_lock.c @ 277:e91499541410

nginx-0.0.2-2004-03-03-19:14:15 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 03 Mar 2004 16:14:15 +0000
parents a2d1ccc96111
children ee394e997c77
comparison
equal deleted inserted replaced
276:a2d1ccc96111 277:e91499541410
1 1
2 #include <ngx_config.h> 2 #include <ngx_config.h>
3 #include <ngx_core.h> 3 #include <ngx_core.h>
4 #include <ngx_event.h> 4 #include <ngx_event.h>
5
6 /*
7 * TODO: eliminate mutex and use atomic_xchg():
8 * ev->next = ev; ngx_atomic_xchg(ngx_posted_events, ev->next);
9 * in ngx_event_busy_unlock() and ngx_event_busy_lock_handler()
10 */
11 5
12 6
13 static int ngx_event_busy_lock_look_cachable(ngx_event_busy_lock_t *bl, 7 static int ngx_event_busy_lock_look_cachable(ngx_event_busy_lock_t *bl,
14 ngx_event_busy_lock_ctx_t *ctx); 8 ngx_event_busy_lock_ctx_t *ctx);
15 static void ngx_event_busy_lock_handler(ngx_event_t *ev); 9 static void ngx_event_busy_lock_handler(ngx_event_t *ev);
26 ngx_int_t ngx_event_busy_lock(ngx_event_busy_lock_t *bl, 20 ngx_int_t ngx_event_busy_lock(ngx_event_busy_lock_t *bl,
27 ngx_event_busy_lock_ctx_t *ctx) 21 ngx_event_busy_lock_ctx_t *ctx)
28 { 22 {
29 ngx_int_t rc; 23 ngx_int_t rc;
30 24
31 #if (NGX_THREADS)
32 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) { 25 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
33 return NGX_ERROR; 26 return NGX_ERROR;
34 } 27 }
35 #endif
36 28
37 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ctx->event->log, 0, 29 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ctx->event->log, 0,
38 "event busy lock: b:%d mb:%d", 30 "event busy lock: b:%d mb:%d",
39 bl->busy, bl->max_busy); 31 bl->busy, bl->max_busy);
40 32
58 50
59 } else { 51 } else {
60 rc = NGX_BUSY; 52 rc = NGX_BUSY;
61 } 53 }
62 54
63 #if (NGX_THREADS)
64 ngx_mutex_unlock(bl->mutex); 55 ngx_mutex_unlock(bl->mutex);
65 #endif
66 56
67 return rc; 57 return rc;
68 } 58 }
69 59
70 60
71 ngx_int_t ngx_event_busy_lock_cachable(ngx_event_busy_lock_t *bl, 61 ngx_int_t ngx_event_busy_lock_cachable(ngx_event_busy_lock_t *bl,
72 ngx_event_busy_lock_ctx_t *ctx) 62 ngx_event_busy_lock_ctx_t *ctx)
73 { 63 {
74 ngx_int_t rc; 64 ngx_int_t rc;
75 65
76 #if (NGX_THREADS)
77 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) { 66 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
78 return NGX_ERROR; 67 return NGX_ERROR;
79 } 68 }
80 #endif
81 69
82 rc = ngx_event_busy_lock_look_cachable(bl, ctx); 70 rc = ngx_event_busy_lock_look_cachable(bl, ctx);
83 71
84 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ctx->event->log, 0, 72 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ctx->event->log, 0,
85 "event busy lock: %d w:%d mw:%d", 73 "event busy lock: %d w:%d mw:%d",
108 } else { 96 } else {
109 rc = NGX_BUSY; 97 rc = NGX_BUSY;
110 } 98 }
111 } 99 }
112 100
113 #if (NGX_THREADS)
114 ngx_mutex_unlock(bl->mutex); 101 ngx_mutex_unlock(bl->mutex);
115 #endif
116 102
117 return rc; 103 return rc;
118 } 104 }
119 105
120 106
122 ngx_event_busy_lock_ctx_t *ctx) 108 ngx_event_busy_lock_ctx_t *ctx)
123 { 109 {
124 ngx_event_t *ev; 110 ngx_event_t *ev;
125 ngx_event_busy_lock_ctx_t *wakeup; 111 ngx_event_busy_lock_ctx_t *wakeup;
126 112
127 #if (NGX_THREADS)
128 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) { 113 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
129 return NGX_ERROR; 114 return NGX_ERROR;
130 } 115 }
131 #endif
132 116
133 if (bl->events) { 117 if (bl->events) {
134 wakeup = bl->events; 118 wakeup = bl->events;
135 bl->events = bl->events->next; 119 bl->events = bl->events->next;
136 120
138 wakeup = NULL; 122 wakeup = NULL;
139 bl->busy--; 123 bl->busy--;
140 } 124 }
141 125
142 /* 126 /*
143 * MP: 127 * MP: all ctx's and their queue must be in shared memory,
144 * nocachable (bl->md5 == NULL): ngx_shared_mutex_unlock(mutex, !wakeup) 128 * each ctx has pid to wake up
145 * cachable (bl->md5): ???
146 */ 129 */
147 130
148 if (wakeup == NULL) { 131 if (wakeup == NULL) {
149 #if (NGX_THREADS)
150 ngx_mutex_unlock(bl->mutex); 132 ngx_mutex_unlock(bl->mutex);
151 #endif
152 return NGX_OK; 133 return NGX_OK;
153 } 134 }
154 135
155 if (ctx->md5) { 136 if (ctx->md5) {
156 for (wakeup = bl->events; wakeup; wakeup = wakeup->next) { 137 for (wakeup = bl->events; wakeup; wakeup = wakeup->next) {
157 if (wakeup->md5 == NULL) { 138 if (wakeup->md5 == NULL || wakeup->slot != ctx->slot) {
158 continue; 139 continue;
159 } 140 }
160 141
161 if (ngx_memcmp(ctx->md5, wakeup->md5, 16) != 0) {
162 continue;
163 }
164
165 wakeup->handler = ngx_event_busy_lock_posted_handler; 142 wakeup->handler = ngx_event_busy_lock_posted_handler;
166 wakeup->cache_updated = 1; 143 wakeup->cache_updated = 1;
167 144
168 ev = wakeup->event; 145 ev = wakeup->event;
169 146
170 #if (NGX_THREADS)
171 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { 147 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
172 return NGX_ERROR; 148 return NGX_ERROR;
173 } 149 }
174 #endif
175 150
176 ev->next = (ngx_event_t *) ngx_posted_events; 151 ev->next = (ngx_event_t *) ngx_posted_events;
177 ngx_posted_events = ev; 152 ngx_posted_events = ev;
178 153
179 #if (NGX_THREADS)
180 ngx_mutex_unlock(ngx_posted_events_mutex); 154 ngx_mutex_unlock(ngx_posted_events_mutex);
181 #endif 155 }
182 } 156
183
184 #if (NGX_THREADS)
185 ngx_mutex_unlock(bl->mutex); 157 ngx_mutex_unlock(bl->mutex);
186 #endif
187 158
188 } else { 159 } else {
189 bl->waiting--; 160 bl->waiting--;
190 161
191 #if (NGX_THREADS)
192 ngx_mutex_unlock(bl->mutex); 162 ngx_mutex_unlock(bl->mutex);
193 #endif
194 163
195 wakeup->handler = ngx_event_busy_lock_posted_handler; 164 wakeup->handler = ngx_event_busy_lock_posted_handler;
196 wakeup->locked = 1; 165 wakeup->locked = 1;
197 166
198 ev = wakeup->event; 167 ev = wakeup->event;
199 168
200 if (ev->timer_set) { 169 if (ev->timer_set) {
201 ngx_del_timer(ev); 170 ngx_del_timer(ev);
202 } 171 }
203 172
204 #if (NGX_THREADS)
205 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { 173 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
206 return NGX_ERROR; 174 return NGX_ERROR;
207 } 175 }
208 #endif
209 176
210 ev->next = (ngx_event_t *) ngx_posted_events; 177 ev->next = (ngx_event_t *) ngx_posted_events;
211 ngx_posted_events = ev; 178 ngx_posted_events = ev;
212 179
213 #if (NGX_THREADS)
214 ngx_mutex_unlock(ngx_posted_events_mutex); 180 ngx_mutex_unlock(ngx_posted_events_mutex);
215 #endif
216 } 181 }
217 182
218 return NGX_OK; 183 return NGX_OK;
219 } 184 }
220 185
222 ngx_int_t ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl, 187 ngx_int_t ngx_event_busy_lock_cancel(ngx_event_busy_lock_t *bl,
223 ngx_event_busy_lock_ctx_t *ctx) 188 ngx_event_busy_lock_ctx_t *ctx)
224 { 189 {
225 ngx_event_busy_lock_ctx_t *c, *p; 190 ngx_event_busy_lock_ctx_t *c, *p;
226 191
227 #if (NGX_THREADS)
228 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) { 192 if (ngx_mutex_lock(bl->mutex) == NGX_ERROR) {
229 return NGX_ERROR; 193 return NGX_ERROR;
230 } 194 }
231 #endif
232 195
233 bl->waiting--; 196 bl->waiting--;
234 197
235 if (ctx == bl->events) { 198 if (ctx == bl->events) {
236 bl->events = ctx->next; 199 bl->events = ctx->next;
244 } 207 }
245 p = c; 208 p = c;
246 } 209 }
247 } 210 }
248 211
249 #if (NGX_THREADS)
250 ngx_mutex_unlock(bl->mutex); 212 ngx_mutex_unlock(bl->mutex);
251 #endif
252 213
253 return NGX_OK; 214 return NGX_OK;
254 } 215 }
255 216
256 217
274 mask = bl->md5_mask[i / 8]; 235 mask = bl->md5_mask[i / 8];
275 } 236 }
276 237
277 if (mask & 1) { 238 if (mask & 1) {
278 if (ngx_memcmp(&bl->md5[i * 16], ctx->md5, 16) == 0) { 239 if (ngx_memcmp(&bl->md5[i * 16], ctx->md5, 16) == 0) {
240 ctx->waiting = 1;
241 ctx->slot = i;
279 return NGX_AGAIN; 242 return NGX_AGAIN;
280 } 243 }
281 cachable++; 244 cachable++;
282 245
283 } else if (free == -1) { 246 } else if (free == -1) {
317 } 280 }
318 281
319 282
320 static void ngx_event_busy_lock_handler(ngx_event_t *ev) 283 static void ngx_event_busy_lock_handler(ngx_event_t *ev)
321 { 284 {
322 ev->event_handler = ngx_event_busy_lock_posted_handler;
323
324 #if (NGX_THREADS)
325 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) { 285 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
326 return; 286 return;
327 } 287 }
328 #endif
329 288
330 ev->next = (ngx_event_t *) ngx_posted_events; 289 ev->next = (ngx_event_t *) ngx_posted_events;
331 ngx_posted_events = ev; 290 ngx_posted_events = ev;
332 291
333 #if (NGX_THREADS)
334 ngx_mutex_unlock(ngx_posted_events_mutex); 292 ngx_mutex_unlock(ngx_posted_events_mutex);
335 #endif 293
294 ev->event_handler = ngx_event_busy_lock_posted_handler;
336 } 295 }
337 296
338 297
339 static void ngx_event_busy_lock_posted_handler(ngx_event_t *ev) 298 static void ngx_event_busy_lock_posted_handler(ngx_event_t *ev)
340 { 299 {