src/event/ngx_event.h - nginx-1.7.10

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_EVENT_H_INCLUDED_
  6. #define _NGX_EVENT_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>


  9. #define NGX_INVALID_INDEX  0xd0d0d0d0


  10. #if (NGX_HAVE_IOCP)

  11. typedef struct {
  12.     WSAOVERLAPPED    ovlp;
  13.     ngx_event_t     *event;
  14.     int              error;
  15. } ngx_event_ovlp_t;

  16. #endif


  17. typedef struct {
  18.     ngx_uint_t       lock;

  19.     ngx_event_t     *events;
  20.     ngx_event_t     *last;
  21. } ngx_event_mutex_t;


  22. struct ngx_event_s {
  23.     void            *data;

  24.     unsigned         write:1;

  25.     unsigned         accept:1;

  26.     /* used to detect the stale events in kqueue, rtsig, and epoll */
  27.     unsigned         instance:1;

  28.     /*
  29.      * the event was passed or would be passed to a kernel;
  30.      * in aio mode - operation was posted.
  31.      */
  32.     unsigned         active:1;

  33.     unsigned         disabled:1;

  34.     /* the ready event; in aio mode 0 means that no operation can be posted */
  35.     unsigned         ready:1;

  36.     unsigned         oneshot:1;

  37.     /* aio operation is complete */
  38.     unsigned         complete:1;

  39.     unsigned         eof:1;
  40.     unsigned         error:1;

  41.     unsigned         timedout:1;
  42.     unsigned         timer_set:1;

  43.     unsigned         delayed:1;

  44.     unsigned         deferred_accept:1;

  45.     /* the pending eof reported by kqueue, epoll or in aio chain operation */
  46.     unsigned         pending_eof:1;

  47.     unsigned         posted:1;

  48. #if (NGX_WIN32)
  49.     /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was successful */
  50.     unsigned         accept_context_updated:1;
  51. #endif

  52. #if (NGX_HAVE_KQUEUE)
  53.     unsigned         kq_vnode:1;

  54.     /* the pending errno reported by kqueue */
  55.     int              kq_errno;
  56. #endif

  57.     /*
  58.      * kqueue only:
  59.      *   accept:     number of sockets that wait to be accepted
  60.      *   read:       bytes to read when event is ready
  61.      *               or lowat when event is set with NGX_LOWAT_EVENT flag
  62.      *   write:      available space in buffer when event is ready
  63.      *               or lowat when event is set with NGX_LOWAT_EVENT flag
  64.      *
  65.      * iocp: TODO
  66.      *
  67.      * otherwise:
  68.      *   accept:     1 if accept many, 0 otherwise
  69.      */

  70. #if (NGX_HAVE_KQUEUE) || (NGX_HAVE_IOCP)
  71.     int              available;
  72. #else
  73.     unsigned         available:1;
  74. #endif

  75.     ngx_event_handler_pt  handler;


  76. #if (NGX_HAVE_AIO)

  77. #if (NGX_HAVE_IOCP)
  78.     ngx_event_ovlp_t ovlp;
  79. #else
  80.     struct aiocb     aiocb;
  81. #endif

  82. #endif

  83.     ngx_uint_t       index;

  84.     ngx_log_t       *log;

  85.     ngx_rbtree_node_t   timer;

  86.     /* the posted queue */
  87.     ngx_queue_t      queue;

  88.     unsigned         closed:1;

  89.     /* to test on worker exit */
  90.     unsigned         channel:1;
  91.     unsigned         resolver:1;

  92.     unsigned         cancelable:1;


  93. #if 0

  94.     /* the threads support */

  95.     /*
  96.      * the event thread context, we store it here
  97.      * if $(CC) does not understand __thread declaration
  98.      * and pthread_getspecific() is too costly
  99.      */

  100.     void            *thr_ctx;

  101. #if (NGX_EVENT_T_PADDING)

  102.     /* event should not cross cache line in SMP */

  103.     uint32_t         padding[NGX_EVENT_T_PADDING];
  104. #endif
  105. #endif
  106. };


  107. #if (NGX_HAVE_FILE_AIO)

  108. struct ngx_event_aio_s {
  109.     void                      *data;
  110.     ngx_event_handler_pt       handler;
  111.     ngx_file_t                *file;

  112.     ngx_fd_t                   fd;

  113. #if (NGX_HAVE_EVENTFD)
  114.     int64_t                    res;
  115. #if (NGX_TEST_BUILD_EPOLL)
  116.     ngx_err_t                  err;
  117.     size_t                     nbytes;
  118. #endif
  119. #else
  120.     ngx_err_t                  err;
  121.     size_t                     nbytes;
  122. #endif

  123. #if (NGX_HAVE_AIO_SENDFILE)
  124.     off_t                      last_offset;
  125. #endif

  126.     ngx_aiocb_t                aiocb;
  127.     ngx_event_t                event;
  128. };

  129. #endif


  130. typedef struct {
  131.     ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
  132.     ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

  133.     ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
  134.     ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

  135.     ngx_int_t  (*add_conn)(ngx_connection_t *c);
  136.     ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);

  137.     ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);
  138.     ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
  139.                    ngx_uint_t flags);

  140.     ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
  141.     void       (*done)(ngx_cycle_t *cycle);
  142. } ngx_event_actions_t;


  143. extern ngx_event_actions_t   ngx_event_actions;


  144. /*
  145. * The event filter requires to read/write the whole data:
  146. * select, poll, /dev/poll, kqueue, epoll.
  147. */
  148. #define NGX_USE_LEVEL_EVENT      0x00000001

  149. /*
  150. * The event filter is deleted after a notification without an additional
  151. * syscall: kqueue, epoll.
  152. */
  153. #define NGX_USE_ONESHOT_EVENT    0x00000002

  154. /*
  155. * The event filter notifies only the changes and an initial level:
  156. * kqueue, epoll.
  157. */
  158. #define NGX_USE_CLEAR_EVENT      0x00000004

  159. /*
  160. * The event filter has kqueue features: the eof flag, errno,
  161. * available data, etc.
  162. */
  163. #define NGX_USE_KQUEUE_EVENT     0x00000008

  164. /*
  165. * The event filter supports low water mark: kqueue's NOTE_LOWAT.
  166. * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
  167. */
  168. #define NGX_USE_LOWAT_EVENT      0x00000010

  169. /*
  170. * The event filter requires to do i/o operation until EAGAIN: epoll, rtsig.
  171. */
  172. #define NGX_USE_GREEDY_EVENT     0x00000020

  173. /*
  174. * The event filter is epoll.
  175. */
  176. #define NGX_USE_EPOLL_EVENT      0x00000040

  177. /*
  178. * No need to add or delete the event filters: rtsig.
  179. */
  180. #define NGX_USE_RTSIG_EVENT      0x00000080

  181. /*
  182. * No need to add or delete the event filters: overlapped, aio_read,
  183. * aioread, io_submit.
  184. */
  185. #define NGX_USE_AIO_EVENT        0x00000100

  186. /*
  187. * Need to add socket or handle only once: i/o completion port.
  188. * It also requires NGX_HAVE_AIO and NGX_USE_AIO_EVENT to be set.
  189. */
  190. #define NGX_USE_IOCP_EVENT       0x00000200

  191. /*
  192. * The event filter has no opaque data and requires file descriptors table:
  193. * poll, /dev/poll, rtsig.
  194. */
  195. #define NGX_USE_FD_EVENT         0x00000400

  196. /*
  197. * The event module handles periodic or absolute timer event by itself:
  198. * kqueue in FreeBSD 4.4, NetBSD 2.0, and MacOSX 10.4, Solaris 10's event ports.
  199. */
  200. #define NGX_USE_TIMER_EVENT      0x00000800

  201. /*
  202. * All event filters on file descriptor are deleted after a notification:
  203. * Solaris 10's event ports.
  204. */
  205. #define NGX_USE_EVENTPORT_EVENT  0x00001000

  206. /*
  207. * The event filter support vnode notifications: kqueue.
  208. */
  209. #define NGX_USE_VNODE_EVENT      0x00002000


  210. /*
  211. * The event filter is deleted just before the closing file.
  212. * Has no meaning for select and poll.
  213. * kqueue, epoll, rtsig, eventport:  allows to avoid explicit delete,
  214. *                                   because filter automatically is deleted
  215. *                                   on file close,
  216. *
  217. * /dev/poll:                        we need to flush POLLREMOVE event
  218. *                                   before closing file.
  219. */
  220. #define NGX_CLOSE_EVENT    1

  221. /*
  222. * disable temporarily event filter, this may avoid locks
  223. * in kernel malloc()/free(): kqueue.
  224. */
  225. #define NGX_DISABLE_EVENT  2

  226. /*
  227. * event must be passed to kernel right now, do not wait until batch processing.
  228. */
  229. #define NGX_FLUSH_EVENT    4


  230. /* these flags have a meaning only for kqueue */
  231. #define NGX_LOWAT_EVENT    0
  232. #define NGX_VNODE_EVENT    0


  233. #if (NGX_HAVE_EPOLL) && !(NGX_HAVE_EPOLLRDHUP)
  234. #define EPOLLRDHUP         0
  235. #endif


  236. #if (NGX_HAVE_KQUEUE)

  237. #define NGX_READ_EVENT     EVFILT_READ
  238. #define NGX_WRITE_EVENT    EVFILT_WRITE

  239. #undef  NGX_VNODE_EVENT
  240. #define NGX_VNODE_EVENT    EVFILT_VNODE

  241. /*
  242. * NGX_CLOSE_EVENT, NGX_LOWAT_EVENT, and NGX_FLUSH_EVENT are the module flags
  243. * and they must not go into a kernel so we need to choose the value
  244. * that must not interfere with any existent and future kqueue flags.
  245. * kqueue has such values - EV_FLAG1, EV_EOF, and EV_ERROR:
  246. * they are reserved and cleared on a kernel entrance.
  247. */
  248. #undef  NGX_CLOSE_EVENT
  249. #define NGX_CLOSE_EVENT    EV_EOF

  250. #undef  NGX_LOWAT_EVENT
  251. #define NGX_LOWAT_EVENT    EV_FLAG1

  252. #undef  NGX_FLUSH_EVENT
  253. #define NGX_FLUSH_EVENT    EV_ERROR

  254. #define NGX_LEVEL_EVENT    0
  255. #define NGX_ONESHOT_EVENT  EV_ONESHOT
  256. #define NGX_CLEAR_EVENT    EV_CLEAR

  257. #undef  NGX_DISABLE_EVENT
  258. #define NGX_DISABLE_EVENT  EV_DISABLE


  259. #elif (NGX_HAVE_DEVPOLL || NGX_HAVE_EVENTPORT)

  260. #define NGX_READ_EVENT     POLLIN
  261. #define NGX_WRITE_EVENT    POLLOUT

  262. #define NGX_LEVEL_EVENT    0
  263. #define NGX_ONESHOT_EVENT  1


  264. #elif (NGX_HAVE_EPOLL)

  265. #define NGX_READ_EVENT     (EPOLLIN|EPOLLRDHUP)
  266. #define NGX_WRITE_EVENT    EPOLLOUT

  267. #define NGX_LEVEL_EVENT    0
  268. #define NGX_CLEAR_EVENT    EPOLLET
  269. #define NGX_ONESHOT_EVENT  0x70000000
  270. #if 0
  271. #define NGX_ONESHOT_EVENT  EPOLLONESHOT
  272. #endif


  273. #elif (NGX_HAVE_POLL)

  274. #define NGX_READ_EVENT     POLLIN
  275. #define NGX_WRITE_EVENT    POLLOUT

  276. #define NGX_LEVEL_EVENT    0
  277. #define NGX_ONESHOT_EVENT  1


  278. #else /* select */

  279. #define NGX_READ_EVENT     0
  280. #define NGX_WRITE_EVENT    1

  281. #define NGX_LEVEL_EVENT    0
  282. #define NGX_ONESHOT_EVENT  1

  283. #endif /* NGX_HAVE_KQUEUE */


  284. #if (NGX_HAVE_IOCP)
  285. #define NGX_IOCP_ACCEPT      0
  286. #define NGX_IOCP_IO          1
  287. #define NGX_IOCP_CONNECT     2
  288. #endif


  289. #ifndef NGX_CLEAR_EVENT
  290. #define NGX_CLEAR_EVENT    0    /* dummy declaration */
  291. #endif


  292. #define ngx_process_changes  ngx_event_actions.process_changes
  293. #define ngx_process_events   ngx_event_actions.process_events
  294. #define ngx_done_events      ngx_event_actions.done

  295. #define ngx_add_event        ngx_event_actions.add
  296. #define ngx_del_event        ngx_event_actions.del
  297. #define ngx_add_conn         ngx_event_actions.add_conn
  298. #define ngx_del_conn         ngx_event_actions.del_conn

  299. #define ngx_add_timer        ngx_event_add_timer
  300. #define ngx_del_timer        ngx_event_del_timer


  301. extern ngx_os_io_t  ngx_io;

  302. #define ngx_recv             ngx_io.recv
  303. #define ngx_recv_chain       ngx_io.recv_chain
  304. #define ngx_udp_recv         ngx_io.udp_recv
  305. #define ngx_send             ngx_io.send
  306. #define ngx_send_chain       ngx_io.send_chain


  307. #define NGX_EVENT_MODULE      0x544E5645  /* "EVNT" */
  308. #define NGX_EVENT_CONF        0x02000000


  309. typedef struct {
  310.     ngx_uint_t    connections;
  311.     ngx_uint_t    use;

  312.     ngx_flag_t    multi_accept;
  313.     ngx_flag_t    accept_mutex;

  314.     ngx_msec_t    accept_mutex_delay;

  315.     u_char       *name;

  316. #if (NGX_DEBUG)
  317.     ngx_array_t   debug_connection;
  318. #endif
  319. } ngx_event_conf_t;


  320. typedef struct {
  321.     ngx_str_t              *name;

  322.     void                 *(*create_conf)(ngx_cycle_t *cycle);
  323.     char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);

  324.     ngx_event_actions_t     actions;
  325. } ngx_event_module_t;


  326. extern ngx_atomic_t          *ngx_connection_counter;

  327. extern ngx_atomic_t          *ngx_accept_mutex_ptr;
  328. extern ngx_shmtx_t            ngx_accept_mutex;
  329. extern ngx_uint_t             ngx_use_accept_mutex;
  330. extern ngx_uint_t             ngx_accept_events;
  331. extern ngx_uint_t             ngx_accept_mutex_held;
  332. extern ngx_msec_t             ngx_accept_mutex_delay;
  333. extern ngx_int_t              ngx_accept_disabled;


  334. #if (NGX_STAT_STUB)

  335. extern ngx_atomic_t  *ngx_stat_accepted;
  336. extern ngx_atomic_t  *ngx_stat_handled;
  337. extern ngx_atomic_t  *ngx_stat_requests;
  338. extern ngx_atomic_t  *ngx_stat_active;
  339. extern ngx_atomic_t  *ngx_stat_reading;
  340. extern ngx_atomic_t  *ngx_stat_writing;
  341. extern ngx_atomic_t  *ngx_stat_waiting;

  342. #endif


  343. #define NGX_UPDATE_TIME         1
  344. #define NGX_POST_EVENTS         2


  345. extern sig_atomic_t           ngx_event_timer_alarm;
  346. extern ngx_uint_t             ngx_event_flags;
  347. extern ngx_module_t           ngx_events_module;
  348. extern ngx_module_t           ngx_event_core_module;


  349. #define ngx_event_get_conf(conf_ctx, module)                                  \
  350.              (*(ngx_get_conf(conf_ctx, ngx_events_module))) [module.ctx_index];



  351. void ngx_event_accept(ngx_event_t *ev);
  352. ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle);
  353. u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);


  354. void ngx_process_events_and_timers(ngx_cycle_t *cycle);
  355. ngx_int_t ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags);
  356. ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);


  357. #if (NGX_WIN32)
  358. void ngx_event_acceptex(ngx_event_t *ev);
  359. ngx_int_t ngx_event_post_acceptex(ngx_listening_t *ls, ngx_uint_t n);
  360. u_char *ngx_acceptex_log_error(ngx_log_t *log, u_char *buf, size_t len);
  361. #endif


  362. ngx_int_t ngx_send_lowat(ngx_connection_t *c, size_t lowat);


  363. /* used in ngx_log_debugX() */
  364. #define ngx_event_ident(p)  ((ngx_connection_t *) (p))->fd


  365. #include <ngx_event_timer.h>
  366. #include <ngx_event_posted.h>
  367. #include <ngx_event_busy_lock.h>

  368. #if (NGX_WIN32)
  369. #include <ngx_iocp_module.h>
  370. #endif


  371. #endif /* _NGX_EVENT_H_INCLUDED_ */