Mercurial > hg > nginx
comparison src/event/ngx_event.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 | d7f606e25b99 |
children | ef8c87afcfc5 |
comparison
equal
deleted
inserted
replaced
113:d7f606e25b99 | 114:ac69ab96328d |
---|---|
19 | 19 |
20 #if (HAVE_AIO) | 20 #if (HAVE_AIO) |
21 #include <ngx_aio_module.h> | 21 #include <ngx_aio_module.h> |
22 #endif | 22 #endif |
23 | 23 |
24 static int ngx_event_init_module(ngx_cycle_t *cycle); | |
25 static int ngx_event_init_child(ngx_cycle_t *cycle); | |
26 static int ngx_event_init(ngx_cycle_t *cycle); | 24 static int ngx_event_init(ngx_cycle_t *cycle); |
27 | |
28 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); |
29 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 26 static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
30 static void *ngx_event_create_conf(ngx_pool_t *pool); | 27 static void *ngx_event_create_conf(ngx_cycle_t *cycle); |
31 static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf); | 28 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf); |
32 | 29 |
33 | 30 |
34 int ngx_event_flags; | 31 int ngx_event_flags; |
35 ngx_event_actions_t ngx_event_actions; | 32 ngx_event_actions_t ngx_event_actions; |
36 | |
37 int ngx_max_connections; | |
38 ngx_connection_t *ngx_connections; | |
39 ngx_event_t *ngx_read_events, *ngx_write_events; | |
40 | 33 |
41 | 34 |
42 static int ngx_event_max_module; | 35 static int ngx_event_max_module; |
43 | 36 |
44 | 37 |
109 ngx_module_t ngx_event_core_module = { | 102 ngx_module_t ngx_event_core_module = { |
110 NGX_MODULE, | 103 NGX_MODULE, |
111 &ngx_event_core_module_ctx, /* module context */ | 104 &ngx_event_core_module_ctx, /* module context */ |
112 ngx_event_core_commands, /* module directives */ | 105 ngx_event_core_commands, /* module directives */ |
113 NGX_EVENT_MODULE, /* module type */ | 106 NGX_EVENT_MODULE, /* module type */ |
114 ngx_event_init_module, /* init module */ | 107 NULL, /* init module */ |
115 ngx_event_init_child /* init child */ | 108 ngx_event_init /* init child */ |
116 }; | 109 }; |
117 | |
118 | |
119 | |
120 static int ngx_event_init_module(ngx_cycle_t *cycle) | |
121 { | |
122 if (cycle->one_process) { | |
123 return ngx_event_init(cycle); | |
124 } | |
125 | |
126 return NGX_OK; | |
127 } | |
128 | |
129 | |
130 static int ngx_event_init_child(ngx_cycle_t *cycle) | |
131 { | |
132 if (cycle->one_process) { | |
133 return NGX_OK; | |
134 } | |
135 | |
136 return ngx_event_init(cycle); | |
137 } | |
138 | 110 |
139 | 111 |
140 static int ngx_event_init(ngx_cycle_t *cycle) | 112 static int ngx_event_init(ngx_cycle_t *cycle) |
141 { | 113 { |
142 int m, i, fd; | 114 int m, i, fd; |
152 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); | 124 ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module); |
153 | 125 |
154 ngx_log_debug(cycle->log, "CONN: %d" _ ecf->connections); | 126 ngx_log_debug(cycle->log, "CONN: %d" _ ecf->connections); |
155 ngx_log_debug(cycle->log, "TYPE: %d" _ ecf->use); | 127 ngx_log_debug(cycle->log, "TYPE: %d" _ ecf->use); |
156 | 128 |
129 cycle->connection_n = ecf->connections; | |
130 | |
157 for (m = 0; ngx_modules[m]; m++) { | 131 for (m = 0; ngx_modules[m]; m++) { |
158 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { | 132 if (ngx_modules[m]->type != NGX_EVENT_MODULE) { |
159 continue; | 133 continue; |
160 } | 134 } |
161 | 135 |
166 } | 140 } |
167 break; | 141 break; |
168 } | 142 } |
169 } | 143 } |
170 | 144 |
171 if (ngx_max_connections && ngx_max_connections < ecf->connections) { | 145 if (cycle->old_cycle && cycle->old_cycle->connection_n < ecf->connections) { |
172 /* TODO: push into delayed array and temporary pool */ | 146 /* TODO: push into delayed array and temporary pool */ |
173 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "NOT READY"); | 147 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "NOT READY: connections"); |
174 exit(1); | 148 exit(1); |
175 } | 149 } |
176 | 150 |
177 ngx_max_connections = ecf->connections; | 151 ngx_test_null(cycle->connections, |
178 | |
179 ngx_test_null(ngx_connections, | |
180 ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, | 152 ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, |
181 cycle->log), | 153 cycle->log), |
182 NGX_ERROR); | 154 NGX_ERROR); |
183 | 155 |
184 ngx_test_null(ngx_read_events, | 156 c = cycle->connections; |
157 for (i = 0; i < cycle->connection_n; i++) { | |
158 c[i].fd = -1; | |
159 } | |
160 | |
161 ngx_test_null(cycle->read_events, | |
185 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), | 162 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), |
186 NGX_ERROR); | 163 NGX_ERROR); |
187 | 164 |
188 ngx_test_null(ngx_write_events, | 165 ngx_test_null(cycle->write_events, |
189 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), | 166 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, cycle->log), |
190 NGX_ERROR); | 167 NGX_ERROR); |
191 | 168 |
192 /* for each listening socket */ | 169 /* for each listening socket */ |
193 | 170 |
194 for (s = cycle->listening.elts, i = 0; i < cycle->listening.nelts; i++) { | 171 s = cycle->listening.elts; |
172 for (i = 0; i < cycle->listening.nelts; i++) { | |
195 | 173 |
196 fd = s[i].fd; | 174 fd = s[i].fd; |
197 | 175 |
198 #if (WIN32) | 176 #if (WIN32) |
199 /* | 177 /* |
200 * Winsock assignes a socket number divisible by 4 | 178 * Winsock assignes a socket number divisible by 4 |
201 * so to find a connection we divide a socket number by 4. | 179 * so to find a connection we divide a socket number by 4. |
202 */ | 180 */ |
203 | 181 |
204 c = &ngx_connections[fd / 4]; | 182 fd /= 4; |
205 rev = &ngx_read_events[fd / 4]; | 183 #endif |
206 wev = &ngx_write_events[fd / 4]; | 184 |
207 #else | 185 c = &cycle->connections[fd]; |
208 c = &ngx_connections[fd]; | 186 rev = &cycle->read_events[fd]; |
209 rev = &ngx_read_events[fd]; | 187 wev = &cycle->write_events[fd]; |
210 wev = &ngx_write_events[fd]; | |
211 #endif | |
212 | 188 |
213 ngx_memzero(c, sizeof(ngx_connection_t)); | 189 ngx_memzero(c, sizeof(ngx_connection_t)); |
214 ngx_memzero(rev, sizeof(ngx_event_t)); | 190 ngx_memzero(rev, sizeof(ngx_event_t)); |
215 | 191 |
216 c->fd = fd; | 192 c->fd = s[i].fd; |
217 c->listening = &s[i]; | 193 c->listening = &s[i]; |
218 | 194 |
219 c->ctx = s[i].ctx; | 195 c->ctx = s[i].ctx; |
220 c->servers = s[i].servers; | 196 c->servers = s[i].servers; |
221 c->log = s[i].log; | 197 c->log = s[i].log; |
222 | 198 c->read = rev; |
199 | |
200 /* required by iocp in "c->write->active = 1" */ | |
201 c->write = wev; | |
202 | |
203 #if 0 | |
223 ngx_test_null(rev->log, ngx_palloc(cycle->pool, sizeof(ngx_log_t)), | 204 ngx_test_null(rev->log, ngx_palloc(cycle->pool, sizeof(ngx_log_t)), |
224 NGX_ERROR); | 205 NGX_ERROR); |
225 | 206 |
226 ngx_memcpy(rev->log, c->log, sizeof(ngx_log_t)); | 207 ngx_memcpy(rev->log, c->log, sizeof(ngx_log_t)); |
227 c->read = rev; | 208 #endif |
228 c->write = wev; | 209 |
210 rev->log = c->log; | |
229 rev->data = c; | 211 rev->data = c; |
230 rev->index = NGX_INVALID_INDEX; | 212 rev->index = NGX_INVALID_INDEX; |
213 | |
231 #if 0 | 214 #if 0 |
232 rev->listening = 1; | 215 rev->listening = 1; |
233 #endif | 216 #endif |
234 | 217 |
235 rev->available = 0; | 218 rev->available = 0; |
236 | 219 |
237 #if (HAVE_DEFERRED_ACCEPT) | 220 #if (HAVE_DEFERRED_ACCEPT) |
238 rev->deferred_accept = s[i].deferred_accept; | 221 rev->deferred_accept = s[i].deferred_accept; |
239 #endif | 222 #endif |
240 | 223 |
224 /* required by poll */ | |
225 wev->index = NGX_INVALID_INDEX; | |
226 | |
227 if ((ngx_event_flags & NGX_HAVE_IOCP_EVENT) == 0) { | |
228 if (s[i].remain) { | |
229 | |
230 if (ngx_del_event(&cycle->old_cycle->read_events[fd], | |
231 NGX_READ_EVENT, 0) == NGX_ERROR) { | |
232 return NGX_ERROR; | |
233 } | |
234 | |
235 cycle->old_cycle->connections[fd].fd = -1; | |
236 } | |
237 } | |
238 | |
241 #if (WIN32) | 239 #if (WIN32) |
242 | 240 |
243 if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) { | 241 if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) { |
244 rev->event_handler = &ngx_event_acceptex; | 242 rev->event_handler = &ngx_event_acceptex; |
245 | 243 |
246 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) { | 244 if (ngx_add_event(rev, 0, NGX_IOCP_ACCEPT) == NGX_ERROR) { |
247 return NGX_ERROR; | 245 return NGX_ERROR; |
248 } | 246 } |
249 | 247 |
250 iocpcf = ngx_event_get_conf(ngx_iocp_module); | 248 iocpcf = ngx_event_get_conf(cycle->conf_ctx, ngx_iocp_module); |
251 if (ngx_event_post_acceptex(&s[i], iocpcf->acceptex) == NGX_ERROR) { | 249 if (ngx_event_post_acceptex(&s[i], iocpcf->acceptex) == NGX_ERROR) { |
252 return NGX_ERROR; | 250 return NGX_ERROR; |
253 } | 251 } |
254 | 252 |
255 } else { | 253 } else { |
303 | 301 |
304 module = ngx_modules[m]->ctx; | 302 module = ngx_modules[m]->ctx; |
305 | 303 |
306 if (module->create_conf) { | 304 if (module->create_conf) { |
307 ngx_test_null((*ctx)[ngx_modules[m]->ctx_index], | 305 ngx_test_null((*ctx)[ngx_modules[m]->ctx_index], |
308 module->create_conf(cf->pool), | 306 module->create_conf(cf->cycle), |
309 NGX_CONF_ERROR); | 307 NGX_CONF_ERROR); |
310 } | 308 } |
311 } | 309 } |
312 | 310 |
313 pcf = *cf; | 311 pcf = *cf; |
326 } | 324 } |
327 | 325 |
328 module = ngx_modules[m]->ctx; | 326 module = ngx_modules[m]->ctx; |
329 | 327 |
330 if (module->init_conf) { | 328 if (module->init_conf) { |
331 rv = module->init_conf(cf->pool, (*ctx)[ngx_modules[m]->ctx_index]); | 329 rv = module->init_conf(cf->cycle, |
330 (*ctx)[ngx_modules[m]->ctx_index]); | |
332 if (rv != NGX_CONF_OK) { | 331 if (rv != NGX_CONF_OK) { |
333 return rv; | 332 return rv; |
334 } | 333 } |
335 } | 334 } |
336 } | 335 } |
369 | 368 |
370 return "invalid event type"; | 369 return "invalid event type"; |
371 } | 370 } |
372 | 371 |
373 | 372 |
374 static void *ngx_event_create_conf(ngx_pool_t *pool) | 373 static void *ngx_event_create_conf(ngx_cycle_t *cycle) |
375 { | 374 { |
376 ngx_event_conf_t *ecf; | 375 ngx_event_conf_t *ecf; |
377 | 376 |
378 ngx_test_null(ecf, ngx_palloc(pool, sizeof(ngx_event_conf_t)), | 377 ngx_test_null(ecf, ngx_palloc(cycle->pool, sizeof(ngx_event_conf_t)), |
379 NGX_CONF_ERROR); | 378 NGX_CONF_ERROR); |
380 | 379 |
381 ecf->connections = NGX_CONF_UNSET; | 380 ecf->connections = NGX_CONF_UNSET; |
382 ecf->timer_queues = NGX_CONF_UNSET; | 381 ecf->timer_queues = NGX_CONF_UNSET; |
383 ecf->use = NGX_CONF_UNSET; | 382 ecf->use = NGX_CONF_UNSET; |
384 | 383 |
385 return ecf; | 384 return ecf; |
386 } | 385 } |
387 | 386 |
388 | 387 |
389 static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf) | 388 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf) |
390 { | 389 { |
391 ngx_event_conf_t *ecf = conf; | 390 ngx_event_conf_t *ecf = conf; |
392 | 391 |
393 #if (HAVE_KQUEUE) | 392 #if (HAVE_KQUEUE) |
394 | 393 |