comparison src/event/modules/ngx_epoll_module.c @ 259:d30f2c39caae

nginx-0.0.2-2004-02-12-23:57:10 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 12 Feb 2004 20:57:10 +0000
parents 733dffa1fe97
children 4b1a3a4acc60
comparison
equal deleted inserted replaced
258:733dffa1fe97 259:d30f2c39caae
109 { 109 {
110 ngx_epoll_add_event, /* add an event */ 110 ngx_epoll_add_event, /* add an event */
111 ngx_epoll_del_event, /* delete an event */ 111 ngx_epoll_del_event, /* delete an event */
112 ngx_epoll_add_event, /* enable an event */ 112 ngx_epoll_add_event, /* enable an event */
113 ngx_epoll_del_event, /* disable an event */ 113 ngx_epoll_del_event, /* disable an event */
114 ngx_epoll_add_connection, /* add an connection */ 114 NULL, /* add an connection */
115 ngx_epoll_del_connection, /* delete an connection */ 115 NULL, /* delete an connection */
116 ngx_epoll_process_events, /* process the events */ 116 ngx_epoll_process_events, /* process the events */
117 ngx_epoll_init, /* init the events */ 117 ngx_epoll_init, /* init the events */
118 ngx_epoll_done, /* done the events */ 118 ngx_epoll_done, /* done the events */
119 } 119 }
120 }; 120 };
165 165
166 ngx_io = ngx_os_io; 166 ngx_io = ngx_os_io;
167 167
168 ngx_event_actions = ngx_epoll_module_ctx.actions; 168 ngx_event_actions = ngx_epoll_module_ctx.actions;
169 169
170 ngx_event_flags = NGX_USE_EDGE_EVENT; 170 #if (HAVE_CLEAR_EVENT)
171 ngx_event_flags = NGX_USE_CLEAR_EVENT;
172 #else
173 ngx_event_flags = NGX_USE_LEVEL_EVENT;
174 #endif
171 175
172 return NGX_OK; 176 return NGX_OK;
173 } 177 }
174 178
175 179
189 } 193 }
190 194
191 195
192 static int ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags) 196 static int ngx_epoll_add_event(ngx_event_t *ev, int event, u_int flags)
193 { 197 {
198 int op, prev;
199 ngx_event_t *e;
200 ngx_connection_t *c;
194 struct epoll_event ee; 201 struct epoll_event ee;
202
203 c = ev->data;
204
205 if (event == NGX_READ_EVENT) {
206 e = c->write;
207 prev = EPOLLOUT;
208 #if (NGX_READ_EVENT != EPOLLIN)
209 event = EPOLLIN;
210 #endif
211
212 } else {
213 e = c->read;
214 prev = EPOLLIN;
215 #if (NGX_WRITE_EVENT != EPOLLOUT)
216 event = EPOLLOUT;
217 #endif
218 }
219
220 if (e->active) {
221 op = EPOLL_CTL_MOD;
222 event |= prev;
223
224 } else {
225 op = EPOLL_CTL_ADD;
226 }
227
228 ee.events = event | flags;
229 ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
230
231 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
232 "epoll add event: fd:%d op:%d ev:%08X",
233 c->fd, op, ee.events);
234
235 if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
236 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
237 "epoll_ctl(%d, %d) failed", op, c->fd);
238 return NGX_ERROR;
239 }
240
241 ev->active = 1;
242 #if 0
243 ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
244 #endif
245
246 return NGX_OK;
247 }
248
249
250 static int ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags)
251 {
252 int op, prev;
253 ngx_event_t *e;
195 ngx_connection_t *c; 254 ngx_connection_t *c;
255 struct epoll_event ee;
256
257 /*
258 * when the file descriptor is closed the epoll automatically deletes
259 * it from its queue so we do not need to delete explicity the event
260 * before the closing the file descriptor.
261 */
262
263 if (flags & NGX_CLOSE_EVENT) {
264 ev->active = 0;
265 return NGX_OK;
266 }
196 267
197 c = ev->data; 268 c = ev->data;
198 269
199 #if (NGX_READ_EVENT != EPOLLIN) || (NGX_WRITE_EVENT != EPOLLOUT)
200 if (event == NGX_READ_EVENT) { 270 if (event == NGX_READ_EVENT) {
201 event = EPOLLIN; 271 e = c->write;
202 272 prev = EPOLLOUT;
203 } else { 273
204 event = EPOLLOUT; 274 } else {
205 } 275 e = c->read;
206 #endif 276 prev = EPOLLIN;
207 277 }
208 ee.events = event|EPOLLET; 278
209 ee.data.ptr = (void *) ((uintptr_t) c | c->read->instance); 279 if (e->active) {
210 280 op = EPOLL_CTL_MOD;
211 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, 281 ee.events = prev | flags;
212 "epoll add event: fd:%d ev:%08X", c->fd, ee.events); 282 ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
213 283
214 if (epoll_ctl(ep, EPOLL_CTL_ADD, c->fd, &ee) == -1) { 284 } else {
215 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno, 285 op = EPOLL_CTL_DEL;
216 "epoll_ctl(EPOLL_CTL_MOD, %d) failed", c->fd); 286 ee.events = 0;
287 ee.data.ptr = NULL;
288 }
289
290 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
291 "epoll del event: fd:%d op:%d ev:%08X",
292 c->fd, op, ee.events);
293
294 if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
295 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
296 "epoll_ctl(%d, %d) failed", op, c->fd);
217 return NGX_ERROR; 297 return NGX_ERROR;
218 } 298 }
219 299
220 ev->active = 1;
221
222 return NGX_OK;
223 }
224
225
226 static int ngx_epoll_del_event(ngx_event_t *ev, int event, u_int flags)
227 {
228 struct epoll_event ee;
229 ngx_connection_t *c;
230
231 c = ev->data;
232
233 ee.events = 0;
234 ee.data.ptr = NULL;
235
236 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
237 "epoll del event: fd:%d", c->fd);
238
239 if (epoll_ctl(ep, EPOLL_CTL_DEL, c->fd, &ee) == -1) {
240 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
241 "epoll_ctl(EPOLL_CTL_MOD, %d) failed", c->fd);
242 return NGX_ERROR;
243 }
244
245 ev->active = 0; 300 ev->active = 0;
246 301
247 return NGX_OK; 302 return NGX_OK;
248 } 303 }
249 304
250 305
306 #if 0
251 static int ngx_epoll_add_connection(ngx_connection_t *c) 307 static int ngx_epoll_add_connection(ngx_connection_t *c)
252 { 308 {
253 struct epoll_event ee; 309 struct epoll_event ee;
254 310
255 ee.events = EPOLLIN|EPOLLOUT|EPOLLET; 311 ee.events = EPOLLIN|EPOLLOUT|EPOLLET;
276 c->read->active = 0; 332 c->read->active = 0;
277 c->write->active = 0; 333 c->write->active = 0;
278 334
279 return NGX_OK; 335 return NGX_OK;
280 } 336 }
337 #endif
281 338
282 339
283 int ngx_epoll_process_events(ngx_log_t *log) 340 int ngx_epoll_process_events(ngx_log_t *log)
284 { 341 {
285 int events; 342 int events;