Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #ifndef _NGX_EVENT_H_INCLUDED_
0009 #define _NGX_EVENT_H_INCLUDED_
0010 
0011 
0012 #include <ngx_config.h>
0013 #include <ngx_core.h>
0014 
0015 
0016 #define NGX_INVALID_INDEX  0xd0d0d0d0
0017 
0018 
0019 #if (NGX_HAVE_IOCP)
0020 
0021 typedef struct {
0022     WSAOVERLAPPED    ovlp;
0023     ngx_event_t     *event;
0024     int              error;
0025 } ngx_event_ovlp_t;
0026 
0027 #endif
0028 
0029 
0030 struct ngx_event_s {
0031     void            *data;
0032 
0033     unsigned         write:1;
0034 
0035     unsigned         accept:1;
0036 
0037     /* used to detect the stale events in kqueue and epoll */
0038     unsigned         instance:1;
0039 
0040     /*
0041      * the event was passed or would be passed to a kernel;
0042      * in aio mode - operation was posted.
0043      */
0044     unsigned         active:1;
0045 
0046     unsigned         disabled:1;
0047 
0048     /* the ready event; in aio mode 0 means that no operation can be posted */
0049     unsigned         ready:1;
0050 
0051     unsigned         oneshot:1;
0052 
0053     /* aio operation is complete */
0054     unsigned         complete:1;
0055 
0056     unsigned         eof:1;
0057     unsigned         error:1;
0058 
0059     unsigned         timedout:1;
0060     unsigned         timer_set:1;
0061 
0062     unsigned         delayed:1;
0063 
0064     unsigned         deferred_accept:1;
0065 
0066     /* the pending eof reported by kqueue, epoll or in aio chain operation */
0067     unsigned         pending_eof:1;
0068 
0069     unsigned         posted:1;
0070 
0071     unsigned         closed:1;
0072 
0073     /* to test on worker exit */
0074     unsigned         channel:1;
0075     unsigned         resolver:1;
0076 
0077     unsigned         cancelable:1;
0078 
0079 #if (NGX_HAVE_KQUEUE)
0080     unsigned         kq_vnode:1;
0081 
0082     /* the pending errno reported by kqueue */
0083     int              kq_errno;
0084 #endif
0085 
0086     /*
0087      * kqueue only:
0088      *   accept:     number of sockets that wait to be accepted
0089      *   read:       bytes to read when event is ready
0090      *               or lowat when event is set with NGX_LOWAT_EVENT flag
0091      *   write:      available space in buffer when event is ready
0092      *               or lowat when event is set with NGX_LOWAT_EVENT flag
0093      *
0094      * epoll with EPOLLRDHUP:
0095      *   accept:     1 if accept many, 0 otherwise
0096      *   read:       1 if there can be data to read, 0 otherwise
0097      *
0098      * iocp: TODO
0099      *
0100      * otherwise:
0101      *   accept:     1 if accept many, 0 otherwise
0102      */
0103 
0104 #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
0105     int              available;
0106 #else
0107     unsigned         available:1;
0108 #endif
0109 
0110     ngx_event_handler_pt  handler;
0111 
0112 
0113 #if (NGX_HAVE_IOCP)
0114     ngx_event_ovlp_t ovlp;
0115 #endif
0116 
0117     ngx_uint_t       index;
0118 
0119     ngx_log_t       *log;
0120 
0121     ngx_rbtree_node_t   timer;
0122 
0123     /* the posted queue */
0124     ngx_queue_t      queue;
0125 
0126 #if 0
0127 
0128     /* the threads support */
0129 
0130     /*
0131      * the event thread context, we store it here
0132      * if $(CC) does not understand __thread declaration
0133      * and pthread_getspecific() is too costly
0134      */
0135 
0136     void            *thr_ctx;
0137 
0138 #if (NGX_EVENT_T_PADDING)
0139 
0140     /* event should not cross cache line in SMP */
0141 
0142     uint32_t         padding[NGX_EVENT_T_PADDING];
0143 #endif
0144 #endif
0145 };
0146 
0147 
0148 #if (NGX_HAVE_FILE_AIO)
0149 
0150 struct ngx_event_aio_s {
0151     void                      *data;
0152     ngx_event_handler_pt       handler;
0153     ngx_file_t                *file;
0154 
0155     ngx_fd_t                   fd;
0156 
0157 #if (NGX_HAVE_AIO_SENDFILE || NGX_COMPAT)
0158     ssize_t                  (*preload_handler)(ngx_buf_t *file);
0159 #endif
0160 
0161 #if (NGX_HAVE_EVENTFD)
0162     int64_t                    res;
0163 #endif
0164 
0165 #if !(NGX_HAVE_EVENTFD) || (NGX_TEST_BUILD_EPOLL)
0166     ngx_err_t                  err;
0167     size_t                     nbytes;
0168 #endif
0169 
0170     ngx_aiocb_t                aiocb;
0171     ngx_event_t                event;
0172 };
0173 
0174 #endif
0175 
0176 
0177 typedef struct {
0178     ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
0179     ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
0180 
0181     ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
0182     ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
0183 
0184     ngx_int_t  (*add_conn)(ngx_connection_t *c);
0185     ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
0186 
0187     ngx_int_t  (*notify)(ngx_event_handler_pt handler);
0188 
0189     ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
0190                                  ngx_uint_t flags);
0191 
0192     ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
0193     void       (*done)(ngx_cycle_t *cycle);
0194 } ngx_event_actions_t;
0195 
0196 
0197 extern ngx_event_actions_t   ngx_event_actions;
0198 #if (NGX_HAVE_EPOLLRDHUP)
0199 extern ngx_uint_t            ngx_use_epoll_rdhup;
0200 #endif
0201 
0202 
0203 /*
0204  * The event filter requires to read/write the whole data:
0205  * select, poll, /dev/poll, kqueue, epoll.
0206  */
0207 #define NGX_USE_LEVEL_EVENT      0x00000001
0208 
0209 /*
0210  * The event filter is deleted after a notification without an additional
0211  * syscall: kqueue, epoll.
0212  */
0213 #define NGX_USE_ONESHOT_EVENT    0x00000002
0214 
0215 /*
0216  * The event filter notifies only the changes and an initial level:
0217  * kqueue, epoll.
0218  */
0219 #define NGX_USE_CLEAR_EVENT      0x00000004
0220 
0221 /*
0222  * The event filter has kqueue features: the eof flag, errno,
0223  * available data, etc.
0224  */
0225 #define NGX_USE_KQUEUE_EVENT     0x00000008
0226 
0227 /*
0228  * The event filter supports low water mark: kqueue's NOTE_LOWAT.
0229  * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
0230  */
0231 #define NGX_USE_LOWAT_EVENT      0x00000010
0232 
0233 /*
0234  * The event filter requires to do i/o operation until EAGAIN: epoll.
0235  */
0236 #define NGX_USE_GREEDY_EVENT     0x00000020
0237 
0238 /*
0239  * The event filter is epoll.
0240  */
0241 #define NGX_USE_EPOLL_EVENT      0x00000040
0242 
0243 /*
0244  * Obsolete.
0245  */
0246 #define NGX_USE_RTSIG_EVENT      0x00000080
0247 
0248 /*
0249  * Obsolete.
0250  */
0251 #define NGX_USE_AIO_EVENT        0x00000100
0252 
0253 /*
0254  * Need to add socket or handle only once: i/o completion port.
0255  */
0256 #define NGX_USE_IOCP_EVENT       0x00000200
0257 
0258 /*
0259  * The event filter has no opaque data and requires file descriptors table:
0260  * poll, /dev/poll.
0261  */
0262 #define NGX_USE_FD_EVENT         0x00000400
0263 
0264 /*
0265  * The event module handles periodic or absolute timer event by itself:
0266  * kqueue in FreeBSD 4.4, NetBSD 2.0, and MacOSX 10.4, Solaris 10's event ports.
0267  */
0268 #define NGX_USE_TIMER_EVENT      0x00000800
0269 
0270 /*
0271  * All event filters on file descriptor are deleted after a notification:
0272  * Solaris 10's event ports.
0273  */
0274 #define NGX_USE_EVENTPORT_EVENT  0x00001000
0275 
0276 /*
0277  * The event filter support vnode notifications: kqueue.
0278  */
0279 #define NGX_USE_VNODE_EVENT      0x00002000
0280 
0281 
0282 /*
0283  * The event filter is deleted just before the closing file.
0284  * Has no meaning for select and poll.
0285  * kqueue, epoll, eventport:         allows to avoid explicit delete,
0286  *                                   because filter automatically is deleted
0287  *                                   on file close,
0288  *
0289  * /dev/poll:                        we need to flush POLLREMOVE event
0290  *                                   before closing file.
0291  */
0292 #define NGX_CLOSE_EVENT    1
0293 
0294 /*
0295  * disable temporarily event filter, this may avoid locks
0296  * in kernel malloc()/free(): kqueue.
0297  */
0298 #define NGX_DISABLE_EVENT  2
0299 
0300 /*
0301  * event must be passed to kernel right now, do not wait until batch processing.
0302  */
0303 #define NGX_FLUSH_EVENT    4
0304 
0305 
0306 /* these flags have a meaning only for kqueue */
0307 #define NGX_LOWAT_EVENT    0
0308 #define NGX_VNODE_EVENT    0
0309 
0310 
0311 #if (NGX_HAVE_EPOLL) && !(NGX_HAVE_EPOLLRDHUP)
0312 #define EPOLLRDHUP         0
0313 #endif
0314 
0315 
0316 #if (NGX_HAVE_KQUEUE)
0317 
0318 #define NGX_READ_EVENT     EVFILT_READ
0319 #define NGX_WRITE_EVENT    EVFILT_WRITE
0320 
0321 #undef  NGX_VNODE_EVENT
0322 #define NGX_VNODE_EVENT    EVFILT_VNODE
0323 
0324 /*
0325  * NGX_CLOSE_EVENT, NGX_LOWAT_EVENT, and NGX_FLUSH_EVENT are the module flags
0326  * and they must not go into a kernel so we need to choose the value
0327  * that must not interfere with any existent and future kqueue flags.
0328  * kqueue has such values - EV_FLAG1, EV_EOF, and EV_ERROR:
0329  * they are reserved and cleared on a kernel entrance.
0330  */
0331 #undef  NGX_CLOSE_EVENT
0332 #define NGX_CLOSE_EVENT    EV_EOF
0333 
0334 #undef  NGX_LOWAT_EVENT
0335 #define NGX_LOWAT_EVENT    EV_FLAG1
0336 
0337 #undef  NGX_FLUSH_EVENT
0338 #define NGX_FLUSH_EVENT    EV_ERROR
0339 
0340 #define NGX_LEVEL_EVENT    0
0341 #define NGX_ONESHOT_EVENT  EV_ONESHOT
0342 #define NGX_CLEAR_EVENT    EV_CLEAR
0343 
0344 #undef  NGX_DISABLE_EVENT
0345 #define NGX_DISABLE_EVENT  EV_DISABLE
0346 
0347 
0348 #elif (NGX_HAVE_DEVPOLL && !(NGX_TEST_BUILD_DEVPOLL)) \
0349       || (NGX_HAVE_EVENTPORT && !(NGX_TEST_BUILD_EVENTPORT))
0350 
0351 #define NGX_READ_EVENT     POLLIN
0352 #define NGX_WRITE_EVENT    POLLOUT
0353 
0354 #define NGX_LEVEL_EVENT    0
0355 #define NGX_ONESHOT_EVENT  1
0356 
0357 
0358 #elif (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)
0359 
0360 #define NGX_READ_EVENT     (EPOLLIN|EPOLLRDHUP)
0361 #define NGX_WRITE_EVENT    EPOLLOUT
0362 
0363 #define NGX_LEVEL_EVENT    0
0364 #define NGX_CLEAR_EVENT    EPOLLET
0365 #define NGX_ONESHOT_EVENT  0x70000000
0366 #if 0
0367 #define NGX_ONESHOT_EVENT  EPOLLONESHOT
0368 #endif
0369 
0370 #if (NGX_HAVE_EPOLLEXCLUSIVE)
0371 #define NGX_EXCLUSIVE_EVENT  EPOLLEXCLUSIVE
0372 #endif
0373 
0374 #elif (NGX_HAVE_POLL)
0375 
0376 #define NGX_READ_EVENT     POLLIN
0377 #define NGX_WRITE_EVENT    POLLOUT
0378 
0379 #define NGX_LEVEL_EVENT    0
0380 #define NGX_ONESHOT_EVENT  1
0381 
0382 
0383 #else /* select */
0384 
0385 #define NGX_READ_EVENT     0
0386 #define NGX_WRITE_EVENT    1
0387 
0388 #define NGX_LEVEL_EVENT    0
0389 #define NGX_ONESHOT_EVENT  1
0390 
0391 #endif /* NGX_HAVE_KQUEUE */
0392 
0393 
0394 #if (NGX_HAVE_IOCP)
0395 #define NGX_IOCP_ACCEPT      0
0396 #define NGX_IOCP_IO          1
0397 #define NGX_IOCP_CONNECT     2
0398 #endif
0399 
0400 
0401 #if (NGX_TEST_BUILD_EPOLL)
0402 #define NGX_EXCLUSIVE_EVENT  0
0403 #endif
0404 
0405 
0406 #ifndef NGX_CLEAR_EVENT
0407 #define NGX_CLEAR_EVENT    0    /* dummy declaration */
0408 #endif
0409 
0410 
0411 #define ngx_process_events   ngx_event_actions.process_events
0412 #define ngx_done_events      ngx_event_actions.done
0413 
0414 #define ngx_add_event        ngx_event_actions.add
0415 #define ngx_del_event        ngx_event_actions.del
0416 #define ngx_add_conn         ngx_event_actions.add_conn
0417 #define ngx_del_conn         ngx_event_actions.del_conn
0418 
0419 #define ngx_notify           ngx_event_actions.notify
0420 
0421 #define ngx_add_timer        ngx_event_add_timer
0422 #define ngx_del_timer        ngx_event_del_timer
0423 
0424 
0425 extern ngx_os_io_t  ngx_io;
0426 
0427 #define ngx_recv             ngx_io.recv
0428 #define ngx_recv_chain       ngx_io.recv_chain
0429 #define ngx_udp_recv         ngx_io.udp_recv
0430 #define ngx_send             ngx_io.send
0431 #define ngx_send_chain       ngx_io.send_chain
0432 #define ngx_udp_send         ngx_io.udp_send
0433 #define ngx_udp_send_chain   ngx_io.udp_send_chain
0434 
0435 
0436 #define NGX_EVENT_MODULE      0x544E5645  /* "EVNT" */
0437 #define NGX_EVENT_CONF        0x02000000
0438 
0439 
0440 typedef struct {
0441     ngx_uint_t    connections;
0442     ngx_uint_t    use;
0443 
0444     ngx_flag_t    multi_accept;
0445     ngx_flag_t    accept_mutex;
0446 
0447     ngx_msec_t    accept_mutex_delay;
0448 
0449     u_char       *name;
0450 
0451 #if (NGX_DEBUG)
0452     ngx_array_t   debug_connection;
0453 #endif
0454 } ngx_event_conf_t;
0455 
0456 
0457 typedef struct {
0458     ngx_str_t              *name;
0459 
0460     void                 *(*create_conf)(ngx_cycle_t *cycle);
0461     char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);
0462 
0463     ngx_event_actions_t     actions;
0464 } ngx_event_module_t;
0465 
0466 
0467 extern ngx_atomic_t          *ngx_connection_counter;
0468 
0469 extern ngx_atomic_t          *ngx_accept_mutex_ptr;
0470 extern ngx_shmtx_t            ngx_accept_mutex;
0471 extern ngx_uint_t             ngx_use_accept_mutex;
0472 extern ngx_uint_t             ngx_accept_events;
0473 extern ngx_uint_t             ngx_accept_mutex_held;
0474 extern ngx_msec_t             ngx_accept_mutex_delay;
0475 extern ngx_int_t              ngx_accept_disabled;
0476 
0477 
0478 #if (NGX_STAT_STUB)
0479 
0480 extern ngx_atomic_t  *ngx_stat_accepted;
0481 extern ngx_atomic_t  *ngx_stat_handled;
0482 extern ngx_atomic_t  *ngx_stat_requests;
0483 extern ngx_atomic_t  *ngx_stat_active;
0484 extern ngx_atomic_t  *ngx_stat_reading;
0485 extern ngx_atomic_t  *ngx_stat_writing;
0486 extern ngx_atomic_t  *ngx_stat_waiting;
0487 
0488 #endif
0489 
0490 
0491 #define NGX_UPDATE_TIME         1
0492 #define NGX_POST_EVENTS         2
0493 
0494 
0495 extern sig_atomic_t           ngx_event_timer_alarm;
0496 extern ngx_uint_t             ngx_event_flags;
0497 extern ngx_module_t           ngx_events_module;
0498 extern ngx_module_t           ngx_event_core_module;
0499 
0500 
0501 #define ngx_event_get_conf(conf_ctx, module)                                  \
0502              (*(ngx_get_conf(conf_ctx, ngx_events_module))) [module.ctx_index];
0503 
0504 
0505 
0506 void ngx_event_accept(ngx_event_t *ev);
0507 #if !(NGX_WIN32)
0508 void ngx_event_recvmsg(ngx_event_t *ev);
0509 #endif
0510 ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle);
0511 u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);
0512 
0513 
0514 void ngx_process_events_and_timers(ngx_cycle_t *cycle);
0515 ngx_int_t ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags);
0516 ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);
0517 
0518 
0519 #if (NGX_WIN32)
0520 void ngx_event_acceptex(ngx_event_t *ev);
0521 ngx_int_t ngx_event_post_acceptex(ngx_listening_t *ls, ngx_uint_t n);
0522 u_char *ngx_acceptex_log_error(ngx_log_t *log, u_char *buf, size_t len);
0523 #endif
0524 
0525 
0526 ngx_int_t ngx_send_lowat(ngx_connection_t *c, size_t lowat);
0527 
0528 
0529 /* used in ngx_log_debugX() */
0530 #define ngx_event_ident(p)  ((ngx_connection_t *) (p))->fd
0531 
0532 
0533 #include <ngx_event_timer.h>
0534 #include <ngx_event_posted.h>
0535 
0536 #if (NGX_WIN32)
0537 #include <ngx_iocp_module.h>
0538 #endif
0539 
0540 
0541 #endif /* _NGX_EVENT_H_INCLUDED_ */