comparison src/event/modules/ngx_select_module.c @ 314:d71c87d11b16

nginx-0.0.3-2004-04-14-09:57:36 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 14 Apr 2004 05:57:36 +0000
parents 1526e7686b20
children 39b6f2df45c0
comparison
equal deleted inserted replaced
313:98f1a8028067 314:d71c87d11b16
5 5
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10
10 11
11 12
12 static int ngx_select_init(ngx_cycle_t *cycle); 13 static int ngx_select_init(ngx_cycle_t *cycle);
13 static void ngx_select_done(ngx_cycle_t *cycle); 14 static void ngx_select_done(ngx_cycle_t *cycle);
14 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags); 15 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
28 static int max_write; 29 static int max_write;
29 #else 30 #else
30 static int max_fd; 31 static int max_fd;
31 #endif 32 #endif
32 33
33 static int nevents; 34 static ngx_uint_t nevents;
34 35
35 static ngx_event_t **event_index; 36 static ngx_event_t **event_index;
37 #if 0
36 static ngx_event_t **ready_index; 38 static ngx_event_t **ready_index;
39 #endif
40
41 static ngx_event_t *accept_events;
37 42
38 43
39 static ngx_str_t select_name = ngx_string("select"); 44 static ngx_str_t select_name = ngx_string("select");
40 45
41 ngx_event_module_t ngx_select_module_ctx = { 46 ngx_event_module_t ngx_select_module_ctx = {
75 FD_ZERO(&master_read_fd_set); 80 FD_ZERO(&master_read_fd_set);
76 FD_ZERO(&master_write_fd_set); 81 FD_ZERO(&master_write_fd_set);
77 nevents = 0; 82 nevents = 0;
78 } 83 }
79 84
80 if (cycle->old_cycle == NULL 85 if (ngx_process == NGX_PROCESS_WORKER
86 || cycle->old_cycle == NULL
81 || cycle->old_cycle->connection_n < cycle->connection_n) 87 || cycle->old_cycle->connection_n < cycle->connection_n)
82 { 88 {
83 ngx_test_null(index, 89 ngx_test_null(index,
84 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n, 90 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
85 cycle->log), 91 cycle->log),
89 ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents); 95 ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
90 ngx_free(event_index); 96 ngx_free(event_index);
91 } 97 }
92 event_index = index; 98 event_index = index;
93 99
100 #if 0
94 if (ready_index) { 101 if (ready_index) {
95 ngx_free(ready_index); 102 ngx_free(ready_index);
96 } 103 }
97 ngx_test_null(ready_index, 104 ngx_test_null(ready_index,
98 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n, 105 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
99 cycle->log), 106 cycle->log),
100 NGX_ERROR); 107 NGX_ERROR);
108 #endif
101 } 109 }
102 110
103 ngx_io = ngx_os_io; 111 ngx_io = ngx_os_io;
104 112
105 ngx_event_actions = ngx_select_module_ctx.actions; 113 ngx_event_actions = ngx_select_module_ctx.actions;
117 125
118 126
119 static void ngx_select_done(ngx_cycle_t *cycle) 127 static void ngx_select_done(ngx_cycle_t *cycle)
120 { 128 {
121 ngx_free(event_index); 129 ngx_free(event_index);
130 #if 0
122 ngx_free(ready_index); 131 ngx_free(ready_index);
132 #endif
123 133
124 event_index = NULL; 134 event_index = NULL;
125 } 135 }
126 136
127 137
190 { 200 {
191 ngx_connection_t *c; 201 ngx_connection_t *c;
192 202
193 c = ev->data; 203 c = ev->data;
194 204
205 ev->active = 0;
206 ev->posted = 0;
207
195 if (ev->index == NGX_INVALID_INDEX) { 208 if (ev->index == NGX_INVALID_INDEX) {
196 return NGX_OK; 209 return NGX_OK;
197 } 210 }
198 211
199 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, 212 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
228 if (ev->index < (u_int) --nevents) { 241 if (ev->index < (u_int) --nevents) {
229 event_index[ev->index] = event_index[nevents]; 242 event_index[ev->index] = event_index[nevents];
230 event_index[ev->index]->index = ev->index; 243 event_index[ev->index]->index = ev->index;
231 } 244 }
232 245
233 ev->active = 0;
234 ev->index = NGX_INVALID_INDEX; 246 ev->index = NGX_INVALID_INDEX;
235 247
236 return NGX_OK; 248 return NGX_OK;
237 } 249 }
238 250
239 251
240 static int ngx_select_process_events(ngx_cycle_t *cycle) 252 static int ngx_select_process_events(ngx_cycle_t *cycle)
241 { 253 {
242 int i, ready, nready,found; 254 int ready, nready;
243 ngx_err_t err; 255 ngx_uint_t i, found, lock, expire;
244 ngx_msec_t timer; 256 ngx_err_t err;
245 ngx_event_t *ev; 257 ngx_msec_t timer;
246 ngx_connection_t *c; 258 ngx_event_t *ev;
247 ngx_epoch_msec_t delta; 259 ngx_connection_t *c;
248 struct timeval tv, *tp; 260 ngx_epoch_msec_t delta;
261 struct timeval tv, *tp;
249 #if (HAVE_SELECT_CHANGE_TIMEOUT) 262 #if (HAVE_SELECT_CHANGE_TIMEOUT)
250 static ngx_epoch_msec_t deltas = 0; 263 static ngx_epoch_msec_t deltas = 0;
251 #endif 264 #endif
252 265
253 work_read_fd_set = master_read_fd_set; 266 work_read_fd_set = master_read_fd_set;
254 work_write_fd_set = master_write_fd_set; 267 work_write_fd_set = master_write_fd_set;
255 268
258 271
259 if (timer) { 272 if (timer) {
260 tv.tv_sec = timer / 1000; 273 tv.tv_sec = timer / 1000;
261 tv.tv_usec = (timer % 1000) * 1000; 274 tv.tv_usec = (timer % 1000) * 1000;
262 tp = &tv; 275 tp = &tv;
276 expire = 1;
263 277
264 } else { 278 } else {
265 tp = NULL; 279 tp = NULL;
266 } 280 expire = 0;
267 281 }
268 #if !(WIN32)
269 if (max_fd == -1) {
270 for (i = 0; i < nevents; i++) {
271 c = event_index[i]->data;
272 if (max_fd < c->fd) {
273 max_fd = c->fd;
274 }
275 }
276
277 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
278 "change max_fd: %d", max_fd);
279 }
280 #endif
281 282
282 #if (NGX_DEBUG) 283 #if (NGX_DEBUG)
283 for (i = 0; i < nevents; i++) { 284 for (i = 0; i < nevents; i++) {
284 ev = event_index[i]; 285 ev = event_index[i];
285 c = ev->data; 286 c = ev->data;
286 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 287 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
287 "select event: fd:%d wr:%d", c->fd,ev->write); 288 "select event: fd:%d wr:%d", c->fd,ev->write);
288 } 289 }
289 #endif 290 #endif
290 291
292 #if !(WIN32)
293
294 if (max_fd == -1) {
295 for (i = 0; i < nevents; i++) {
296 c = event_index[i]->data;
297 if (max_fd < c->fd) {
298 max_fd = c->fd;
299 }
300 }
301
302 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
303 "change max_fd: %d", max_fd);
304 }
305
306 if (ngx_accept_mutex) {
307 if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
308 return NGX_ERROR;
309 }
310
311 if (ngx_accept_mutex_held == 0 && timer > ngx_accept_mutex_delay) {
312 timer = ngx_accept_mutex_delay;
313 expire = 0;
314 }
315 }
316
317 #endif
318
291 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 319 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
292 "select timer: %d", timer); 320 "select timer: %d", timer);
293 321
294 #if (WIN32) 322 #if (WIN32)
295 ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp); 323 ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp);
370 cycle->log, err, "select() failed"); 398 cycle->log, err, "select() failed");
371 #endif 399 #endif
372 return NGX_ERROR; 400 return NGX_ERROR;
373 } 401 }
374 402
403
404 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
405 ngx_accept_mutex_unlock();
406 return NGX_ERROR;
407 }
408
409 lock = 1;
375 nready = 0; 410 nready = 0;
376 411
377 for (i = 0; i < nevents; i++) { 412 for (i = 0; i < nevents; i++) {
378 ev = event_index[i]; 413 ev = event_index[i];
379 c = ev->data; 414 c = ev->data;
393 "select read %d", c->fd); 428 "select read %d", c->fd);
394 } 429 }
395 } 430 }
396 431
397 if (found) { 432 if (found) {
433 ev->ready = 1;
434
435 if (ev->oneshot) {
436 if (ev->timer_set) {
437 ngx_del_timer(ev);
438 }
439
440 if (ev->write) {
441 ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
442 } else {
443 ngx_select_del_event(ev, NGX_READ_EVENT, 0);
444 }
445 }
446
447 if (ev->accept) {
448 ev->next = accept_events;
449 accept_events = ev;
450 } else {
451 ngx_post_event(ev);
452 }
453
454 nready++;
455
456 #if 0
398 ready_index[nready++] = ev; 457 ready_index[nready++] = ev;
399 } 458 #endif
400 } 459 }
401 460 }
461
462 #if 0
402 for (i = 0; i < nready; i++) { 463 for (i = 0; i < nready; i++) {
403 ev = ready_index[i]; 464 ev = ready_index[i];
404 ready--; 465 ready--;
405 466
406 if (!ev->active) { 467 if (!ev->active) {
421 } 482 }
422 } 483 }
423 484
424 ev->event_handler(ev); 485 ev->event_handler(ev);
425 } 486 }
426 487 #endif
427 if (ready != 0) { 488
489 ev = accept_events;
490
491 for ( ;; ) {
492
493 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
494 "accept event " PTR_FMT, ev);
495
496 if (ev == NULL) {
497 break;
498 }
499
500 ngx_mutex_unlock(ngx_posted_events_mutex);
501
502 ev->event_handler(ev);
503
504 ev = ev->next;
505
506 if (ev == NULL) {
507 lock = 0;
508 break;
509 }
510
511 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
512 ngx_accept_mutex_unlock();
513 return NGX_ERROR;
514 }
515
516 }
517
518 if (lock) {
519 ngx_mutex_unlock(ngx_posted_events_mutex);
520 }
521
522 ngx_accept_mutex_unlock();
523 accept_events = NULL;
524
525 if (ready != nready) {
428 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "select ready != events"); 526 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "select ready != events");
429 } 527 }
430 528
431 if (timer && delta) { 529 if (expire && delta) {
432 ngx_event_expire_timers((ngx_msec_t) delta); 530 ngx_event_expire_timers((ngx_msec_t) delta);
531 }
532
533 if (!ngx_threaded) {
534 ngx_event_process_posted(cycle);
433 } 535 }
434 536
435 return NGX_OK; 537 return NGX_OK;
436 } 538 }
437 539