]> git.proxmox.com Git - qemu.git/blame - aio.c
aio: change qemu_aio_set_fd_handler to return void
[qemu.git] / aio.c
CommitLineData
a76bab49
AL
1/*
2 * QEMU aio implementation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
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.
a76bab49
AL
14 */
15
16#include "qemu-common.h"
17#include "block.h"
72cf2d4f 18#include "qemu-queue.h"
a76bab49
AL
19#include "qemu_socket.h"
20
21typedef struct AioHandler AioHandler;
22
23/* The list of registered AIO handlers */
72cf2d4f 24static QLIST_HEAD(, AioHandler) aio_handlers;
a76bab49
AL
25
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.
29 */
30static int walking_handlers;
31
32struct AioHandler
33{
34 int fd;
35 IOHandler *io_read;
36 IOHandler *io_write;
37 AioFlushHandler *io_flush;
38 int deleted;
39 void *opaque;
72cf2d4f 40 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
41};
42
43static AioHandler *find_aio_handler(int fd)
44{
45 AioHandler *node;
46
72cf2d4f 47 QLIST_FOREACH(node, &aio_handlers, node) {
a76bab49 48 if (node->fd == fd)
79d5ca56
AG
49 if (!node->deleted)
50 return node;
a76bab49
AL
51 }
52
53 return NULL;
54}
55
b078dc3c
PB
56void qemu_aio_set_fd_handler(int fd,
57 IOHandler *io_read,
58 IOHandler *io_write,
59 AioFlushHandler *io_flush,
60 void *opaque)
a76bab49
AL
61{
62 AioHandler *node;
63
64 node = find_aio_handler(fd);
65
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.
76 */
72cf2d4f 77 QLIST_REMOVE(node, node);
7267c094 78 g_free(node);
a76bab49
AL
79 }
80 }
81 } else {
82 if (node == NULL) {
83 /* Alloc and insert if it's not already there */
7267c094 84 node = g_malloc0(sizeof(AioHandler));
a76bab49 85 node->fd = fd;
72cf2d4f 86 QLIST_INSERT_HEAD(&aio_handlers, node, node);
a76bab49
AL
87 }
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;
93 }
94
95 qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
a76bab49
AL
96}
97
98void qemu_aio_flush(void)
99{
bcdc1857 100 while (qemu_aio_wait());
a76bab49
AL
101}
102
bcdc1857 103bool qemu_aio_wait(void)
a76bab49 104{
9eb0bfca
PB
105 AioHandler *node;
106 fd_set rdfds, wrfds;
107 int max_fd = -1;
a76bab49 108 int ret;
9eb0bfca 109 bool busy;
a76bab49 110
8febfa26
KW
111 /*
112 * If there are callbacks left that have been queued, we need to call then.
bcdc1857
PB
113 * Do not call select in this case, because it is possible that the caller
114 * does not need a complete flush (as is the case for qemu_aio_wait loops).
8febfa26 115 */
bafbd6a1 116 if (qemu_bh_poll()) {
bcdc1857 117 return true;
bafbd6a1 118 }
8febfa26 119
00f78533 120 walking_handlers++;
a76bab49 121
9eb0bfca
PB
122 FD_ZERO(&rdfds);
123 FD_ZERO(&wrfds);
a76bab49 124
9eb0bfca
PB
125 /* fill fd sets */
126 busy = false;
127 QLIST_FOREACH(node, &aio_handlers, node) {
128 /* If there aren't pending AIO operations, don't invoke callbacks.
129 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
130 * wait indefinitely.
131 */
132 if (node->io_flush) {
133 if (node->io_flush(node->opaque) == 0) {
134 continue;
a76bab49 135 }
9eb0bfca
PB
136 busy = true;
137 }
138 if (!node->deleted && node->io_read) {
139 FD_SET(node->fd, &rdfds);
140 max_fd = MAX(max_fd, node->fd + 1);
a76bab49 141 }
9eb0bfca
PB
142 if (!node->deleted && node->io_write) {
143 FD_SET(node->fd, &wrfds);
144 max_fd = MAX(max_fd, node->fd + 1);
145 }
146 }
a76bab49 147
00f78533 148 walking_handlers--;
a76bab49 149
9eb0bfca
PB
150 /* No AIO operations? Get us out of here */
151 if (!busy) {
152 return false;
153 }
a76bab49 154
9eb0bfca
PB
155 /* wait until next event */
156 ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
157
158 /* if we have any readable fds, dispatch event */
159 if (ret > 0) {
9eb0bfca
PB
160 /* we have to walk very carefully in case
161 * qemu_aio_set_fd_handler is called while we're walking */
162 node = QLIST_FIRST(&aio_handlers);
163 while (node) {
164 AioHandler *tmp;
165
2db2bfc0
PB
166 walking_handlers++;
167
9eb0bfca
PB
168 if (!node->deleted &&
169 FD_ISSET(node->fd, &rdfds) &&
170 node->io_read) {
171 node->io_read(node->opaque);
172 }
173 if (!node->deleted &&
174 FD_ISSET(node->fd, &wrfds) &&
175 node->io_write) {
176 node->io_write(node->opaque);
a76bab49
AL
177 }
178
9eb0bfca
PB
179 tmp = node;
180 node = QLIST_NEXT(node, node);
181
2db2bfc0
PB
182 walking_handlers--;
183
184 if (!walking_handlers && tmp->deleted) {
9eb0bfca
PB
185 QLIST_REMOVE(tmp, node);
186 g_free(tmp);
187 }
a76bab49 188 }
9eb0bfca 189 }
bcdc1857
PB
190
191 return true;
a76bab49 192}