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

266 lines
6 KiB
C
Raw Normal View History

2002-12-23 03:29:22 -03:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_types.h>
2002-12-23 15:22:18 -03:00
#include <ngx_errno.h>
2002-12-23 03:29:22 -03:00
#include <ngx_log.h>
#include <ngx_time.h>
#include <ngx_connection.h>
#include <ngx_event.h>
2003-01-23 15:47:54 -03:00
#include <ngx_event_timer.h>
2002-12-23 03:29:22 -03:00
#include <ngx_poll_module.h>
/* should be per-thread */
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;
2003-01-26 18:08:14 -03:00
static ngx_event_t *timer_queue;
2002-12-23 03:29:22 -03:00
/* */
int ngx_poll_init(int max_connections, ngx_log_t *log)
{
ngx_test_null(event_list,
ngx_alloc(sizeof(struct pollfd) * max_connections, log),
NGX_ERROR);
ngx_test_null(event_index,
ngx_alloc(sizeof(ngx_event_t *) * max_connections, log),
NGX_ERROR);
2002-12-24 14:30:59 -03:00
ngx_test_null(ready_index,
ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log),
NGX_ERROR);
2002-12-23 03:29:22 -03:00
nevents = 0;
2003-01-26 18:08:14 -03:00
timer_queue = ngx_event_init_timer(log);
if (timer_queue == NULL) {
return NGX_ERROR;
}
2002-12-23 03:29:22 -03:00
ngx_event_actions.add = ngx_poll_add_event;
ngx_event_actions.del = ngx_poll_del_event;
2003-01-26 18:08:14 -03:00
ngx_event_actions.timer = ngx_event_add_timer;
2002-12-23 03:29:22 -03:00
ngx_event_actions.process = ngx_poll_process_events;
return NGX_OK;
}
int ngx_poll_add_event(ngx_event_t *ev, int event, u_int flags)
{
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
ev->active = 1;
2002-12-23 03:29:22 -03:00
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1: 0;
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;
}
int ngx_poll_del_event(ngx_event_t *ev, int event, u_int flags)
{
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;
}
int ngx_poll_process_events(ngx_log_t *log)
{
2003-02-06 14:21:13 -03:00
int ready, found;
u_int i, nready;
2003-01-26 18:08:14 -03:00
ngx_msec_t timer, delta;
2002-12-23 15:22:18 -03:00
ngx_err_t err;
2002-12-23 03:29:22 -03:00
ngx_event_t *ev;
ngx_connection_t *c;
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
if ((ready = poll(event_list, nevents, timer)) == -1) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "poll() failed");
return NGX_ERROR;
}
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-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)
2002-12-23 03:29:22 -03:00
ngx_log_debug(log, "poll timer: %d, delta: %d" _ timer _ delta);
2002-12-24 14:30:59 -03:00
#endif
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++) {
2002-12-23 03:29:22 -03:00
c = &ngx_connections[event_list[i].fd];
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;
if (event_list[i].revents & POLLIN) {
found = 1;
2002-12-24 14:30:59 -03:00
ready_index[nready++] = c->read;
2002-12-23 03:29:22 -03:00
}
if (event_list[i].revents & POLLOUT) {
found = 1;
2002-12-24 14:30:59 -03:00
ready_index[nready++] = c->write;
2002-12-23 03:29:22 -03:00
}
if (event_list[i].revents & (POLLERR|POLLHUP|POLLNVAL)) {
found = 1;
2002-12-23 15:22:18 -03:00
2003-02-11 04:14:40 -03:00
/* need ot add to ready_index[nready++] = c->read or c->write; */
2002-12-23 15:22:18 -03:00
err = 0;
if (event_list[i].revents & POLLNVAL) {
err = EBADF;
}
ngx_log_error(NGX_LOG_ERR, log, err,
2002-12-23 03:29:22 -03:00
"poll() error on %d:%d",
event_list[i].fd, event_list[i].revents);
}
if (found) {
ready--;
}
}
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;
}