1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10
11
12 extern ngx_event_module_t ngx_kqueue_module_ctx;
13
14
15 static ngx_int_t ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer);
16 static void ngx_aio_done(ngx_cycle_t *cycle);
17 static ngx_int_t ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event,
18 ngx_uint_t flags);
19 static ngx_int_t ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event,
20 ngx_uint_t flags);
21 static ngx_int_t ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags);
22 static ngx_int_t ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
23 ngx_uint_t flags);
24
25
26 ngx_os_io_t ngx_os_aio = {
27 ngx_aio_read,
28 ngx_aio_read_chain,
29 NULL,
30 ngx_aio_write,
31 ngx_aio_write_chain,
32 0
33 };
34
35
36 static ngx_str_t aio_name = ngx_string("aio");
37
38 ngx_event_module_t ngx_aio_module_ctx = {
39 &aio_name,
40 NULL, /* create configuration */
41 NULL, /* init configuration */
42
43 {
44 ngx_aio_add_event, /* add an event */
45 ngx_aio_del_event, /* delete an event */
46 NULL, /* enable an event */
47 NULL, /* disable an event */
48 NULL, /* add an connection */
49 ngx_aio_del_connection, /* delete an connection */
50 NULL, /* process the changes */
51 ngx_aio_process_events, /* process the events */
52 ngx_aio_init, /* init the events */
53 ngx_aio_done /* done the events */
54 }
55
56 };
57
58 ngx_module_t ngx_aio_module = {
59 NGX_MODULE_V1,
60 &ngx_aio_module_ctx, /* module context */
61 NULL, /* module directives */
62 NGX_EVENT_MODULE, /* module type */
63 NULL, /* init master */
64 NULL, /* init module */
65 NULL, /* init process */
66 NULL, /* init thread */
67 NULL, /* exit thread */
68 NULL, /* exit process */
69 NULL, /* exit master */
70 NGX_MODULE_V1_PADDING
71 };
72
73
74 #if (NGX_HAVE_KQUEUE)
75
76 static ngx_int_t
77 ngx_aio_init(ngx_cycle_t *cycle, ngx_msec_t timer)
78 {
79 if (ngx_kqueue_module_ctx.actions.init(cycle, timer) == NGX_ERROR) {
80 return NGX_ERROR;
81 }
82
83 ngx_io = ngx_os_aio;
84
85 ngx_event_flags = NGX_USE_AIO_EVENT;
86 ngx_event_actions = ngx_aio_module_ctx.actions;
87
88
89 return NGX_OK;
90 }
91
92
93 static void
94 ngx_aio_done(ngx_cycle_t *cycle)
95 {
96 ngx_kqueue_module_ctx.actions.done(cycle);
97 }
98
99
100 /* the event adding and deleting are needed for the listening sockets */
101
102 static ngx_int_t
103 ngx_aio_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
104 {
105 return ngx_kqueue_module_ctx.actions.add(ev, event, flags);
106 }
107
108
109 static ngx_int_t
110 ngx_aio_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
111 {
112 return ngx_kqueue_module_ctx.actions.del(ev, event, flags);
113 }
114
115
116 static ngx_int_t
117 ngx_aio_del_connection(ngx_connection_t *c, ngx_uint_t flags)
118 {
119 int rc;
120
121 if (c->read->active == 0 && c->write->active == 0) {
122 return NGX_OK;
123 }
124
125 if (flags & NGX_CLOSE_EVENT) {
126 return NGX_OK;
127 }
128
129 rc = aio_cancel(c->fd, NULL);
130
131 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "aio_cancel: %d", rc);
132
133 if (rc == AIO_CANCELED) {
134 c->read->active = 0;
135 c->write->active = 0;
136 return NGX_OK;
137 }
138
139 if (rc == AIO_ALLDONE) {
140 c->read->active = 0;
141 c->write->active = 0;
142 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
143 "aio_cancel() returned AIO_ALLDONE");
144 return NGX_OK;
145 }
146
147 if (rc == -1) {
148 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
149 "aio_cancel() failed");
150 return NGX_ERROR;
151 }
152
153 if (rc == AIO_NOTCANCELED) {
154 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
155 "aio_cancel() returned AIO_NOTCANCELED");
156
157 return NGX_ERROR;
158 }
159
160 return NGX_OK;
161 }
162
163
164 static ngx_int_t
165 ngx_aio_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
166 {
167 return ngx_kqueue_module_ctx.actions.process_events(cycle, timer, flags);
168 }
169
170 #endif /* NGX_HAVE_KQUEUE */
171
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.