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/osdep.h"
19 #include "qemu-common.h"
20 #include "block/block.h"
21 #include "qemu/queue.h"
22 #include "qemu/sockets.h"
23 #include "qapi/error.h"
24 #include "qemu/rcu_queue.h"
30 EventNotifierHandler
*io_notify
;
35 QLIST_ENTRY(AioHandler
) node
;
38 void aio_set_fd_handler(AioContext
*ctx
,
46 /* fd is a SOCKET in our case */
49 qemu_lockcnt_lock(&ctx
->list_lock
);
50 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
51 if (node
->pfd
.fd
== fd
&& !node
->deleted
) {
56 /* Are we deleting the fd handler? */
57 if (!io_read
&& !io_write
) {
59 /* If aio_poll is in progress, just mark the node as deleted */
60 if (qemu_lockcnt_count(&ctx
->list_lock
)) {
62 node
->pfd
.revents
= 0;
64 /* Otherwise, delete it for real. We can't just mark it as
65 * deleted because deleted nodes are only cleaned up after
66 * releasing the list_lock.
68 QLIST_REMOVE(node
, node
);
76 /* Alloc and insert if it's not already there */
77 node
= g_new0(AioHandler
, 1);
79 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
84 node
->pfd
.events
|= G_IO_IN
;
87 node
->pfd
.events
|= G_IO_OUT
;
90 node
->e
= &ctx
->notifier
;
92 /* Update handler with latest information */
93 node
->opaque
= opaque
;
94 node
->io_read
= io_read
;
95 node
->io_write
= io_write
;
96 node
->is_external
= is_external
;
98 event
= event_notifier_get_handle(&ctx
->notifier
);
99 WSAEventSelect(node
->pfd
.fd
, event
,
100 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
101 FD_CONNECT
| FD_WRITE
| FD_OOB
);
104 qemu_lockcnt_unlock(&ctx
->list_lock
);
108 void aio_set_fd_poll(AioContext
*ctx
, int fd
,
109 IOHandler
*io_poll_begin
,
110 IOHandler
*io_poll_end
)
112 /* Not implemented */
115 void aio_set_event_notifier(AioContext
*ctx
,
118 EventNotifierHandler
*io_notify
,
123 qemu_lockcnt_lock(&ctx
->list_lock
);
124 QLIST_FOREACH(node
, &ctx
->aio_handlers
, node
) {
125 if (node
->e
== e
&& !node
->deleted
) {
130 /* Are we deleting the fd handler? */
133 g_source_remove_poll(&ctx
->source
, &node
->pfd
);
135 /* aio_poll is in progress, just mark the node as deleted */
136 if (qemu_lockcnt_count(&ctx
->list_lock
)) {
138 node
->pfd
.revents
= 0;
140 /* Otherwise, delete it for real. We can't just mark it as
141 * deleted because deleted nodes are only cleaned up after
142 * releasing the list_lock.
144 QLIST_REMOVE(node
, node
);
150 /* Alloc and insert if it's not already there */
151 node
= g_new0(AioHandler
, 1);
153 node
->pfd
.fd
= (uintptr_t)event_notifier_get_handle(e
);
154 node
->pfd
.events
= G_IO_IN
;
155 node
->is_external
= is_external
;
156 QLIST_INSERT_HEAD_RCU(&ctx
->aio_handlers
, node
, node
);
158 g_source_add_poll(&ctx
->source
, &node
->pfd
);
160 /* Update handler with latest information */
161 node
->io_notify
= io_notify
;
164 qemu_lockcnt_unlock(&ctx
->list_lock
);
168 void aio_set_event_notifier_poll(AioContext
*ctx
,
169 EventNotifier
*notifier
,
170 EventNotifierHandler
*io_poll_begin
,
171 EventNotifierHandler
*io_poll_end
)
173 /* Not implemented */
176 bool aio_prepare(AioContext
*ctx
)
178 static struct timeval tv0
;
180 bool have_select_revents
= false;
184 * We have to walk very carefully in case aio_set_fd_handler is
185 * called while we're walking.
187 qemu_lockcnt_inc(&ctx
->list_lock
);
192 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
194 FD_SET ((SOCKET
)node
->pfd
.fd
, &rfds
);
196 if (node
->io_write
) {
197 FD_SET ((SOCKET
)node
->pfd
.fd
, &wfds
);
201 if (select(0, &rfds
, &wfds
, NULL
, &tv0
) > 0) {
202 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
203 node
->pfd
.revents
= 0;
204 if (FD_ISSET(node
->pfd
.fd
, &rfds
)) {
205 node
->pfd
.revents
|= G_IO_IN
;
206 have_select_revents
= true;
209 if (FD_ISSET(node
->pfd
.fd
, &wfds
)) {
210 node
->pfd
.revents
|= G_IO_OUT
;
211 have_select_revents
= true;
216 qemu_lockcnt_dec(&ctx
->list_lock
);
217 return have_select_revents
;
220 bool aio_pending(AioContext
*ctx
)
226 * We have to walk very carefully in case aio_set_fd_handler is
227 * called while we're walking.
229 qemu_lockcnt_inc(&ctx
->list_lock
);
230 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
231 if (node
->pfd
.revents
&& node
->io_notify
) {
236 if ((node
->pfd
.revents
& G_IO_IN
) && node
->io_read
) {
240 if ((node
->pfd
.revents
& G_IO_OUT
) && node
->io_write
) {
246 qemu_lockcnt_dec(&ctx
->list_lock
);
250 static bool aio_dispatch_handlers(AioContext
*ctx
, HANDLE event
)
253 bool progress
= false;
256 qemu_lockcnt_inc(&ctx
->list_lock
);
259 * We have to walk very carefully in case aio_set_fd_handler is
260 * called while we're walking.
262 QLIST_FOREACH_SAFE_RCU(node
, &ctx
->aio_handlers
, node
, tmp
) {
263 int revents
= node
->pfd
.revents
;
265 if (!node
->deleted
&&
266 (revents
|| event_notifier_get_handle(node
->e
) == event
) &&
268 node
->pfd
.revents
= 0;
269 node
->io_notify(node
->e
);
271 /* aio_notify() does not count as progress */
272 if (node
->e
!= &ctx
->notifier
) {
277 if (!node
->deleted
&&
278 (node
->io_read
|| node
->io_write
)) {
279 node
->pfd
.revents
= 0;
280 if ((revents
& G_IO_IN
) && node
->io_read
) {
281 node
->io_read(node
->opaque
);
284 if ((revents
& G_IO_OUT
) && node
->io_write
) {
285 node
->io_write(node
->opaque
);
289 /* if the next select() will return an event, we have progressed */
290 if (event
== event_notifier_get_handle(&ctx
->notifier
)) {
292 WSAEnumNetworkEvents(node
->pfd
.fd
, event
, &ev
);
293 if (ev
.lNetworkEvents
) {
300 if (qemu_lockcnt_dec_if_lock(&ctx
->list_lock
)) {
301 QLIST_REMOVE(node
, node
);
303 qemu_lockcnt_inc_and_unlock(&ctx
->list_lock
);
308 qemu_lockcnt_dec(&ctx
->list_lock
);
312 void aio_dispatch(AioContext
*ctx
)
315 aio_dispatch_handlers(ctx
, INVALID_HANDLE_VALUE
);
316 timerlistgroup_run_timers(&ctx
->tlg
);
319 bool aio_poll(AioContext
*ctx
, bool blocking
)
322 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
323 bool progress
, have_select_revents
, first
;
329 /* aio_notify can avoid the expensive event_notifier_set if
330 * everything (file descriptors, bottom halves, timers) will
331 * be re-evaluated before the next blocking poll(). This is
332 * already true when aio_poll is called with blocking == false;
333 * if blocking == true, it is only true after poll() returns,
334 * so disable the optimization now.
337 atomic_add(&ctx
->notify_me
, 2);
340 qemu_lockcnt_inc(&ctx
->list_lock
);
341 have_select_revents
= aio_prepare(ctx
);
345 QLIST_FOREACH_RCU(node
, &ctx
->aio_handlers
, node
) {
346 if (!node
->deleted
&& node
->io_notify
347 && aio_node_check(ctx
, node
->is_external
)) {
348 events
[count
++] = event_notifier_get_handle(node
->e
);
352 qemu_lockcnt_dec(&ctx
->list_lock
);
355 /* ctx->notifier is always registered. */
358 /* Multiple iterations, all of them non-blocking except the first,
359 * may be necessary to process all pending events. After the first
360 * WaitForMultipleObjects call ctx->notify_me will be decremented.
366 timeout
= blocking
&& !have_select_revents
367 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx
)) : 0;
368 ret
= WaitForMultipleObjects(count
, events
, FALSE
, timeout
);
371 atomic_sub(&ctx
->notify_me
, 2);
375 aio_notify_accept(ctx
);
376 progress
|= aio_bh_poll(ctx
);
380 /* if we have any signaled events, dispatch event */
382 if ((DWORD
) (ret
- WAIT_OBJECT_0
) < count
) {
383 event
= events
[ret
- WAIT_OBJECT_0
];
384 events
[ret
- WAIT_OBJECT_0
] = events
[--count
];
385 } else if (!have_select_revents
) {
389 have_select_revents
= false;
392 progress
|= aio_dispatch_handlers(ctx
, event
);
395 progress
|= timerlistgroup_run_timers(&ctx
->tlg
);
399 void aio_context_setup(AioContext
*ctx
)
403 void aio_context_set_poll_params(AioContext
*ctx
, int64_t max_ns
,
404 int64_t grow
, int64_t shrink
, Error
**errp
)
406 error_setg(errp
, "AioContext polling is not implemented on Windows");