blob: 8fbdbe5d2712d2343c5727c1b1c660b1ced8eece [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
2#include <ngx_config.h>
Igor Sysoev31f88182002-09-27 15:05:29 +00003#include <ngx_core.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00004#include <ngx_types.h>
Igor Sysoeva58e3ca2002-09-02 14:48:24 +00005#include <ngx_string.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00006#include <ngx_log.h>
7#include <ngx_alloc.h>
Igor Sysoev2b542382002-08-20 14:48:28 +00008#include <ngx_listen.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00009#include <ngx_connection.h>
10#include <ngx_event.h>
11#include <ngx_event_accept.h>
12
13#include <ngx_select_module.h>
14#if (HAVE_KQUEUE)
15#include <ngx_kqueue_module.h>
16#endif
17
18
19ngx_connection_t *ngx_connections;
20ngx_event_t *ngx_read_events, *ngx_write_events;
21
22#if !(USE_KQUEUE)
23
Igor Sysoev31f88182002-09-27 15:05:29 +000024#if 1
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000025ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
26#else
27ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT;
28#endif
29
30ngx_event_actions_t ngx_event_actions;
31
32/* ngx_event_type_e order */
Igor Sysoev31f88182002-09-27 15:05:29 +000033static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000034 ngx_select_init,
35#if (HAVE_POLL)
36 ngx_poll_init,
37#endif
38#if (HAVE_KQUEUE)
39 ngx_kqueue_init
40#endif
41};
42
43#endif /* USE_KQUEUE */
44
45
Igor Sysoev2b542382002-08-20 14:48:28 +000046void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000047{
48 int i, fd;
Igor Sysoev2b542382002-08-20 14:48:28 +000049 ngx_listen_t *s;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000050
Igor Sysoev0ad17c02002-08-26 15:18:19 +000051 /* STUB */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000052 int max_connections = 512;
53
Igor Sysoev31f88182002-09-27 15:05:29 +000054 if (ngx_init_events(max_connections, log) == NGX_ERROR)
55 exit(1);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000056
Igor Sysoev0ad17c02002-08-26 15:18:19 +000057 ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
58 * max_connections, log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000059 ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
60 ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000061
62 /* for each listening socket */
Igor Sysoev2b542382002-08-20 14:48:28 +000063 s = (ngx_listen_t *) ls->elts;
64 for (i = 0; i < ls->nelts; i++) {
65
66 fd = s[i].fd;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000067
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000068 ngx_memzero(&ngx_connections[fd], sizeof(ngx_connection_t));
Igor Sysoev0ad17c02002-08-26 15:18:19 +000069 ngx_memzero(&ngx_read_events[fd], sizeof(ngx_event_t));
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000070
71 ngx_connections[fd].fd = fd;
Igor Sysoev0ad17c02002-08-26 15:18:19 +000072 ngx_connections[fd].family = s[i].family;
73 ngx_connections[fd].socklen = s[i].socklen;
74 ngx_connections[fd].sockaddr = ngx_palloc(pool, s[i].socklen);
75 ngx_connections[fd].addr = s[i].addr;
76 ngx_connections[fd].addr_text = s[i].addr_text;
77 ngx_connections[fd].addr_textlen = s[i].addr_textlen;
78 ngx_connections[fd].post_accept_timeout = s[i].post_accept_timeout;
79
Igor Sysoev2b542382002-08-20 14:48:28 +000080 ngx_connections[fd].server = s[i].server;
Igor Sysoev2b542382002-08-20 14:48:28 +000081 ngx_connections[fd].handler = s[i].handler;
Igor Sysoev0ad17c02002-08-26 15:18:19 +000082 ngx_connections[fd].log = s[i].log;
83
Igor Sysoeva58e3ca2002-09-02 14:48:24 +000084 ngx_test_null(ngx_read_events[fd].log,
85 ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
86 ngx_memcpy(ngx_read_events[fd].log, ngx_connections[fd].log,
87 sizeof(ngx_log_t));
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000088 ngx_read_events[fd].data = &ngx_connections[fd];
89 ngx_read_events[fd].event_handler = &ngx_event_accept;
90 ngx_read_events[fd].listening = 1;
91
92 ngx_read_events[fd].available = 0;
93
94#if (HAVE_DEFERRED_ACCEPT)
Igor Sysoev0ad17c02002-08-26 15:18:19 +000095 ngx_read_events[fd].deferred_accept = s[i].deferred_accept;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000096#endif
97 ngx_add_event(&ngx_read_events[fd], NGX_READ_EVENT, 0);
98 }
Igor Sysoev2b542382002-08-20 14:48:28 +000099}
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000100
Igor Sysoev2b542382002-08-20 14:48:28 +0000101void ngx_worker(ngx_log_t *log)
102{
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000103 while (1) {
104 ngx_log_debug(log, "ngx_worker cycle");
105
106 ngx_process_events(log);
107 }
108}