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
)
37 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
38 if (node
->e
== e
&& !node
->deleted
) {
43 /* Are we deleting the fd handler? */
46 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
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 g_source_add_poll(&ctx
->source
, &node
->pfd
);
72 /* Update handler with latest information */
73 node
->io_notify
= io_notify
;
79 bool aio_pending(AioContext
*ctx
)
83 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
84 if (node
->pfd
.revents
&& node
->io_notify
) {
92 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
95 bool progress
= false;
98 * We have to walk very carefully in case aio_set_fd_handler is
99 * called while we're walking.
101 node
= QLIST_FIRST(&ctx
->aio_handlers
);
105 ctx
->walking_handlers
++;
107 if (!node
->deleted
&&
108 (node
->pfd
.revents
|| event_notifier_get_handle(node
->e
) == event
) &&
110 node
->pfd
.revents
= 0;
111 node
->io_notify(node
->e
);
113 /* aio_notify() does not count as progress */
114 if (node
->e
!= &ctx
->notifier
) {
120 node
= QLIST_NEXT(node
, node
);
122 ctx
->walking_handlers
--;
124 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
125 QLIST_REMOVE(tmp
, node
);
133 bool aio_dispatch(AioContext
*ctx
)
137 progress
= aio_bh_poll(ctx
);
138 progress
|= aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
139 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
143 bool aio_poll(AioContext
*ctx
, bool blocking
)
146 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
147 bool was_dispatching
, progress
, first
;
151 was_dispatching
= ctx
->dispatching
;
154 /* aio_notify can avoid the expensive event_notifier_set if
155 * everything (file descriptors, bottom halves, timers) will
156 * be re-evaluated before the next blocking poll(). This is
157 * already true when aio_poll is called with blocking == false;
158 * if blocking == true, it is only true after poll() returns.
160 * If we're in a nested event loop, ctx->dispatching might be true.
161 * In that case we can restore it just before returning, but we
162 * have to clear it now.
164 aio_set_dispatching(ctx
, !blocking
);
166 ctx
->walking_handlers
++;
170 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
171 if (!node
->deleted
&& node
->io_notify
) {
172 events
[count
++] = event_notifier_get_handle(node
->e
);
176 ctx
->walking_handlers
--;
179 /* wait until next event */
184 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
185 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
186 aio_set_dispatching(ctx
, true);
188 if (first
&& aio_bh_poll(ctx
)) {
193 /* if we have any signaled events, dispatch event */
194 if ((DWORD
) (ret
- WAIT_OBJECT_0
) >= count
) {
200 progress
|= aio_dispatch_handlers(ctx
, events
[ret
- WAIT_OBJECT_0
]);
202 /* Try again, but only call each handler once. */
203 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
206 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
208 aio_set_dispatching(ctx
, was_dispatching
);