nginx-quic/src/event/modules/ngx_poll_module.c

386 lines
9.3 KiB
C
Raw Normal View History

2002-12-23 03:29:22 -03:00
2003-05-20 11:37:55 -04:00
/*
* Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru
*/
2002-12-23 03:29:22 -03:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
2003-07-07 02:11:50 -04:00
static int ngx_poll_init(ngx_cycle_t *cycle);
static void ngx_poll_done(ngx_cycle_t *cycle);
2003-05-20 11:37:55 -04:00
static int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags);
static int ngx_poll_process_events(ngx_log_t *log);
2002-12-23 03:29:22 -03:00
static struct pollfd *event_list;
2003-02-06 14:21:13 -03:00
static u_int nevents;
2002-12-23 03:29:22 -03:00
static ngx_event_t **event_index;
2002-12-24 14:30:59 -03:00
static ngx_event_t **ready_index;
2002-12-23 03:29:22 -03:00
2003-05-20 11:37:55 -04:00
static ngx_str_t poll_name = ngx_string("poll");
ngx_event_module_t ngx_poll_module_ctx = {
&poll_name,
NULL, /* create configuration */
NULL, /* init configuration */
{
ngx_poll_add_event, /* add an event */
ngx_poll_del_event, /* delete an event */
ngx_poll_add_event, /* enable an event */
ngx_poll_del_event, /* disable an event */
NULL, /* add an connection */
NULL, /* delete an connection */
ngx_poll_process_events, /* process the events */
ngx_poll_init, /* init the events */
ngx_poll_done /* done the events */
}
};
ngx_module_t ngx_poll_module = {
2003-05-27 08:18:54 -04:00
NGX_MODULE,
2003-05-20 11:37:55 -04:00
&ngx_poll_module_ctx, /* module context */
NULL, /* module directives */
2003-05-27 08:18:54 -04:00
NGX_EVENT_MODULE, /* module type */
2003-07-07 02:11:50 -04:00
NULL, /* init module */
NULL /* init child */
2003-05-20 11:37:55 -04:00
};
2003-07-07 02:11:50 -04:00
static int ngx_poll_init(ngx_cycle_t *cycle)
2002-12-23 03:29:22 -03:00
{
2003-07-07 02:11:50 -04:00
struct pollfd *list;
ngx_event_t **index;
2003-05-20 11:37:55 -04:00
2003-07-07 02:11:50 -04:00
if (event_list == NULL) {
nevents = 0;
}
2003-05-20 11:37:55 -04:00
2003-07-07 02:11:50 -04:00
if (cycle->old_cycle == NULL
|| cycle->old_cycle->connection_n < cycle->connection_n)
{
ngx_test_null(list,
ngx_alloc(sizeof(struct pollfd) * cycle->connection_n,
cycle->log),
NGX_ERROR);
if (event_list) {
ngx_memcpy(list, event_list, sizeof(ngx_event_t *) * nevents);
ngx_free(event_list);
}
2002-12-23 03:29:22 -03:00
2003-07-07 02:11:50 -04:00
event_list = list;
2002-12-23 03:29:22 -03:00
2003-07-07 02:11:50 -04:00
ngx_test_null(index,
ngx_alloc(sizeof(ngx_event_t *) * cycle->connection_n,
cycle->log),
NGX_ERROR);
2002-12-24 14:30:59 -03:00
2003-07-07 02:11:50 -04:00
if (event_index) {
ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
ngx_free(event_index);
}
event_index = index;
if (ready_index) {
ngx_free(ready_index);
}
2002-12-23 03:29:22 -03:00
2003-07-07 02:11:50 -04:00
ngx_test_null(ready_index,
ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
cycle->log),
NGX_ERROR);
}
if (ngx_event_timer_init(cycle) == NGX_ERROR) {
2003-01-26 18:08:14 -03:00
return NGX_ERROR;
}
2002-12-23 03:29:22 -03:00
2003-07-07 02:11:50 -04:00
ngx_io = ngx_os_io;
2003-05-20 11:37:55 -04:00
ngx_event_actions = ngx_poll_module_ctx.actions;
ngx_event_flags = NGX_HAVE_LEVEL_EVENT
|NGX_HAVE_ONESHOT_EVENT
|NGX_USE_LEVEL_EVENT;
2002-12-23 03:29:22 -03:00
return NGX_OK;
}
2003-05-20 11:37:55 -04:00
2003-07-07 02:11:50 -04:00
static void ngx_poll_done(ngx_cycle_t *cycle)
2003-05-20 11:37:55 -04:00
{
2003-07-07 02:11:50 -04:00
ngx_event_timer_done(cycle);
2003-05-20 11:37:55 -04:00
ngx_free(event_list);
ngx_free(event_index);
ngx_free(ready_index);
2003-07-07 02:11:50 -04:00
event_list = NULL;
2003-07-10 12:26:57 -04:00
event_index = NULL;
ready_index = NULL;
2003-05-20 11:37:55 -04:00
}
static int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags)
2002-12-23 03:29:22 -03:00
{
2002-12-24 14:30:59 -03:00
ngx_event_t *e;
ngx_connection_t *c;
2002-12-23 03:29:22 -03:00
2003-05-20 11:37:55 -04:00
c = ev->data;
2002-12-23 03:29:22 -03:00
2002-12-24 14:30:59 -03:00
ev->active = 1;
2003-05-20 11:37:55 -04:00
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
2002-12-23 03:29:22 -03:00
if (event == NGX_READ_EVENT) {
e = c->write;
#if (NGX_READ_EVENT != POLLIN)
event = POLLIN;
#endif
} else {
e = c->read;
#if (NGX_WRITE_EVENT != POLLOUT)
event = POLLOUT;
#endif
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
ngx_log_debug(ev->log, "add event: %d:%d" _ c->fd _ event);
#endif
2002-12-23 03:29:22 -03:00
if (e == NULL || e->index == NGX_INVALID_INDEX) {
event_list[nevents].fd = c->fd;
event_list[nevents].events = event;
event_list[nevents].revents = 0;
event_index[nevents] = ev;
ev->index = nevents;
nevents++;
} else {
event_list[e->index].events |= event;
ev->index = e->index;
}
return NGX_OK;
}
2003-05-20 11:37:55 -04:00
static int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags)
2002-12-23 03:29:22 -03:00
{
2002-12-24 14:30:59 -03:00
ngx_event_t *e;
ngx_connection_t *c;
2002-12-23 03:29:22 -03:00
c = (ngx_connection_t *) ev->data;
2002-12-24 14:30:59 -03:00
if (ev->index == NGX_INVALID_INDEX) {
2002-12-23 03:29:22 -03:00
return NGX_OK;
2002-12-24 14:30:59 -03:00
}
2002-12-23 03:29:22 -03:00
if (event == NGX_READ_EVENT) {
e = c->write;
#if (NGX_READ_EVENT != POLLIN)
event = POLLIN;
#endif
} else {
e = c->read;
#if (NGX_WRITE_EVENT != POLLOUT)
event = POLLOUT;
#endif
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-12-23 03:29:22 -03:00
ngx_log_debug(c->log, "del event: %d, %d" _ c->fd _ event);
2002-12-24 14:30:59 -03:00
#endif
2002-12-23 03:29:22 -03:00
if (e == NULL || e->index == NGX_INVALID_INDEX) {
if (ev->index < --nevents) {
2002-12-23 15:22:18 -03:00
event_list[ev->index] = event_list[nevents];
2002-12-23 03:29:22 -03:00
event_index[ev->index] = event_index[nevents];
event_index[ev->index]->index = ev->index;
}
} else {
event_list[e->index].events &= ~event;
}
2002-12-24 14:30:59 -03:00
ev->active = 0;
2002-12-23 03:29:22 -03:00
ev->index = NGX_INVALID_INDEX;
return NGX_OK;
}
2003-05-20 11:37:55 -04:00
static int ngx_poll_process_events(ngx_log_t *log)
2002-12-23 03:29:22 -03:00
{
2003-07-11 00:50:59 -04:00
int ready, found, j;
u_int nready, i;
2003-07-07 02:11:50 -04:00
ngx_msec_t timer, delta;
ngx_err_t err;
ngx_cycle_t **cycle;
ngx_event_t *ev;
ngx_connection_t *c;
2002-12-23 03:29:22 -03:00
2003-01-26 18:08:14 -03:00
timer = ngx_event_find_timer();
if (timer) {
2002-12-23 03:29:22 -03:00
delta = ngx_msec();
} else {
timer = INFTIM;
delta = 0;
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-12-23 03:29:22 -03:00
for (i = 0; i < nevents; i++) {
ngx_log_debug(log, "poll: %d, %d" _
event_list[i].fd _ event_list[i].events);
}
ngx_log_debug(log, "poll timer: %d" _ timer);
2002-12-24 14:30:59 -03:00
#endif
2002-12-23 03:29:22 -03:00
2003-07-07 02:11:50 -04:00
ready = poll(event_list, nevents, timer);
if (ready == -1) {
err = ngx_errno;
} else {
err = 0;
2002-12-23 03:29:22 -03:00
}
ngx_log_debug(log, "poll ready %d" _ ready);
2003-02-06 14:21:13 -03:00
if ((int) timer != INFTIM) {
2002-12-23 03:29:22 -03:00
delta = ngx_msec() - delta;
2003-07-07 02:11:50 -04:00
#if (NGX_DEBUG_EVENT)
ngx_log_debug(log, "poll timer: %d, delta: %d" _ timer _ delta);
#endif
2003-03-20 12:09:44 -04:00
ngx_event_expire_timers(delta);
2002-12-23 03:29:22 -03:00
} else {
2002-12-24 14:30:59 -03:00
if (ready == 0) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
"poll() returns no events without timeout");
return NGX_ERROR;
}
2002-12-23 03:29:22 -03:00
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2003-07-07 02:11:50 -04:00
ngx_log_debug(log, "poll timer: %d, delta: %d" _ timer _ delta);
2002-12-24 14:30:59 -03:00
#endif
2003-07-07 02:11:50 -04:00
}
if (err) {
ngx_log_error(NGX_LOG_ALERT, log, err, "poll() failed");
return NGX_ERROR;
}
2002-12-23 03:29:22 -03:00
2002-12-24 14:30:59 -03:00
nready = 0;
2002-12-23 03:29:22 -03:00
2002-12-24 04:09:57 -03:00
for (i = 0; i < nevents && ready; i++) {
2003-07-07 02:11:50 -04:00
c = &ngx_cycle->connections[event_list[i].fd];
if (c->fd == -1) {
cycle = ngx_old_cycles.elts;
2003-07-11 00:50:59 -04:00
for (j = 0; j < ngx_old_cycles.nelts; j++) {
if (cycle[j] == NULL) {
2003-07-07 02:11:50 -04:00
continue;
}
2003-07-11 00:50:59 -04:00
c = &cycle[j]->connections[event_list[i].fd];
2003-07-07 02:11:50 -04:00
if (c->fd != -1) {
break;
}
}
}
if (c->fd == -1) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "unkonwn cycle");
exit(1);
}
2002-12-23 03:29:22 -03:00
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-12-23 03:29:22 -03:00
ngx_log_debug(log, "poll: fd:%d, ev:%d, rev:%d" _
event_list[i].fd _
event_list[i].events _ event_list[i].revents);
2002-12-24 14:30:59 -03:00
#endif
2002-12-23 03:29:22 -03:00
found = 0;
2003-05-20 11:37:55 -04:00
if (event_list[i].revents & POLLNVAL) {
ngx_log_error(NGX_LOG_ALERT, log, EBADF,
"poll() error on %d", event_list[i].fd);
continue;
2002-12-23 03:29:22 -03:00
}
2003-05-20 11:37:55 -04:00
if (event_list[i].revents & POLLIN
|| (event_list[i].revents & (POLLERR|POLLHUP)
&& c->read->active))
{
2002-12-23 03:29:22 -03:00
found = 1;
2003-05-20 11:37:55 -04:00
ready_index[nready++] = c->read;
2002-12-23 03:29:22 -03:00
}
2003-05-20 11:37:55 -04:00
if (event_list[i].revents & POLLOUT
|| (event_list[i].revents & (POLLERR|POLLHUP)
&& c->write->active))
{
2002-12-23 03:29:22 -03:00
found = 1;
2003-05-20 11:37:55 -04:00
ready_index[nready++] = c->write;
2002-12-23 03:29:22 -03:00
}
if (found) {
ready--;
2003-05-20 11:37:55 -04:00
continue;
}
if (event_list[i].revents & (POLLERR|POLLHUP)) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
"strange poll() error on %d:%d:%d",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
2002-12-23 03:29:22 -03:00
}
}
2002-12-24 14:30:59 -03:00
for (i = 0; i < nready; i++) {
ev = ready_index[i];
if (!ev->active) {
continue;
}
ev->ready = 1;
if (ev->oneshot) {
2003-05-19 12:39:14 -04:00
if (ev->timer_set) {
ngx_del_timer(ev);
ev->timer_set = 0;
}
2002-12-24 14:30:59 -03:00
if (ev->write) {
ngx_poll_del_event(ev, NGX_WRITE_EVENT, 0);
} else {
ngx_poll_del_event(ev, NGX_READ_EVENT, 0);
}
}
2003-05-13 12:02:32 -04:00
ev->event_handler(ev);
2002-12-24 14:30:59 -03:00
}
if (ready != 0) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "poll ready != events");
}
2002-12-23 03:29:22 -03:00
return NGX_OK;
}