3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 #include <ngx_kqueue_module.h>
17 static ngx_int_t ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer);
18 static void ngx_aio_done(ngx_cycle_t *cycle);
19 static ngx_int_t ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event,
21 static ngx_int_t ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event,
23 static ngx_int_t ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags);
24 static ngx_int_t ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
28 ngx_os_io_t ngx_os_aio = {
38 static ngx_str_t aio_name = ngx_string("aio");
40 ngx_event_module_t ngx_aio_module_ctx = {
42 NULL, /* create configuration */
43 NULL, /* init configuration */
46 ngx_aio_add_event, /* add an event */
47 ngx_aio_del_event, /* delete an event */
48 NULL, /* enable an event */
49 NULL, /* disable an event */
50 NULL, /* add an connection */
51 ngx_aio_del_connection, /* delete an connection */
52 NULL, /* process the changes */
53 ngx_aio_process_events, /* process the events */
54 ngx_aio_init, /* init the events */
55 ngx_aio_done /* done the events */
60 ngx_module_t ngx_aio_module = {
62 &ngx_aio_module_ctx, /* module context */
63 NULL, /* module directives */
64 NGX_EVENT_MODULE, /* module type */
65 NULL, /* init master */
66 NULL, /* init module */
67 NULL, /* init process */
68 NULL, /* init thread */
69 NULL, /* exit thread */
70 NULL, /* exit process */
71 NULL, /* exit master */
80 ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer)
82 if (ngx_kqueue_module_ctx.actions.init(cycle, timer) == NGX_ERROR) {
88 ngx_event_flags = NGX_USE_AIO_EVENT;
89 ngx_event_actions = ngx_aio_module_ctx.actions;
97 ngx_aio_done(ngx_cycle_t *cycle)
99 ngx_kqueue_module_ctx.actions.done(cycle);
103 /* the event adding and deleting are needed for the listening sockets */
106 ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
108 return ngx_kqueue_module_ctx.actions.add(ev, event, flags);
113 ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
115 return ngx_kqueue_module_ctx.actions.del(ev, event, flags);
120 ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags)
124 if (c->read->active == 0 && c->write->active == 0) {
128 if (flags & NGX_CLOSE_EVENT) {
132 rc = aio_cancel(c->fd, NULL);
134 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_cancel: %d", rc);
136 if (rc == AIO_CANCELED) {
137 c->read->active = c->write->active = 0;
141 if (rc == AIO_ALLDONE) {
142 c->read->active = c->write->active = 0;
143 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
144 "aio_cancel() returned AIO_ALLDONE");
149 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
150 "aio_cancel() failed");
154 if (rc == AIO_NOTCANCELED) {
155 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
156 "aio_cancel() returned AIO_NOTCANCELED");
166 ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
168 return ngx_kqueue_module_ctx.actions.process_events(cycle, timer, flags);
171 #endif /* NGX_HAVE_KQUEUE */