]> git.proxmox.com Git - qemu.git/blob - aio-posix.c
aio: add Win32 implementation
[qemu.git] / aio-posix.c
1 /*
2 * QEMU aio implementation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
15
16 #include "qemu-common.h"
17 #include "block.h"
18 #include "qemu-queue.h"
19 #include "qemu_socket.h"
20
21 struct AioHandler
22 {
23 GPollFD pfd;
24 IOHandler *io_read;
25 IOHandler *io_write;
26 AioFlushHandler *io_flush;
27 int deleted;
28 void *opaque;
29 QLIST_ENTRY(AioHandler) node;
30 };
31
32 static AioHandler *find_aio_handler(AioContext *ctx, int fd)
33 {
34 AioHandler *node;
35
36 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
37 if (node->pfd.fd == fd)
38 if (!node->deleted)
39 return node;
40 }
41
42 return NULL;
43 }
44
45 void aio_set_fd_handler(AioContext *ctx,
46 int fd,
47 IOHandler *io_read,
48 IOHandler *io_write,
49 AioFlushHandler *io_flush,
50 void *opaque)
51 {
52 AioHandler *node;
53
54 node = find_aio_handler(ctx, fd);
55
56 /* Are we deleting the fd handler? */
57 if (!io_read && !io_write) {
58 if (node) {
59 /* If the lock is held, just mark the node as deleted */
60 if (ctx->walking_handlers) {
61 node->deleted = 1;
62 node->pfd.revents = 0;
63 } else {
64 /* Otherwise, delete it for real. We can't just mark it as
65 * deleted because deleted nodes are only cleaned up after
66 * releasing the walking_handlers lock.
67 */
68 QLIST_REMOVE(node, node);
69 g_free(node);
70 }
71 }
72 } else {
73 if (node == NULL) {
74 /* Alloc and insert if it's not already there */
75 node = g_malloc0(sizeof(AioHandler));
76 node->pfd.fd = fd;
77 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
78 }
79 /* Update handler with latest information */
80 node->io_read = io_read;
81 node->io_write = io_write;
82 node->io_flush = io_flush;
83 node->opaque = opaque;
84
85 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0);
86 node->pfd.events |= (io_write ? G_IO_OUT : 0);
87 }
88 }
89
90 void aio_set_event_notifier(AioContext *ctx,
91 EventNotifier *notifier,
92 EventNotifierHandler *io_read,
93 AioFlushEventNotifierHandler *io_flush)
94 {
95 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
96 (IOHandler *)io_read, NULL,
97 (AioFlushHandler *)io_flush, notifier);
98 }
99
100 bool aio_pending(AioContext *ctx)
101 {
102 AioHandler *node;
103
104 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
105 int revents;
106
107 /*
108 * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
109 * main-loop.c is still select based (due to the slirp legacy).
110 * If main-loop.c ever switches to poll, G_IO_ERR should be
111 * tested too. Dispatching G_IO_ERR to both handlers should be
112 * okay, since handlers need to be ready for spurious wakeups.
113 */
114 revents = node->pfd.revents & node->pfd.events;
115 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
116 return true;
117 }
118 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
119 return true;
120 }
121 }
122
123 return false;
124 }
125
126 bool aio_poll(AioContext *ctx, bool blocking)
127 {
128 static struct timeval tv0;
129 AioHandler *node;
130 fd_set rdfds, wrfds;
131 int max_fd = -1;
132 int ret;
133 bool busy, progress;
134
135 progress = false;
136
137 /*
138 * If there are callbacks left that have been queued, we need to call then.
139 * Do not call select in this case, because it is possible that the caller
140 * does not need a complete flush (as is the case for qemu_aio_wait loops).
141 */
142 if (aio_bh_poll(ctx)) {
143 blocking = false;
144 progress = true;
145 }
146
147 /*
148 * Then dispatch any pending callbacks from the GSource.
149 *
150 * We have to walk very carefully in case qemu_aio_set_fd_handler is
151 * called while we're walking.
152 */
153 node = QLIST_FIRST(&ctx->aio_handlers);
154 while (node) {
155 AioHandler *tmp;
156 int revents;
157
158 ctx->walking_handlers++;
159
160 revents = node->pfd.revents & node->pfd.events;
161 node->pfd.revents = 0;
162
163 /* See comment in aio_pending. */
164 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
165 node->io_read(node->opaque);
166 progress = true;
167 }
168 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
169 node->io_write(node->opaque);
170 progress = true;
171 }
172
173 tmp = node;
174 node = QLIST_NEXT(node, node);
175
176 ctx->walking_handlers--;
177
178 if (!ctx->walking_handlers && tmp->deleted) {
179 QLIST_REMOVE(tmp, node);
180 g_free(tmp);
181 }
182 }
183
184 if (progress && !blocking) {
185 return true;
186 }
187
188 ctx->walking_handlers++;
189
190 FD_ZERO(&rdfds);
191 FD_ZERO(&wrfds);
192
193 /* fill fd sets */
194 busy = false;
195 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
196 /* If there aren't pending AIO operations, don't invoke callbacks.
197 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
198 * wait indefinitely.
199 */
200 if (!node->deleted && node->io_flush) {
201 if (node->io_flush(node->opaque) == 0) {
202 continue;
203 }
204 busy = true;
205 }
206 if (!node->deleted && node->io_read) {
207 FD_SET(node->pfd.fd, &rdfds);
208 max_fd = MAX(max_fd, node->pfd.fd + 1);
209 }
210 if (!node->deleted && node->io_write) {
211 FD_SET(node->pfd.fd, &wrfds);
212 max_fd = MAX(max_fd, node->pfd.fd + 1);
213 }
214 }
215
216 ctx->walking_handlers--;
217
218 /* No AIO operations? Get us out of here */
219 if (!busy) {
220 return progress;
221 }
222
223 /* wait until next event */
224 ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
225
226 /* if we have any readable fds, dispatch event */
227 if (ret > 0) {
228 /* we have to walk very carefully in case
229 * qemu_aio_set_fd_handler is called while we're walking */
230 node = QLIST_FIRST(&ctx->aio_handlers);
231 while (node) {
232 AioHandler *tmp;
233
234 ctx->walking_handlers++;
235
236 if (!node->deleted &&
237 FD_ISSET(node->pfd.fd, &rdfds) &&
238 node->io_read) {
239 node->io_read(node->opaque);
240 progress = true;
241 }
242 if (!node->deleted &&
243 FD_ISSET(node->pfd.fd, &wrfds) &&
244 node->io_write) {
245 node->io_write(node->opaque);
246 progress = true;
247 }
248
249 tmp = node;
250 node = QLIST_NEXT(node, node);
251
252 ctx->walking_handlers--;
253
254 if (!ctx->walking_handlers && tmp->deleted) {
255 QLIST_REMOVE(tmp, node);
256 g_free(tmp);
257 }
258 }
259 }
260
261 return progress;
262 }