2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
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/block.h"
18 #include "qemu/queue.h"
19 #include "qemu/sockets.h"
26 AioFlushHandler
*io_flush
;
29 QLIST_ENTRY(AioHandler
) node
;
32 static AioHandler
*find_aio_handler(AioContext
*ctx
, int fd
)
36 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
37 if (node
->pfd
.fd
== fd
)
45 void aio_set_fd_handler(AioContext
*ctx
,
49 AioFlushHandler
*io_flush
,
54 node
= find_aio_handler(ctx
, fd
);
56 /* Are we deleting the fd handler? */
57 if (!io_read
&& !io_write
) {
59 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
61 /* If the lock is held, just mark the node as deleted */
62 if (ctx
->walking_handlers
) {
64 node
->pfd
.revents
= 0;
66 /* Otherwise, delete it for real. We can't just mark it as
67 * deleted because deleted nodes are only cleaned up after
68 * releasing the walking_handlers lock.
70 QLIST_REMOVE(node
, node
);
76 /* Alloc and insert if it's not already there */
77 node
= g_malloc0(sizeof(AioHandler
));
79 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
81 g_source_add_poll(&ctx
->source
, &node
->pfd
);
83 /* Update handler with latest information */
84 node
->io_read
= io_read
;
85 node
->io_write
= io_write
;
86 node
->io_flush
= io_flush
;
87 node
->opaque
= opaque
;
89 node
->pfd
.events
= (io_read
? G_IO_IN
| G_IO_HUP
: 0);
90 node
->pfd
.events
|= (io_write
? G_IO_OUT
: 0);
96 void aio_set_event_notifier(AioContext
*ctx
,
97 EventNotifier
*notifier
,
98 EventNotifierHandler
*io_read
,
99 AioFlushEventNotifierHandler
*io_flush
)
101 aio_set_fd_handler(ctx
, event_notifier_get_fd(notifier
),
102 (IOHandler
*)io_read
, NULL
,
103 (AioFlushHandler
*)io_flush
, notifier
);
106 bool aio_pending(AioContext
*ctx
)
110 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
114 * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
115 * main-loop.c is still select based (due to the slirp legacy).
116 * If main-loop.c ever switches to poll, G_IO_ERR should be
117 * tested too. Dispatching G_IO_ERR to both handlers should be
118 * okay, since handlers need to be ready for spurious wakeups.
120 revents
= node
->pfd
.revents
& node
->pfd
.events
;
121 if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
) && node
->io_read
) {
124 if (revents
& (G_IO_OUT
| G_IO_ERR
) && node
->io_write
) {
132 bool aio_poll(AioContext
*ctx
, bool blocking
)
134 static struct timeval tv0
;
144 * If there are callbacks left that have been queued, we need to call then.
145 * Do not call select in this case, because it is possible that the caller
146 * does not need a complete flush (as is the case for qemu_aio_wait loops).
148 if (aio_bh_poll(ctx
)) {
154 * Then dispatch any pending callbacks from the GSource.
156 * We have to walk very carefully in case qemu_aio_set_fd_handler is
157 * called while we're walking.
159 node
= QLIST_FIRST(&ctx
->aio_handlers
);
164 ctx
->walking_handlers
++;
166 revents
= node
->pfd
.revents
& node
->pfd
.events
;
167 node
->pfd
.revents
= 0;
169 /* See comment in aio_pending. */
170 if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
) && node
->io_read
) {
171 node
->io_read(node
->opaque
);
174 if (revents
& (G_IO_OUT
| G_IO_ERR
) && node
->io_write
) {
175 node
->io_write(node
->opaque
);
180 node
= QLIST_NEXT(node
, node
);
182 ctx
->walking_handlers
--;
184 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
185 QLIST_REMOVE(tmp
, node
);
190 if (progress
&& !blocking
) {
194 ctx
->walking_handlers
++;
201 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
202 /* If there aren't pending AIO operations, don't invoke callbacks.
203 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
206 if (!node
->deleted
&& node
->io_flush
) {
207 if (node
->io_flush(node
->opaque
) == 0) {
212 if (!node
->deleted
&& node
->io_read
) {
213 FD_SET(node
->pfd
.fd
, &rdfds
);
214 max_fd
= MAX(max_fd
, node
->pfd
.fd
+ 1);
216 if (!node
->deleted
&& node
->io_write
) {
217 FD_SET(node
->pfd
.fd
, &wrfds
);
218 max_fd
= MAX(max_fd
, node
->pfd
.fd
+ 1);
222 ctx
->walking_handlers
--;
224 /* No AIO operations? Get us out of here */
229 /* wait until next event */
230 ret
= select(max_fd
, &rdfds
, &wrfds
, NULL
, blocking
? NULL
: &tv0
);
232 /* if we have any readable fds, dispatch event */
234 /* we have to walk very carefully in case
235 * qemu_aio_set_fd_handler is called while we're walking */
236 node
= QLIST_FIRST(&ctx
->aio_handlers
);
240 ctx
->walking_handlers
++;
242 if (!node
->deleted
&&
243 FD_ISSET(node
->pfd
.fd
, &rdfds
) &&
245 node
->io_read(node
->opaque
);
248 if (!node
->deleted
&&
249 FD_ISSET(node
->pfd
.fd
, &wrfds
) &&
251 node
->io_write(node
->opaque
);
256 node
= QLIST_NEXT(node
, node
);
258 ctx
->walking_handlers
--;
260 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
261 QLIST_REMOVE(tmp
, node
);
267 assert(progress
|| busy
);