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"
27 EventNotifierHandler
*io_notify
;
31 QLIST_ENTRY(AioHandler
) node
;
34 void aio_set_fd_handler(AioContext
*ctx
,
40 /* fd is a SOCKET in our case */
43 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
44 if (node
->pfd
.fd
== fd
&& !node
->deleted
) {
49 /* Are we deleting the fd handler? */
50 if (!io_read
&& !io_write
) {
52 /* If the lock is held, just mark the node as deleted */
53 if (ctx
->walking_handlers
) {
55 node
->pfd
.revents
= 0;
57 /* Otherwise, delete it for real. We can't just mark it as
58 * deleted because deleted nodes are only cleaned up after
59 * releasing the walking_handlers lock.
61 QLIST_REMOVE(node
, node
);
69 /* Alloc and insert if it's not already there */
70 node
= g_malloc0(sizeof(AioHandler
));
72 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
77 node
->pfd
.events
|= G_IO_IN
;
80 node
->pfd
.events
|= G_IO_OUT
;
83 node
->e
= &ctx
->notifier
;
85 /* Update handler with latest information */
86 node
->opaque
= opaque
;
87 node
->io_read
= io_read
;
88 node
->io_write
= io_write
;
90 event
= event_notifier_get_handle(&ctx
->notifier
);
91 WSAEventSelect(node
->pfd
.fd
, event
,
92 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
93 FD_CONNECT
| FD_WRITE
| FD_OOB
);
99 void aio_set_event_notifier(AioContext
*ctx
,
101 EventNotifierHandler
*io_notify
)
105 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
106 if (node
->e
== e
&& !node
->deleted
) {
111 /* Are we deleting the fd handler? */
114 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
116 /* If the lock is held, just mark the node as deleted */
117 if (ctx
->walking_handlers
) {
119 node
->pfd
.revents
= 0;
121 /* Otherwise, delete it for real. We can't just mark it as
122 * deleted because deleted nodes are only cleaned up after
123 * releasing the walking_handlers lock.
125 QLIST_REMOVE(node
, node
);
131 /* Alloc and insert if it's not already there */
132 node
= g_malloc0(sizeof(AioHandler
));
134 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
135 node
->pfd
.events
= G_IO_IN
;
136 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
138 g_source_add_poll(&ctx
->source
, &node
->pfd
);
140 /* Update handler with latest information */
141 node
->io_notify
= io_notify
;
147 bool aio_prepare(AioContext
*ctx
)
149 static struct timeval tv0
;
151 bool have_select_revents
= false;
157 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
159 FD_SET ((SOCKET
)node
->pfd
.fd
, &rfds
);
161 if (node
->io_write
) {
162 FD_SET ((SOCKET
)node
->pfd
.fd
, &wfds
);
166 if (select(0, &rfds
, &wfds
, NULL
, &tv0
) > 0) {
167 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
168 node
->pfd
.revents
= 0;
169 if (FD_ISSET(node
->pfd
.fd
, &rfds
)) {
170 node
->pfd
.revents
|= G_IO_IN
;
171 have_select_revents
= true;
174 if (FD_ISSET(node
->pfd
.fd
, &wfds
)) {
175 node
->pfd
.revents
|= G_IO_OUT
;
176 have_select_revents
= true;
181 return have_select_revents
;
184 bool aio_pending(AioContext
*ctx
)
188 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
189 if (node
->pfd
.revents
&& node
->io_notify
) {
193 if ((node
->pfd
.revents
& G_IO_IN
) && node
->io_read
) {
196 if ((node
->pfd
.revents
& G_IO_OUT
) && node
->io_write
) {
204 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
207 bool progress
= false;
210 * We have to walk very carefully in case aio_set_fd_handler is
211 * called while we're walking.
213 node
= QLIST_FIRST(&ctx
->aio_handlers
);
216 int revents
= node
->pfd
.revents
;
218 ctx
->walking_handlers
++;
220 if (!node
->deleted
&&
221 (revents
|| event_notifier_get_handle(node
->e
) == event
) &&
223 node
->pfd
.revents
= 0;
224 node
->io_notify(node
->e
);
226 /* aio_notify() does not count as progress */
227 if (node
->e
!= &ctx
->notifier
) {
232 if (!node
->deleted
&&
233 (node
->io_read
|| node
->io_write
)) {
234 node
->pfd
.revents
= 0;
235 if ((revents
& G_IO_IN
) && node
->io_read
) {
236 node
->io_read(node
->opaque
);
239 if ((revents
& G_IO_OUT
) && node
->io_write
) {
240 node
->io_write(node
->opaque
);
244 /* if the next select() will return an event, we have progressed */
245 if (event
== event_notifier_get_handle(&ctx
->notifier
)) {
247 WSAEnumNetworkEvents(node
->pfd
.fd
, event
, &ev
);
248 if (ev
.lNetworkEvents
) {
255 node
= QLIST_NEXT(node
, node
);
257 ctx
->walking_handlers
--;
259 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
260 QLIST_REMOVE(tmp
, node
);
268 bool aio_dispatch(AioContext
*ctx
)
272 progress
= aio_bh_poll(ctx
);
273 progress
|= aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
274 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
278 bool aio_poll(AioContext
*ctx
, bool blocking
)
281 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
282 bool was_dispatching
, progress
, have_select_revents
, first
;
286 have_select_revents
= aio_prepare(ctx
);
287 if (have_select_revents
) {
291 was_dispatching
= ctx
->dispatching
;
294 /* aio_notify can avoid the expensive event_notifier_set if
295 * everything (file descriptors, bottom halves, timers) will
296 * be re-evaluated before the next blocking poll(). This is
297 * already true when aio_poll is called with blocking == false;
298 * if blocking == true, it is only true after poll() returns.
300 * If we're in a nested event loop, ctx->dispatching might be true.
301 * In that case we can restore it just before returning, but we
302 * have to clear it now.
304 aio_set_dispatching(ctx
, !blocking
);
306 ctx
->walking_handlers
++;
310 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
311 if (!node
->deleted
&& node
->io_notify
) {
312 events
[count
++] = event_notifier_get_handle(node
->e
);
316 ctx
->walking_handlers
--;
319 /* wait until next event */
325 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
326 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
327 aio_set_dispatching(ctx
, true);
329 if (first
&& aio_bh_poll(ctx
)) {
334 /* if we have any signaled events, dispatch event */
336 if ((DWORD
) (ret
- WAIT_OBJECT_0
) < count
) {
337 event
= events
[ret
- WAIT_OBJECT_0
];
338 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
339 } else if (!have_select_revents
) {
343 have_select_revents
= false;
346 progress
|= aio_dispatch_handlers(ctx
, event
);
349 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
351 aio_set_dispatching(ctx
, was_dispatching
);