comparison src/event/modules/ngx_select_module.c @ 114:ac69ab96328d

nginx-0.0.1-2003-07-07-10:11:50 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 07 Jul 2003 06:11:50 +0000
parents b5be4b0448d3
children cd54bcbaf3b5
comparison
equal deleted inserted replaced
113:d7f606e25b99 114:ac69ab96328d
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 static int ngx_select_init(ngx_log_t *log); 12 static int ngx_select_init(ngx_cycle_t *cycle);
13 static void ngx_select_done(ngx_log_t *log); 13 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); 14 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
15 static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags); 15 static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags);
16 static int ngx_select_process_events(ngx_log_t *log); 16 static int ngx_select_process_events(ngx_log_t *log);
17 17
18 static char *ngx_select_init_conf(ngx_pool_t *pool, void *conf); 18 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf);
19 19
20 20
21 static fd_set master_read_fd_set; 21 static fd_set master_read_fd_set;
22 static fd_set master_write_fd_set; 22 static fd_set master_write_fd_set;
23 static fd_set work_read_fd_set; 23 static fd_set work_read_fd_set;
60 ngx_module_t ngx_select_module = { 60 ngx_module_t ngx_select_module = {
61 NGX_MODULE, 61 NGX_MODULE,
62 &ngx_select_module_ctx, /* module context */ 62 &ngx_select_module_ctx, /* module context */
63 NULL, /* module directives */ 63 NULL, /* module directives */
64 NGX_EVENT_MODULE, /* module type */ 64 NGX_EVENT_MODULE, /* module type */
65 NULL /* init module */ 65 NULL, /* init module */
66 NULL /* init child */
66 }; 67 };
67 68
68 69
69 static int ngx_select_init(ngx_log_t *log) 70 static int ngx_select_init(ngx_cycle_t *cycle)
70 { 71 {
71 ngx_event_conf_t *ecf; 72 ngx_event_t **index;
72 73
73 ecf = ngx_event_get_conf(ngx_event_core_module); 74 if (event_index == NULL) {
74 75 FD_ZERO(&master_read_fd_set);
75 FD_ZERO(&master_read_fd_set); 76 FD_ZERO(&master_write_fd_set);
76 FD_ZERO(&master_write_fd_set); 77 nevents = 0;
77 78 }
78 ngx_test_null(event_index, 79
79 ngx_alloc(sizeof(ngx_event_t *) * 2 * ecf->connections, log), 80 if (cycle->old_cycle == NULL
80 NGX_ERROR); 81 || cycle->old_cycle->connection_n < cycle->connection_n)
81 82 {
82 ngx_test_null(ready_index, 83 ngx_test_null(index,
83 ngx_alloc(sizeof(ngx_event_t *) * 2 * ecf->connections, log), 84 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
84 NGX_ERROR); 85 cycle->log),
85 86 NGX_ERROR);
86 nevents = 0; 87
87 88 if (event_index) {
88 if (ngx_event_timer_init(log) == NGX_ERROR) { 89 ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
90 ngx_free(event_index);
91 }
92 event_index = index;
93
94 if (ready_index) {
95 ngx_free(ready_index);
96 }
97 ngx_test_null(ready_index,
98 ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
99 cycle->log),
100 NGX_ERROR);
101 }
102
103 if (ngx_event_timer_init(cycle) == NGX_ERROR) {
89 return NGX_ERROR; 104 return NGX_ERROR;
90 } 105 }
106
107 ngx_io = ngx_os_io;
91 108
92 ngx_event_actions = ngx_select_module_ctx.actions; 109 ngx_event_actions = ngx_select_module_ctx.actions;
93 110
94 ngx_event_flags = NGX_HAVE_LEVEL_EVENT 111 ngx_event_flags = NGX_HAVE_LEVEL_EVENT
95 |NGX_HAVE_ONESHOT_EVENT 112 |NGX_HAVE_ONESHOT_EVENT
103 120
104 return NGX_OK; 121 return NGX_OK;
105 } 122 }
106 123
107 124
108 static void ngx_select_done(ngx_log_t *log) 125 static void ngx_select_done(ngx_cycle_t *cycle)
109 { 126 {
110 ngx_event_timer_done(log); 127 ngx_event_timer_done(cycle);
111 128
112 ngx_free(event_index); 129 ngx_free(event_index);
113 ngx_free(ready_index); 130 ngx_free(ready_index);
131
132 event_index = NULL;
114 } 133 }
115 134
116 135
117 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags) 136 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags)
118 { 137 {
227 246
228 static int ngx_select_process_events(ngx_log_t *log) 247 static int ngx_select_process_events(ngx_log_t *log)
229 { 248 {
230 int ready, found; 249 int ready, found;
231 u_int i, nready; 250 u_int i, nready;
251 ngx_err_t err;
232 ngx_msec_t timer, delta; 252 ngx_msec_t timer, delta;
233 ngx_event_t *ev; 253 ngx_event_t *ev;
234 ngx_connection_t *c; 254 ngx_connection_t *c;
235 struct timeval tv, *tp; 255 struct timeval tv, *tp;
236 256
275 295
276 ngx_log_debug(log, "select timer: %d" _ timer); 296 ngx_log_debug(log, "select timer: %d" _ timer);
277 #endif 297 #endif
278 298
279 #if (WIN32) 299 #if (WIN32)
280 if ((ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp)) 300 ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp);
281 #else 301 #else
282 if ((ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, 302 ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp);
283 NULL, tp)) 303 #endif
284 #endif 304
285 == -1) 305 if (ready == -1) {
286 { 306 err = ngx_socket_errno;
287 ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, "select() failed"); 307 } else {
288 return NGX_ERROR; 308 err = 0;
289 } 309 }
290 310
291 #if (NGX_DEBUG_EVENT) 311 #if (NGX_DEBUG_EVENT)
292 ngx_log_debug(log, "select ready %d" _ ready); 312 ngx_log_debug(log, "select ready %d" _ ready);
293 #endif 313 #endif
294 314
295 if (timer) { 315 if (timer) {
296 /* TODO: Linux returns time in tv */ 316 /* TODO: Linux returns time in tv */
297 delta = ngx_msec() - delta; 317 delta = ngx_msec() - delta;
318
319 #if (NGX_DEBUG_EVENT)
320 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ delta);
321 #endif
298 ngx_event_expire_timers(delta); 322 ngx_event_expire_timers(delta);
299 323
300 } else { 324 } else {
301 if (ready == 0) { 325 if (ready == 0) {
302 ngx_log_error(NGX_LOG_ALERT, log, 0, 326 ngx_log_error(NGX_LOG_ALERT, log, 0,
303 "select() returns no events without timeout"); 327 "select() returns no events without timeout");
304 return NGX_ERROR; 328 return NGX_ERROR;
305 } 329 }
306 } 330
307 331 #if (NGX_DEBUG_EVENT)
308 #if (NGX_DEBUG_EVENT) 332 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ delta);
309 ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ delta); 333 #endif
310 #endif 334 ngx_event_expire_timers(delta);
335 }
336
337 if (err) {
338 ngx_log_error(NGX_LOG_ALERT, log, err, "select() failed");
339 return NGX_ERROR;
340 }
311 341
312 nready = 0; 342 nready = 0;
313 343
314 for (i = 0; i < nevents; i++) { 344 for (i = 0; i < nevents; i++) {
315 ev = event_index[i]; 345 ev = event_index[i];
370 400
371 return NGX_OK; 401 return NGX_OK;
372 } 402 }
373 403
374 404
375 static char *ngx_select_init_conf(ngx_pool_t *pool, void *conf) 405 static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf)
376 { 406 {
377 ngx_event_conf_t *ecf; 407 ngx_event_conf_t *ecf;
378 408
379 ecf = ngx_event_get_conf(ngx_event_core_module); 409 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
380 410
381 /* the default FD_SETSIZE is 1024U in FreeBSD 5.x */ 411 /* the default FD_SETSIZE is 1024U in FreeBSD 5.x */
382 412
383 if ((unsigned) ecf->connections > FD_SETSIZE) { 413 if ((unsigned) ecf->connections > FD_SETSIZE) {
384 return "maximum number of connections " 414 return "maximum number of connections "