2 * QEMU aio implementation
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
14 * Contributions after 2012-01-13 are licensed under the terms of the
15 * GNU GPL, version 2 or (at your option) any later version.
18 #include "qemu-common.h"
20 #include "qemu-queue.h"
21 #include "qemu_socket.h"
25 EventNotifierHandler
*io_notify
;
26 AioFlushEventNotifierHandler
*io_flush
;
29 QLIST_ENTRY(AioHandler
) node
;
32 void aio_set_event_notifier(AioContext
*ctx
,
34 EventNotifierHandler
*io_notify
,
35 AioFlushEventNotifierHandler
*io_flush
)
39 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
40 if (node
->e
== e
&& !node
->deleted
) {
45 /* Are we deleting the fd handler? */
48 /* If the lock is held, just mark the node as deleted */
49 if (ctx
->walking_handlers
) {
51 node
->pfd
.revents
= 0;
53 /* Otherwise, delete it for real. We can't just mark it as
54 * deleted because deleted nodes are only cleaned up after
55 * releasing the walking_handlers lock.
57 QLIST_REMOVE(node
, node
);
63 /* Alloc and insert if it's not already there */
64 node
= g_malloc0(sizeof(AioHandler
));
66 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
67 node
->pfd
.events
= G_IO_IN
;
68 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
70 /* Update handler with latest information */
71 node
->io_notify
= io_notify
;
72 node
->io_flush
= io_flush
;
76 bool aio_pending(AioContext
*ctx
)
80 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
81 if (node
->pfd
.revents
&& node
->io_notify
) {
89 bool aio_poll(AioContext
*ctx
, bool blocking
)
92 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
99 * If there are callbacks left that have been queued, we need to call then.
100 * Do not call select in this case, because it is possible that the caller
101 * does not need a complete flush (as is the case for qemu_aio_wait loops).
103 if (aio_bh_poll(ctx
)) {
109 * Then dispatch any pending callbacks from the GSource.
111 * We have to walk very carefully in case qemu_aio_set_fd_handler is
112 * called while we're walking.
114 node
= QLIST_FIRST(&ctx
->aio_handlers
);
118 ctx
->walking_handlers
++;
120 if (node
->pfd
.revents
&& node
->io_notify
) {
121 node
->pfd
.revents
= 0;
122 node
->io_notify(node
->e
);
127 node
= QLIST_NEXT(node
, node
);
129 ctx
->walking_handlers
--;
131 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
132 QLIST_REMOVE(tmp
, node
);
137 if (progress
&& !blocking
) {
141 ctx
->walking_handlers
++;
146 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
147 /* If there aren't pending AIO operations, don't invoke callbacks.
148 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
151 if (!node
->deleted
&& node
->io_flush
) {
152 if (node
->io_flush(node
->e
) == 0) {
157 if (!node
->deleted
&& node
->io_notify
) {
158 events
[count
++] = event_notifier_get_handle(node
->e
);
162 ctx
->walking_handlers
--;
164 /* No AIO operations? Get us out of here */
169 /* wait until next event */
171 int timeout
= blocking
? INFINITE
: 0;
172 int ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
174 /* if we have any signaled events, dispatch event */
175 if ((DWORD
) (ret
- WAIT_OBJECT_0
) >= count
) {
181 /* we have to walk very carefully in case
182 * qemu_aio_set_fd_handler is called while we're walking */
183 node
= QLIST_FIRST(&ctx
->aio_handlers
);
187 ctx
->walking_handlers
++;
189 if (!node
->deleted
&&
190 event_notifier_get_handle(node
->e
) == events
[ret
- WAIT_OBJECT_0
] &&
192 node
->io_notify(node
->e
);
197 node
= QLIST_NEXT(node
, node
);
199 ctx
->walking_handlers
--;
201 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
202 QLIST_REMOVE(tmp
, node
);