From 31f8818eb110d18e58fef72684418c33ddbd7a0b Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Fri, 27 Sep 2002 15:05:29 +0000 Subject: [PATCH] nginx-0.0.1-2002-09-27-19:05:29 import --- src/event/modules/ngx_kqueue_module.c | 12 ++-- src/event/modules/ngx_kqueue_module.h | 2 +- src/event/modules/ngx_select_module.c | 82 +++++++++++++++------------ src/event/modules/ngx_select_module.h | 2 +- src/event/ngx_event.c | 8 ++- src/event/ngx_event.h | 2 + src/http/ngx_http_event.c | 2 +- 7 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c index aa9d8cb8b..aad8581a9 100644 --- a/src/event/modules/ngx_kqueue_module.c +++ b/src/event/modules/ngx_kqueue_module.c @@ -27,7 +27,7 @@ static int nchanges, nevents; static ngx_event_t timer_queue; -void ngx_kqueue_init(int max_connections, ngx_log_t *log) +int ngx_kqueue_init(int max_connections, ngx_log_t *log) { int size = sizeof(struct kevent) * 512; @@ -38,11 +38,11 @@ void ngx_kqueue_init(int max_connections, ngx_log_t *log) if (kq == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "kqueue() failed"); - exit(1); + return NGX_ERROR; } - change_list = ngx_alloc(size, log); - event_list = ngx_alloc(size, log); + ngx_test_null(change_list, ngx_alloc(size, log), NGX_ERROR); + ngx_test_null(event_list, ngx_alloc(size, log), NGX_ERROR); timer_queue.timer_prev = &timer_queue; timer_queue.timer_next = &timer_queue; @@ -53,6 +53,8 @@ void ngx_kqueue_init(int max_connections, ngx_log_t *log) ngx_event_actions.timer = ngx_kqueue_add_timer; ngx_event_actions.process = ngx_kqueue_process_events; #endif + + return NGX_OK; } int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags) @@ -77,7 +79,7 @@ int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags) if (nchanges >= nevents) { ngx_log_error(NGX_LOG_WARN, ev->log, 0, - "ngx_kqueue_set_event: change list is filled up"); + "kqueue change list is filled up"); if (kevent(kq, change_list, nchanges, NULL, 0, &ts) == -1) { ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, "kevent failed"); diff --git a/src/event/modules/ngx_kqueue_module.h b/src/event/modules/ngx_kqueue_module.h index 389ff67e2..ce2aacde3 100644 --- a/src/event/modules/ngx_kqueue_module.h +++ b/src/event/modules/ngx_kqueue_module.h @@ -6,7 +6,7 @@ #include #include -void ngx_kqueue_init(int max_connections, ngx_log_t *log); +int ngx_kqueue_init(int max_connections, ngx_log_t *log); int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags); int ngx_kqueue_del_event(ngx_event_t *ev, int event); int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags); diff --git a/src/event/modules/ngx_select_module.c b/src/event/modules/ngx_select_module.c index cc44d9147..884eea7bb 100644 --- a/src/event/modules/ngx_select_module.c +++ b/src/event/modules/ngx_select_module.c @@ -20,14 +20,17 @@ static int max_write; static int max_fd; #endif -static ngx_event_t event_queue; -static ngx_event_t timer_queue; +static int nevents; + +static ngx_event_t **event_index; +static ngx_event_t **ready_index; +static ngx_event_t timer_queue; static fd_set *ngx_select_get_fd_set(ngx_socket_t fd, int event, ngx_log_t *log); -void ngx_select_init(int max_connections, ngx_log_t *log) +int ngx_select_init(int max_connections, ngx_log_t *log) { if (max_connections > FD_SETSIZE) { ngx_log_error(NGX_LOG_EMERG, log, 0, @@ -44,8 +47,15 @@ void ngx_select_init(int max_connections, ngx_log_t *log) FD_ZERO(&master_read_fd_set); FD_ZERO(&master_write_fd_set); - event_queue.prev = &event_queue; - event_queue.next = &event_queue; + 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; timer_queue.timer_prev = &timer_queue; timer_queue.timer_next = &timer_queue; @@ -60,6 +70,8 @@ void ngx_select_init(int max_connections, ngx_log_t *log) #else max_fd = -1; #endif + + return NGX_OK; } int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags) @@ -102,10 +114,9 @@ int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags) ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1: 0; - ev->prev = &event_queue; - ev->next = event_queue.next; - event_queue.next->prev = ev; - event_queue.next = ev; + event_index[nevents] = ev; + ev->index = nevents; + nevents++; return NGX_OK; } @@ -135,23 +146,19 @@ int ngx_select_del_event(ngx_event_t *ev, int event) max_fd = -1; #endif - if (ev->prev) - ev->prev->next = ev->next; - - if (ev->next) { - ev->next->prev = ev->prev; - ev->prev = NULL; + if (ev->index < nevents) { + event_index[ev->index] = event_index[nevents]; + event_index[ev->index]->index = ev->index; } - if (ev->prev) - ev->next = NULL; + nevents--; return NGX_OK; } int ngx_select_process_events(ngx_log_t *log) { - int ready, found; + int i, ready, found, nready; u_int timer, delta; ngx_event_t *ev, *nx; ngx_connection_t *c; @@ -176,8 +183,8 @@ int ngx_select_process_events(ngx_log_t *log) #if !(WIN32) if (max_fd == -1) { - for (ev = event_queue.next; ev != &event_queue; ev = ev->next) { - c = (ngx_connection_t *) ev->data; + for (i = 0; i < nevents; i++) { + c = (ngx_connection_t *) event_index[i]->data; if (max_fd < c->fd) max_fd = c->fd; } @@ -231,7 +238,10 @@ int ngx_select_process_events(ngx_log_t *log) } } - for (ev = event_queue.next; ev != &event_queue; /* void */) { + nready = 0; + + for (i = 0; i < nevents; i++) { + ev = event_index[i]; c = (ngx_connection_t *) ev->data; found = 0; @@ -250,26 +260,28 @@ int ngx_select_process_events(ngx_log_t *log) } } - nx = ev->next; - if (found) { - ev->ready = 1; + ready_index[nready++] = ev; + } + } - if (ev->oneshot) { - ngx_del_timer(ev); - if (ev->write) - ngx_select_del_event(ev, NGX_WRITE_EVENT); - else - ngx_select_del_event(ev, NGX_READ_EVENT); - } + for (i = 0; i < nready; i++) { + ev = ready_index[i]; - if (ev->event_handler(ev) == -1) - ev->close_handler(ev); + ev->ready = 1; - ready--; + if (ev->oneshot) { + ngx_del_timer(ev); + if (ev->write) + ngx_select_del_event(ev, NGX_WRITE_EVENT); + else + ngx_select_del_event(ev, NGX_READ_EVENT); } - ev = nx; + if (ev->event_handler(ev) == -1) + ev->close_handler(ev); + + ready--; } ngx_assert((ready == 0), /* void */ ; , log, "select ready != events"); diff --git a/src/event/modules/ngx_select_module.h b/src/event/modules/ngx_select_module.h index ea08591bb..e2583d744 100644 --- a/src/event/modules/ngx_select_module.h +++ b/src/event/modules/ngx_select_module.h @@ -6,7 +6,7 @@ #include #include -void ngx_select_init(int max_connections, ngx_log_t *log); +int ngx_select_init(int max_connections, ngx_log_t *log); int ngx_select_add_event(ngx_event_t *ev, int event, u_int flags); int ngx_select_del_event(ngx_event_t *ev, int event); int ngx_select_set_event(ngx_event_t *ev, int filter, u_int flags); diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c index 62d3c1599..8fbdbe5d2 100644 --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -20,7 +21,7 @@ ngx_event_t *ngx_read_events, *ngx_write_events; #if !(USE_KQUEUE) -#if 0 +#if 1 ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT; #else ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT; @@ -29,7 +30,7 @@ ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT; ngx_event_actions_t ngx_event_actions; /* ngx_event_type_e order */ -static void (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = { +static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = { ngx_select_init, #if (HAVE_POLL) ngx_poll_init, @@ -50,7 +51,8 @@ void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log) /* STUB */ int max_connections = 512; - ngx_init_events(max_connections, log); + if (ngx_init_events(max_connections, log) == NGX_ERROR) + exit(1); ngx_connections = ngx_alloc(sizeof(ngx_connection_t) * max_connections, log); diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h index 7ac88051c..a4e8dd5ba 100644 --- a/src/event/ngx_event.h +++ b/src/event/ngx_event.h @@ -20,6 +20,8 @@ struct ngx_event_s { void *context; char *action; + int index; + ngx_event_t *prev; /* queue in select(), poll(), mutex(), */ ngx_event_t *next; /* aio_read(), aio_write() */ diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c index 9c6a77b76..aa56704be 100644 --- a/src/http/ngx_http_event.c +++ b/src/http/ngx_http_event.c @@ -491,7 +491,7 @@ static int ngx_http_handler(ngx_http_request_t *r) r->connection->read->event_handler = ngx_http_block_read; /* STUB: should find handler */ -#if 0 +#if 1 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; #endif rc = ngx_http_set_default_handler(r);