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

318 lines
7.3 KiB
C
Raw Normal View History

#include <ngx_config.h>
2002-09-02 10:48:24 -04:00
#include <ngx_core.h>
#include <ngx_types.h>
#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>
#include <ngx_select_module.h>
2002-12-15 03:25:09 -03:00
/* should be per-thread */
static fd_set master_read_fd_set;
static fd_set master_write_fd_set;
static fd_set work_read_fd_set;
static fd_set work_write_fd_set;
#if (WIN32)
2002-12-15 03:25:09 -03:00
static int max_read;
static int max_write;
#else
2002-12-15 03:25:09 -03:00
static int max_fd;
#endif
2003-02-06 14:21:13 -03:00
static u_int nevents;
2002-09-27 11:05:29 -04:00
static ngx_event_t **event_index;
static ngx_event_t **ready_index;
2003-01-26 18:08:14 -03:00
static ngx_event_t *timer_queue;
2002-12-15 03:25:09 -03:00
/* */
2002-09-27 11:05:29 -04:00
int ngx_select_init(int max_connections, ngx_log_t *log)
{
2002-08-26 11:18:19 -04:00
if (max_connections > FD_SETSIZE) {
ngx_log_error(NGX_LOG_EMERG, log, 0,
2002-08-26 11:18:19 -04:00
#if (WIN32)
"maximum number of descriptors "
"supported by select() is %d", FD_SETSIZE);
#else
2002-08-26 11:18:19 -04:00
"maximum descriptor number"
"supported by select() is %d", FD_SETSIZE - 1);
#endif
2002-08-26 11:18:19 -04:00
exit(1);
}
2002-09-02 10:48:24 -04:00
FD_ZERO(&master_read_fd_set);
FD_ZERO(&master_write_fd_set);
2002-09-27 11:05:29 -04:00
ngx_test_null(event_index,
ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log),
NGX_ERROR);
ngx_test_null(ready_index,
ngx_alloc(sizeof(ngx_event_t *) * 2 * max_connections, log),
NGX_ERROR);
nevents = 0;
2003-01-26 18:08:14 -03:00
timer_queue = ngx_event_init_timer(log);
if (timer_queue == NULL) {
return NGX_ERROR;
}
ngx_event_actions.add = ngx_select_add_event;
ngx_event_actions.del = ngx_select_del_event;
2003-01-26 18:08:14 -03:00
ngx_event_actions.timer = ngx_event_add_timer;
ngx_event_actions.process = ngx_select_process_events;
#if (WIN32)
max_read = max_write = 0;
#else
max_fd = -1;
#endif
2002-09-27 11:05:29 -04:00
return NGX_OK;
}
int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags)
{
2002-12-24 14:30:59 -03:00
ngx_connection_t *c;
2002-09-02 10:48:24 -04:00
c = (ngx_connection_t *) ev->data;
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-09-02 10:48:24 -04:00
ngx_log_debug(ev->log, "select fd:%d event:%d" _ c->fd _ event);
2002-12-24 14:30:59 -03:00
#endif
2002-12-24 04:09:57 -03:00
if (ev->index != NGX_INVALID_INDEX) {
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
"%d:%d is already set", c->fd, event);
return NGX_OK;
}
#if (WIN32)
2002-09-02 10:48:24 -04:00
if ((event == NGX_READ_EVENT) && (max_read >= FD_SETSIZE)
|| (event == NGX_WRITE_EVENT) && (max_write >= FD_SETSIZE))
{
ngx_log_error(NGX_LOG_ERR, ev->log, 0,
"maximum number of descriptors "
"supported by select() is %d", FD_SETSIZE);
return NGX_ERROR;
}
if (event == NGX_READ_EVENT) {
FD_SET(c->fd, &master_read_fd_set);
max_read++;
2002-09-02 10:48:24 -04:00
} else if (event == NGX_WRITE_EVENT) {
FD_SET(c->fd, &master_write_fd_set);
max_write++;
}
#else
2002-09-02 10:48:24 -04:00
if (event == NGX_READ_EVENT)
FD_SET(c->fd, &master_read_fd_set);
else if (event == NGX_WRITE_EVENT)
FD_SET(c->fd, &master_write_fd_set);
if (max_fd != -1 && max_fd < c->fd)
max_fd = c->fd;
#endif
2002-12-24 14:30:59 -03:00
ev->active = 1;
2002-09-02 10:48:24 -04:00
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1: 0;
2002-09-27 11:05:29 -04:00
event_index[nevents] = ev;
ev->index = nevents;
nevents++;
2002-09-02 10:48:24 -04:00
return NGX_OK;
}
2002-12-15 03:25:09 -03:00
int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags)
{
2002-09-02 10:48:24 -04:00
ngx_connection_t *c;
c = (ngx_connection_t *) ev->data;
2002-12-15 03:25:09 -03:00
if (ev->index == NGX_INVALID_INDEX)
return NGX_OK;
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-12-15 03:25:09 -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-06 13:32:33 -03:00
2002-09-02 10:48:24 -04:00
#if (WIN32)
if (event == NGX_READ_EVENT) {
FD_CLR(c->fd, &master_read_fd_set);
max_read--;
} else if (event == NGX_WRITE_EVENT) {
FD_CLR(c->fd, &master_write_fd_set);
max_write--;
}
2002-09-02 10:48:24 -04:00
#else
if (event == NGX_READ_EVENT)
FD_CLR(c->fd, &master_read_fd_set);
2002-09-02 10:48:24 -04:00
else if (event == NGX_WRITE_EVENT)
FD_CLR(c->fd, &master_write_fd_set);
if (max_fd == c->fd)
max_fd = -1;
#endif
2002-12-15 03:25:09 -03:00
if (ev->index < --nevents) {
2002-09-27 11:05:29 -04:00
event_index[ev->index] = event_index[nevents];
event_index[ev->index]->index = ev->index;
}
2002-12-24 14:30:59 -03:00
ev->active = 0;
2002-12-15 03:25:09 -03:00
ev->index = NGX_INVALID_INDEX;
2002-09-02 10:48:24 -04:00
return NGX_OK;
}
int ngx_select_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-15 03:25:09 -03:00
ngx_event_t *ev;
2002-09-02 10:48:24 -04:00
ngx_connection_t *c;
struct timeval tv, *tp;
2002-09-02 10:48:24 -04:00
work_read_fd_set = master_read_fd_set;
work_write_fd_set = master_write_fd_set;
2003-01-26 18:08:14 -03:00
timer = ngx_event_find_timer();
if (timer) {
tv.tv_sec = timer / 1000;
tv.tv_usec = (timer % 1000) * 1000;
tp = &tv;
delta = ngx_msec();
} else {
timer = 0;
tp = NULL;
delta = 0;
}
#if !(WIN32)
if (max_fd == -1) {
2002-09-27 11:05:29 -04:00
for (i = 0; i < nevents; i++) {
c = (ngx_connection_t *) event_index[i]->data;
2002-09-02 10:48:24 -04:00
if (max_fd < c->fd)
max_fd = c->fd;
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-09-02 10:48:24 -04:00
ngx_log_debug(log, "change max_fd: %d" _ max_fd);
2002-12-24 14:30:59 -03:00
#endif
}
#endif
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-12-15 03:25:09 -03:00
for (i = 0; i < nevents; i++) {
ev = event_index[i];
c = (ngx_connection_t *) ev->data;
2002-12-24 04:09:57 -03:00
ngx_log_debug(log, "select: %d:%d" _ c->fd _ ev->write);
2002-12-15 03:25:09 -03:00
}
2002-09-16 11:01:44 -04:00
ngx_log_debug(log, "select timer: %d" _ timer);
2002-12-24 14:30:59 -03:00
#endif
#if (WIN32)
2002-09-02 10:48:24 -04:00
if ((ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp))
#else
2002-09-02 10:48:24 -04:00
if ((ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set,
NULL, tp))
#endif
2002-12-24 14:30:59 -03:00
== -1)
{
2002-09-16 11:01:44 -04:00
ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, "select() failed");
2002-09-02 10:48:24 -04:00
return NGX_ERROR;
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-09-16 11:01:44 -04:00
ngx_log_debug(log, "select ready %d" _ ready);
2002-12-24 14:30:59 -03:00
#endif
if (timer) {
delta = ngx_msec() - delta;
2003-03-20 12:09:44 -04:00
ngx_event_expire_timers(delta);
} else {
2002-12-24 14:30:59 -03:00
if (ready == 0) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
"select() returns no events without timeout");
return NGX_ERROR;
}
}
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
2002-09-16 11:01:44 -04:00
ngx_log_debug(log, "select timer: %d, delta: %d" _ timer _ delta);
2002-12-24 14:30:59 -03:00
#endif
2002-09-27 11:05:29 -04:00
nready = 0;
for (i = 0; i < nevents; i++) {
ev = event_index[i];
2002-09-02 10:48:24 -04:00
c = (ngx_connection_t *) ev->data;
found = 0;
if (ev->write) {
2002-09-02 10:48:24 -04:00
if (FD_ISSET(c->fd, &work_write_fd_set)) {
found = 1;
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
ngx_log_debug(log, "select write %d" _ c->fd);
#endif
}
} else {
2002-09-02 10:48:24 -04:00
if (FD_ISSET(c->fd, &work_read_fd_set)) {
found = 1;
2002-12-24 14:30:59 -03:00
#if (NGX_DEBUG_EVENT)
ngx_log_debug(log, "select read %d" _ c->fd);
#endif
}
}
if (found) {
2002-09-27 11:05:29 -04:00
ready_index[nready++] = ev;
}
}
2002-09-02 10:48:24 -04:00
2002-09-27 11:05:29 -04:00
for (i = 0; i < nready; i++) {
ev = ready_index[i];
2002-12-24 14:30:59 -03:00
ready--;
if (!ev->active) {
continue;
}
2002-09-02 10:48:24 -04:00
2002-09-27 11:05:29 -04:00
ev->ready = 1;
2002-09-27 11:05:29 -04:00
if (ev->oneshot) {
ngx_del_timer(ev);
2002-12-15 03:25:09 -03:00
2002-09-27 11:05:29 -04:00
if (ev->write)
2002-12-15 03:25:09 -03:00
ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
2002-09-27 11:05:29 -04:00
else
2002-12-15 03:25:09 -03:00
ngx_select_del_event(ev, NGX_READ_EVENT, 0);
}
2002-12-15 03:25:09 -03:00
if (ev->event_handler(ev) == NGX_ERROR)
2002-09-27 11:05:29 -04:00
ev->close_handler(ev);
2002-12-24 14:30:59 -03:00
}
2002-09-27 11:05:29 -04:00
2002-12-24 14:30:59 -03:00
if (ready != 0) {
ngx_log_error(NGX_LOG_ALERT, log, 0, "select ready != events");
}
2002-09-02 10:48:24 -04:00
return NGX_OK;
}