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
;
32 QLIST_ENTRY(AioHandler
) node
;
35 void aio_set_fd_handler(AioContext
*ctx
,
42 /* fd is a SOCKET in our case */
45 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
46 if (node
->pfd
.fd
== fd
&& !node
->deleted
) {
51 /* Are we deleting the fd handler? */
52 if (!io_read
&& !io_write
) {
54 /* If the lock is held, just mark the node as deleted */
55 if (ctx
->walking_handlers
) {
57 node
->pfd
.revents
= 0;
59 /* Otherwise, delete it for real. We can't just mark it as
60 * deleted because deleted nodes are only cleaned up after
61 * releasing the walking_handlers lock.
63 QLIST_REMOVE(node
, node
);
71 /* Alloc and insert if it's not already there */
72 node
= g_new0(AioHandler
, 1);
74 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
79 node
->pfd
.events
|= G_IO_IN
;
82 node
->pfd
.events
|= G_IO_OUT
;
85 node
->e
= &ctx
->notifier
;
87 /* Update handler with latest information */
88 node
->opaque
= opaque
;
89 node
->io_read
= io_read
;
90 node
->io_write
= io_write
;
91 node
->is_external
= is_external
;
93 event
= event_notifier_get_handle(&ctx
->notifier
);
94 WSAEventSelect(node
->pfd
.fd
, event
,
95 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
96 FD_CONNECT
| FD_WRITE
| FD_OOB
);
102 void aio_set_event_notifier(AioContext
*ctx
,
105 EventNotifierHandler
*io_notify
)
109 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
110 if (node
->e
== e
&& !node
->deleted
) {
115 /* Are we deleting the fd handler? */
118 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
120 /* If the lock is held, just mark the node as deleted */
121 if (ctx
->walking_handlers
) {
123 node
->pfd
.revents
= 0;
125 /* Otherwise, delete it for real. We can't just mark it as
126 * deleted because deleted nodes are only cleaned up after
127 * releasing the walking_handlers lock.
129 QLIST_REMOVE(node
, node
);
135 /* Alloc and insert if it's not already there */
136 node
= g_new0(AioHandler
, 1);
138 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
139 node
->pfd
.events
= G_IO_IN
;
140 node
->is_external
= is_external
;
141 QLIST_INSERT_HEAD(&ctx
->aio_handlers
, node
, node
);
143 g_source_add_poll(&ctx
->source
, &node
->pfd
);
145 /* Update handler with latest information */
146 node
->io_notify
= io_notify
;
152 bool aio_prepare(AioContext
*ctx
)
154 static struct timeval tv0
;
156 bool have_select_revents
= false;
162 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
164 FD_SET ((SOCKET
)node
->pfd
.fd
, &rfds
);
166 if (node
->io_write
) {
167 FD_SET ((SOCKET
)node
->pfd
.fd
, &wfds
);
171 if (select(0, &rfds
, &wfds
, NULL
, &tv0
) > 0) {
172 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
173 node
->pfd
.revents
= 0;
174 if (FD_ISSET(node
->pfd
.fd
, &rfds
)) {
175 node
->pfd
.revents
|= G_IO_IN
;
176 have_select_revents
= true;
179 if (FD_ISSET(node
->pfd
.fd
, &wfds
)) {
180 node
->pfd
.revents
|= G_IO_OUT
;
181 have_select_revents
= true;
186 return have_select_revents
;
189 bool aio_pending(AioContext
*ctx
)
193 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
194 if (node
->pfd
.revents
&& node
->io_notify
) {
198 if ((node
->pfd
.revents
& G_IO_IN
) && node
->io_read
) {
201 if ((node
->pfd
.revents
& G_IO_OUT
) && node
->io_write
) {
209 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
212 bool progress
= false;
215 * We have to walk very carefully in case aio_set_fd_handler is
216 * called while we're walking.
218 node
= QLIST_FIRST(&ctx
->aio_handlers
);
221 int revents
= node
->pfd
.revents
;
223 ctx
->walking_handlers
++;
225 if (!node
->deleted
&&
226 (revents
|| event_notifier_get_handle(node
->e
) == event
) &&
228 node
->pfd
.revents
= 0;
229 node
->io_notify(node
->e
);
231 /* aio_notify() does not count as progress */
232 if (node
->e
!= &ctx
->notifier
) {
237 if (!node
->deleted
&&
238 (node
->io_read
|| node
->io_write
)) {
239 node
->pfd
.revents
= 0;
240 if ((revents
& G_IO_IN
) && node
->io_read
) {
241 node
->io_read(node
->opaque
);
244 if ((revents
& G_IO_OUT
) && node
->io_write
) {
245 node
->io_write(node
->opaque
);
249 /* if the next select() will return an event, we have progressed */
250 if (event
== event_notifier_get_handle(&ctx
->notifier
)) {
252 WSAEnumNetworkEvents(node
->pfd
.fd
, event
, &ev
);
253 if (ev
.lNetworkEvents
) {
260 node
= QLIST_NEXT(node
, node
);
262 ctx
->walking_handlers
--;
264 if (!ctx
->walking_handlers
&& tmp
->deleted
) {
265 QLIST_REMOVE(tmp
, node
);
273 bool aio_dispatch(AioContext
*ctx
)
277 progress
= aio_bh_poll(ctx
);
278 progress
|= aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
279 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
283 bool aio_poll(AioContext
*ctx
, bool blocking
)
286 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
287 bool progress
, have_select_revents
, first
;
291 aio_context_acquire(ctx
);
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,
299 * so disable the optimization now.
302 atomic_add(&ctx
->notify_me
, 2);
305 have_select_revents
= aio_prepare(ctx
);
307 ctx
->walking_handlers
++;
311 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
312 if (!node
->deleted
&& node
->io_notify
313 && aio_node_check(ctx
, node
->is_external
)) {
314 events
[count
++] = event_notifier_get_handle(node
->e
);
318 ctx
->walking_handlers
--;
321 /* ctx->notifier is always registered. */
324 /* Multiple iterations, all of them non-blocking except the first,
325 * may be necessary to process all pending events. After the first
326 * WaitForMultipleObjects call ctx->notify_me will be decremented.
332 timeout
= blocking
&& !have_select_revents
333 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
335 aio_context_release(ctx
);
337 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
340 atomic_sub(&ctx
->notify_me
, 2);
343 aio_context_acquire(ctx
);
347 aio_notify_accept(ctx
);
348 progress
|= aio_bh_poll(ctx
);
352 /* if we have any signaled events, dispatch event */
354 if ((DWORD
) (ret
- WAIT_OBJECT_0
) < count
) {
355 event
= events
[ret
- WAIT_OBJECT_0
];
356 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
357 } else if (!have_select_revents
) {
361 have_select_revents
= false;
364 progress
|= aio_dispatch_handlers(ctx
, event
);
367 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
369 aio_context_release(ctx
);