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"
19 #include "block/block.h"
20 #include "qemu/queue.h"
21 #include "qemu/sockets.h"
25 EventNotifierHandler
*io_notify
;
28 QLIST_ENTRY(AioHandler
) node
;
31 void aio_set_event_notifier(AioContext
*ctx
,
33 EventNotifierHandler
*io_notify
,
34 AioFlushEventNotifierHandler
*io_flush
)
38 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
39 if (node
->e
== e
&& !node
->deleted
) {
44 /* Are we deleting the fd handler? */
47 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
49 /* If the lock is held, just mark the node as deleted */
50 if (ctx
->walking_handlers
) {
52 node
->pfd
.revents
= 0;
54 /* Otherwise, delete it for real. We can't just mark it as
55 * deleted because deleted nodes are only cleaned up after
56 * releasing the walking_handlers lock.
58 QLIST_REMOVE(node
, node
);
64 /* Alloc and insert if it's not already there */
65 node
= g_malloc0(sizeof(AioHandler
));
67 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
68 node
->pfd
.events
= G_IO_IN
;
69 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
71 g_source_add_poll(&ctx
->source
, &node
->pfd
);
73 /* Update handler with latest information */
74 node
->io_notify
= io_notify
;
80 bool aio_pending(AioContext
*ctx
)
84 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
85 if (node
->pfd
.revents
&& node
->io_notify
) {
93 bool aio_poll(AioContext
*ctx
, bool blocking
)
96 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
103 * If there are callbacks left that have been queued, we need to call then.
104 * Do not call select in this case, because it is possible that the caller
105 * does not need a complete flush (as is the case for qemu_aio_wait loops).
107 if (aio_bh_poll(ctx
)) {
113 * Then dispatch any pending callbacks from the GSource.
115 * We have to walk very carefully in case qemu_aio_set_fd_handler is
116 * called while we're walking.
118 node
= QLIST_FIRST(&ctx
->aio_handlers
);
122 ctx
->walking_handlers
++;
124 if (node
->pfd
.revents
&& node
->io_notify
) {
125 node
->pfd
.revents
= 0;
126 node
->io_notify(node
->e
);
128 /* aio_notify() does not count as progress */
129 if (node
->opaque
!= &ctx
->notifier
) {
135 node
= QLIST_NEXT(node
, node
);
137 ctx
->walking_handlers
--;
139 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
140 QLIST_REMOVE(tmp
, node
);
145 if (progress
&& !blocking
) {
149 ctx
->walking_handlers
++;
153 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
154 if (!node
->deleted
&& node
->io_notify
) {
155 events
[count
++] = event_notifier_get_handle(node
->e
);
159 ctx
->walking_handlers
--;
161 /* early return if we only have the aio_notify() fd */
166 /* wait until next event */
168 int timeout
= blocking
? INFINITE
: 0;
169 int ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
171 /* if we have any signaled events, dispatch event */
172 if ((DWORD
) (ret
- WAIT_OBJECT_0
) >= count
) {
178 /* we have to walk very carefully in case
179 * qemu_aio_set_fd_handler is called while we're walking */
180 node
= QLIST_FIRST(&ctx
->aio_handlers
);
184 ctx
->walking_handlers
++;
186 if (!node
->deleted
&&
187 event_notifier_get_handle(node
->e
) == events
[ret
- WAIT_OBJECT_0
] &&
189 node
->io_notify(node
->e
);
191 /* aio_notify() does not count as progress */
192 if (node
->opaque
!= &ctx
->notifier
) {
198 node
= QLIST_NEXT(node
, node
);
200 ctx
->walking_handlers
--;
202 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
203 QLIST_REMOVE(tmp
, node
);
208 /* Try again, but only call each handler once. */
209 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];