src/event/ngx_event.h - nginx source code

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. struct ngx_event_s {
  18.     void            *data;

  19.     unsigned         write:1;

  20.     unsigned         accept:1;

  21.     /* used to detect the stale events in kqueue and epoll */
  22.     unsigned         instance:1;

  23.     /*
  24.      * the event was passed or would be passed to a kernel;
  25.      * in aio mode - operation was posted.
  26.      */
  27.     unsigned         active:1;

  28.     unsigned         disabled:1;

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

  31.     unsigned         oneshot:1;

  32.     /* aio operation is complete */
  33.     unsigned         complete:1;

  34.     unsigned         eof:1;
  35.     unsigned         error:1;

  36.     unsigned         timedout:1;
  37.     unsigned         timer_set:1;

  38.     unsigned         delayed:1;

  39.     unsigned         deferred_accept:1;

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

  42.     unsigned         posted:1;

  43.     unsigned         closed:1;

  44.     /* to test on worker exit */
  45.     unsigned         channel:1;
  46.     unsigned         resolver:1;

  47.     unsigned         cancelable:1;

  48. #if (NGX_HAVE_KQUEUE)
  49.     unsigned         kq_vnode:1;

  50.     /* the pending errno reported by kqueue */
  51.     int              kq_errno;
  52. #endif

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

  67.     int              available;

  68.     ngx_event_handler_pt  handler;


  69. #if (NGX_HAVE_IOCP)
  70.     ngx_event_ovlp_t ovlp;
  71. #endif

  72.     ngx_uint_t       index;

  73.     ngx_log_t       *log;

  74.     ngx_rbtree_node_t   timer;

  75.     /* the posted queue */
  76.     ngx_queue_t      queue;

  77. #if 0

  78.     /* the threads support */

  79.     /*
  80.      * the event thread context, we store it here
  81.      * if $(CC) does not understand __thread declaration
  82.      * and pthread_getspecific() is too costly
  83.      */

  84.     void            *thr_ctx;

  85. #if (NGX_EVENT_T_PADDING)

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

  87.     uint32_t         padding[NGX_EVENT_T_PADDING];
  88. #endif
  89. #endif
  90. };


  91. #if (NGX_HAVE_FILE_AIO)

  92. struct ngx_event_aio_s {
  93.     void                      *data;
  94.     ngx_event_handler_pt       handler;
  95.     ngx_file_t                *file;

  96.     ngx_fd_t                   fd;

  97. #if (NGX_HAVE_EVENTFD)
  98.     int64_t                    res;
  99. #endif

  100. #if !(NGX_HAVE_EVENTFD) || (NGX_TEST_BUILD_EPOLL)
  101.     ngx_err_t                  err;
  102.     size_t                     nbytes;
  103. #endif

  104.     ngx_aiocb_t                aiocb;
  105.     ngx_event_t                event;
  106. };

  107. #endif


  108. typedef struct {
  109.     ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
  110.     ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

  111.     ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
  112.     ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

  113.     ngx_int_t  (*add_conn)(ngx_connection_t *c);
  114.     ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);

  115.     ngx_int_t  (*notify)(ngx_event_handler_pt handler);

  116.     ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
  117.                                  ngx_uint_t flags);

  118.     ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
  119.     void       (*done)(ngx_cycle_t *cycle);
  120. } ngx_event_actions_t;


  121. extern ngx_event_actions_t   ngx_event_actions;
  122. #if (NGX_HAVE_EPOLLRDHUP)
  123. extern ngx_uint_t            ngx_use_epoll_rdhup;
  124. #endif


  125. /*
  126. * The event filter requires to read/write the whole data:
  127. * select, poll, /dev/poll, kqueue, epoll.
  128. */
  129. #define NGX_USE_LEVEL_EVENT      0x00000001

  130. /*
  131. * The event filter is deleted after a notification without an additional
  132. * syscall: kqueue, epoll.
  133. */
  134. #define NGX_USE_ONESHOT_EVENT    0x00000002

  135. /*
  136. * The event filter notifies only the changes and an initial level:
  137. * kqueue, epoll.
  138. */
  139. #define NGX_USE_CLEAR_EVENT      0x00000004

  140. /*
  141. * The event filter has kqueue features: the eof flag, errno,
  142. * available data, etc.
  143. */
  144. #define NGX_USE_KQUEUE_EVENT     0x00000008

  145. /*
  146. * The event filter supports low water mark: kqueue's NOTE_LOWAT.
  147. * kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
  148. */
  149. #define NGX_USE_LOWAT_EVENT      0x00000010

  150. /*
  151. * The event filter requires to do i/o operation until EAGAIN: epoll.
  152. */
  153. #define NGX_USE_GREEDY_EVENT     0x00000020

  154. /*
  155. * The event filter is epoll.
  156. */
  157. #define NGX_USE_EPOLL_EVENT      0x00000040

  158. /*
  159. * Obsolete.
  160. */
  161. #define NGX_USE_RTSIG_EVENT      0x00000080

  162. /*
  163. * Obsolete.
  164. */
  165. #define NGX_USE_AIO_EVENT        0x00000100

  166. /*
  167. * Need to add socket or handle only once: i/o completion port.
  168. */
  169. #define NGX_USE_IOCP_EVENT       0x00000200

  170. /*
  171. * The event filter has no opaque data and requires file descriptors table:
  172. * poll, /dev/poll.
  173. */
  174. #define NGX_USE_FD_EVENT         0x00000400

  175. /*
  176. * The event module handles periodic or absolute timer event by itself:
  177. * kqueue in FreeBSD 4.4, NetBSD 2.0, and MacOSX 10.4, Solaris 10's event ports.
  178. */
  179. #define NGX_USE_TIMER_EVENT      0x00000800

  180. /*
  181. * All event filters on file descriptor are deleted after a notification:
  182. * Solaris 10's event ports.
  183. */
  184. #define NGX_USE_EVENTPORT_EVENT  0x00001000

  185. /*
  186. * The event filter support vnode notifications: kqueue.
  187. */
  188. #define NGX_USE_VNODE_EVENT      0x00002000


  189. /*
  190. * The event filter is deleted just before the closing file.
  191. * Has no meaning for select and poll.
  192. * kqueue, epoll, eventport:         allows to avoid explicit delete,
  193. *                                   because filter automatically is deleted
  194. *                                   on file close,
  195. *
  196. * /dev/poll:                        we need to flush POLLREMOVE event
  197. *                                   before closing file.
  198. */
  199. #define NGX_CLOSE_EVENT    1

  200. /*
  201. * disable temporarily event filter, this may avoid locks
  202. * in kernel malloc()/free(): kqueue.
  203. */
  204. #define NGX_DISABLE_EVENT  2

  205. /*
  206. * event must be passed to kernel right now, do not wait until batch processing.
  207. */
  208. #define NGX_FLUSH_EVENT    4


  209. /* these flags have a meaning only for kqueue */
  210. #define NGX_LOWAT_EVENT    0
  211. #define NGX_VNODE_EVENT    0


  212. #if (NGX_HAVE_EPOLL) && !(NGX_HAVE_EPOLLRDHUP)
  213. #define EPOLLRDHUP         0
  214. #endif


  215. #if (NGX_HAVE_KQUEUE)

  216. #define NGX_READ_EVENT     EVFILT_READ
  217. #define NGX_WRITE_EVENT    EVFILT_WRITE

  218. #undef  NGX_VNODE_EVENT
  219. #define NGX_VNODE_EVENT    EVFILT_VNODE

  220. /*
  221. * NGX_CLOSE_EVENT, NGX_LOWAT_EVENT, and NGX_FLUSH_EVENT are the module flags
  222. * and they must not go into a kernel so we need to choose the value
  223. * that must not interfere with any existent and future kqueue flags.
  224. * kqueue has such values - EV_FLAG1, EV_EOF, and EV_ERROR:
  225. * they are reserved and cleared on a kernel entrance.
  226. */
  227. #undef  NGX_CLOSE_EVENT
  228. #define NGX_CLOSE_EVENT    EV_EOF

  229. #undef  NGX_LOWAT_EVENT
  230. #define NGX_LOWAT_EVENT    EV_FLAG1

  231. #undef  NGX_FLUSH_EVENT
  232. #define NGX_FLUSH_EVENT    EV_ERROR

  233. #define NGX_LEVEL_EVENT    0
  234. #define NGX_ONESHOT_EVENT  EV_ONESHOT
  235. #define NGX_CLEAR_EVENT    EV_CLEAR

  236. #undef  NGX_DISABLE_EVENT
  237. #define NGX_DISABLE_EVENT  EV_DISABLE


  238. #elif (NGX_HAVE_DEVPOLL && !(NGX_TEST_BUILD_DEVPOLL)) \
  239.       || (NGX_HAVE_EVENTPORT && !(NGX_TEST_BUILD_EVENTPORT))

  240. #define NGX_READ_EVENT     POLLIN
  241. #define NGX_WRITE_EVENT    POLLOUT

  242. #define NGX_LEVEL_EVENT    0
  243. #define NGX_ONESHOT_EVENT  1


  244. #elif (NGX_HAVE_EPOLL) && !(NGX_TEST_BUILD_EPOLL)

  245. #define NGX_READ_EVENT     (EPOLLIN|EPOLLRDHUP)
  246. #define NGX_WRITE_EVENT    EPOLLOUT

  247. #define NGX_LEVEL_EVENT    0
  248. #define NGX_CLEAR_EVENT    EPOLLET
  249. #define NGX_ONESHOT_EVENT  0x70000000
  250. #if 0
  251. #define NGX_ONESHOT_EVENT  EPOLLONESHOT
  252. #endif

  253. #if (NGX_HAVE_EPOLLEXCLUSIVE)
  254. #define NGX_EXCLUSIVE_EVENT  EPOLLEXCLUSIVE
  255. #endif

  256. #elif (NGX_HAVE_POLL)

  257. #define NGX_READ_EVENT     POLLIN
  258. #define NGX_WRITE_EVENT    POLLOUT

  259. #define NGX_LEVEL_EVENT    0
  260. #define NGX_ONESHOT_EVENT  1


  261. #else /* select */

  262. #define NGX_READ_EVENT     0
  263. #define NGX_WRITE_EVENT    1

  264. #define NGX_LEVEL_EVENT    0
  265. #define NGX_ONESHOT_EVENT  1

  266. #endif /* NGX_HAVE_KQUEUE */


  267. #if (NGX_HAVE_IOCP)
  268. #define NGX_IOCP_ACCEPT      0
  269. #define NGX_IOCP_IO          1
  270. #define NGX_IOCP_CONNECT     2
  271. #endif


  272. #if (NGX_TEST_BUILD_EPOLL)
  273. #define NGX_EXCLUSIVE_EVENT  0
  274. #endif


  275. #ifndef NGX_CLEAR_EVENT
  276. #define NGX_CLEAR_EVENT    0    /* dummy declaration */
  277. #endif


  278. #define ngx_process_events   ngx_event_actions.process_events
  279. #define ngx_done_events      ngx_event_actions.done

  280. #define ngx_add_event        ngx_event_actions.add
  281. #define ngx_del_event        ngx_event_actions.del
  282. #define ngx_add_conn         ngx_event_actions.add_conn
  283. #define ngx_del_conn         ngx_event_actions.del_conn

  284. #define ngx_notify           ngx_event_actions.notify

  285. #define ngx_add_timer        ngx_event_add_timer
  286. #define ngx_del_timer        ngx_event_del_timer


  287. extern ngx_os_io_t  ngx_io;

  288. #define ngx_recv             ngx_io.recv
  289. #define ngx_recv_chain       ngx_io.recv_chain
  290. #define ngx_udp_recv         ngx_io.udp_recv
  291. #define ngx_send             ngx_io.send
  292. #define ngx_send_chain       ngx_io.send_chain
  293. #define ngx_udp_send         ngx_io.udp_send
  294. #define ngx_udp_send_chain   ngx_io.udp_send_chain


  295. #define NGX_EVENT_MODULE      0x544E5645  /* "EVNT" */
  296. #define NGX_EVENT_CONF        0x02000000


  297. typedef struct {
  298.     ngx_uint_t    connections;
  299.     ngx_uint_t    use;

  300.     ngx_flag_t    multi_accept;
  301.     ngx_flag_t    accept_mutex;

  302.     ngx_msec_t    accept_mutex_delay;

  303.     u_char       *name;

  304. #if (NGX_DEBUG)
  305.     ngx_array_t   debug_connection;
  306. #endif
  307. } ngx_event_conf_t;


  308. typedef struct {
  309.     ngx_str_t              *name;

  310.     void                 *(*create_conf)(ngx_cycle_t *cycle);
  311.     char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);

  312.     ngx_event_actions_t     actions;
  313. } ngx_event_module_t;


  314. extern ngx_atomic_t          *ngx_connection_counter;

  315. extern ngx_atomic_t          *ngx_accept_mutex_ptr;
  316. extern ngx_shmtx_t            ngx_accept_mutex;
  317. extern ngx_uint_t             ngx_use_accept_mutex;
  318. extern ngx_uint_t             ngx_accept_events;
  319. extern ngx_uint_t             ngx_accept_mutex_held;
  320. extern ngx_msec_t             ngx_accept_mutex_delay;
  321. extern ngx_int_t              ngx_accept_disabled;
  322. extern ngx_uint_t             ngx_use_exclusive_accept;


  323. #if (NGX_STAT_STUB)

  324. extern ngx_atomic_t  *ngx_stat_accepted;
  325. extern ngx_atomic_t  *ngx_stat_handled;
  326. extern ngx_atomic_t  *ngx_stat_requests;
  327. extern ngx_atomic_t  *ngx_stat_active;
  328. extern ngx_atomic_t  *ngx_stat_reading;
  329. extern ngx_atomic_t  *ngx_stat_writing;
  330. extern ngx_atomic_t  *ngx_stat_waiting;

  331. #endif


  332. #define NGX_UPDATE_TIME         1
  333. #define NGX_POST_EVENTS         2


  334. extern sig_atomic_t           ngx_event_timer_alarm;
  335. extern ngx_uint_t             ngx_event_flags;
  336. extern ngx_module_t           ngx_events_module;
  337. extern ngx_module_t           ngx_event_core_module;


  338. #define ngx_event_get_conf(conf_ctx, module)                                  \
  339.              (*(ngx_get_conf(conf_ctx, ngx_events_module))) [module.ctx_index]



  340. void ngx_event_accept(ngx_event_t *ev);
  341. ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle);
  342. ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle);
  343. u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);
  344. #if (NGX_DEBUG)
  345. void ngx_debug_accepted_connection(ngx_event_conf_t *ecf, ngx_connection_t *c);
  346. #endif


  347. void ngx_process_events_and_timers(ngx_cycle_t *cycle);
  348. ngx_int_t ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags);
  349. ngx_int_t ngx_handle_write_event(ngx_event_t *wev, size_t lowat);


  350. #if (NGX_WIN32)
  351. void ngx_event_acceptex(ngx_event_t *ev);
  352. ngx_int_t ngx_event_post_acceptex(ngx_listening_t *ls, ngx_uint_t n);
  353. u_char *ngx_acceptex_log_error(ngx_log_t *log, u_char *buf, size_t len);
  354. #endif


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


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


  358. #include <ngx_event_timer.h>
  359. #include <ngx_event_posted.h>
  360. #include <ngx_event_udp.h>

  361. #if (NGX_WIN32)
  362. #include <ngx_iocp_module.h>
  363. #endif


  364. #endif /* _NGX_EVENT_H_INCLUDED_ */