2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-09-27 23:05:29 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_types.h>
|
2002-09-02 22:48:24 +08:00
|
|
|
#include <ngx_string.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_log.h>
|
|
|
|
#include <ngx_alloc.h>
|
2002-12-16 05:08:04 +08:00
|
|
|
#include <ngx_array.h>
|
2002-08-20 22:48:28 +08:00
|
|
|
#include <ngx_listen.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_connection.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
#include <ngx_event_accept.h>
|
|
|
|
|
|
|
|
#include <ngx_select_module.h>
|
2002-12-23 14:29:22 +08:00
|
|
|
#if (HAVE_POLL)
|
|
|
|
#include <ngx_poll_module.h>
|
|
|
|
#endif
|
2002-12-25 01:30:59 +08:00
|
|
|
#if (HAVE_DEVPOLL)
|
|
|
|
#include <ngx_devpoll_module.h>
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
ngx_connection_t *ngx_connections;
|
|
|
|
ngx_event_t *ngx_read_events, *ngx_write_events;
|
|
|
|
|
|
|
|
#if !(USE_KQUEUE)
|
|
|
|
|
2002-12-20 14:54:55 +08:00
|
|
|
#if (HAVE_KQUEUE)
|
2002-12-23 14:29:22 +08:00
|
|
|
|
|
|
|
#if 0
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
|
2002-12-25 01:30:59 +08:00
|
|
|
#elif 0
|
2002-12-23 14:29:22 +08:00
|
|
|
ngx_event_type_e ngx_event_type = NGX_POLL_EVENT;
|
2002-08-07 00:39:45 +08:00
|
|
|
#else
|
|
|
|
ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT;
|
|
|
|
#endif
|
2002-12-23 14:29:22 +08:00
|
|
|
|
2002-12-25 01:30:59 +08:00
|
|
|
#elif (HAVE_DEVPOLL)
|
2002-12-24 02:22:18 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
|
2002-12-25 01:30:59 +08:00
|
|
|
#elif 0
|
2002-12-24 02:22:18 +08:00
|
|
|
ngx_event_type_e ngx_event_type = NGX_POLL_EVENT;
|
2002-12-25 01:30:59 +08:00
|
|
|
#else
|
|
|
|
ngx_event_type_e ngx_event_type = NGX_DEVPOLL_EVENT;
|
2002-12-24 02:22:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2002-12-20 14:54:55 +08:00
|
|
|
ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
|
2002-12-24 02:22:18 +08:00
|
|
|
|
2002-12-20 14:54:55 +08:00
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
ngx_event_actions_t ngx_event_actions;
|
|
|
|
|
|
|
|
/* ngx_event_type_e order */
|
2002-09-27 23:05:29 +08:00
|
|
|
static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_select_init,
|
|
|
|
#if (HAVE_POLL)
|
|
|
|
ngx_poll_init,
|
|
|
|
#endif
|
2002-12-25 01:30:59 +08:00
|
|
|
#if (HAVE_DEVPOLL)
|
|
|
|
ngx_devpoll_init,
|
|
|
|
#endif
|
2002-08-07 00:39:45 +08:00
|
|
|
#if (HAVE_KQUEUE)
|
|
|
|
ngx_kqueue_init
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
|
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
|
|
|
int i, fd;
|
2002-12-23 14:29:22 +08:00
|
|
|
|
|
|
|
ngx_listen_t *s;
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_connection_t *c;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
/* STUB */
|
2002-08-07 00:39:45 +08:00
|
|
|
int max_connections = 512;
|
|
|
|
|
2002-09-27 23:05:29 +08:00
|
|
|
if (ngx_init_events(max_connections, log) == NGX_ERROR)
|
|
|
|
exit(1);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-08-26 23:18:19 +08:00
|
|
|
ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
|
|
|
|
* max_connections, log);
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
|
|
|
|
ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
|
|
|
|
|
|
|
|
/* for each listening socket */
|
2002-08-20 22:48:28 +08:00
|
|
|
s = (ngx_listen_t *) ls->elts;
|
|
|
|
for (i = 0; i < ls->nelts; i++) {
|
|
|
|
|
|
|
|
fd = s[i].fd;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-23 14:29:22 +08:00
|
|
|
c = &ngx_connections[fd];
|
|
|
|
ev = &ngx_read_events[fd];
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_memzero(&ngx_connections[fd], sizeof(ngx_connection_t));
|
2002-08-26 23:18:19 +08:00
|
|
|
ngx_memzero(&ngx_read_events[fd], sizeof(ngx_event_t));
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
ngx_connections[fd].fd = fd;
|
2002-08-26 23:18:19 +08:00
|
|
|
ngx_connections[fd].family = s[i].family;
|
|
|
|
ngx_connections[fd].socklen = s[i].socklen;
|
|
|
|
ngx_connections[fd].sockaddr = ngx_palloc(pool, s[i].socklen);
|
|
|
|
ngx_connections[fd].addr = s[i].addr;
|
|
|
|
ngx_connections[fd].addr_text = s[i].addr_text;
|
|
|
|
ngx_connections[fd].post_accept_timeout = s[i].post_accept_timeout;
|
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
ngx_connections[fd].server = s[i].server;
|
|
|
|
ngx_connections[fd].handler = s[i].handler;
|
2002-08-26 23:18:19 +08:00
|
|
|
ngx_connections[fd].log = s[i].log;
|
|
|
|
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_test_null(ngx_read_events[fd].log,
|
|
|
|
ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
|
|
|
|
ngx_memcpy(ngx_read_events[fd].log, ngx_connections[fd].log,
|
|
|
|
sizeof(ngx_log_t));
|
2002-12-23 14:29:22 +08:00
|
|
|
c->read = ev;
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_read_events[fd].data = &ngx_connections[fd];
|
|
|
|
ngx_read_events[fd].event_handler = &ngx_event_accept;
|
|
|
|
ngx_read_events[fd].listening = 1;
|
2002-12-24 15:09:57 +08:00
|
|
|
ev->index = NGX_INVALID_INDEX;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
ngx_read_events[fd].available = 0;
|
|
|
|
|
|
|
|
#if (HAVE_DEFERRED_ACCEPT)
|
2002-08-26 23:18:19 +08:00
|
|
|
ngx_read_events[fd].deferred_accept = s[i].deferred_accept;
|
2002-08-07 00:39:45 +08:00
|
|
|
#endif
|
|
|
|
ngx_add_event(&ngx_read_events[fd], NGX_READ_EVENT, 0);
|
|
|
|
}
|
2002-08-20 22:48:28 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-08-20 22:48:28 +08:00
|
|
|
void ngx_worker(ngx_log_t *log)
|
|
|
|
{
|
2002-08-07 00:39:45 +08:00
|
|
|
while (1) {
|
|
|
|
ngx_log_debug(log, "ngx_worker cycle");
|
|
|
|
|
|
|
|
ngx_process_events(log);
|
|
|
|
}
|
|
|
|
}
|