i8254: Do not raise IRQ level on reset
[qemu-kvm.git] / aio.c
blobeb3bf42574fd07c480cea8ed0f2674293fbe3217
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 AioProcessQueue *io_process_queue;
39 int deleted;
40 void *opaque;
41 QLIST_ENTRY(AioHandler) node;
44 static AioHandler *find_aio_handler(int fd)
46 AioHandler *node;
48 QLIST_FOREACH(node, &aio_handlers, node) {
49 if (node->fd == fd)
50 if (!node->deleted)
51 return node;
54 return NULL;
57 int qemu_aio_set_fd_handler(int fd,
58 IOHandler *io_read,
59 IOHandler *io_write,
60 AioFlushHandler *io_flush,
61 AioProcessQueue *io_process_queue,
62 void *opaque)
64 AioHandler *node;
66 node = find_aio_handler(fd);
68 /* Are we deleting the fd handler? */
69 if (!io_read && !io_write) {
70 if (node) {
71 /* If the lock is held, just mark the node as deleted */
72 if (walking_handlers)
73 node->deleted = 1;
74 else {
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);
80 g_free(node);
83 } else {
84 if (node == NULL) {
85 /* Alloc and insert if it's not already there */
86 node = g_malloc0(sizeof(AioHandler));
87 node->fd = fd;
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);
100 return 0;
103 void qemu_aio_flush(void)
105 AioHandler *node;
106 int ret;
108 do {
109 ret = 0;
112 * If there are pending emulated aio start them now so flush
113 * will be able to return 1.
115 qemu_aio_wait();
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)
127 AioHandler *node;
128 int ret = 0;
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)) {
135 ret = 1;
140 walking_handlers = 0;
142 return ret;
145 void qemu_aio_wait(void)
147 int ret;
149 if (qemu_bh_poll())
150 return;
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())
157 return;
159 do {
160 AioHandler *node;
161 fd_set rdfds, wrfds;
162 int max_fd = -1;
164 walking_handlers = 1;
166 FD_ZERO(&rdfds);
167 FD_ZERO(&wrfds);
169 /* fill fd sets */
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
173 * wait indefinitely.
175 if (node->io_flush && node->io_flush(node->opaque) == 0)
176 continue;
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 */
191 if (max_fd == -1)
192 break;
194 /* wait until next event */
195 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
196 if (ret == -1 && errno == EINTR)
197 continue;
199 /* if we have any readable fds, dispatch event */
200 if (ret > 0) {
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);
206 while (node) {
207 AioHandler *tmp;
209 if (!node->deleted &&
210 FD_ISSET(node->fd, &rdfds) &&
211 node->io_read) {
212 node->io_read(node->opaque);
214 if (!node->deleted &&
215 FD_ISSET(node->fd, &wrfds) &&
216 node->io_write) {
217 node->io_write(node->opaque);
220 tmp = node;
221 node = QLIST_NEXT(node, node);
223 if (tmp->deleted) {
224 QLIST_REMOVE(tmp, node);
225 g_free(tmp);
229 walking_handlers = 0;
231 } while (ret == 0);