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;
40 QLIST_ENTRY(AioHandler) node;
43 static AioHandler *find_aio_handler(int fd)
47 QLIST_FOREACH(node, &aio_handlers, node) {
56 int qemu_aio_set_fd_handler(int fd,
59 AioFlushHandler *io_flush,
64 node = find_aio_handler(fd);
66 /* Are we deleting the fd handler? */
67 if (!io_read && !io_write) {
69 /* If the lock is held, just mark the node as deleted */
73 /* Otherwise, delete it for real. We can't just mark it as
74 * deleted because deleted nodes are only cleaned up after
75 * releasing the walking_handlers lock.
77 QLIST_REMOVE(node, node);
83 /* Alloc and insert if it's not already there */
84 node = g_malloc0(sizeof(AioHandler));
86 QLIST_INSERT_HEAD(&aio_handlers, node, node);
88 /* Update handler with latest information */
89 node->io_read = io_read;
90 node->io_write = io_write;
91 node->io_flush = io_flush;
92 node->opaque = opaque;
95 qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
100 void qemu_aio_flush(void)
102 while (qemu_aio_wait());
105 bool qemu_aio_wait(void)
114 * If there are callbacks left that have been queued, we need to call then.
115 * Do not call select in this case, because it is possible that the caller
116 * does not need a complete flush (as is the case for qemu_aio_wait loops).
118 if (qemu_bh_poll()) {
122 walking_handlers = 1;
129 QLIST_FOREACH(node, &aio_handlers, node) {
130 /* If there aren't pending AIO operations, don't invoke callbacks.
131 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
134 if (node->io_flush) {
135 if (node->io_flush(node->opaque) == 0) {
140 if (!node->deleted && node->io_read) {
141 FD_SET(node->fd, &rdfds);
142 max_fd = MAX(max_fd, node->fd + 1);
144 if (!node->deleted && node->io_write) {
145 FD_SET(node->fd, &wrfds);
146 max_fd = MAX(max_fd, node->fd + 1);
150 walking_handlers = 0;
152 /* No AIO operations? Get us out of here */
157 /* wait until next event */
158 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
160 /* if we have any readable fds, dispatch event */
162 walking_handlers = 1;
164 /* we have to walk very carefully in case
165 * qemu_aio_set_fd_handler is called while we're walking */
166 node = QLIST_FIRST(&aio_handlers);
170 if (!node->deleted &&
171 FD_ISSET(node->fd, &rdfds) &&
173 node->io_read(node->opaque);
175 if (!node->deleted &&
176 FD_ISSET(node->fd, &wrfds) &&
178 node->io_write(node->opaque);
182 node = QLIST_NEXT(node, node);
185 QLIST_REMOVE(tmp, node);
190 walking_handlers = 0;