Mercurial > hg > nginx
annotate src/event/modules/ngx_eventport_module.c @ 6021:117c77b22db1
Events: implemented eventport notification mechanism.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Sat, 14 Mar 2015 17:37:21 +0300 |
parents | 466bd63b63d1 |
children | 4652f8f26b12 |
rev | line source |
---|---|
719 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
719 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_event.h> | |
11 | |
12 | |
13 #if (NGX_TEST_BUILD_EVENTPORT) | |
14 | |
15 #define ushort_t u_short | |
16 #define uint_t u_int | |
17 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
18 #ifndef CLOCK_REALTIME |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
19 #define CLOCK_REALTIME 0 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
20 typedef int clockid_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
21 typedef void * timer_t; |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
22 #endif |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
23 |
719 | 24 /* Solaris declarations */ |
25 | |
26 #define PORT_SOURCE_AIO 1 | |
27 #define PORT_SOURCE_TIMER 2 | |
28 #define PORT_SOURCE_USER 3 | |
29 #define PORT_SOURCE_FD 4 | |
30 #define PORT_SOURCE_ALERT 5 | |
31 #define PORT_SOURCE_MQ 6 | |
32 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
33 #ifndef ETIME |
719 | 34 #define ETIME 64 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
35 #endif |
719 | 36 |
37 #define SIGEV_PORT 4 | |
38 | |
39 typedef struct { | |
40 int portev_events; /* event data is source specific */ | |
41 ushort_t portev_source; /* event source */ | |
42 ushort_t portev_pad; /* port internal use */ | |
43 uintptr_t portev_object; /* source specific object */ | |
44 void *portev_user; /* user cookie */ | |
45 } port_event_t; | |
46 | |
47 typedef struct port_notify { | |
48 int portnfy_port; /* bind request(s) to port */ | |
49 void *portnfy_user; /* user defined */ | |
50 } port_notify_t; | |
51 | |
1939
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
52 #if (__FreeBSD_version < 700005) |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
53 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
54 typedef struct itimerspec { /* definition per POSIX.4 */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
55 struct timespec it_interval;/* timer period */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
56 struct timespec it_value; /* timer expiration */ |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
57 } itimerspec_t; |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
58 |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
59 #endif |
6e7daf852eec
restore building --test-build-rtsig and --test-build-eventport on FreeBSD 6
Igor Sysoev <igor@sysoev.ru>
parents:
1870
diff
changeset
|
60 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
61 int port_create(void); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
62 |
719 | 63 int port_create(void) |
64 { | |
65 return -1; | |
66 } | |
67 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
68 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
69 int port_associate(int port, int source, uintptr_t object, int events, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
70 void *user); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
71 |
719 | 72 int port_associate(int port, int source, uintptr_t object, int events, |
73 void *user) | |
74 { | |
75 return -1; | |
76 } | |
77 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
78 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
79 int port_dissociate(int port, int source, uintptr_t object); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
80 |
719 | 81 int port_dissociate(int port, int source, uintptr_t object) |
82 { | |
83 return -1; | |
84 } | |
85 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
86 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
87 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
88 struct timespec *timeout); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
89 |
719 | 90 int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, |
91 struct timespec *timeout) | |
92 { | |
93 return -1; | |
94 } | |
95 | |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
96 int port_send(int port, int events, void *user); |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
97 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
98 int port_send(int port, int events, void *user) |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
99 { |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
100 return -1; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
101 } |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
102 |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
103 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
104 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
105 |
719 | 106 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid) |
107 { | |
108 return -1; | |
109 } | |
110 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
111 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
112 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
113 struct itimerspec *ovalue); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
114 |
719 | 115 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, |
116 struct itimerspec *ovalue) | |
117 { | |
118 return -1; | |
119 } | |
120 | |
4759
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
121 |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
122 int timer_delete(timer_t timerid); |
4c36e15651f7
Fixed compilation with -Wmissing-prototypes.
Ruslan Ermilov <ru@nginx.com>
parents:
4572
diff
changeset
|
123 |
719 | 124 int timer_delete(timer_t timerid) |
125 { | |
126 return -1; | |
127 } | |
128 | |
129 #endif | |
130 | |
131 | |
132 typedef struct { | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
133 ngx_uint_t events; |
719 | 134 } ngx_eventport_conf_t; |
135 | |
136 | |
137 static ngx_int_t ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer); | |
138 static void ngx_eventport_done(ngx_cycle_t *cycle); | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
139 static ngx_int_t ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
140 ngx_uint_t flags); |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
141 static ngx_int_t ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, |
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
142 ngx_uint_t flags); |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
143 static ngx_int_t ngx_eventport_notify(ngx_event_handler_pt handler); |
719 | 144 static ngx_int_t ngx_eventport_process_events(ngx_cycle_t *cycle, |
145 ngx_msec_t timer, ngx_uint_t flags); | |
146 | |
147 static void *ngx_eventport_create_conf(ngx_cycle_t *cycle); | |
148 static char *ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf); | |
149 | |
150 static int ep = -1; | |
151 static port_event_t *event_list; | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
152 static ngx_uint_t nevents; |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
153 static timer_t event_timer = (timer_t) -1; |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
154 static ngx_event_t notify_event; |
719 | 155 |
156 static ngx_str_t eventport_name = ngx_string("eventport"); | |
157 | |
158 | |
159 static ngx_command_t ngx_eventport_commands[] = { | |
160 | |
161 { ngx_string("eventport_events"), | |
162 NGX_EVENT_CONF|NGX_CONF_TAKE1, | |
163 ngx_conf_set_num_slot, | |
164 0, | |
165 offsetof(ngx_eventport_conf_t, events), | |
166 NULL }, | |
167 | |
168 ngx_null_command | |
169 }; | |
170 | |
171 | |
172 ngx_event_module_t ngx_eventport_module_ctx = { | |
173 &eventport_name, | |
174 ngx_eventport_create_conf, /* create configuration */ | |
175 ngx_eventport_init_conf, /* init configuration */ | |
176 | |
177 { | |
178 ngx_eventport_add_event, /* add an event */ | |
179 ngx_eventport_del_event, /* delete an event */ | |
180 ngx_eventport_add_event, /* enable an event */ | |
181 ngx_eventport_del_event, /* disable an event */ | |
182 NULL, /* add an connection */ | |
183 NULL, /* delete an connection */ | |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
184 ngx_eventport_notify, /* trigger a notify */ |
719 | 185 NULL, /* process the changes */ |
186 ngx_eventport_process_events, /* process the events */ | |
187 ngx_eventport_init, /* init the events */ | |
188 ngx_eventport_done, /* done the events */ | |
189 } | |
190 | |
191 }; | |
192 | |
193 ngx_module_t ngx_eventport_module = { | |
194 NGX_MODULE_V1, | |
195 &ngx_eventport_module_ctx, /* module context */ | |
196 ngx_eventport_commands, /* module directives */ | |
197 NGX_EVENT_MODULE, /* module type */ | |
198 NULL, /* init master */ | |
199 NULL, /* init module */ | |
200 NULL, /* init process */ | |
201 NULL, /* init thread */ | |
202 NULL, /* exit thread */ | |
203 NULL, /* exit process */ | |
204 NULL, /* exit master */ | |
205 NGX_MODULE_V1_PADDING | |
206 }; | |
207 | |
208 | |
209 static ngx_int_t | |
210 ngx_eventport_init(ngx_cycle_t *cycle, ngx_msec_t timer) | |
211 { | |
212 port_notify_t pn; | |
213 struct itimerspec its; | |
214 struct sigevent sev; | |
215 ngx_eventport_conf_t *epcf; | |
216 | |
217 epcf = ngx_event_get_conf(cycle->conf_ctx, ngx_eventport_module); | |
218 | |
219 if (ep == -1) { | |
220 ep = port_create(); | |
221 | |
222 if (ep == -1) { | |
223 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
224 "port_create() failed"); | |
225 return NGX_ERROR; | |
226 } | |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
227 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
228 notify_event.active = 1; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
229 notify_event.log = cycle->log; |
719 | 230 } |
231 | |
232 if (nevents < epcf->events) { | |
233 if (event_list) { | |
234 ngx_free(event_list); | |
235 } | |
236 | |
237 event_list = ngx_alloc(sizeof(port_event_t) * epcf->events, | |
238 cycle->log); | |
239 if (event_list == NULL) { | |
240 return NGX_ERROR; | |
241 } | |
242 } | |
243 | |
244 ngx_event_flags = NGX_USE_EVENTPORT_EVENT; | |
245 | |
246 if (timer) { | |
247 ngx_memzero(&pn, sizeof(port_notify_t)); | |
248 pn.portnfy_port = ep; | |
249 | |
250 ngx_memzero(&sev, sizeof(struct sigevent)); | |
251 sev.sigev_notify = SIGEV_PORT; | |
252 #if !(NGX_TEST_BUILD_EVENTPORT) | |
253 sev.sigev_value.sival_ptr = &pn; | |
254 #endif | |
255 | |
256 if (timer_create(CLOCK_REALTIME, &sev, &event_timer) == -1) { | |
257 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
258 "timer_create() failed"); | |
259 return NGX_ERROR; | |
260 } | |
261 | |
262 its.it_interval.tv_sec = timer / 1000; | |
263 its.it_interval.tv_nsec = (timer % 1000) * 1000000; | |
264 its.it_value.tv_sec = timer / 1000; | |
265 its.it_value.tv_nsec = (timer % 1000) * 1000000; | |
266 | |
267 if (timer_settime(event_timer, 0, &its, NULL) == -1) { | |
268 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | |
269 "timer_settime() failed"); | |
270 return NGX_ERROR; | |
271 } | |
272 | |
273 ngx_event_flags |= NGX_USE_TIMER_EVENT; | |
274 } | |
275 | |
276 nevents = epcf->events; | |
277 | |
278 ngx_io = ngx_os_io; | |
279 | |
280 ngx_event_actions = ngx_eventport_module_ctx.actions; | |
281 | |
282 return NGX_OK; | |
283 } | |
284 | |
285 | |
286 static void | |
287 ngx_eventport_done(ngx_cycle_t *cycle) | |
288 { | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
289 if (event_timer != (timer_t) -1) { |
719 | 290 if (timer_delete(event_timer) == -1) { |
291 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
292 "timer_delete() failed"); | |
293 } | |
294 | |
1870
a62fb6c156c5
fix building --test-build-rtsig and --test-build-eventport on FreeBSD 7
Igor Sysoev <igor@sysoev.ru>
parents:
1354
diff
changeset
|
295 event_timer = (timer_t) -1; |
719 | 296 } |
297 | |
298 if (close(ep) == -1) { | |
299 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | |
300 "close() event port failed"); | |
301 } | |
302 | |
303 ep = -1; | |
304 | |
305 ngx_free(event_list); | |
306 | |
307 event_list = NULL; | |
308 nevents = 0; | |
309 } | |
310 | |
311 | |
312 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
313 ngx_eventport_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 314 { |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
315 ngx_int_t events, prev; |
719 | 316 ngx_event_t *e; |
317 ngx_connection_t *c; | |
318 | |
319 c = ev->data; | |
320 | |
321 events = event; | |
322 | |
323 if (event == NGX_READ_EVENT) { | |
324 e = c->write; | |
325 prev = POLLOUT; | |
326 #if (NGX_READ_EVENT != POLLIN) | |
327 events = POLLIN; | |
328 #endif | |
329 | |
330 } else { | |
331 e = c->read; | |
332 prev = POLLIN; | |
333 #if (NGX_WRITE_EVENT != POLLOUT) | |
334 events = POLLOUT; | |
335 #endif | |
336 } | |
337 | |
338 if (e->oneshot) { | |
339 events |= prev; | |
340 } | |
341 | |
342 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
343 "eventport add event: fd:%d ev:%04Xi", c->fd, events); |
719 | 344 |
345 if (port_associate(ep, PORT_SOURCE_FD, c->fd, events, | |
346 (void *) ((uintptr_t) ev | ev->instance)) | |
347 == -1) | |
348 { | |
349 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
350 "port_associate() failed"); | |
351 return NGX_ERROR; | |
352 } | |
353 | |
354 ev->active = 1; | |
355 ev->oneshot = 1; | |
356 | |
357 return NGX_OK; | |
358 } | |
359 | |
360 | |
361 static ngx_int_t | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
362 ngx_eventport_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags) |
719 | 363 { |
364 ngx_event_t *e; | |
365 ngx_connection_t *c; | |
366 | |
367 /* | |
368 * when the file descriptor is closed, the event port automatically | |
4572
67653855682e
Fixed spelling in multiline C comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
369 * dissociates it from the port, so we do not need to dissociate explicitly |
719 | 370 * the event before the closing the file descriptor |
371 */ | |
372 | |
373 if (flags & NGX_CLOSE_EVENT) { | |
374 ev->active = 0; | |
375 ev->oneshot = 0; | |
376 return NGX_OK; | |
377 } | |
378 | |
379 c = ev->data; | |
380 | |
381 if (event == NGX_READ_EVENT) { | |
382 e = c->write; | |
383 event = POLLOUT; | |
384 | |
385 } else { | |
386 e = c->read; | |
387 event = POLLIN; | |
388 } | |
389 | |
390 if (e->oneshot) { | |
391 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
392 "eventport change event: fd:%d ev:%04Xi", c->fd, event); |
719 | 393 |
394 if (port_associate(ep, PORT_SOURCE_FD, c->fd, event, | |
395 (void *) ((uintptr_t) ev | ev->instance)) | |
396 == -1) | |
397 { | |
398 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
399 "port_associate() failed"); | |
400 return NGX_ERROR; | |
401 } | |
402 | |
403 } else { | |
404 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0, | |
405 "eventport del event: fd:%d", c->fd); | |
406 | |
407 if (port_dissociate(ep, PORT_SOURCE_FD, c->fd) == -1) { | |
408 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
409 "port_dissociate() failed"); | |
410 return NGX_ERROR; | |
411 } | |
412 } | |
413 | |
414 ev->active = 0; | |
415 ev->oneshot = 0; | |
416 | |
417 return NGX_OK; | |
418 } | |
419 | |
420 | |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
421 static ngx_int_t |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
422 ngx_eventport_notify(ngx_event_handler_pt handler) |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
423 { |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
424 notify_event.handler = handler; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
425 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
426 if (port_send(ep, 0, ¬ify_event) != 0) { |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
427 ngx_log_error(NGX_LOG_ALERT, notify_event.log, ngx_errno, |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
428 "port_send() failed"); |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
429 return NGX_ERROR; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
430 } |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
431 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
432 return NGX_OK; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
433 } |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
434 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
435 |
719 | 436 ngx_int_t |
437 ngx_eventport_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, | |
438 ngx_uint_t flags) | |
439 { | |
440 int n, revents; | |
441 u_int events; | |
442 ngx_err_t err; | |
443 ngx_int_t instance; | |
444 ngx_uint_t i, level; | |
5821
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
445 ngx_event_t *ev, *rev, *wev; |
3f5f0ab59b35
Events: processing of posted events changed from LIFO to FIFO.
Valentin Bartenev <vbart@nginx.com>
parents:
5820
diff
changeset
|
446 ngx_queue_t *queue; |
719 | 447 ngx_connection_t *c; |
448 struct timespec ts, *tp; | |
449 | |
450 if (timer == NGX_TIMER_INFINITE) { | |
451 tp = NULL; | |
452 | |
453 } else { | |
454 ts.tv_sec = timer / 1000; | |
455 ts.tv_nsec = (timer % 1000) * 1000000; | |
456 tp = &ts; | |
457 } | |
458 | |
459 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
460 "eventport timer: %M", timer); | |
461 | |
462 events = 1; | |
463 | |
1354
f69d1aab6a0f
make 64-bit ngx_int_t on 64-bit platforms
Igor Sysoev <igor@sysoev.ru>
parents:
1287
diff
changeset
|
464 n = port_getn(ep, event_list, (u_int) nevents, &events, tp); |
719 | 465 |
466 err = ngx_errno; | |
467 | |
468 if (flags & NGX_UPDATE_TIME) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
469 ngx_time_update(); |
719 | 470 } |
471 | |
472 if (n == -1) { | |
473 if (err == ETIME) { | |
474 if (timer != NGX_TIMER_INFINITE) { | |
475 return NGX_OK; | |
476 } | |
477 | |
478 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
479 "port_getn() returned no events without timeout"); | |
480 return NGX_ERROR; | |
481 } | |
482 | |
483 level = (err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT; | |
484 ngx_log_error(level, cycle->log, err, "port_getn() failed"); | |
485 return NGX_ERROR; | |
486 } | |
487 | |
488 if (events == 0) { | |
489 if (timer != NGX_TIMER_INFINITE) { | |
490 return NGX_OK; | |
491 } | |
492 | |
493 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
494 "port_getn() returned no events without timeout"); | |
495 return NGX_ERROR; | |
496 } | |
497 | |
498 for (i = 0; i < events; i++) { | |
499 | |
500 if (event_list[i].portev_source == PORT_SOURCE_TIMER) { | |
3475
ab353d7dc182
*) introduce ngx_time_sigsafe_update() to update the error log time only
Igor Sysoev <igor@sysoev.ru>
parents:
3474
diff
changeset
|
501 ngx_time_update(); |
719 | 502 continue; |
503 } | |
504 | |
505 ev = event_list[i].portev_user; | |
506 | |
507 switch (event_list[i].portev_source) { | |
508 | |
509 case PORT_SOURCE_FD: | |
510 | |
511 instance = (uintptr_t) ev & 1; | |
512 ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1); | |
513 | |
514 if (ev->closed || ev->instance != instance) { | |
515 | |
516 /* | |
517 * the stale event from a file descriptor | |
518 * that was just closed in this iteration | |
519 */ | |
520 | |
521 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
522 "eventport: stale event %p", ev); | |
523 continue; | |
524 } | |
525 | |
526 revents = event_list[i].portev_events; | |
527 | |
528 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, | |
529 "eventport: fd:%d, ev:%04Xd", | |
530 event_list[i].portev_object, revents); | |
531 | |
532 if (revents & (POLLERR|POLLHUP|POLLNVAL)) { | |
1128
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
533 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
534 "port_getn() error fd:%d ev:%04Xd", |
06479b01caff
decrease log level from alert to debug for POLLERR|POLLHUP|POLLNVAL
Igor Sysoev <igor@sysoev.ru>
parents:
929
diff
changeset
|
535 event_list[i].portev_object, revents); |
719 | 536 } |
537 | |
538 if (revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL)) { | |
539 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
540 "strange port_getn() events fd:%d ev:%04Xd", | |
541 event_list[i].portev_object, revents); | |
542 } | |
543 | |
544 if ((revents & (POLLERR|POLLHUP|POLLNVAL)) | |
545 && (revents & (POLLIN|POLLOUT)) == 0) | |
546 { | |
547 /* | |
548 * if the error events were returned without POLLIN or POLLOUT, | |
549 * then add these flags to handle the events at least in one | |
550 * active handler | |
551 */ | |
552 | |
553 revents |= POLLIN|POLLOUT; | |
554 } | |
555 | |
556 c = ev->data; | |
557 rev = c->read; | |
558 wev = c->write; | |
559 | |
560 rev->active = 0; | |
561 wev->active = 0; | |
562 | |
563 if (revents & POLLIN) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
564 rev->ready = 1; |
719 | 565 |
566 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
567 queue = rev->accept ? &ngx_posted_accept_events |
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
568 : &ngx_posted_events; |
719 | 569 |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
570 ngx_post_event(rev, queue); |
719 | 571 |
572 } else { | |
573 rev->handler(rev); | |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
574 |
5170
6362bd26e4b0
Events: protection from stale events in eventport and devpoll.
Valentin Bartenev <vbart@nginx.com>
parents:
4759
diff
changeset
|
575 if (ev->closed || ev->instance != instance) { |
1287
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
576 continue; |
3dd9883fa121
fix segfault when event port returns POLLERR without POLLIN or POLLOUT
Igor Sysoev <igor@sysoev.ru>
parents:
1128
diff
changeset
|
577 } |
719 | 578 } |
579 | |
580 if (rev->accept) { | |
581 if (ngx_use_accept_mutex) { | |
582 ngx_accept_events = 1; | |
583 continue; | |
584 } | |
585 | |
586 if (port_associate(ep, PORT_SOURCE_FD, c->fd, POLLIN, | |
587 (void *) ((uintptr_t) ev | ev->instance)) | |
588 == -1) | |
589 { | |
590 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, | |
591 "port_associate() failed"); | |
592 return NGX_ERROR; | |
593 } | |
594 } | |
595 } | |
596 | |
597 if (revents & POLLOUT) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
598 wev->ready = 1; |
719 | 599 |
600 if (flags & NGX_POST_EVENTS) { | |
5820
3377f9459e99
Events: removed broken thread support from posted events.
Valentin Bartenev <vbart@nginx.com>
parents:
5175
diff
changeset
|
601 ngx_post_event(wev, &ngx_posted_events); |
719 | 602 |
603 } else { | |
604 wev->handler(wev); | |
605 } | |
606 } | |
607 | |
608 continue; | |
609 | |
6021
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
610 case PORT_SOURCE_USER: |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
611 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
612 ev->handler(ev); |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
613 |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
614 continue; |
117c77b22db1
Events: implemented eventport notification mechanism.
Ruslan Ermilov <ru@nginx.com>
parents:
6018
diff
changeset
|
615 |
719 | 616 default: |
617 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, | |
6005
d84f0abd4a53
Events: fixed typo in the error message.
Ruslan Ermilov <ru@nginx.com>
parents:
5821
diff
changeset
|
618 "unexpected eventport object %d", |
719 | 619 event_list[i].portev_object); |
620 continue; | |
621 } | |
622 } | |
623 | |
624 return NGX_OK; | |
625 } | |
626 | |
627 | |
628 static void * | |
629 ngx_eventport_create_conf(ngx_cycle_t *cycle) | |
630 { | |
631 ngx_eventport_conf_t *epcf; | |
632 | |
633 epcf = ngx_palloc(cycle->pool, sizeof(ngx_eventport_conf_t)); | |
634 if (epcf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
1939
diff
changeset
|
635 return NULL; |
719 | 636 } |
637 | |
638 epcf->events = NGX_CONF_UNSET; | |
639 | |
640 return epcf; | |
641 } | |
642 | |
643 | |
644 static char * | |
645 ngx_eventport_init_conf(ngx_cycle_t *cycle, void *conf) | |
646 { | |
647 ngx_eventport_conf_t *epcf = conf; | |
648 | |
649 ngx_conf_init_uint_value(epcf->events, 32); | |
650 | |
651 return NGX_CONF_OK; | |
652 } |