comparison src/event/modules/ngx_poll_module.c @ 92:19cc647ecd91

nginx-0.0.1-2003-05-20-19:37:55 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 20 May 2003 15:37:55 +0000
parents 637625a2acdb
children a23d010f356d
comparison
equal deleted inserted replaced
91:637625a2acdb 92:19cc647ecd91
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_errno.h>
6 #include <ngx_log.h>
7 #include <ngx_time.h>
8 #include <ngx_connection.h> 9 #include <ngx_connection.h>
9 #include <ngx_event.h> 10 #include <ngx_event.h>
10 #include <ngx_event_timer.h> 11
11 #include <ngx_poll_module.h> 12
12 13 static int ngx_poll_init(ngx_log_t *log);
13 14 static void ngx_poll_done(ngx_log_t *log);
14 /* should be per-thread */ 15 static int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags);
16 static int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags);
17 static int ngx_poll_process_events(ngx_log_t *log);
18
19
15 static struct pollfd *event_list; 20 static struct pollfd *event_list;
16 static u_int nevents; 21 static u_int nevents;
17 22
18 static ngx_event_t **event_index; 23 static ngx_event_t **event_index;
19 static ngx_event_t **ready_index; 24 static ngx_event_t **ready_index;
20 static ngx_event_t *timer_queue; 25
21 /* */ 26
22 27 static ngx_str_t poll_name = ngx_string("poll");
23 int ngx_poll_init(int max_connections, ngx_log_t *log) 28
24 { 29 ngx_event_module_t ngx_poll_module_ctx = {
30 NGX_EVENT_MODULE,
31 &poll_name,
32 NULL, /* create configuration */
33 NULL, /* init configuration */
34
35 {
36 ngx_poll_add_event, /* add an event */
37 ngx_poll_del_event, /* delete an event */
38 ngx_poll_add_event, /* enable an event */
39 ngx_poll_del_event, /* disable an event */
40 NULL, /* add an connection */
41 NULL, /* delete an connection */
42 ngx_poll_process_events, /* process the events */
43 ngx_poll_init, /* init the events */
44 ngx_poll_done /* done the events */
45 }
46
47 };
48
49 ngx_module_t ngx_poll_module = {
50 &ngx_poll_module_ctx, /* module context */
51 0, /* module index */
52 NULL, /* module directives */
53 NGX_EVENT_MODULE_TYPE, /* module type */
54 NULL /* init module */
55 };
56
57
58
59 static int ngx_poll_init(ngx_log_t *log)
60 {
61 ngx_event_conf_t *ecf;
62
63 ecf = ngx_event_get_conf(ngx_event_module_ctx);
64
25 ngx_test_null(event_list, 65 ngx_test_null(event_list,
26 ngx_alloc(sizeof(struct pollfd) * max_connections, log), 66 ngx_alloc(sizeof(struct pollfd) * ecf->connections, log),
27 NGX_ERROR); 67 NGX_ERROR);
28 68
29 ngx_test_null(event_index, 69 ngx_test_null(event_index,
30 ngx_alloc(sizeof(ngx_event_t *) * max_connections, log), 70 ngx_alloc(sizeof(ngx_event_t *) * ecf->connections, log),
31 NGX_ERROR); 71 NGX_ERROR);
32 72
33 ngx_test_null(ready_index, 73 ngx_test_null(ready_index,
34 ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log), 74 ngx_alloc(sizeof(ngx_event_t *) * 2 * ecf->connections, log),
35 NGX_ERROR); 75 NGX_ERROR);
36 76
37 nevents = 0; 77 nevents = 0;
38 78
39 timer_queue = ngx_event_init_timer(log); 79 if (ngx_event_timer_init(log) == NGX_ERROR) {
40 if (timer_queue == NULL) {
41 return NGX_ERROR; 80 return NGX_ERROR;
42 } 81 }
43 82
44 ngx_event_actions.add = ngx_poll_add_event; 83 ngx_event_actions = ngx_poll_module_ctx.actions;
45 ngx_event_actions.del = ngx_poll_del_event; 84
46 ngx_event_actions.timer = ngx_event_add_timer; 85 ngx_event_flags = NGX_HAVE_LEVEL_EVENT
47 ngx_event_actions.process = ngx_poll_process_events; 86 |NGX_HAVE_ONESHOT_EVENT
87 |NGX_USE_LEVEL_EVENT;
48 88
49 return NGX_OK; 89 return NGX_OK;
50 } 90 }
51 91
52 int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags) 92
93 static void ngx_poll_done(ngx_log_t *log)
94 {
95 ngx_event_timer_done(log);
96
97 ngx_free(event_list);
98 ngx_free(event_index);
99 ngx_free(ready_index);
100 }
101
102
103 static int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags)
53 { 104 {
54 ngx_event_t *e; 105 ngx_event_t *e;
55 ngx_connection_t *c; 106 ngx_connection_t *c;
56 107
57 c = (ngx_connection_t *) ev->data; 108 c = ev->data;
58 109
59 ev->active = 1; 110 ev->active = 1;
60 ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1: 0; 111 ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
61 112
62 if (event == NGX_READ_EVENT) { 113 if (event == NGX_READ_EVENT) {
63 e = c->write; 114 e = c->write;
64 #if (NGX_READ_EVENT != POLLIN) 115 #if (NGX_READ_EVENT != POLLIN)
65 event = POLLIN; 116 event = POLLIN;
91 } 142 }
92 143
93 return NGX_OK; 144 return NGX_OK;
94 } 145 }
95 146
96 int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags) 147
148 static int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags)
97 { 149 {
98 ngx_event_t *e; 150 ngx_event_t *e;
99 ngx_connection_t *c; 151 ngx_connection_t *c;
100 152
101 c = (ngx_connection_t *) ev->data; 153 c = (ngx_connection_t *) ev->data;
136 ev->index = NGX_INVALID_INDEX; 188 ev->index = NGX_INVALID_INDEX;
137 189
138 return NGX_OK; 190 return NGX_OK;
139 } 191 }
140 192
141 int ngx_poll_process_events(ngx_log_t *log) 193
194 static int ngx_poll_process_events(ngx_log_t *log)
142 { 195 {
143 int ready, found; 196 int ready, found;
144 u_int i, nready; 197 u_int i, nready;
145 ngx_msec_t timer, delta; 198 ngx_msec_t timer, delta;
146 ngx_err_t err; 199 ngx_err_t err;
200 event_list[i].events _ event_list[i].revents); 253 event_list[i].events _ event_list[i].revents);
201 #endif 254 #endif
202 255
203 found = 0; 256 found = 0;
204 257
205 if (event_list[i].revents & POLLIN) { 258 if (event_list[i].revents & POLLNVAL) {
259 ngx_log_error(NGX_LOG_ALERT, log, EBADF,
260 "poll() error on %d", event_list[i].fd);
261 continue;
262 }
263
264 if (event_list[i].revents & POLLIN
265 || (event_list[i].revents & (POLLERR|POLLHUP)
266 && c->read->active))
267 {
206 found = 1; 268 found = 1;
207 ready_index[nready++] = c->read; 269 ready_index[nready++] = c->read;
208 } 270 }
209 271
210 if (event_list[i].revents & POLLOUT) { 272 if (event_list[i].revents & POLLOUT
273 || (event_list[i].revents & (POLLERR|POLLHUP)
274 && c->write->active))
275 {
211 found = 1; 276 found = 1;
212 ready_index[nready++] = c->write; 277 ready_index[nready++] = c->write;
213 } 278 }
214 279
215 if (event_list[i].revents & (POLLERR|POLLHUP|POLLNVAL)) {
216 found = 1;
217
218 /* need ot add to ready_index[nready++] = c->read or c->write; */
219
220 err = 0;
221 if (event_list[i].revents & POLLNVAL) {
222 err = EBADF;
223 }
224
225 ngx_log_error(NGX_LOG_ERR, log, err,
226 "poll() error on %d:%d",
227 event_list[i].fd, event_list[i].revents);
228 }
229
230 if (found) { 280 if (found) {
231 ready--; 281 ready--;
282 continue;
283 }
284
285 if (event_list[i].revents & (POLLERR|POLLHUP)) {
286 ngx_log_error(NGX_LOG_ALERT, log, 0,
287 "strange poll() error on %d:%d:%d",
288 event_list[i].fd,
289 event_list[i].events, event_list[i].revents);
232 } 290 }
233 } 291 }
234 292
235 for (i = 0; i < nready; i++) { 293 for (i = 0; i < nready; i++) {
236 ev = ready_index[i]; 294 ev = ready_index[i];