comparison src/event/modules/ngx_select_module.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 5f6d848dcbef
children 19cc647ecd91
comparison
equal deleted inserted replaced
90:37530da31268 91:637625a2acdb
1
2 /*
3 * Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru
4 */
5
1 6
2 #include <ngx_config.h> 7 #include <ngx_config.h>
3 #include <ngx_core.h> 8 #include <ngx_core.h>
4 #include <ngx_types.h>
5 #include <ngx_log.h>
6 #include <ngx_time.h>
7 #include <ngx_connection.h> 9 #include <ngx_connection.h>
8 #include <ngx_event.h> 10 #include <ngx_event.h>
9 #include <ngx_event_timer.h> 11
10 #include <ngx_select_module.h> 12
11 13 static int ngx_select_init(ngx_log_t *log);
12 14 static void ngx_select_done(ngx_log_t *log);
13 /* should be per-thread */ 15 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
16 static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags);
17 static int ngx_select_process_events(ngx_log_t *log);
18
19 static char *ngx_select_init_conf(ngx_pool_t *pool, void *conf);
20
21
14 static fd_set master_read_fd_set; 22 static fd_set master_read_fd_set;
15 static fd_set master_write_fd_set; 23 static fd_set master_write_fd_set;
16 static fd_set work_read_fd_set; 24 static fd_set work_read_fd_set;
17 static fd_set work_write_fd_set; 25 static fd_set work_write_fd_set;
18 26
25 33
26 static u_int nevents; 34 static u_int nevents;
27 35
28 static ngx_event_t **event_index; 36 static ngx_event_t **event_index;
29 static ngx_event_t **ready_index; 37 static ngx_event_t **ready_index;
30 static ngx_event_t *timer_queue; 38
31 /* */ 39
32 40 static ngx_str_t select_name = ngx_string("select");
33 int ngx_select_init(int max_connections, ngx_log_t *log) 41
34 { 42 ngx_event_module_t ngx_select_module_ctx = {
35 if (max_connections > FD_SETSIZE) { 43 NGX_EVENT_MODULE,
36 ngx_log_error(NGX_LOG_EMERG, log, 0, 44 &select_name,
37 #if (WIN32) 45 NULL, /* create configuration */
38 "maximum number of descriptors " 46 ngx_select_init_conf, /* init configuration */
39 "supported by select() is %d", FD_SETSIZE); 47
40 #else 48 {
41 "maximum descriptor number" 49 ngx_select_add_event, /* add an event */
42 "supported by select() is %d", FD_SETSIZE - 1); 50 ngx_select_del_event, /* delete an event */
43 #endif 51 ngx_select_add_event, /* enable an event */
44 exit(1); 52 ngx_select_del_event, /* disable an event */
45 } 53 NULL, /* add an connection */
54 NULL, /* delete an connection */
55 ngx_select_process_events, /* process the events */
56 ngx_select_init, /* init the events */
57 ngx_select_done /* done the events */
58 }
59
60 };
61
62 ngx_module_t ngx_select_module = {
63 &ngx_select_module_ctx, /* module context */
64 0, /* module index */
65 NULL, /* module directives */
66 NGX_EVENT_MODULE_TYPE, /* module type */
67 NULL /* init module */
68 };
69
70
71 static int ngx_select_init(ngx_log_t *log)
72 {
73 ngx_event_conf_t *ecf;
74
75 ecf = ngx_event_get_conf(ngx_event_module_ctx);
46 76
47 FD_ZERO(&master_read_fd_set); 77 FD_ZERO(&master_read_fd_set);
48 FD_ZERO(&master_write_fd_set); 78 FD_ZERO(&master_write_fd_set);
49 79
50 ngx_test_null(event_index, 80 ngx_test_null(event_index,
51 ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log), 81 ngx_alloc(sizeof(ngx_event_t *) * 2 * ecf->connections, log),
52 NGX_ERROR); 82 NGX_ERROR);
53 83
54 ngx_test_null(ready_index, 84 ngx_test_null(ready_index,
55 ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log), 85 ngx_alloc(sizeof(ngx_event_t *) * 2 * ecf->connections, log),
56 NGX_ERROR); 86 NGX_ERROR);
57 87
58 nevents = 0; 88 nevents = 0;
59 89
60 timer_queue = ngx_event_init_timer(log); 90 if (ngx_event_timer_init(log) == NGX_ERROR) {
61 if (timer_queue == NULL) {
62 return NGX_ERROR; 91 return NGX_ERROR;
63 } 92 }
64 93
65 ngx_event_actions.add = ngx_select_add_event; 94 ngx_event_actions = ngx_select_module_ctx.actions;
66 ngx_event_actions.del = ngx_select_del_event; 95
67 ngx_event_actions.timer = ngx_event_add_timer; 96 ngx_event_flags = NGX_HAVE_LEVEL_EVENT
68 ngx_event_actions.process = ngx_select_process_events; 97 |NGX_HAVE_ONESHOT_EVENT
98 |NGX_USE_LEVEL_EVENT;
69 99
70 #if (WIN32) 100 #if (WIN32)
71 max_read = max_write = 0; 101 max_read = max_write = 0;
72 #else 102 #else
73 max_fd = -1; 103 max_fd = -1;
74 #endif 104 #endif
75 105
76 return NGX_OK; 106 return NGX_OK;
77 } 107 }
78 108
79 int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags) 109
110 static void ngx_select_done(ngx_log_t *log)
111 {
112 ngx_free(event_index);
113 ngx_free(ready_index);
114 }
115
116
117 static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags)
80 { 118 {
81 ngx_connection_t *c; 119 ngx_connection_t *c;
82 120
83 c = (ngx_connection_t *) ev->data; 121 c = ev->data;
84 122
85 #if (NGX_DEBUG_EVENT) 123 #if (NGX_DEBUG_EVENT)
86 ngx_log_debug(ev->log, "select fd:%d event:%d" _ c->fd _ event); 124 ngx_log_debug(ev->log, "select fd:%d event:%d" _ c->fd _ event);
87 #endif 125 #endif
88 126
130 nevents++; 168 nevents++;
131 169
132 return NGX_OK; 170 return NGX_OK;
133 } 171 }
134 172
135 int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags) 173
136 { 174 static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags)
137 ngx_connection_t *c; 175 {
138 c = (ngx_connection_t *) ev->data; 176 ngx_connection_t *c;
177
178 c = ev->data;
139 179
140 if (ev->index == NGX_INVALID_INDEX) 180 if (ev->index == NGX_INVALID_INDEX)
141 return NGX_OK; 181 return NGX_OK;
142 182
143 #if (NGX_DEBUG_EVENT) 183 #if (NGX_DEBUG_EVENT)
173 ev->index = NGX_INVALID_INDEX; 213 ev->index = NGX_INVALID_INDEX;
174 214
175 return NGX_OK; 215 return NGX_OK;
176 } 216 }
177 217
178 int ngx_select_process_events(ngx_log_t *log) 218
219 static int ngx_select_process_events(ngx_log_t *log)
179 { 220 {
180 int ready, found; 221 int ready, found;
181 u_int i, nready; 222 u_int i, nready;
182 ngx_msec_t timer, delta; 223 ngx_msec_t timer, delta;
183 ngx_event_t *ev; 224 ngx_event_t *ev;
296 } 337 }
297 338
298 ev->ready = 1; 339 ev->ready = 1;
299 340
300 if (ev->oneshot) { 341 if (ev->oneshot) {
301 ngx_del_timer(ev); 342 if (ev->timer_set) {
343 ngx_del_timer(ev);
344 ev->timer_set = 0;
345 }
302 346
303 if (ev->write) 347 if (ev->write)
304 ngx_select_del_event(ev, NGX_WRITE_EVENT, 0); 348 ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
305 else 349 else
306 ngx_select_del_event(ev, NGX_READ_EVENT, 0); 350 ngx_select_del_event(ev, NGX_READ_EVENT, 0);
313 ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events"); 357 ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events");
314 } 358 }
315 359
316 return NGX_OK; 360 return NGX_OK;
317 } 361 }
362
363
364 static char *ngx_select_init_conf(ngx_pool_t *pool, void *conf)
365 {
366 ngx_event_conf_t *ecf;
367
368 ecf = ngx_event_get_conf(ngx_event_module_ctx);
369
370 if (ecf->connections > FD_SETSIZE) {
371 return "maximum number of connections "
372 "supported by select() is " ngx_value(FD_SETSIZE);
373 }
374
375 return NGX_CONF_OK;
376 }