Mercurial > hg > nginx
comparison src/event/ngx_event.c @ 209:e1c815be05ae
nginx-0.0.1-2003-12-09-18:08:11 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 09 Dec 2003 15:08:11 +0000 |
parents | 4a9a2b1dd6fa |
children | f1d0e5f09c1e |
comparison
equal
deleted
inserted
replaced
208:0b67be7d4489 | 209:e1c815be05ae |
---|---|
21 #include <ngx_aio_module.h> | 21 #include <ngx_aio_module.h> |
22 #endif | 22 #endif |
23 | 23 |
24 static int ngx_event_init(ngx_cycle_t *cycle); | 24 static int ngx_event_init(ngx_cycle_t *cycle); |
25 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 25 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
26 | |
27 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, | |
28 void *conf); | |
26 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 29 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
30 | |
27 static void *ngx_event_create_conf(ngx_cycle_t *cycle); | 31 static void *ngx_event_create_conf(ngx_cycle_t *cycle); |
28 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf); | 32 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf); |
29 | 33 |
30 | 34 |
31 int ngx_event_flags; | 35 int ngx_event_flags; |
38 | 42 |
39 static ngx_str_t events_name = ngx_string("events"); | 43 static ngx_str_t events_name = ngx_string("events"); |
40 | 44 |
41 static ngx_command_t ngx_events_commands[] = { | 45 static ngx_command_t ngx_events_commands[] = { |
42 | 46 |
43 {ngx_string("events"), | 47 { ngx_string("events"), |
44 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | 48 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, |
45 ngx_events_block, | 49 ngx_events_block, |
46 0, | 50 0, |
47 0, | 51 0, |
48 NULL}, | 52 NULL }, |
49 | 53 |
50 ngx_null_command | 54 ngx_null_command |
51 }; | 55 }; |
52 | 56 |
53 | 57 |
54 ngx_module_t ngx_events_module = { | 58 ngx_module_t ngx_events_module = { |
55 NGX_MODULE, | 59 NGX_MODULE, |
63 | 67 |
64 static ngx_str_t event_core_name = ngx_string("event_core"); | 68 static ngx_str_t event_core_name = ngx_string("event_core"); |
65 | 69 |
66 static ngx_command_t ngx_event_core_commands[] = { | 70 static ngx_command_t ngx_event_core_commands[] = { |
67 | 71 |
68 {ngx_string("connections"), | 72 { ngx_string("connections"), |
69 NGX_EVENT_CONF|NGX_CONF_TAKE1, | 73 NGX_EVENT_CONF|NGX_CONF_TAKE1, |
70 ngx_conf_set_num_slot, | 74 ngx_event_connections, |
71 0, | 75 0, |
72 offsetof(ngx_event_conf_t, connections), | 76 0, |
73 NULL}, | 77 NULL }, |
74 | 78 |
75 {ngx_string("use"), | 79 { ngx_string("use"), |
76 NGX_EVENT_CONF|NGX_CONF_TAKE1, | 80 NGX_EVENT_CONF|NGX_CONF_TAKE1, |
77 ngx_event_use, | 81 ngx_event_use, |
78 0, | 82 0, |
79 0, | 83 0, |
80 NULL}, | 84 NULL }, |
81 | 85 |
82 {ngx_string("timer_queues"), | 86 ngx_null_command |
83 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
84 ngx_conf_set_num_slot, | |
85 0, | |
86 offsetof(ngx_event_conf_t, timer_queues), | |
87 NULL}, | |
88 | |
89 ngx_null_command | |
90 }; | 87 }; |
91 | 88 |
92 | 89 |
93 ngx_event_module_t ngx_event_core_module_ctx = { | 90 ngx_event_module_t ngx_event_core_module_ctx = { |
94 &event_core_name, | 91 &event_core_name, |
119 ngx_event_module_t *module; | 116 ngx_event_module_t *module; |
120 #if (WIN32) | 117 #if (WIN32) |
121 ngx_iocp_conf_t *iocpcf; | 118 ngx_iocp_conf_t *iocpcf; |
122 #endif | 119 #endif |
123 | 120 |
121 | |
122 if (cycle->old_cycle == NULL) { | |
123 ngx_event_timer_init(cycle); | |
124 } | |
125 | |
126 | |
124 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); | 127 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); |
125 | |
126 ngx_log_debug(cycle->log, "CONN: %d" _ ecf->connections); | |
127 ngx_log_debug(cycle->log, "TYPE: %d" _ ecf->use); | |
128 | 128 |
129 cycle->connection_n = ecf->connections; | 129 cycle->connection_n = ecf->connections; |
130 | 130 |
131 for (m = 0; ngx_modules[m]; m++) { | 131 for (m = 0; ngx_modules[m]; m++) { |
132 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | 132 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { |
140 } | 140 } |
141 break; | 141 break; |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 if (cycle->old_cycle && cycle->old_cycle->connection_n < ecf->connections) { | 145 cycle->connections = ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, |
146 /* TODO: push into delayed array and temporary pool */ | 146 cycle->log); |
147 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "NOT READY: connections"); | 147 if (cycle->connections == NULL) { |
148 exit(1); | 148 return NGX_ERROR; |
149 } | 149 } |
150 | |
151 ngx_test_null(cycle->connections, | |
152 ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, | |
153 cycle->log), | |
154 NGX_ERROR); | |
155 | 150 |
156 c = cycle->connections; | 151 c = cycle->connections; |
157 for (i = 0; i < cycle->connection_n; i++) { | 152 for (i = 0; i < cycle->connection_n; i++) { |
158 c[i].fd = -1; | 153 c[i].fd = -1; |
159 } | 154 } |
160 | 155 |
161 ngx_test_null(cycle->read_events, | 156 cycle->read_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections, |
162 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), | 157 cycle->log); |
163 NGX_ERROR); | 158 if (cycle->read_events == NULL) { |
164 | 159 return NGX_ERROR; |
165 ngx_test_null(cycle->write_events, | 160 } |
166 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), | 161 |
167 NGX_ERROR); | 162 cycle->write_events = ngx_alloc(sizeof(ngx_event_t) * ecf->connections, |
163 cycle->log); | |
164 if (cycle->write_events == NULL) { | |
165 return NGX_ERROR; | |
166 } | |
168 | 167 |
169 /* for each listening socket */ | 168 /* for each listening socket */ |
170 | 169 |
171 s = cycle->listening.elts; | 170 s = cycle->listening.elts; |
172 for (i = 0; i < cycle->listening.nelts; i++) { | 171 for (i = 0; i < cycle->listening.nelts; i++) { |
198 c->read = rev; | 197 c->read = rev; |
199 | 198 |
200 /* required by iocp in "c->write->active = 1" */ | 199 /* required by iocp in "c->write->active = 1" */ |
201 c->write = wev; | 200 c->write = wev; |
202 | 201 |
202 /* required by poll */ | |
203 wev->index = NGX_INVALID_INDEX; | |
204 | |
203 rev->log = c->log; | 205 rev->log = c->log; |
204 rev->data = c; | 206 rev->data = c; |
205 rev->index = NGX_INVALID_INDEX; | 207 rev->index = NGX_INVALID_INDEX; |
206 | 208 |
207 rev->available = 0; | 209 rev->available = 0; |
208 | 210 |
209 #if (HAVE_DEFERRED_ACCEPT) | 211 #if (HAVE_DEFERRED_ACCEPT) |
210 rev->deferred_accept = s[i].deferred_accept; | 212 rev->deferred_accept = s[i].deferred_accept; |
211 #endif | 213 #endif |
212 | 214 |
213 /* required by poll */ | 215 if (!(ngx_event_flags & NGX_USE_IOCP_EVENT)) { |
214 wev->index = NGX_INVALID_INDEX; | |
215 | |
216 if ((ngx_event_flags & NGX_USE_IOCP_EVENT) == 0) { | |
217 if (s[i].remain) { | 216 if (s[i].remain) { |
217 | |
218 /* | |
219 * delete the old accept events that were bound to | |
220 * the old cycle read events array | |
221 */ | |
218 | 222 |
219 if (ngx_del_event(&cycle->old_cycle->read_events[fd], | 223 if (ngx_del_event(&cycle->old_cycle->read_events[fd], |
220 NGX_READ_EVENT, 0) == NGX_ERROR) { | 224 NGX_READ_EVENT, 0) == NGX_ERROR) { |
221 return NGX_ERROR; | 225 return NGX_ERROR; |
222 } | 226 } |
325 | 329 |
326 return NGX_CONF_OK; | 330 return NGX_CONF_OK; |
327 } | 331 } |
328 | 332 |
329 | 333 |
334 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd, | |
335 void *conf) | |
336 { | |
337 ngx_event_conf_t *ecf = conf; | |
338 | |
339 ngx_str_t *value; | |
340 | |
341 if (ecf->connections != NGX_CONF_UNSET) { | |
342 return "is duplicate" ; | |
343 } | |
344 | |
345 value = cf->args->elts; | |
346 ecf->connections = ngx_atoi(value[1].data, value[1].len); | |
347 if (ecf->connections == NGX_ERROR) { | |
348 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
349 "invalid number \"%s\"", value[1].data); | |
350 | |
351 return NGX_CONF_ERROR; | |
352 } | |
353 | |
354 cf->cycle->connection_n = ecf->connections; | |
355 | |
356 return NGX_CONF_OK; | |
357 } | |
358 | |
359 | |
330 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 360 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
331 { | 361 { |
332 ngx_event_conf_t *ecf = conf; | 362 ngx_event_conf_t *ecf = conf; |
333 | 363 |
334 int m; | 364 int m; |
335 ngx_str_t *args; | 365 ngx_str_t *value; |
366 ngx_event_conf_t *old_ecf; | |
336 ngx_event_module_t *module; | 367 ngx_event_module_t *module; |
337 | 368 |
338 if (ecf->use != NGX_CONF_UNSET) { | 369 if (ecf->use != NGX_CONF_UNSET) { |
339 return "is duplicate" ; | 370 return "is duplicate" ; |
340 } | 371 } |
341 | 372 |
342 args = cf->args->elts; | 373 value = cf->args->elts; |
374 | |
375 if (cf->cycle->old_cycle) { | |
376 old_ecf = ngx_event_get_conf(cf->cycle->old_cycle->conf_ctx, | |
377 ngx_event_core_module); | |
378 } else { | |
379 old_ecf = NULL; | |
380 } | |
343 | 381 |
344 for (m = 0; ngx_modules[m]; m++) { | 382 for (m = 0; ngx_modules[m]; m++) { |
345 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | 383 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { |
346 continue; | 384 continue; |
347 } | 385 } |
348 | 386 |
349 module = ngx_modules[m]->ctx; | 387 module = ngx_modules[m]->ctx; |
350 if (module->name->len == args[1].len) { | 388 if (module->name->len == value[1].len) { |
351 if (ngx_strcmp(module->name->data, args[1].data) == 0) { | 389 if (ngx_strcmp(module->name->data, value[1].data) == 0) { |
352 ecf->use = ngx_modules[m]->ctx_index; | 390 ecf->use = ngx_modules[m]->ctx_index; |
391 ecf->name = module->name->data; | |
392 | |
393 if (old_ecf && old_ecf->use != ecf->use) { | |
394 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
395 "the \"%s\" event type must be the same as " | |
396 "in previous configuration - \"%s\" " | |
397 "and it can not be changed on the fly, " | |
398 "to change it you need to stop server " | |
399 "and start it again", | |
400 value[1].data, old_ecf->name); | |
401 return NGX_CONF_ERROR; | |
402 } | |
403 | |
353 return NGX_CONF_OK; | 404 return NGX_CONF_OK; |
354 } | 405 } |
355 } | 406 } |
356 } | 407 } |
357 | 408 |
358 return "invalid event type"; | 409 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
410 "invalid event type \"%s\"", value[1].data); | |
411 | |
412 return NGX_CONF_ERROR; | |
359 } | 413 } |
360 | 414 |
361 | 415 |
362 static void *ngx_event_create_conf(ngx_cycle_t *cycle) | 416 static void *ngx_event_create_conf(ngx_cycle_t *cycle) |
363 { | 417 { |
367 NGX_CONF_ERROR); | 421 NGX_CONF_ERROR); |
368 | 422 |
369 ecf->connections = NGX_CONF_UNSET; | 423 ecf->connections = NGX_CONF_UNSET; |
370 ecf->timer_queues = NGX_CONF_UNSET; | 424 ecf->timer_queues = NGX_CONF_UNSET; |
371 ecf->use = NGX_CONF_UNSET; | 425 ecf->use = NGX_CONF_UNSET; |
426 ecf->name = (void *) NGX_CONF_UNSET; | |
372 | 427 |
373 return ecf; | 428 return ecf; |
374 } | 429 } |
375 | 430 |
376 | 431 |
380 | 435 |
381 #if (HAVE_KQUEUE) | 436 #if (HAVE_KQUEUE) |
382 | 437 |
383 ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS); | 438 ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS); |
384 ngx_conf_init_value(ecf->use, ngx_kqueue_module.ctx_index); | 439 ngx_conf_init_value(ecf->use, ngx_kqueue_module.ctx_index); |
440 ngx_conf_init_ptr_value(ecf->name, ngx_kqueue_module_ctx.name->data); | |
385 | 441 |
386 #elif (HAVE_DEVPOLL) | 442 #elif (HAVE_DEVPOLL) |
387 | 443 |
388 ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS); | 444 ngx_conf_init_value(ecf->connections, DEFAULT_CONNECTIONS); |
389 ngx_conf_init_value(ecf->use, ngx_devpoll_module.ctx_index); | 445 ngx_conf_init_value(ecf->use, ngx_devpoll_module.ctx_index); |
446 ngx_conf_init_ptr_value(ecf->name, ngx_devpoll_module_ctx.name->data); | |
390 | 447 |
391 #else /* HAVE_SELECT */ | 448 #else /* HAVE_SELECT */ |
392 | 449 |
393 ngx_conf_init_value(ecf->connections, | 450 ngx_conf_init_value(ecf->connections, |
394 FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS); | 451 FD_SETSIZE < DEFAULT_CONNECTIONS ? FD_SETSIZE : DEFAULT_CONNECTIONS); |
395 | 452 |
396 ngx_conf_init_value(ecf->use, ngx_select_module.ctx_index); | 453 ngx_conf_init_value(ecf->use, ngx_select_module.ctx_index); |
397 | 454 ngx_conf_init_ptr_value(ecf->name, ngx_select_module_ctx.name->data); |
398 #endif | 455 |
456 #endif | |
457 | |
458 cycle->connection_n = ecf->connections; | |
399 | 459 |
400 ngx_conf_init_value(ecf->timer_queues, 10); | 460 ngx_conf_init_value(ecf->timer_queues, 10); |
401 | 461 |
402 return NGX_CONF_OK; | 462 return NGX_CONF_OK; |
403 } | 463 } |