aio: provide platform-independent API
[qemu/ar7.git] / aio.c
blob44214e1ffcf3645aedc502e9848752fa8795ab9e
1 /*
2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
17 #include "block.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;
32 struct AioHandler
34 int fd;
35 IOHandler *io_read;
36 IOHandler *io_write;
37 AioFlushHandler *io_flush;
38 int deleted;
39 void *opaque;
40 QLIST_ENTRY(AioHandler) node;
43 static AioHandler *find_aio_handler(int fd)
45 AioHandler *node;
47 QLIST_FOREACH(node, &aio_handlers, node) {
48 if (node->fd == fd)
49 if (!node->deleted)
50 return node;
53 return NULL;
56 void qemu_aio_set_fd_handler(int fd,
57 IOHandler *io_read,
58 IOHandler *io_write,
59 AioFlushHandler *io_flush,
60 void *opaque)
62 AioHandler *node;
64 node = find_aio_handler(fd);
66 /* Are we deleting the fd handler? */
67 if (!io_read && !io_write) {
68 if (node) {
69 /* If the lock is held, just mark the node as deleted */
70 if (walking_handlers)
71 node->deleted = 1;
72 else {
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);
78 g_free(node);
81 } else {
82 if (node == NULL) {
83 /* Alloc and insert if it's not already there */
84 node = g_malloc0(sizeof(AioHandler));
85 node->fd = fd;
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);
98 void qemu_aio_set_event_notifier(EventNotifier *notifier,
99 EventNotifierHandler *io_read,
100 AioFlushEventNotifierHandler *io_flush)
102 qemu_aio_set_fd_handler(event_notifier_get_fd(notifier),
103 (IOHandler *)io_read, NULL,
104 (AioFlushHandler *)io_flush, notifier);
107 void qemu_aio_flush(void)
109 while (qemu_aio_wait());
112 bool qemu_aio_wait(void)
114 AioHandler *node;
115 fd_set rdfds, wrfds;
116 int max_fd = -1;
117 int ret;
118 bool busy;
121 * If there are callbacks left that have been queued, we need to call then.
122 * Do not call select in this case, because it is possible that the caller
123 * does not need a complete flush (as is the case for qemu_aio_wait loops).
125 if (qemu_bh_poll()) {
126 return true;
129 walking_handlers++;
131 FD_ZERO(&rdfds);
132 FD_ZERO(&wrfds);
134 /* fill fd sets */
135 busy = false;
136 QLIST_FOREACH(node, &aio_handlers, node) {
137 /* If there aren't pending AIO operations, don't invoke callbacks.
138 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
139 * wait indefinitely.
141 if (node->io_flush) {
142 if (node->io_flush(node->opaque) == 0) {
143 continue;
145 busy = true;
147 if (!node->deleted && node->io_read) {
148 FD_SET(node->fd, &rdfds);
149 max_fd = MAX(max_fd, node->fd + 1);
151 if (!node->deleted && node->io_write) {
152 FD_SET(node->fd, &wrfds);
153 max_fd = MAX(max_fd, node->fd + 1);
157 walking_handlers--;
159 /* No AIO operations? Get us out of here */
160 if (!busy) {
161 return false;
164 /* wait until next event */
165 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
167 /* if we have any readable fds, dispatch event */
168 if (ret > 0) {
169 /* we have to walk very carefully in case
170 * qemu_aio_set_fd_handler is called while we're walking */
171 node = QLIST_FIRST(&aio_handlers);
172 while (node) {
173 AioHandler *tmp;
175 walking_handlers++;
177 if (!node->deleted &&
178 FD_ISSET(node->fd, &rdfds) &&
179 node->io_read) {
180 node->io_read(node->opaque);
182 if (!node->deleted &&
183 FD_ISSET(node->fd, &wrfds) &&
184 node->io_write) {
185 node->io_write(node->opaque);
188 tmp = node;
189 node = QLIST_NEXT(node, node);
191 walking_handlers--;
193 if (!walking_handlers && tmp->deleted) {
194 QLIST_REMOVE(tmp, node);
195 g_free(tmp);
200 return true;