1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #ifndef _NGX_EVENT_H_INCLUDED_
8 #define _NGX_EVENT_H_INCLUDED_
9
10
11 #include <ngx_config.h>
12 #include <ngx_core.h>
13
14
15 #define NGX_INVALID_INDEX 0xd0d0d0d0
16
17
18 #if (NGX_HAVE_IOCP)
19
20 typedef struct {
21 WSAOVERLAPPED ovlp;
22 ngx_event_t *event;
23 int error;
24 } ngx_event_ovlp_t;
25
26 #endif
27
28
29 typedef struct {
30 ngx_uint_t lock;
31
32 ngx_event_t *events;
33 ngx_event_t *last;
34 } ngx_event_mutex_t;
35
36
37 struct ngx_event_s {
38 void *data;
39
40 unsigned write:1;
41
42 unsigned accept:1;
43
44 /* used to detect the stale events in kqueue, rtsig, and epoll */
45 unsigned instance:1;
46
47 /*
48 * the event was passed or would be passed to a kernel;
49 * in aio mode - operation was posted.
50 */
51 unsigned active:1;
52
53 unsigned disabled:1;
54
55 /* the ready event; in aio mode 0 means that no operation can be posted */
56 unsigned ready:1;
57
58 unsigned oneshot:1;
59
60 /* aio operation is complete */
61 unsigned complete:1;
62
63 unsigned eof:1;
64 unsigned error:1;
65
66 unsigned timedout:1;
67 unsigned timer_set:1;
68
69 unsigned delayed:1;
70
71 unsigned read_discarded:1;
72
73 unsigned unexpected_eof:1;
74
75 unsigned deferred_accept:1;
76
77 /* the pending eof reported by kqueue or in aio chain operation */
78 unsigned pending_eof:1;
79
80 #if !(NGX_THREADS)
81 unsigned posted_ready:1;
82 #endif
83
84 #if (NGX_WIN32)
85 /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */
86 unsigned accept_context_updated:1;
87 #endif
88
89 #if (NGX_HAVE_KQUEUE)
90 unsigned kq_vnode:1;
91
92 /* the pending errno reported by kqueue */
93 int kq_errno;
94 #endif
95
96 /*
97 * kqueue only:
98 * accept: number of sockets that wait to be accepted
99 * read: bytes to read when event is ready
100 * or lowat when event is set with NGX_LOWAT_EVENT flag
101 * write: available space in buffer when event is ready
102 * or lowat when event is set with NGX_LOWAT_EVENT flag
103 *
104 * iocp: TODO
105 *
106 * otherwise:
107 * accept: 1 if accept many, 0 otherwise
108 */
109
110 #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
111 int available;
112 #else
113 unsigned available:1;
114 #endif
115
116 ngx_event_handler_pt handler;
117
118
119 #if (NGX_HAVE_AIO)
120
121 #if (NGX_HAVE_IOCP)
122 ngx_event_ovlp_t ovlp;
123 #else
124 struct aiocb aiocb;
125 #endif
126
127 #endif
128
129 ngx_uint_t index;
130
131 ngx_log_t *log;
132
133 ngx_rbtree_node_t timer;
134
135 unsigned closed:1;
136
137 /* to test on worker exit */
138 unsigned channel:1;
139
140 #if (NGX_THREADS)
141
142 unsigned locked:1;
143
144 unsigned posted_ready:1;
145 unsigned posted_timedout:1;
146 unsigned posted_eof:1;
147
148 #if (NGX_HAVE_KQUEUE)
149 /* the pending errno reported by kqueue */
150 int posted_errno;
151 #endif
152
153 #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
154 int posted_available;
155 #else
156 unsigned posted_available:1;
157 #endif
158
159 ngx_atomic_t *lock;
160 ngx_atomic_t *own_lock;
161
162 #endif
163
164 /* the links of the posted queue */
165 ngx_event_t *next;
166 ngx_event_t **prev;
167
168
169 #if 0
170
171 /* the threads support */
172
173 /*
174 * the event thread context, we store it here
175 * if $(CC) does not understand __thread declaration
176 * and pthread_getspecific() is too costly
177 */
178
179 void *thr_ctx;
180
181 #if (NGX_EVENT_T_PADDING)
182
183 /* event should not cross cache line in SMP */
184
185 uint32_t padding[NGX_EVENT_T_PADDING];
186 #endif
187 #endif
188 };
189
190
191 typedef struct {
192 in_addr_t mask;
193 in_addr_t addr;
194 } ngx_event_debug_t;
195
196
197 typedef struct {
198 ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
199 ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
200
201 ngx_int_t (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
202 ngx_int_t (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
203
204 ngx_int_t (*add_conn)(ngx_connection_t *c);
205 ngx_int_t (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
206
207 ngx_int_t (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
208 ngx_int_t (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
209 ngx_uint_t flags);
210
211 ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
212 void (*done)(ngx_cycle_t *cycle);
213 } ngx_event_actions_t;
214
215
216 extern ngx_event_actions_t ngx_event_actions;
217
218
219 /*
220 * The event filter requires to read/write the whole data:
221 * select, poll, /dev/poll, kqueue, epoll.
222 */
223 #define NGX_USE_LEVEL_EVENT 0x00000001
224
225 /*
226 * The event filter is deleted after a notification without an additional
227 * syscall: kqueue, epoll.
228 */
229 #define NGX_USE_ONESHOT_EVENT 0x00000002
230
231 /*
232 * The event filter notifies only the changes and an initial level:
233 * kqueue, epoll.
234 */
235 #define NGX_USE_CLEAR_EVENT 0x00000004
236
237 /*
238 * The event filter has kqueue features: the eof flag, errno,
239 * available data, etc.
240 */
241 #define NGX_USE_KQUEUE_EVENT 0x00000008
242
243 /*
244 * The event filter supports low water mark: kqueue's NOTE_LOWAT.
245 * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
246 */
247 #define NGX_USE_LOWAT_EVENT 0x00000010
248
249 /*
250 * The event filter requires to do i/o operation until EAGAIN: epoll, rtsig.
251 */
252 #define NGX_USE_GREEDY_EVENT 0x00000020
253
254 /*
255 * The event filter is epoll.
256 */
257 #define NGX_USE_EPOLL_EVENT 0x00000040
258
259 /*
260 * No need to add or delete the event filters: rtsig.
261 */
262 #define NGX_USE_RTSIG_EVENT 0x00000080
263
264 /*
265 * No need to add or delete the event filters: overlapped, aio_read,
266 * aioread, io_submit.
267 */
268 #define NGX_USE_AIO_EVENT 0x00000100
269
270 /*
271 * Need to add socket or handle only once: i/o completion port.
272 * It also requires NGX_HAVE_AIO and NGX_USE_AIO_EVENT to be set.
273 */
274 #define NGX_USE_IOCP_EVENT 0x00000200
275
276 /*
277 * The event filter has no opaque data and requires file descriptors table:
278 * poll, /dev/poll, rtsig.
279 */
280 #define NGX_USE_FD_EVENT 0x00000400
281
282 /*
283 * The event module handles periodic or absolute timer event by itself:
284 * kqueue in FreeBSD 4.4, NetBSD 2.0, and MacOSX 10.4, Solaris 10's event ports.
285 */
286 #define NGX_USE_TIMER_EVENT 0x00000800
287
288 /*
289 * All event filters on file descriptor are deleted after a notification:
290 * Solaris 10's event ports.
291 */
292 #define NGX_USE_EVENTPORT_EVENT 0x00001000
293
294 /*
295 * The event filter support vnode notifications: kqueue.
296 */
297 #define NGX_USE_VNODE_EVENT 0x00002000
298
299
300 /*
301 * The event filter is deleted just before the closing file.
302 * Has no meaning for select and poll.
303 * kqueue, epoll, rtsig, eventport: allows to avoid explicit delete,
304 * because filter automatically is deleted
305 * on file close,
306 *
307 * /dev/poll: we need to flush POLLREMOVE event
308 * before closing file.
309 */
310 #define NGX_CLOSE_EVENT 1
311
312 /*
313 * disable temporarily event filter, this may avoid locks
314 * in kernel malloc()/free(): kqueue.
315 */
316 #define NGX_DISABLE_EVENT 2
317
318 /*
319 * event must be passed to kernel right now, do not wait until batch processing.
320 */
321 #define NGX_FLUSH_EVENT 4
322
323
324 /* these flags have a meaning only for kqueue */
325 #define NGX_LOWAT_EVENT 0
326 #define NGX_VNODE_EVENT 0
327
328
329 #if (NGX_HAVE_KQUEUE)
330
331 #define NGX_READ_EVENT EVFILT_READ
332 #define NGX_WRITE_EVENT EVFILT_WRITE
333
334 #undef NGX_VNODE_EVENT
335 #define NGX_VNODE_EVENT EVFILT_VNODE
336
337 /*
338 * NGX_CLOSE_EVENT, NGX_LOWAT_EVENT, and NGX_FLUSH_EVENT are the module flags
339 * and they must not go into a kernel so we need to choose the value
340 * that must not interfere with any existent and future kqueue flags.
341 * kqueue has such values - EV_FLAG1, EV_EOF, and EV_ERROR:
342 * they are reserved and cleared on a kernel entrance.
343 */
344 #undef NGX_CLOSE_EVENT
345 #define NGX_CLOSE_EVENT EV_EOF
346
347 #undef NGX_LOWAT_EVENT
348 #define NGX_LOWAT_EVENT EV_FLAG1
349
350 #undef NGX_FLUSH_EVENT
351 #define NGX_FLUSH_EVENT EV_ERROR
352
353 #define NGX_LEVEL_EVENT 0
354 #define NGX_ONESHOT_EVENT EV_ONESHOT
355 #define NGX_CLEAR_EVENT EV_CLEAR
356
357 #undef NGX_DISABLE_EVENT
358 #define NGX_DISABLE_EVENT EV_DISABLE
359
360
361 #elif (NGX_HAVE_DEVPOLL || NGX_HAVE_EVENTPORT)
362
363 #define NGX_READ_EVENT POLLIN
364 #define NGX_WRITE_EVENT POLLOUT
365
366 #define NGX_LEVEL_EVENT 0
367 #define NGX_ONESHOT_EVENT 1
368
369
370 #elif (NGX_HAVE_EPOLL)
371
372 #define NGX_READ_EVENT EPOLLIN
373 #define NGX_WRITE_EVENT EPOLLOUT
374
375 #define NGX_LEVEL_EVENT 0
376 #define NGX_CLEAR_EVENT EPOLLET
377 #define NGX_ONESHOT_EVENT 0x70000000
378 #if 0
379 #define NGX_ONESHOT_EVENT EPOLLONESHOT
380 #endif
381
382
383 #elif (NGX_HAVE_POLL)
384
385 #define NGX_READ_EVENT POLLIN
386 #define NGX_WRITE_EVENT POLLOUT
387
388 #define NGX_LEVEL_EVENT 0
389 #define NGX_ONESHOT_EVENT 1
390
391
392 #else /* select */
393
394 #define NGX_READ_EVENT 0
395 #define NGX_WRITE_EVENT 1
396
397 #define NGX_LEVEL_EVENT 0
398 #define NGX_ONESHOT_EVENT 1
399
400 #endif /* NGX_HAVE_KQUEUE */
401
402
403 #if (NGX_HAVE_IOCP)
404 #define NGX_IOCP_ACCEPT 0
405 #define NGX_IOCP_IO 1
406 #define NGX_IOCP_CONNECT 2
407 #endif
408
409
410 #ifndef NGX_CLEAR_EVENT
411 #define NGX_CLEAR_EVENT 0 /* dummy declaration */
412 #endif
413
414
415 #define ngx_process_changes ngx_event_actions.process_changes
416 #define ngx_process_events ngx_event_actions.process_events
417 #define ngx_done_events ngx_event_actions.done
418
419 #define ngx_add_event ngx_event_actions.add
420 #define ngx_del_event ngx_event_actions.del
421 #define ngx_add_conn ngx_event_actions.add_conn
422 #define ngx_del_conn ngx_event_actions.del_conn
423
424 #define ngx_add_timer ngx_event_add_timer
425 #define ngx_del_timer ngx_event_del_timer
426
427
428 extern ngx_os_io_t ngx_io;
429
430 #define ngx_recv ngx_io.recv
431 #define ngx_recv_chain ngx_io.recv_chain
432 #define ngx_udp_recv ngx_io.udp_recv
433 #define ngx_send ngx_io.send
434 #define ngx_send_chain ngx_io.send_chain
435
436
437 #define NGX_EVENT_MODULE 0x544E5645 /* "EVNT" */
438 #define NGX_EVENT_CONF 0x02000000
439
440
441 typedef struct {
442 ngx_uint_t connections;
443 ngx_uint_t use;
444
445 ngx_flag_t multi_accept;
446 ngx_flag_t accept_mutex;
447
448 ngx_msec_t accept_mutex_delay;
449
450 u_char *name;
451
452 #if (NGX_DEBUG)
453 ngx_array_t debug_connection;
454 #endif
455 } ngx_event_conf_t;
456
457
458 typedef struct {
459 ngx_str_t *name;
460
461 void *(*create_conf)(ngx_cycle_t *cycle);
462 char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
463
464 ngx_event_actions_t actions;
465 } ngx_event_module_t;
466
467
468 extern ngx_atomic_t *ngx_connection_counter;
469
470 extern ngx_atomic_t *ngx_accept_mutex_ptr;
471 extern ngx_shmtx_t ngx_accept_mutex;
472 extern ngx_uint_t ngx_use_accept_mutex;
473 extern ngx_uint_t ngx_accept_events;
474 extern ngx_uint_t ngx_accept_mutex_held;
475 extern ngx_msec_t ngx_accept_mutex_delay;
476 extern ngx_int_t ngx_accept_disabled;
477
478
479 #if (NGX_STAT_STUB)
480
481 extern ngx_atomic_t *ngx_stat_accepted;
482 extern ngx_atomic_t *ngx_stat_handled;
483 extern ngx_atomic_t *ngx_stat_requests;
484 extern ngx_atomic_t *ngx_stat_active;
485 extern ngx_atomic_t *ngx_stat_reading;
486 extern ngx_atomic_t *ngx_stat_writing;
487
488 #endif
489
490
491 #define NGX_UPDATE_TIME 1
492 #define NGX_POST_EVENTS 2
493 #define NGX_POST_THREAD_EVENTS 4
494
495
496 extern sig_atomic_t ngx_event_timer_alarm;
497 extern ngx_uint_t ngx_event_flags;
498 extern ngx_module_t ngx_events_module;
499 extern ngx_module_t ngx_event_core_module;
500
501
502 #define ngx_event_get_conf(conf_ctx, module) \
503 (*(ngx_get_conf(conf_ctx, ngx_events_module))) [module.ctx_index];
504
505
506
507 void ngx_event_accept(ngx_event_t *ev);
508 ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle);
509 u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);
510
511
512 void ngx_process_events_and_timers(ngx_cycle_t *cycle);
513 ngx_int_t ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags);
514 ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);
515
516
517 #if (NGX_WIN32)
518 void ngx_event_acceptex(ngx_event_t *ev);
519 ngx_int_t ngx_event_post_acceptex(ngx_listening_t *ls, ngx_uint_t n);
520 u_char *ngx_acceptex_log_error(ngx_log_t *log, u_char *buf, size_t len);
521 #endif
522
523
524 ngx_int_t ngx_send_lowat(ngx_connection_t *c, size_t lowat);
525
526
527 /* used in ngx_log_debugX() */
528 #define ngx_event_ident(p) ((ngx_connection_t *) (p))->fd
529
530
531 #include <ngx_event_timer.h>
532 #include <ngx_event_posted.h>
533 #include <ngx_event_busy_lock.h>
534
535 #if (NGX_WIN32)
536 #include <ngx_iocp_module.h>
537 #endif
538
539
540 #endif /* _NGX_EVENT_H_INCLUDED_ */
541
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.