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"
18 #include "qemu-queue.h"
19 #include "qemu_socket.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 /* If the lock is held, just mark the node as deleted */
60 if (ctx
->walking_handlers
) {
62 node
->pfd
.revents
= 0;
64 /* Otherwise, delete it for real. We can't just mark it as
65 * deleted because deleted nodes are only cleaned up after
66 * releasing the walking_handlers lock.
68 QLIST_REMOVE(node
, node
);
74 /* Alloc and insert if it's not already there */
75 node
= g_malloc0(sizeof(AioHandler
));
77 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
79 /* Update handler with latest information */
80 node
->io_read
= io_read
;
81 node
->io_write
= io_write
;
82 node
->io_flush
= io_flush
;
83 node
->opaque
= opaque
;
85 node
->pfd
.events
= (io_read
? G_IO_IN
| G_IO_HUP
: 0);
86 node
->pfd
.events
|= (io_write
? G_IO_OUT
: 0);
90 void aio_set_event_notifier(AioContext
*ctx
,
91 EventNotifier
*notifier
,
92 EventNotifierHandler
*io_read
,
93 AioFlushEventNotifierHandler
*io_flush
)
95 aio_set_fd_handler(ctx
, event_notifier_get_fd(notifier
),
96 (IOHandler
*)io_read
, NULL
,
97 (AioFlushHandler
*)io_flush
, notifier
);
100 bool aio_pending(AioContext
*ctx
)
104 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
108 * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
109 * main-loop.c is still select based (due to the slirp legacy).
110 * If main-loop.c ever switches to poll, G_IO_ERR should be
111 * tested too. Dispatching G_IO_ERR to both handlers should be
112 * okay, since handlers need to be ready for spurious wakeups.
114 revents
= node
->pfd
.revents
& node
->pfd
.events
;
115 if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
) && node
->io_read
) {
118 if (revents
& (G_IO_OUT
| G_IO_ERR
) && node
->io_write
) {
126 bool aio_poll(AioContext
*ctx
, bool blocking
)
128 static struct timeval tv0
;
138 * If there are callbacks left that have been queued, we need to call then.
139 * Do not call select in this case, because it is possible that the caller
140 * does not need a complete flush (as is the case for qemu_aio_wait loops).
142 if (aio_bh_poll(ctx
)) {
148 * Then dispatch any pending callbacks from the GSource.
150 * We have to walk very carefully in case qemu_aio_set_fd_handler is
151 * called while we're walking.
153 node
= QLIST_FIRST(&ctx
->aio_handlers
);
158 ctx
->walking_handlers
++;
160 revents
= node
->pfd
.revents
& node
->pfd
.events
;
161 node
->pfd
.revents
= 0;
163 /* See comment in aio_pending. */
164 if (revents
& (G_IO_IN
| G_IO_HUP
| G_IO_ERR
) && node
->io_read
) {
165 node
->io_read(node
->opaque
);
168 if (revents
& (G_IO_OUT
| G_IO_ERR
) && node
->io_write
) {
169 node
->io_write(node
->opaque
);
174 node
= QLIST_NEXT(node
, node
);
176 ctx
->walking_handlers
--;
178 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
179 QLIST_REMOVE(tmp
, node
);
184 if (progress
&& !blocking
) {
188 ctx
->walking_handlers
++;
195 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
196 /* If there aren't pending AIO operations, don't invoke callbacks.
197 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
200 if (!node
->deleted
&& node
->io_flush
) {
201 if (node
->io_flush(node
->opaque
) == 0) {
206 if (!node
->deleted
&& node
->io_read
) {
207 FD_SET(node
->pfd
.fd
, &rdfds
);
208 max_fd
= MAX(max_fd
, node
->pfd
.fd
+ 1);
210 if (!node
->deleted
&& node
->io_write
) {
211 FD_SET(node
->pfd
.fd
, &wrfds
);
212 max_fd
= MAX(max_fd
, node
->pfd
.fd
+ 1);
216 ctx
->walking_handlers
--;
218 /* No AIO operations? Get us out of here */
223 /* wait until next event */
224 ret
= select(max_fd
, &rdfds
, &wrfds
, NULL
, blocking
? NULL
: &tv0
);
226 /* if we have any readable fds, dispatch event */
228 /* we have to walk very carefully in case
229 * qemu_aio_set_fd_handler is called while we're walking */
230 node
= QLIST_FIRST(&ctx
->aio_handlers
);
234 ctx
->walking_handlers
++;
236 if (!node
->deleted
&&
237 FD_ISSET(node
->pfd
.fd
, &rdfds
) &&
239 node
->io_read(node
->opaque
);
242 if (!node
->deleted
&&
243 FD_ISSET(node
->pfd
.fd
, &wrfds
) &&
245 node
->io_write(node
->opaque
);
250 node
= QLIST_NEXT(node
, node
);
252 ctx
->walking_handlers
--;
254 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
255 QLIST_REMOVE(tmp
, node
);