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
) {
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
)
63 /* Otherwise, delete it for real. We can't just mark it as
64 * deleted because deleted nodes are only cleaned up after
65 * releasing the walking_handlers lock.
67 QLIST_REMOVE(node
, node
);
73 /* Alloc and insert if it's not already there */
74 node
= g_malloc0(sizeof(AioHandler
));
76 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
78 /* Update handler with latest information */
79 node
->io_read
= io_read
;
80 node
->io_write
= io_write
;
81 node
->io_flush
= io_flush
;
82 node
->opaque
= opaque
;
86 void aio_set_event_notifier(AioContext
*ctx
,
87 EventNotifier
*notifier
,
88 EventNotifierHandler
*io_read
,
89 AioFlushEventNotifierHandler
*io_flush
)
91 aio_set_fd_handler(ctx
, event_notifier_get_fd(notifier
),
92 (IOHandler
*)io_read
, NULL
,
93 (AioFlushHandler
*)io_flush
, notifier
);
96 bool aio_poll(AioContext
*ctx
, bool blocking
)
98 static struct timeval tv0
;
108 * If there are callbacks left that have been queued, we need to call then.
109 * Do not call select in this case, because it is possible that the caller
110 * does not need a complete flush (as is the case for qemu_aio_wait loops).
112 if (aio_bh_poll(ctx
)) {
117 if (progress
&& !blocking
) {
121 ctx
->walking_handlers
++;
128 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
129 /* If there aren't pending AIO operations, don't invoke callbacks.
130 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
133 if (!node
->deleted
&& node
->io_flush
) {
134 if (node
->io_flush(node
->opaque
) == 0) {
139 if (!node
->deleted
&& node
->io_read
) {
140 FD_SET(node
->fd
, &rdfds
);
141 max_fd
= MAX(max_fd
, node
->fd
+ 1);
143 if (!node
->deleted
&& node
->io_write
) {
144 FD_SET(node
->fd
, &wrfds
);
145 max_fd
= MAX(max_fd
, node
->fd
+ 1);
149 ctx
->walking_handlers
--;
151 /* No AIO operations? Get us out of here */
156 /* wait until next event */
157 ret
= select(max_fd
, &rdfds
, &wrfds
, NULL
, blocking
? NULL
: &tv0
);
159 /* if we have any readable fds, dispatch event */
161 /* we have to walk very carefully in case
162 * qemu_aio_set_fd_handler is called while we're walking */
163 node
= QLIST_FIRST(&ctx
->aio_handlers
);
167 ctx
->walking_handlers
++;
169 if (!node
->deleted
&&
170 FD_ISSET(node
->fd
, &rdfds
) &&
173 node
->io_read(node
->opaque
);
175 if (!node
->deleted
&&
176 FD_ISSET(node
->fd
, &wrfds
) &&
179 node
->io_write(node
->opaque
);
183 node
= QLIST_NEXT(node
, node
);
185 ctx
->walking_handlers
--;
187 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
188 QLIST_REMOVE(tmp
, node
);