2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu-common.h"
18 #include "qemu-queue.h"
19 #include "qemu_socket.h"
21 typedef struct AioHandler AioHandler;
23 /* The list of registered AIO handlers */
24 static QLIST_HEAD(, AioHandler) aio_handlers;
26 /* This is a simple lock used to protect the aio_handlers list. Specifically,
27 * it's used to ensure that no callbacks are removed while we're walking and
28 * dispatching callbacks.
30 static int walking_handlers;
37 AioFlushHandler *io_flush;
38 AioProcessQueue *io_process_queue;
41 QLIST_ENTRY(AioHandler) node;
44 static AioHandler *find_aio_handler(int fd)
48 QLIST_FOREACH(node, &aio_handlers, node) {
57 int qemu_aio_set_fd_handler(int fd,
60 AioFlushHandler *io_flush,
61 AioProcessQueue *io_process_queue,
66 node = find_aio_handler(fd);
68 /* Are we deleting the fd handler? */
69 if (!io_read && !io_write) {
71 /* If the lock is held, just mark the node as deleted */
75 /* Otherwise, delete it for real. We can't just mark it as
76 * deleted because deleted nodes are only cleaned up after
77 * releasing the walking_handlers lock.
79 QLIST_REMOVE(node, node);
85 /* Alloc and insert if it's not already there */
86 node = g_malloc0(sizeof(AioHandler));
88 QLIST_INSERT_HEAD(&aio_handlers, node, node);
90 /* Update handler with latest information */
91 node->io_read = io_read;
92 node->io_write = io_write;
93 node->io_flush = io_flush;
94 node->io_process_queue = io_process_queue;
95 node->opaque = opaque;
98 qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
103 void qemu_aio_flush(void)
112 * If there are pending emulated aio start them now so flush
113 * will be able to return 1.
117 QLIST_FOREACH(node, &aio_handlers, node) {
118 if (node->io_flush) {
119 ret |= node->io_flush(node->opaque);
122 } while (qemu_bh_poll() || ret > 0);
125 int qemu_aio_process_queue(void)
130 walking_handlers = 1;
132 QLIST_FOREACH(node, &aio_handlers, node) {
133 if (node->io_process_queue) {
134 if (node->io_process_queue(node->opaque)) {
140 walking_handlers = 0;
145 void qemu_aio_wait(void)
153 * If there are callbacks left that have been queued, we need to call then.
154 * Return afterwards to avoid waiting needlessly in select().
156 if (qemu_aio_process_queue())
164 walking_handlers = 1;
170 QLIST_FOREACH(node, &aio_handlers, node) {
171 /* If there aren't pending AIO operations, don't invoke callbacks.
172 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
175 if (node->io_flush && node->io_flush(node->opaque) == 0)
178 if (!node->deleted && node->io_read) {
179 FD_SET(node->fd, &rdfds);
180 max_fd = MAX(max_fd, node->fd + 1);
182 if (!node->deleted && node->io_write) {
183 FD_SET(node->fd, &wrfds);
184 max_fd = MAX(max_fd, node->fd + 1);
188 walking_handlers = 0;
190 /* No AIO operations? Get us out of here */
194 /* wait until next event */
195 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
196 if (ret == -1 && errno == EINTR)
199 /* if we have any readable fds, dispatch event */
201 walking_handlers = 1;
203 /* we have to walk very carefully in case
204 * qemu_aio_set_fd_handler is called while we're walking */
205 node = QLIST_FIRST(&aio_handlers);
209 if (!node->deleted &&
210 FD_ISSET(node->fd, &rdfds) &&
212 node->io_read(node->opaque);
214 if (!node->deleted &&
215 FD_ISSET(node->fd, &wrfds) &&
217 node->io_write(node->opaque);
221 node = QLIST_NEXT(node, node);
224 QLIST_REMOVE(tmp, node);
229 walking_handlers = 0;