Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ngx_event.c « event « src - github.com/nginx/nginx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2dabf609076c16ff791c4e4d337d942bc0bff085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_types.h>
#include <ngx_string.h>
#include <ngx_log.h>
#include <ngx_alloc.h>
#include <ngx_array.h>
#include <ngx_listen.h>
#include <ngx_connection.h>
#include <ngx_event.h>
#include <ngx_event_accept.h>

#include <ngx_select_module.h>

#if (HAVE_POLL)
#include <ngx_poll_module.h>
#endif

#if (HAVE_DEVPOLL)
#include <ngx_devpoll_module.h>
#endif

#if (HAVE_KQUEUE)
#include <ngx_kqueue_module.h>
#endif

#if (HAVE_AIO)
#include <ngx_aio_module.h>
#endif

#if (HAVE_IOCP)
#include <ngx_event_acceptex.h>
#include <ngx_iocp_module.h>
#endif


ngx_connection_t    *ngx_connections;
ngx_event_t         *ngx_read_events, *ngx_write_events;

#if !(USE_KQUEUE)

ngx_event_type_e     ngx_event_type;

int                  ngx_event_flags;

ngx_event_actions_t  ngx_event_actions;

/* ngx_event_type_e order */
static int (*ngx_event_init[]) (int max_connections, ngx_log_t *log) = {
    ngx_select_init,
#if (HAVE_POLL)
    ngx_poll_init,
#endif
#if (HAVE_DEVPOLL)
    ngx_devpoll_init,
#endif
#if (HAVE_KQUEUE)
    ngx_kqueue_init,
#endif
#if (HAVE_AIO)
    ngx_aio_init,
#endif
#if (HAVE_IOCP)
    ngx_iocp_init
#endif
};

#endif /* USE_KQUEUE */


void ngx_pre_thread(ngx_array_t *ls, ngx_pool_t *pool, ngx_log_t *log)
{
    int  i, fd;

    ngx_listen_t      *s;
    ngx_event_t       *ev;
    ngx_connection_t  *c;

    /* STUB */
    int max_connections = 512;

#if 0
    ngx_event_type = NGX_POLL_EVENT_N;
#endif
#if 1
    ngx_event_type = NGX_KQUEUE_EVENT_N;
#endif
#if 0
    ngx_event_type = NGX_DEVPOLL_EVENT_N;
#endif
#if 0
    ngx_event_type = NGX_AIO_EVENT_N;
#endif
#if 0
    ngx_event_type = NGX_IOCP_EVENT_N;
#endif

    if (ngx_init_events(max_connections, log) == NGX_ERROR) {
        exit(1);
    }

    ngx_connections = ngx_alloc(sizeof(ngx_connection_t)
                                                       * max_connections, log);
    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 */
    s = (ngx_listen_t *) ls->elts;
    for (i = 0; i < ls->nelts; i++) {

        fd = s[i].fd;

        c = &ngx_connections[fd];
        ev = &ngx_read_events[fd];

        ngx_memzero(c, sizeof(ngx_connection_t));
        ngx_memzero(ev, sizeof(ngx_event_t));

        c->fd = fd;
        c->family = s[i].family;
        c->socklen = s[i].socklen;
        c->sockaddr = ngx_palloc(pool, s[i].socklen);
        c->addr = s[i].addr;
        c->addr_text = s[i].addr_text;
        c->addr_text_max_len = s[i].addr_text_max_len;
        c->post_accept_timeout = s[i].post_accept_timeout;

        c->handler = s[i].handler;
        c->ctx = s[i].ctx;
        c->servers = s[i].servers;
        c->log = s[i].log;
        c->pool_size = s[i].pool_size;

        ngx_test_null(ev->log,
                      ngx_palloc(pool, sizeof(ngx_log_t)), /* void */ ; );
        ngx_memcpy(ev->log, c->log, sizeof(ngx_log_t));
        c->read = ev;
        ev->data = c;
        ev->index = NGX_INVALID_INDEX;
#if 0
        ev->listening = 1;
#endif

        ev->available = 0;

#if (HAVE_DEFERRED_ACCEPT)
        ev->deferred_accept = s[i].deferred_accept;
#endif

#if (HAVE_IOCP)

        if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
            ev->event_handler = &ngx_event_acceptex;

            /* LOOK: we call ngx_iocp_add_event() also
               in ngx_event_post_acceptex() */
            if (ngx_iocp_add_event(ev) == NGX_ERROR) {
                return NGX_ERROR;
            }

            ngx_event_post_acceptex(&s[i], 1);

        } else {
            ev->event_handler = &ngx_event_accept;
        }

#else

        ev->event_handler = &ngx_event_accept;
        ngx_add_event(ev, NGX_READ_EVENT, 0);

#endif
    }
}

void ngx_worker(ngx_log_t *log)
{
    for ( ;; ) {
        ngx_log_debug(log, "ngx_worker cycle");

        ngx_process_events(log);
    }
}