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

580 lines
14 KiB
C
Raw Normal View History

2002-12-23 03:29:22 -03:00
2003-05-20 11:37:55 -04:00
/*
2004-01-29 18:45:01 -03:00
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
2003-05-20 11:37:55 -04:00
*/
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);
2004-04-02 11:13:20 -04:00
int ngx_poll_process_events(ngx_cycle_t *cycle);
2003-05-20 11:37:55 -04:00
2002-12-23 03:29:22 -03:00
static struct pollfd *event_list;
2003-11-21 03:30:49 -03:00
static int nevents;
2002-12-23 03:29:22 -03:00
2004-04-13 11:08:48 -04:00
#if 0
2002-12-24 14:30:59 -03:00
static ngx_event_t **ready_index;
2004-04-13 11:08:48 -04:00
#endif
static ngx_event_t *accept_events;
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 */
2004-04-02 11:13:20 -04:00
NULL /* init process */
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;
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
2004-04-14 01:57:36 -04:00
if (ngx_process == NGX_PROCESS_WORKER
|| cycle->old_cycle == NULL
2003-07-07 02:11:50 -04:00
|| 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
2004-04-13 11:08:48 -04:00
#if 0
2003-07-07 02:11:50 -04:00
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);
2004-04-13 11:08:48 -04:00
#endif
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;
2003-10-12 13:49:16 -03:00
ngx_event_flags = NGX_USE_LEVEL_EVENT|NGX_USE_ONESHOT_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
{
ngx_free(event_list);
2004-04-13 11:08:48 -04:00
#if 0
2003-05-20 11:37:55 -04:00
ngx_free(ready_index);
2004-04-13 11:08:48 -04:00
#endif
2003-07-07 02:11:50 -04:00
event_list = NULL;
2004-04-13 11:08:48 -04:00
#if 0
2003-07-10 12:26:57 -04:00
ready_index = NULL;
2004-04-13 11:08:48 -04:00
#endif
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
2004-04-14 13:44:28 -04:00
ev->active = 1;
2004-02-03 17:27:11 -03:00
if (ev->index != NGX_INVALID_INDEX) {
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"poll event fd:%d ev:%d is already set", c->fd, event);
return NGX_OK;
}
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
}
2004-01-29 18:45:01 -03:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"poll add event: fd:%d ev:%d", c->fd, event);
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;
ev->index = nevents;
nevents++;
} else {
2004-02-03 17:27:11 -03:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
2004-02-12 17:57:10 -03:00
"poll add index: %d", e->index);
2004-02-03 17:27:11 -03:00
2002-12-23 03:29:22 -03:00
event_list[e->index].events |= event;
ev->index = e->index;
}
2004-02-05 13:58:36 -03:00
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
2002-12-23 03:29:22 -03:00
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
{
2004-04-02 11:13:20 -04:00
ngx_uint_t i;
2004-02-05 13:58:36 -03:00
ngx_cycle_t **cycle;
ngx_event_t *e;
ngx_connection_t *c;
2002-12-23 03:29:22 -03:00
2004-02-05 13:58:36 -03:00
c = ev->data;
2002-12-23 03:29:22 -03:00
2004-04-13 11:08:48 -04:00
ev->active = 0;
ev->posted = 0;
2002-12-24 14:30:59 -03:00
if (ev->index == NGX_INVALID_INDEX) {
2004-02-05 13:58:36 -03:00
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"poll event fd:%d ev:%d is already deleted",
c->fd, event);
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
}
2004-01-29 18:45:01 -03:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"poll del event: fd:%d ev:%d", c->fd, event);
2002-12-23 03:29:22 -03:00
if (e == NULL || e->index == NGX_INVALID_INDEX) {
2004-02-03 17:27:11 -03:00
nevents--;
if (ev->index < (u_int) nevents) {
2004-02-12 17:57:10 -03:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
"index: copy event %d to %d", nevents, ev->index);
2002-12-23 15:22:18 -03:00
event_list[ev->index] = event_list[nevents];
2004-02-05 13:58:36 -03:00
c = &ngx_cycle->connections[event_list[nevents].fd];
if (c->fd == -1) {
cycle = ngx_old_cycles.elts;
for (i = 0; i < ngx_old_cycles.nelts; i++) {
if (cycle[i] == NULL) {
continue;
}
c = &cycle[i]->connections[event_list[nevents].fd];
if (c->fd != -1) {
break;
}
}
}
if (c->fd == -1) {
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"unexpected last event");
} else {
if (c->read->index == (u_int) nevents) {
c->read->index = ev->index;
2004-02-12 17:57:10 -03:00
}
2004-02-05 13:58:36 -03:00
2004-02-12 17:57:10 -03:00
if (c->write->index == (u_int) nevents) {
2004-02-05 13:58:36 -03:00
c->write->index = ev->index;
}
}
2002-12-23 03:29:22 -03:00
}
} else {
2004-02-03 17:27:11 -03:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
2004-02-12 17:57:10 -03:00
"poll del index: %d", e->index);
2004-02-03 17:27:11 -03:00
2002-12-23 03:29:22 -03:00
event_list[e->index].events &= ~event;
}
ev->index = NGX_INVALID_INDEX;
return NGX_OK;
}
2003-05-20 11:37:55 -04:00
2004-04-02 11:13:20 -04:00
int ngx_poll_process_events(ngx_cycle_t *cycle)
2002-12-23 03:29:22 -03:00
{
2004-02-05 13:58:36 -03:00
int ready;
2004-04-02 11:13:20 -04:00
ngx_int_t i, nready;
2004-04-13 11:08:48 -04:00
ngx_uint_t n, found, lock, expire;
2003-11-11 15:13:43 -03:00
ngx_msec_t timer;
2003-07-07 02:11:50 -04:00
ngx_err_t err;
2004-04-02 11:13:20 -04:00
ngx_cycle_t **old_cycle;
2003-07-07 02:11:50 -04:00
ngx_event_t *ev;
2004-01-29 18:45:01 -03:00
ngx_epoch_msec_t delta;
2003-07-07 02:11:50 -04:00
ngx_connection_t *c;
2004-01-29 18:45:01 -03:00
struct timeval tv;
2002-12-23 03:29:22 -03:00
2004-02-17 18:11:27 -03:00
if (ngx_event_flags & NGX_OVERFLOW_EVENT) {
timer = 0;
2004-04-13 11:08:48 -04:00
expire = 0;
2004-02-17 18:11:27 -03:00
} else {
2004-04-14 16:34:05 -04:00
for ( ;; ) {
timer = ngx_event_find_timer();
2003-01-26 18:08:14 -03:00
2004-04-14 16:34:05 -04:00
if (timer != 0) {
break;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"poll expired timer");
2004-04-15 11:34:36 -04:00
ngx_event_expire_timers(ngx_elapsed_msec - ngx_old_elapsed_msec);
2004-04-14 16:34:05 -04:00
}
/* NGX_TIMER_INFINITE == INFTIM */
2004-04-14 13:44:28 -04:00
2004-04-14 16:34:05 -04:00
if (timer == NGX_TIMER_INFINITE) {
2004-04-13 11:08:48 -04:00
expire = 0;
} else {
expire = 1;
2004-02-17 18:11:27 -03:00
}
2002-12-23 03:29:22 -03:00
}
2004-02-17 18:11:27 -03:00
ngx_old_elapsed_msec = ngx_elapsed_msec;
2004-02-05 13:58:36 -03:00
#if (NGX_DEBUG0)
2002-12-23 03:29:22 -03:00
for (i = 0; i < nevents; i++) {
2004-04-02 11:13:20 -04:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"poll: %d: fd:%d ev:%04X",
2004-02-03 17:27:11 -03:00
i, event_list[i].fd, event_list[i].events);
2002-12-23 03:29:22 -03:00
}
2004-04-13 11:08:48 -04:00
#endif
if (ngx_accept_mutex) {
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
return NGX_ERROR;
}
2004-04-14 13:44:28 -04:00
if (ngx_accept_mutex_held == 0
2004-04-14 16:34:05 -04:00
&& (timer == NGX_TIMER_INFINITE || timer > ngx_accept_mutex_delay))
2004-04-14 13:44:28 -04:00
{
2004-04-13 11:08:48 -04:00
timer = ngx_accept_mutex_delay;
expire = 0;
}
}
2002-12-23 03:29:22 -03:00
2004-04-02 11:13:20 -04:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "poll timer: %d", timer);
2002-12-23 03:29:22 -03:00
2003-11-21 03:30:49 -03:00
ready = poll(event_list, (u_int) nevents, (int) timer);
2003-07-07 02:11:50 -04:00
if (ready == -1) {
err = ngx_errno;
} else {
err = 0;
2002-12-23 03:29:22 -03:00
}
2003-11-11 15:13:43 -03:00
ngx_gettimeofday(&tv);
2004-01-05 17:55:48 -03:00
ngx_time_update(tv.tv_sec);
2003-11-10 18:09:22 -03:00
2004-01-29 18:45:01 -03:00
delta = ngx_elapsed_msec;
ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec;
2003-07-07 02:11:50 -04:00
2004-04-02 11:13:20 -04:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
2004-02-09 04:46:43 -03:00
"poll ready %d of %d", ready, nevents);
2004-01-30 14:39:00 -03:00
if (err) {
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
2004-04-02 11:13:20 -04:00
cycle->log, err, "poll() failed");
2004-04-13 11:08:48 -04:00
ngx_accept_mutex_unlock();
2004-01-30 14:39:00 -03:00
return NGX_ERROR;
}
2004-04-14 16:34:05 -04:00
if (timer != NGX_TIMER_INFINITE) {
2004-01-29 18:45:01 -03:00
delta = ngx_elapsed_msec - delta;
2002-12-23 03:29:22 -03:00
2004-04-02 11:13:20 -04:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
2004-01-29 18:45:01 -03:00
"poll timer: %d, delta: %d", timer, (int) delta);
2002-12-23 03:29:22 -03:00
} else {
2002-12-24 14:30:59 -03:00
if (ready == 0) {
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
2003-07-20 17:15:59 -04:00
"poll() returned no events without timeout");
2004-04-13 11:08:48 -04:00
ngx_accept_mutex_unlock();
2002-12-24 14:30:59 -03:00
return NGX_ERROR;
}
2003-07-07 02:11:50 -04:00
}
2004-04-14 13:44:28 -04:00
if ((ngx_event_flags & NGX_OVERFLOW_EVENT) && timer == 0 && ready == 0) {
2004-02-17 18:11:27 -03:00
/* the overflowed rt signals queue has been drained */
2004-04-14 13:44:28 -04:00
ngx_accept_mutex_unlock();
2004-02-17 18:11:27 -03:00
return NGX_OK;
}
2004-04-13 11:08:48 -04:00
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
lock = 1;
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++) {
2004-02-01 05:10:52 -03:00
2004-02-05 13:58:36 -03:00
#if 0
2004-04-02 11:13:20 -04:00
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
2004-02-03 17:27:11 -03:00
"poll: %d: fd:%d ev:%04X rev:%04X",
i, event_list[i].fd,
2004-02-01 05:10:52 -03:00
event_list[i].events, event_list[i].revents);
2004-02-05 13:58:36 -03:00
#else
if (event_list[i].revents) {
2004-04-02 11:13:20 -04:00
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
2004-02-05 13:58:36 -03:00
"poll: %d: fd:%d ev:%04X rev:%04X",
i, event_list[i].fd,
event_list[i].events, event_list[i].revents);
}
#endif
2004-02-01 05:10:52 -03:00
2004-04-13 11:08:48 -04:00
if (event_list[i].revents & (POLLERR|POLLNVAL)) {
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
2004-02-01 05:10:52 -03:00
"poll() error fd:%d ev:%04X rev:%04X",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
}
if (event_list[i].revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL))
{
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
2004-02-01 05:10:52 -03:00
"strange poll() events fd:%d ev:%04X rev:%04X",
event_list[i].fd,
event_list[i].events, event_list[i].revents);
}
2004-02-04 17:30:08 -03:00
if (event_list[i].fd == -1) {
2004-04-13 11:08:48 -04:00
/*
2004-04-14 01:57:36 -04:00
* the disabled event, a workaround for our possible bug,
* see the comment below
2004-04-13 11:08:48 -04:00
*/
2004-02-04 17:30:08 -03:00
continue;
}
2003-07-07 02:11:50 -04:00
c = &ngx_cycle->connections[event_list[i].fd];
if (c->fd == -1) {
2004-04-02 11:13:20 -04:00
old_cycle = ngx_old_cycles.elts;
for (n = 0; n < ngx_old_cycles.nelts; n++) {
if (old_cycle[n] == NULL) {
2003-07-07 02:11:50 -04:00
continue;
}
2004-04-02 11:13:20 -04:00
c = &old_cycle[n]->connections[event_list[i].fd];
2003-07-07 02:11:50 -04:00
if (c->fd != -1) {
break;
}
}
}
if (c->fd == -1) {
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "unexpected event");
2004-02-03 17:27:11 -03:00
/*
* it is certainly our fault and it should be investigated,
* in the meantime we disable this event to avoid a CPU spinning
*/
if (i == nevents - 1) {
nevents--;
} else {
event_list[i].fd = -1;
}
2004-02-01 05:10:52 -03:00
continue;
2003-07-07 02:11:50 -04:00
}
2002-12-23 03:29:22 -03:00
found = 0;
2004-02-02 18:19:52 -03:00
if (event_list[i].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) {
2002-12-23 03:29:22 -03:00
found = 1;
2004-04-13 11:08:48 -04:00
ev = c->read;
ev->ready = 1;
if (ev->oneshot) {
if (ev->timer_set) {
ngx_del_timer(ev);
}
ngx_poll_del_event(ev, NGX_READ_EVENT, 0);
}
if (ev->accept) {
ev->next = accept_events;
accept_events = ev;
} else {
ngx_post_event(ev);
}
#if 0
2003-05-20 11:37:55 -04:00
ready_index[nready++] = c->read;
2004-04-13 11:08:48 -04:00
#endif
2002-12-23 03:29:22 -03:00
}
2004-02-02 18:19:52 -03:00
if (event_list[i].revents & (POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
2002-12-23 03:29:22 -03:00
found = 1;
2004-04-13 11:08:48 -04:00
ev = c->write;
ev->ready = 1;
if (ev->oneshot) {
if (ev->timer_set) {
ngx_del_timer(ev);
}
ngx_poll_del_event(ev, NGX_WRITE_EVENT, 0);
}
ngx_post_event(ev);
#if 0
2003-05-20 11:37:55 -04:00
ready_index[nready++] = c->write;
2004-04-13 11:08:48 -04:00
#endif
2002-12-23 03:29:22 -03:00
}
if (found) {
ready--;
2003-05-20 11:37:55 -04:00
continue;
}
2002-12-23 03:29:22 -03:00
}
2004-04-13 11:08:48 -04:00
#if 0
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);
}
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
}
2004-04-13 11:08:48 -04:00
#endif
ev = accept_events;
for ( ;; ) {
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
"accept event " PTR_FMT, ev);
if (ev == NULL) {
break;
}
ngx_mutex_unlock(ngx_posted_events_mutex);
ev->event_handler(ev);
ev = ev->next;
if (ev == NULL) {
lock = 0;
break;
}
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
ngx_accept_mutex_unlock();
return NGX_ERROR;
}
}
if (lock) {
ngx_mutex_unlock(ngx_posted_events_mutex);
}
ngx_accept_mutex_unlock();
accept_events = NULL;
2002-12-24 14:30:59 -03:00
if (ready != 0) {
2004-04-02 11:13:20 -04:00
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "poll ready != events");
2002-12-24 14:30:59 -03:00
}
2004-04-13 11:08:48 -04:00
if (expire && delta) {
2004-01-29 18:45:01 -03:00
ngx_event_expire_timers((ngx_msec_t) delta);
}
2004-04-13 11:08:48 -04:00
if (!ngx_threaded) {
ngx_event_process_posted(cycle);
}
2004-02-17 18:11:27 -03:00
return nready;
2002-12-23 03:29:22 -03:00
}