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.
14 #include "qemu-common.h"
16 #include "sys-queue.h"
17 #include "qemu_socket.h"
19 typedef struct AioHandler AioHandler;
21 /* The list of registered AIO handlers */
22 static LIST_HEAD(, AioHandler) aio_handlers;
24 /* This is a simple lock used to protect the aio_handlers list. Specifically,
25 * it's used to ensure that no callbacks are removed while we're walking and
26 * dispatching callbacks.
28 static int walking_handlers;
35 AioFlushHandler *io_flush;
38 LIST_ENTRY(AioHandler) node;
41 static AioHandler *find_aio_handler(int fd)
45 LIST_FOREACH(node, &aio_handlers, node) {
53 int qemu_aio_set_fd_handler(int fd,
56 AioFlushHandler *io_flush,
61 node = find_aio_handler(fd);
63 /* Are we deleting the fd handler? */
64 if (!io_read && !io_write) {
66 /* If the lock is held, just mark the node as deleted */
70 /* Otherwise, delete it for real. We can't just mark it as
71 * deleted because deleted nodes are only cleaned up after
72 * releasing the walking_handlers lock.
74 LIST_REMOVE(node, node);
80 /* Alloc and insert if it's not already there */
81 node = qemu_mallocz(sizeof(AioHandler));
83 LIST_INSERT_HEAD(&aio_handlers, node, node);
85 /* Update handler with latest information */
86 node->io_read = io_read;
87 node->io_write = io_write;
88 node->io_flush = io_flush;
89 node->opaque = opaque;
92 qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
97 void qemu_aio_flush(void)
105 LIST_FOREACH(node, &aio_handlers, node) {
106 ret |= node->io_flush(node->opaque);
113 void qemu_aio_wait(void)
125 walking_handlers = 1;
131 LIST_FOREACH(node, &aio_handlers, node) {
132 /* If there aren't pending AIO operations, don't invoke callbacks.
133 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
136 if (node->io_flush && node->io_flush(node->opaque) == 0)
139 if (!node->deleted && node->io_read) {
140 FD_SET(node->fd, &rdfds);
141 max_fd = MAX(max_fd, node->fd + 1);
143 if (!node->deleted && node->io_write) {
144 FD_SET(node->fd, &wrfds);
145 max_fd = MAX(max_fd, node->fd + 1);
149 walking_handlers = 0;
151 /* No AIO operations? Get us out of here */
155 /* wait until next event */
156 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
157 if (ret == -1 && errno == EINTR)
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 = LIST_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 = LIST_NEXT(node, node);
185 LIST_REMOVE(tmp, node);
190 walking_handlers = 0;