comparison src/event/ngx_event.c @ 91:637625a2acdb

nginx-0.0.1-2003-05-19-20:39:14 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 19 May 2003 16:39:14 +0000
parents 37530da31268
children 19cc647ecd91
comparison
equal deleted inserted replaced
90:37530da31268 91:637625a2acdb
9 #include <ngx_listen.h> 9 #include <ngx_listen.h>
10 #include <ngx_connection.h> 10 #include <ngx_connection.h>
11 #include <ngx_event.h> 11 #include <ngx_event.h>
12 #include <ngx_conf_file.h> 12 #include <ngx_conf_file.h>
13 13
14 #include <ngx_select_module.h> 14 extern ngx_event_module_t ngx_select_module_ctx;
15 15
16 #if (HAVE_POLL) 16 #if (HAVE_POLL)
17 #include <ngx_poll_module.h> 17 #include <ngx_poll_module.h>
18 #endif 18 #endif
19 19
20 #if (HAVE_DEVPOLL) 20 #if (HAVE_DEVPOLL)
21 #include <ngx_devpoll_module.h> 21 #include <ngx_devpoll_module.h>
22 #endif 22 #endif
23 23
24 #if (HAVE_KQUEUE) 24 #if (HAVE_KQUEUE)
25 extern ngx_event_module_t ngx_kqueue_module_ctx;
25 #include <ngx_kqueue_module.h> 26 #include <ngx_kqueue_module.h>
26 #endif 27 #endif
27 28
28 #if (HAVE_AIO) 29 #if (HAVE_AIO)
29 #include <ngx_aio_module.h> 30 #include <ngx_aio_module.h>
34 #include <ngx_iocp_module.h> 35 #include <ngx_iocp_module.h>
35 #endif 36 #endif
36 37
37 38
38 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy); 39 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
39 40 static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
41 static void *ngx_event_create_conf(ngx_pool_t *pool);
42 static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf);
43
44
45 int ngx_event_flags;
46 ngx_event_actions_t ngx_event_actions;
40 47
41 ngx_connection_t *ngx_connections; 48 ngx_connection_t *ngx_connections;
42 ngx_event_t *ngx_read_events, *ngx_write_events; 49 ngx_event_t *ngx_read_events, *ngx_write_events;
43 50
44 #if !(USE_KQUEUE) 51
45 52 static int ngx_event_max_module;
46 ngx_event_type_e ngx_event_type;
47
48 int ngx_event_flags;
49
50 ngx_event_actions_t ngx_event_actions;
51
52 /* ngx_event_type_e order */
53 static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
54 ngx_select_init,
55 #if (HAVE_POLL)
56 ngx_poll_init,
57 #endif
58 #if (HAVE_DEVPOLL)
59 ngx_devpoll_init,
60 #endif
61 #if (HAVE_KQUEUE)
62 ngx_kqueue_init,
63 #endif
64 #if (HAVE_AIO)
65 ngx_aio_init,
66 #endif
67 #if (HAVE_IOCP)
68 ngx_iocp_init
69 #endif
70 };
71
72 #endif /* USE_KQUEUE */
73 53
74 54
75 static int ngx_event_connections; 55 static int ngx_event_connections;
76 56
77 57
78 static ngx_str_t events_name = ngx_string("events"); 58 static ngx_str_t events_name = ngx_string("events");
59 static ngx_str_t event_name = ngx_string("event");
79 60
80 static ngx_command_t ngx_events_commands[] = { 61 static ngx_command_t ngx_events_commands[] = {
81 62
82 {ngx_string("events"), 63 {ngx_string("events"),
83 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, 64 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
104 85
105 {ngx_string("connections"), 86 {ngx_string("connections"),
106 NGX_EVENT_CONF|NGX_CONF_TAKE1, 87 NGX_EVENT_CONF|NGX_CONF_TAKE1,
107 ngx_conf_set_num_slot, 88 ngx_conf_set_num_slot,
108 0, 89 0,
109 addressof(ngx_event_connections), 90 offsetof(ngx_event_conf_t, connections),
110 NULL}, 91 NULL},
111 92
112 #if 0
113 {ngx_string("type"), 93 {ngx_string("type"),
114 NGX_EVENT_CONF|NGX_CONF_TAKE1, 94 NGX_EVENT_CONF|NGX_CONF_TAKE1,
115 ngx_event_set_type, 95 ngx_event_set_type,
116 0, 96 0,
117 0, 97 0,
118 NULL}, 98 NULL},
119 #endif 99
100 {ngx_string("timer_queues"),
101 NGX_EVENT_CONF|NGX_CONF_TAKE1,
102 ngx_conf_set_num_slot,
103 0,
104 offsetof(ngx_event_conf_t, timer_queues),
105 NULL},
120 106
121 {ngx_string(""), 0, NULL, 0, 0, NULL} 107 {ngx_string(""), 0, NULL, 0, 0, NULL}
122 }; 108 };
123 109
124 110
111 ngx_event_module_t ngx_event_module_ctx = {
112 NGX_EVENT_MODULE,
113 &event_name,
114 ngx_event_create_conf, /* create configuration */
115 ngx_event_init_conf, /* init configuration */
116
117 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
118 };
119
120
125 ngx_module_t ngx_event_module = { 121 ngx_module_t ngx_event_module = {
126 NULL, /* module context */ 122 &ngx_event_module_ctx, /* module context */
127 0, /* module index */ 123 0, /* module index */
128 ngx_events_commands, /* module directives */ 124 ngx_event_commands, /* module directives */
129 NGX_EVENT_MODULE_TYPE, /* module type */ 125 NGX_EVENT_MODULE_TYPE, /* module type */
130 NULL /* init module */ 126 NULL /* init module */
131 }; 127 };
132 128
133 129
134 130
135 void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log) 131 int ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
136 { 132 {
137 int i, fd; 133 int m, i, fd;
138 134
139 ngx_listen_t *s; 135 ngx_listen_t *s;
140 ngx_event_t *ev; 136 ngx_event_t *ev;
141 ngx_connection_t *c; 137 ngx_connection_t *c;
142 138 ngx_event_conf_t *ecf;
143 /* STUB */ 139 ngx_event_module_t *module;
144 int max_connections = 512; 140
145 141 ecf = ngx_event_get_conf(ngx_event_module_ctx);
146 #if 0 142
147 ngx_event_type = NGX_POLL_EVENT_N; 143 ngx_log_debug(log, "CONN: %d" _ ecf->connections);
148 #endif 144 ngx_log_debug(log, "TYPE: %d" _ ecf->type);
149 #if 1 145
150 ngx_event_type = NGX_KQUEUE_EVENT_N; 146 for (m = 0; ngx_modules[m]; m++) {
151 #endif 147 if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
152 #if 0 148 continue;
153 ngx_event_type = NGX_DEVPOLL_EVENT_N; 149 }
154 #endif 150
155 #if 0 151 module = ngx_modules[m]->ctx;
156 ngx_event_type = NGX_AIO_EVENT_N; 152 if (module->index == ecf->type) {
157 #endif 153 if (module->actions.init(log) == NGX_ERROR) {
158 #if 0 154 return NGX_ERROR;
159 ngx_event_type = NGX_IOCP_EVENT_N; 155 }
160 #endif 156 break;
161 157 }
162 if (ngx_init_events(max_connections, log) == NGX_ERROR) { 158 }
163 exit(1); 159
164 } 160 ngx_test_null(ngx_connections,
165 161 ngx_alloc(sizeof(ngx_connection_t) * ecf->connections, log),
166 ngx_connections = ngx_alloc(sizeof(ngx_connection_t) 162 NGX_ERROR);
167 * max_connections, log); 163
168 ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log); 164 ngx_test_null(ngx_read_events,
169 ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log); 165 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
166 NGX_ERROR);
167
168 ngx_test_null(ngx_write_events,
169 ngx_alloc(sizeof(ngx_event_t) * ecf->connections, log),
170 NGX_ERROR);
170 171
171 /* for each listening socket */ 172 /* for each listening socket */
172 s = (ngx_listen_t *) ls->elts; 173 s = (ngx_listen_t *) ls->elts;
173 for (i = 0; i < ls->nelts; i++) { 174 for (i = 0; i < ls->nelts; i++) {
174 175
194 c->servers = s[i].servers; 195 c->servers = s[i].servers;
195 c->log = s[i].log; 196 c->log = s[i].log;
196 c->pool_size = s[i].pool_size; 197 c->pool_size = s[i].pool_size;
197 198
198 ngx_test_null(ev->log, 199 ngx_test_null(ev->log,
199 ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; ); 200 ngx_palloc(pool, sizeof(ngx_log_t)),
201 NGX_ERROR);
202
200 ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t)); 203 ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t));
201 c->read = ev; 204 c->read = ev;
202 ev->data = c; 205 ev->data = c;
203 ev->index = NGX_INVALID_INDEX; 206 ev->index = NGX_INVALID_INDEX;
204 #if 0 207 #if 0
233 ev->event_handler = &ngx_event_accept; 236 ev->event_handler = &ngx_event_accept;
234 ngx_add_event(ev, NGX_READ_EVENT, 0); 237 ngx_add_event(ev, NGX_READ_EVENT, 0);
235 238
236 #endif 239 #endif
237 } 240 }
241
242 return NGX_OK;
238 } 243 }
239 244
240 245
241 void ngx_worker(ngx_log_t *log) 246 void ngx_worker(ngx_log_t *log)
242 { 247 {
246 ngx_process_events(log); 251 ngx_process_events(log);
247 } 252 }
248 } 253 }
249 254
250 255
251 static char *ngx_events_init(ngx_pool_t *pool)
252 {
253 ngx_event_connections = -1;
254 ngx_event_type = -1;
255
256 return NGX_CONF_OK;
257 }
258
259
260 static char *ngx_events_postconf(ngx_pool_t *pool)
261 {
262 if (ngx_event_connections == -1) {
263 ngx_event_connections = 512;
264 }
265
266 return NGX_CONF_OK;
267 }
268
269
270 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) 256 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
271 { 257 {
272 char *rv; 258 int m;
273 ngx_conf_t pcf; 259 char *rv;
274 260 void ***ctx;
275 #if 0 261 ngx_conf_t pcf;
276 *(ngx_events_conf_ctx_t **) conf = ctx; 262 ngx_event_conf_t *ecf;
277 #endif 263 ngx_event_module_t *module;
264
265 /* count the number of the event modules and set up their indices */
266
267 ngx_event_max_module = 0;
268 for (m = 0; ngx_modules[m]; m++) {
269 if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
270 continue;
271 }
272
273 module = ngx_modules[m]->ctx;
274 module->index = ngx_event_max_module++;
275 }
276
277 ngx_test_null(ctx, ngx_pcalloc(cf->pool, sizeof(void *)), NGX_CONF_ERROR);
278
279 ngx_test_null(*ctx,
280 ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *)),
281 NGX_CONF_ERROR);
282
283 *(void **) conf = ctx;
284
285 for (m = 0; ngx_modules[m]; m++) {
286 if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
287 continue;
288 }
289
290 module = ngx_modules[m]->ctx;
291
292 if (module->create_conf) {
293 ngx_test_null((*ctx)[module->index], module->create_conf(cf->pool),
294 NGX_CONF_ERROR);
295 }
296 }
278 297
279 pcf = *cf; 298 pcf = *cf;
299 cf->ctx = ctx;
280 cf->module_type = NGX_EVENT_MODULE_TYPE; 300 cf->module_type = NGX_EVENT_MODULE_TYPE;
281 cf->cmd_type = NGX_EVENT_CONF; 301 cf->cmd_type = NGX_EVENT_CONF;
282 rv = ngx_conf_parse(cf, NULL); 302 rv = ngx_conf_parse(cf, NULL);
283 *cf = pcf; 303 *cf = pcf;
284 304
285 if (rv != NGX_CONF_OK) 305 if (rv != NGX_CONF_OK)
286 return rv; 306 return rv;
287 307
308 for (m = 0; ngx_modules[m]; m++) {
309 if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
310 continue;
311 }
312
313 module = ngx_modules[m]->ctx;
314
315 if (module->init_conf) {
316 rv = module->init_conf(cf->pool, (*ctx)[module->index]);
317 if (rv != NGX_CONF_OK) {
318 return rv;
319 }
320 }
321 }
322
288 return NGX_CONF_OK; 323 return NGX_CONF_OK;
289 } 324 }
325
326
327 static char *ngx_event_set_type(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
328 {
329 ngx_event_conf_t *ecf = (ngx_event_conf_t *) conf;
330
331 int m;
332 ngx_str_t *args;
333 ngx_event_module_t *module;
334
335 if (ecf->type != NGX_CONF_UNSET) {
336 return "duplicate event type" ;
337 }
338
339 args = cf->args->elts;
340
341 for (m = 0; ngx_modules[m]; m++) {
342 if (ngx_modules[m]->type != NGX_EVENT_MODULE_TYPE) {
343 continue;
344 }
345
346 module = ngx_modules[m]->ctx;
347 if (module->name->len == args[1].len) {
348 if (ngx_strcmp(module->name->data, args[1].data) == 0) {
349 ecf->type = module->index;
350 return NGX_CONF_OK;
351 }
352 }
353 }
354
355 return "invalid event type";
356 }
357
358
359 static void *ngx_event_create_conf(ngx_pool_t *pool)
360 {
361 ngx_event_conf_t *ecf;
362
363 ngx_test_null(ecf, ngx_palloc(pool, sizeof(ngx_event_conf_t)),
364 NGX_CONF_ERROR);
365
366 ecf->connections = NGX_CONF_UNSET;
367 ecf->type = NGX_CONF_UNSET;
368
369 return ecf;
370 }
371
372
373 static char *ngx_event_init_conf(ngx_pool_t *pool, void *conf)
374 {
375 ngx_event_conf_t *ecf = conf;
376
377 #if (HAVE_KQUEUE)
378
379 ngx_conf_init_value(ecf->connections, 1024);
380 ngx_conf_init_value(ecf->type, ngx_kqueue_module_ctx.index);
381
382 #else /* HAVE_SELECT */
383
384 ngx_conf_init_value(ecf->connections,
385 FD_SETSIZE < 1024 ? FD_SETSIZE : 1024);
386
387 ngx_conf_init_value(ecf->type, ngx_select_module_ctx.index);
388
389 #endif
390
391 ngx_conf_init_value(ecf->timer_queues, 10);
392
393 return NGX_CONF_OK;
394 }