2002-08-06 12:39:45 -04:00
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
/*
|
2004-01-16 03:15:48 -03:00
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
|
2003-05-19 12:39:14 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
#include <ngx_config.h>
|
2002-09-02 10:48:24 -04:00
|
|
|
#include <ngx_core.h>
|
2002-08-06 12:39:45 -04:00
|
|
|
#include <ngx_event.h>
|
|
|
|
|
2002-12-15 03:25:09 -03:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
static int ngx_select_init(ngx_cycle_t *cycle);
|
|
|
|
static void ngx_select_done(ngx_cycle_t *cycle);
|
2003-05-19 12:39:14 -04:00
|
|
|
static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags);
|
|
|
|
static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags);
|
2004-04-01 02:21:13 -04:00
|
|
|
static int ngx_select_process_events(ngx_cycle_t *cycle);
|
2003-05-19 12:39:14 -04:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf);
|
2003-05-19 12:39:14 -04:00
|
|
|
|
|
|
|
|
2002-12-15 03:25:09 -03:00
|
|
|
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;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
|
|
|
#if (WIN32)
|
2002-12-15 03:25:09 -03:00
|
|
|
static int max_read;
|
|
|
|
static int max_write;
|
2002-08-06 12:39:45 -04:00
|
|
|
#else
|
2002-12-15 03:25:09 -03:00
|
|
|
static int max_fd;
|
2002-08-06 12:39:45 -04:00
|
|
|
#endif
|
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
static ngx_uint_t nevents;
|
2002-09-27 11:05:29 -04:00
|
|
|
|
|
|
|
static ngx_event_t **event_index;
|
2004-04-14 01:57:36 -04:00
|
|
|
#if 0
|
2002-09-27 11:05:29 -04:00
|
|
|
static ngx_event_t **ready_index;
|
2004-04-14 01:57:36 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static ngx_event_t *accept_events;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
|
|
|
|
static ngx_str_t select_name = ngx_string("select");
|
|
|
|
|
|
|
|
ngx_event_module_t ngx_select_module_ctx = {
|
|
|
|
&select_name,
|
|
|
|
NULL, /* create configuration */
|
|
|
|
ngx_select_init_conf, /* init configuration */
|
|
|
|
|
|
|
|
{
|
|
|
|
ngx_select_add_event, /* add an event */
|
|
|
|
ngx_select_del_event, /* delete an event */
|
|
|
|
ngx_select_add_event, /* enable an event */
|
|
|
|
ngx_select_del_event, /* disable an event */
|
|
|
|
NULL, /* add an connection */
|
|
|
|
NULL, /* delete an connection */
|
|
|
|
ngx_select_process_events, /* process the events */
|
|
|
|
ngx_select_init, /* init the events */
|
|
|
|
ngx_select_done /* done the events */
|
2002-08-26 11:18:19 -04:00
|
|
|
}
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
ngx_module_t ngx_select_module = {
|
2003-05-27 08:18:54 -04:00
|
|
|
NGX_MODULE,
|
2003-05-19 12:39:14 -04:00
|
|
|
&ngx_select_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-19 12:39:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
static int ngx_select_init(ngx_cycle_t *cycle)
|
2003-05-19 12:39:14 -04:00
|
|
|
{
|
2003-07-07 02:11:50 -04:00
|
|
|
ngx_event_t **index;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
if (event_index == NULL) {
|
|
|
|
FD_ZERO(&master_read_fd_set);
|
|
|
|
FD_ZERO(&master_write_fd_set);
|
|
|
|
nevents = 0;
|
|
|
|
}
|
2002-09-27 11:05:29 -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(index,
|
|
|
|
ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
|
|
|
|
cycle->log),
|
|
|
|
NGX_ERROR);
|
|
|
|
|
|
|
|
if (event_index) {
|
|
|
|
ngx_memcpy(index, event_index, sizeof(ngx_event_t *) * nevents);
|
|
|
|
ngx_free(event_index);
|
|
|
|
}
|
|
|
|
event_index = index;
|
2002-09-27 11:05:29 -04:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
#if 0
|
2003-07-07 02:11:50 -04:00
|
|
|
if (ready_index) {
|
|
|
|
ngx_free(ready_index);
|
|
|
|
}
|
|
|
|
ngx_test_null(ready_index,
|
|
|
|
ngx_alloc(sizeof(ngx_event_t *) * 2 * cycle->connection_n,
|
|
|
|
cycle->log),
|
|
|
|
NGX_ERROR);
|
2004-04-14 01:57:36 -04:00
|
|
|
#endif
|
2003-07-07 02:11:50 -04:00
|
|
|
}
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
ngx_io = ngx_os_io;
|
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
ngx_event_actions = ngx_select_module_ctx.actions;
|
|
|
|
|
2003-10-12 13:49:16 -03:00
|
|
|
ngx_event_flags = NGX_USE_LEVEL_EVENT|NGX_USE_ONESHOT_EVENT;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
|
|
|
#if (WIN32)
|
|
|
|
max_read = max_write = 0;
|
|
|
|
#else
|
|
|
|
max_fd = -1;
|
|
|
|
#endif
|
2002-09-27 11:05:29 -04:00
|
|
|
|
|
|
|
return NGX_OK;
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
static void ngx_select_done(ngx_cycle_t *cycle)
|
2003-05-19 12:39:14 -04:00
|
|
|
{
|
|
|
|
ngx_free(event_index);
|
2004-04-14 01:57:36 -04:00
|
|
|
#if 0
|
2003-05-19 12:39:14 -04:00
|
|
|
ngx_free(ready_index);
|
2004-04-14 01:57:36 -04:00
|
|
|
#endif
|
2003-07-07 02:11:50 -04:00
|
|
|
|
|
|
|
event_index = NULL;
|
2003-05-19 12:39:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags)
|
2002-08-06 12:39:45 -04:00
|
|
|
{
|
2002-12-24 14:30:59 -03:00
|
|
|
ngx_connection_t *c;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
c = ev->data;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"select add event fd:%d ev:%d", c->fd, event);
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2002-12-24 04:09:57 -03:00
|
|
|
if (ev->index != NGX_INVALID_INDEX) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
|
2004-02-03 17:27:11 -03:00
|
|
|
"select event fd:%d ev:%d is already set", c->fd, event);
|
2002-12-24 04:09:57 -03:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
#if (WIN32)
|
2003-11-11 15:13:43 -03:00
|
|
|
|
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);
|
2002-08-06 12:39:45 -04:00
|
|
|
max_read++;
|
2002-09-02 10:48:24 -04:00
|
|
|
|
|
|
|
} else if (event == NGX_WRITE_EVENT) {
|
|
|
|
FD_SET(c->fd, &master_write_fd_set);
|
2002-08-06 12:39:45 -04:00
|
|
|
max_write++;
|
|
|
|
}
|
2003-11-11 15:13:43 -03:00
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
#else
|
2003-11-11 15:13:43 -03:00
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
if (event == NGX_READ_EVENT) {
|
2002-09-02 10:48:24 -04:00
|
|
|
FD_SET(c->fd, &master_read_fd_set);
|
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
} else if (event == NGX_WRITE_EVENT) {
|
2002-09-02 10:48:24 -04:00
|
|
|
FD_SET(c->fd, &master_write_fd_set);
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-09-02 10:48:24 -04:00
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
if (max_fd != -1 && max_fd < c->fd) {
|
2002-09-02 10:48:24 -04:00
|
|
|
max_fd = c->fd;
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-09-02 10:48:24 -04:00
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
#endif
|
|
|
|
|
2002-12-24 14:30:59 -03:00
|
|
|
ev->active = 1;
|
2004-03-16 03:10:12 -04:00
|
|
|
ev->oneshot = (u_char) ((flags & NGX_ONESHOT_EVENT) ? 1 : 0);
|
2002-09-02 10:48:24 -04:00
|
|
|
|
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-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
|
|
|
|
static int ngx_select_del_event(ngx_event_t *ev, int event, u_int flags)
|
2002-08-06 12:39:45 -04:00
|
|
|
{
|
2003-05-19 12:39:14 -04:00
|
|
|
ngx_connection_t *c;
|
|
|
|
|
|
|
|
c = ev->data;
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
ev->active = 0;
|
|
|
|
ev->posted = 0;
|
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
if (ev->index == NGX_INVALID_INDEX) {
|
2002-12-15 03:25:09 -03:00
|
|
|
return NGX_OK;
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-12-15 03:25:09 -03:00
|
|
|
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"select del event fd:%d ev:%d", c->fd, event);
|
2002-12-06 13:32:33 -03:00
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
#if (WIN32)
|
2003-05-20 11:37:55 -04:00
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
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-08-06 12:39:45 -04:00
|
|
|
}
|
2003-05-20 11:37:55 -04:00
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
#else
|
2003-05-20 11:37:55 -04:00
|
|
|
|
|
|
|
if (event == NGX_READ_EVENT) {
|
2002-09-02 10:48:24 -04:00
|
|
|
FD_CLR(c->fd, &master_read_fd_set);
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
} else if (event == NGX_WRITE_EVENT) {
|
2002-09-02 10:48:24 -04:00
|
|
|
FD_CLR(c->fd, &master_write_fd_set);
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-09-02 10:48:24 -04:00
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
if (max_fd == c->fd) {
|
2002-09-02 10:48:24 -04:00
|
|
|
max_fd = -1;
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
#endif
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2003-11-21 03:30:49 -03:00
|
|
|
if (ev->index < (u_int) --nevents) {
|
2002-09-27 11:05:29 -04:00
|
|
|
event_index[ev->index] = event_index[nevents];
|
|
|
|
event_index[ev->index]->index = ev->index;
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2002-12-15 03:25:09 -03:00
|
|
|
ev->index = NGX_INVALID_INDEX;
|
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
return NGX_OK;
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2003-05-19 12:39:14 -04:00
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
static int ngx_select_process_events(ngx_cycle_t *cycle)
|
2002-08-06 12:39:45 -04:00
|
|
|
{
|
2004-04-14 01:57:36 -04:00
|
|
|
int ready, nready;
|
|
|
|
ngx_uint_t i, found, lock, expire;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_msec_t timer;
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_epoch_msec_t delta;
|
|
|
|
struct timeval tv, *tp;
|
2003-11-11 15:13:43 -03:00
|
|
|
#if (HAVE_SELECT_CHANGE_TIMEOUT)
|
2004-04-14 01:57:36 -04:00
|
|
|
static ngx_epoch_msec_t deltas = 0;
|
2003-11-11 15:13:43 -03:00
|
|
|
#endif
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-04-14 16:34:05 -04:00
|
|
|
for ( ;; ) {
|
|
|
|
timer = ngx_event_find_timer();
|
|
|
|
|
|
|
|
if (timer != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"select expired timer");
|
|
|
|
|
|
|
|
ngx_event_expire_timers(0);
|
|
|
|
}
|
|
|
|
|
2004-01-16 03:15:48 -03:00
|
|
|
ngx_old_elapsed_msec = ngx_elapsed_msec;
|
2003-01-26 18:08:14 -03:00
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
expire = 1;
|
2003-11-16 18:49:42 -03:00
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
#if !(WIN32)
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
if (ngx_accept_mutex) {
|
|
|
|
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-04-14 01:57:36 -04:00
|
|
|
|
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
|
|
|
{
|
|
|
|
timer = ngx_accept_mutex_delay;
|
|
|
|
expire = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-04-14 01:57:36 -04:00
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
if (max_fd == -1) {
|
2002-09-27 11:05:29 -04:00
|
|
|
for (i = 0; i < nevents; i++) {
|
2004-02-03 17:27:11 -03:00
|
|
|
c = event_index[i]->data;
|
2003-05-20 11:37:55 -04:00
|
|
|
if (max_fd < c->fd) {
|
2002-09-02 10:48:24 -04:00
|
|
|
max_fd = c->fd;
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-29 18:45:01 -03:00
|
|
|
"change max_fd: %d", max_fd);
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
#endif
|
2004-04-14 01:57:36 -04:00
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
#if (NGX_DEBUG)
|
|
|
|
for (i = 0; i < nevents; i++) {
|
|
|
|
ev = event_index[i];
|
|
|
|
c = ev->data;
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"select event: fd:%d wr:%d", c->fd, ev->write);
|
2002-12-15 03:25:09 -03:00
|
|
|
}
|
2002-12-24 14:30:59 -03:00
|
|
|
#endif
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-04-14 16:34:05 -04:00
|
|
|
if (timer == NGX_TIMER_INFINITE) {
|
|
|
|
tp = NULL;
|
|
|
|
expire = 0;
|
2004-04-14 13:44:28 -04:00
|
|
|
|
2004-04-14 16:34:05 -04:00
|
|
|
} else {
|
2004-04-14 13:44:28 -04:00
|
|
|
tv.tv_sec = timer / 1000;
|
|
|
|
tv.tv_usec = (timer % 1000) * 1000;
|
|
|
|
tp = &tv;
|
|
|
|
}
|
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"select timer: %d", timer);
|
2004-01-29 18:45:01 -03:00
|
|
|
|
2004-04-14 13:44:28 -04:00
|
|
|
work_read_fd_set = master_read_fd_set;
|
|
|
|
work_write_fd_set = master_write_fd_set;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"select read fd_set: %08X", *(int *) &work_read_fd_set);
|
|
|
|
|
2002-08-06 12:39:45 -04:00
|
|
|
#if (WIN32)
|
2003-07-07 02:11:50 -04:00
|
|
|
ready = select(0, &work_read_fd_set, &work_write_fd_set, NULL, tp);
|
2002-08-06 12:39:45 -04:00
|
|
|
#else
|
2003-07-07 02:11:50 -04:00
|
|
|
ready = select(max_fd + 1, &work_read_fd_set, &work_write_fd_set, NULL, tp);
|
2002-08-06 12:39:45 -04:00
|
|
|
#endif
|
2003-07-07 02:11:50 -04:00
|
|
|
|
|
|
|
if (ready == -1) {
|
|
|
|
err = ngx_socket_errno;
|
|
|
|
} else {
|
|
|
|
err = 0;
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2003-11-11 15:13:43 -03:00
|
|
|
#if (HAVE_SELECT_CHANGE_TIMEOUT)
|
2003-11-10 18:09:22 -03:00
|
|
|
|
2004-04-14 16:34:05 -04:00
|
|
|
if (timer != NGX_TIMER_INFINITE) {
|
2003-07-20 17:15:59 -04:00
|
|
|
delta = timer - (tv.tv_sec * 1000 + tv.tv_usec / 1000);
|
2003-07-21 12:24:25 -04:00
|
|
|
|
|
|
|
/*
|
2003-11-11 15:13:43 -03:00
|
|
|
* learn the real time and update the cached time
|
|
|
|
* if the sum of the last deltas overcomes 1 second
|
2003-07-21 12:24:25 -04:00
|
|
|
*/
|
2003-11-11 15:13:43 -03:00
|
|
|
|
2003-07-21 12:24:25 -04:00
|
|
|
deltas += delta;
|
2003-11-11 15:13:43 -03:00
|
|
|
if (deltas > 1000) {
|
|
|
|
ngx_gettimeofday(&tv);
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_time_update(tv.tv_sec);
|
2004-02-02 18:19:52 -03:00
|
|
|
deltas = tv.tv_usec / 1000;
|
2003-07-21 12:24:25 -04:00
|
|
|
}
|
2003-11-11 15:13:43 -03:00
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-29 18:45:01 -03:00
|
|
|
"select timer: %d, delta: %d", timer, (int) delta);
|
2003-11-11 15:13:43 -03:00
|
|
|
|
|
|
|
} else {
|
2004-02-02 18:19:52 -03:00
|
|
|
delta = 0;
|
2003-11-11 15:13:43 -03:00
|
|
|
ngx_gettimeofday(&tv);
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_time_update(tv.tv_sec);
|
2003-11-11 15:13:43 -03:00
|
|
|
|
|
|
|
if (ready == 0) {
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
2003-11-11 15:13:43 -03:00
|
|
|
"select() returned no events without timeout");
|
2004-04-14 13:44:28 -04:00
|
|
|
ngx_accept_mutex_unlock();
|
2003-11-11 15:13:43 -03:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-29 18:45:01 -03:00
|
|
|
#else /* !(HAVE_SELECT_CHANGE_TIMEOUT) */
|
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-11 15:13:43 -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-14 16:34:05 -04:00
|
|
|
if (timer != NGX_TIMER_INFINITE) {
|
2004-01-29 18:45:01 -03:00
|
|
|
delta = ngx_elapsed_msec - delta;
|
2003-11-11 15:13:43 -03:00
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-29 18:45:01 -03:00
|
|
|
"select timer: %d, delta: %d", timer, (int) delta);
|
2002-08-06 12:39:45 -04:00
|
|
|
|
|
|
|
} else {
|
2002-12-24 14:30:59 -03:00
|
|
|
if (ready == 0) {
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
2003-07-20 17:15:59 -04:00
|
|
|
"select() returned no events without timeout");
|
2004-04-14 13:44:28 -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
|
|
|
}
|
|
|
|
|
2003-11-11 15:13:43 -03:00
|
|
|
#endif /* HAVE_SELECT_CHANGE_TIMEOUT */
|
|
|
|
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"select ready %d", ready);
|
2004-01-29 18:45:01 -03:00
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
if (err) {
|
2004-03-04 04:04:55 -03:00
|
|
|
#if (WIN32)
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, err, "select() failed");
|
2004-03-04 04:04:55 -03:00
|
|
|
#else
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
2004-04-01 02:21:13 -04:00
|
|
|
cycle->log, err, "select() failed");
|
2004-03-04 04:04:55 -03:00
|
|
|
#endif
|
2004-04-14 13:44:28 -04:00
|
|
|
ngx_accept_mutex_unlock();
|
2003-07-07 02:11:50 -04:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-08-06 12:39:45 -04:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
|
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
|
|
ngx_accept_mutex_unlock();
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock = 1;
|
2002-09-27 11:05:29 -04:00
|
|
|
nready = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
|
|
|
ev = event_index[i];
|
2004-02-03 17:27:11 -03:00
|
|
|
c = ev->data;
|
2002-08-06 12:39:45 -04:00
|
|
|
found = 0;
|
|
|
|
|
|
|
|
if (ev->write) {
|
2002-09-02 10:48:24 -04:00
|
|
|
if (FD_ISSET(c->fd, &work_write_fd_set)) {
|
2002-08-06 12:39:45 -04:00
|
|
|
found = 1;
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-29 18:45:01 -03:00
|
|
|
"select write %d", c->fd);
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2002-09-02 10:48:24 -04:00
|
|
|
if (FD_ISSET(c->fd, &work_read_fd_set)) {
|
2002-08-06 12:39:45 -04:00
|
|
|
found = 1;
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-29 18:45:01 -03:00
|
|
|
"select read %d", c->fd);
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2004-04-14 01:57:36 -04:00
|
|
|
ev->ready = 1;
|
|
|
|
|
|
|
|
if (ev->oneshot) {
|
|
|
|
if (ev->timer_set) {
|
|
|
|
ngx_del_timer(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->write) {
|
|
|
|
ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
|
|
|
|
} else {
|
|
|
|
ngx_select_del_event(ev, NGX_READ_EVENT, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev->accept) {
|
|
|
|
ev->next = accept_events;
|
|
|
|
accept_events = ev;
|
|
|
|
} else {
|
|
|
|
ngx_post_event(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
nready++;
|
|
|
|
|
|
|
|
#if 0
|
2002-09-27 11:05:29 -04:00
|
|
|
ready_index[nready++] = ev;
|
2004-04-14 01:57:36 -04:00
|
|
|
#endif
|
2002-09-27 11:05:29 -04:00
|
|
|
}
|
|
|
|
}
|
2002-09-02 10:48:24 -04:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
#if 0
|
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-08-06 12:39:45 -04:00
|
|
|
|
2002-09-27 11:05:29 -04:00
|
|
|
if (ev->oneshot) {
|
2003-05-19 12:39:14 -04:00
|
|
|
if (ev->timer_set) {
|
|
|
|
ngx_del_timer(ev);
|
|
|
|
}
|
2002-12-15 03:25:09 -03:00
|
|
|
|
2003-05-20 11:37:55 -04:00
|
|
|
if (ev->write) {
|
2002-12-15 03:25:09 -03:00
|
|
|
ngx_select_del_event(ev, NGX_WRITE_EVENT, 0);
|
2003-05-20 11:37:55 -04:00
|
|
|
} else {
|
2002-12-15 03:25:09 -03:00
|
|
|
ngx_select_del_event(ev, NGX_READ_EVENT, 0);
|
2003-05-20 11:37:55 -04:00
|
|
|
}
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2003-05-13 12:02:32 -04:00
|
|
|
ev->event_handler(ev);
|
2002-12-24 14:30:59 -03:00
|
|
|
}
|
2004-04-14 01:57:36 -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-09-27 11:05:29 -04:00
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
if (ready != nready) {
|
2004-04-01 02:21:13 -04:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "select ready != events");
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
if (expire && delta) {
|
2004-01-29 18:45:01 -03:00
|
|
|
ngx_event_expire_timers((ngx_msec_t) delta);
|
|
|
|
}
|
|
|
|
|
2004-04-14 01:57:36 -04:00
|
|
|
if (!ngx_threaded) {
|
|
|
|
ngx_event_process_posted(cycle);
|
|
|
|
}
|
|
|
|
|
2002-09-02 10:48:24 -04:00
|
|
|
return NGX_OK;
|
2002-08-06 12:39:45 -04:00
|
|
|
}
|
2003-05-19 12:39:14 -04:00
|
|
|
|
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
static char *ngx_select_init_conf(ngx_cycle_t *cycle, void *conf)
|
2003-05-19 12:39:14 -04:00
|
|
|
{
|
|
|
|
ngx_event_conf_t *ecf;
|
|
|
|
|
2003-07-07 02:11:50 -04:00
|
|
|
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
|
2003-05-19 12:39:14 -04:00
|
|
|
|
2003-07-22 15:53:10 -04:00
|
|
|
/* disable warning: the default FD_SETSIZE is 1024U in FreeBSD 5.x */
|
2003-06-11 11:28:34 -04:00
|
|
|
|
|
|
|
if ((unsigned) ecf->connections > FD_SETSIZE) {
|
2004-02-02 18:19:52 -03:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
|
|
"the maximum number of files "
|
|
|
|
"supported by select() is " ngx_value(FD_SETSIZE));
|
|
|
|
return NGX_CONF_ERROR;
|
2003-05-19 12:39:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|