blob: cef4aecf2fddecb9808d9c799cdd3af9068cbb6b [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 Sysoev5eef6182002-12-15 21:08:04 +00008#include <ngx_array.h>
Igor Sysoev2b542382002-08-20 14:48:28 +00009#include <ngx_listen.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000010#include <ngx_connection.h>
11#include <ngx_event.h>
12#include <ngx_event_accept.h>
13
14#include <ngx_select_module.h>
Igor Sysoeva6717c42002-12-23 06:29:22 +000015#if (HAVE_POLL)
16#include <ngx_poll_module.h>
17#endif
Igor Sysoev3a17f242002-12-24 17:30:59 +000018#if (HAVE_DEVPOLL)
19#include <ngx_devpoll_module.h>
20#endif
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000021#if (HAVE_KQUEUE)
22#include <ngx_kqueue_module.h>
23#endif
24
25
26ngx_connection_t *ngx_connections;
27ngx_event_t *ngx_read_events, *ngx_write_events;
28
29#if !(USE_KQUEUE)
30
Igor Sysoev93e50542002-12-20 06:54:55 +000031#if (HAVE_KQUEUE)
Igor Sysoeva6717c42002-12-23 06:29:22 +000032
33#if 0
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000034ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
Igor Sysoev3a17f242002-12-24 17:30:59 +000035#elif 0
Igor Sysoeva6717c42002-12-23 06:29:22 +000036ngx_event_type_e ngx_event_type = NGX_POLL_EVENT;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000037#else
38ngx_event_type_e ngx_event_type = NGX_KQUEUE_EVENT;
39#endif
Igor Sysoeva6717c42002-12-23 06:29:22 +000040
Igor Sysoev3a17f242002-12-24 17:30:59 +000041#elif (HAVE_DEVPOLL)
Igor Sysoev295bb632002-12-23 18:22:18 +000042
43#if 0
Igor Sysoev93e50542002-12-20 06:54:55 +000044ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
Igor Sysoev3a17f242002-12-24 17:30:59 +000045#elif 0
Igor Sysoev295bb632002-12-23 18:22:18 +000046ngx_event_type_e ngx_event_type = NGX_POLL_EVENT;
Igor Sysoev3a17f242002-12-24 17:30:59 +000047#else
48ngx_event_type_e ngx_event_type = NGX_DEVPOLL_EVENT;
Igor Sysoev295bb632002-12-23 18:22:18 +000049#endif
50
51#else
52
53ngx_event_type_e ngx_event_type = NGX_SELECT_EVENT;
54
Igor Sysoev93e50542002-12-20 06:54:55 +000055#endif
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000056
57ngx_event_actions_t ngx_event_actions;
58
59/* ngx_event_type_e order */
Igor Sysoev31f88182002-09-27 15:05:29 +000060static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000061 ngx_select_init,
62#if (HAVE_POLL)
63 ngx_poll_init,
64#endif
Igor Sysoev3a17f242002-12-24 17:30:59 +000065#if (HAVE_DEVPOLL)
66 ngx_devpoll_init,
67#endif
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000068#if (HAVE_KQUEUE)
69 ngx_kqueue_init
70#endif
71};
72
73#endif /* USE_KQUEUE */
74
75
Igor Sysoev2b542382002-08-20 14:48:28 +000076void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000077{
78 int i, fd;
Igor Sysoeva6717c42002-12-23 06:29:22 +000079
80 ngx_listen_t *s;
81 ngx_event_t *ev;
82 ngx_connection_t *c;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000083
Igor Sysoev0ad17c02002-08-26 15:18:19 +000084 /* STUB */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000085 int max_connections = 512;
86
Igor Sysoev31f88182002-09-27 15:05:29 +000087 if (ngx_init_events(max_connections, log) == NGX_ERROR)
88 exit(1);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000089
Igor Sysoev0ad17c02002-08-26 15:18:19 +000090 ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
91 * max_connections, log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000092 ngx_read_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
93 ngx_write_events = ngx_alloc(sizeof(ngx_event_t) * max_connections, log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000094
95 /* for each listening socket */
Igor Sysoev2b542382002-08-20 14:48:28 +000096 s = (ngx_listen_t *) ls->elts;
97 for (i = 0; i < ls->nelts; i++) {
98
99 fd = s[i].fd;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000100
Igor Sysoeva6717c42002-12-23 06:29:22 +0000101 c = &ngx_connections[fd];
102 ev = &ngx_read_events[fd];
103
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000104 ngx_memzero(&ngx_connections[fd], sizeof(ngx_connection_t));
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000105 ngx_memzero(&ngx_read_events[fd], sizeof(ngx_event_t));
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000106
107 ngx_connections[fd].fd = fd;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000108 ngx_connections[fd].family = s[i].family;
109 ngx_connections[fd].socklen = s[i].socklen;
110 ngx_connections[fd].sockaddr = ngx_palloc(pool, s[i].socklen);
111 ngx_connections[fd].addr = s[i].addr;
112 ngx_connections[fd].addr_text = s[i].addr_text;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000113 ngx_connections[fd].post_accept_timeout = s[i].post_accept_timeout;
114
Igor Sysoev2b542382002-08-20 14:48:28 +0000115 ngx_connections[fd].server = s[i].server;
Igor Sysoev2b542382002-08-20 14:48:28 +0000116 ngx_connections[fd].handler = s[i].handler;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000117 ngx_connections[fd].log = s[i].log;
118
Igor Sysoeva58e3ca2002-09-02 14:48:24 +0000119 ngx_test_null(ngx_read_events[fd].log,
120 ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
121 ngx_memcpy(ngx_read_events[fd].log, ngx_connections[fd].log,
122 sizeof(ngx_log_t));
Igor Sysoeva6717c42002-12-23 06:29:22 +0000123 c->read = ev;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000124 ngx_read_events[fd].data = &ngx_connections[fd];
125 ngx_read_events[fd].event_handler = &ngx_event_accept;
126 ngx_read_events[fd].listening = 1;
Igor Sysoev0d2bda52002-12-24 07:09:57 +0000127 ev->index = NGX_INVALID_INDEX;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000128
129 ngx_read_events[fd].available = 0;
130
131#if (HAVE_DEFERRED_ACCEPT)
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000132 ngx_read_events[fd].deferred_accept = s[i].deferred_accept;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000133#endif
134 ngx_add_event(&ngx_read_events[fd], NGX_READ_EVENT, 0);
135 }
Igor Sysoev2b542382002-08-20 14:48:28 +0000136}
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000137
Igor Sysoev2b542382002-08-20 14:48:28 +0000138void ngx_worker(ngx_log_t *log)
139{
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000140 while (1) {
141 ngx_log_debug(log, "ngx_worker cycle");
142
143 ngx_process_events(log);
144 }
145}