2 * QEMU aio implementation
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
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/main-loop.h"
22 #include "qemu/queue.h"
23 #include "qemu/sockets.h"
24 #include "qapi/error.h"
25 #include "qemu/rcu_queue.h"
31 EventNotifierHandler *io_notify;
36 QLIST_ENTRY(AioHandler) node;
39 static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
42 * If the GSource is in the process of being destroyed then
43 * g_source_remove_poll() causes an assertion failure. Skip
44 * removal in that case, because glib cleans up its state during
47 if (!g_source_is_destroyed(&ctx->source)) {
48 g_source_remove_poll(&ctx->source, &node->pfd);
51 /* If aio_poll is in progress, just mark the node as deleted */
52 if (qemu_lockcnt_count(&ctx->list_lock)) {
54 node->pfd.revents = 0;
56 /* Otherwise, delete it for real. We can't just mark it as
57 * deleted because deleted nodes are only cleaned up after
58 * releasing the list_lock.
60 QLIST_REMOVE(node, node);
65 void aio_set_fd_handler(AioContext *ctx,
73 /* fd is a SOCKET in our case */
75 AioHandler *node = NULL;
77 qemu_lockcnt_lock(&ctx->list_lock);
78 QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {
79 if (old_node->pfd.fd == fd && !old_node->deleted) {
84 if (io_read || io_write) {
88 /* Alloc and insert if it's not already there */
89 node = g_new0(AioHandler, 1);
94 node->pfd.events |= G_IO_IN;
97 node->pfd.events |= G_IO_OUT;
100 node->e = &ctx->notifier;
102 /* Update handler with latest information */
103 node->opaque = opaque;
104 node->io_read = io_read;
105 node->io_write = io_write;
106 node->is_external = is_external;
109 bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
113 bitmask |= FD_WRITE | FD_CONNECT;
116 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
117 event = event_notifier_get_handle(&ctx->notifier);
118 WSAEventSelect(node->pfd.fd, event, bitmask);
121 aio_remove_fd_handler(ctx, old_node);
124 qemu_lockcnt_unlock(&ctx->list_lock);
128 void aio_set_fd_poll(AioContext *ctx, int fd,
129 IOHandler *io_poll_begin,
130 IOHandler *io_poll_end)
132 /* Not implemented */
135 void aio_set_event_notifier(AioContext *ctx,
138 EventNotifierHandler *io_notify,
143 qemu_lockcnt_lock(&ctx->list_lock);
144 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
145 if (node->e == e && !node->deleted) {
150 /* Are we deleting the fd handler? */
153 aio_remove_fd_handler(ctx, node);
157 /* Alloc and insert if it's not already there */
158 node = g_new0(AioHandler, 1);
160 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
161 node->pfd.events = G_IO_IN;
162 node->is_external = is_external;
163 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
165 g_source_add_poll(&ctx->source, &node->pfd);
167 /* Update handler with latest information */
168 node->io_notify = io_notify;
171 qemu_lockcnt_unlock(&ctx->list_lock);
175 void aio_set_event_notifier_poll(AioContext *ctx,
176 EventNotifier *notifier,
177 EventNotifierHandler *io_poll_begin,
178 EventNotifierHandler *io_poll_end)
180 /* Not implemented */
183 bool aio_prepare(AioContext *ctx)
185 static struct timeval tv0;
187 bool have_select_revents = false;
191 * We have to walk very carefully in case aio_set_fd_handler is
192 * called while we're walking.
194 qemu_lockcnt_inc(&ctx->list_lock);
199 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
201 FD_SET ((SOCKET)node->pfd.fd, &rfds);
203 if (node->io_write) {
204 FD_SET ((SOCKET)node->pfd.fd, &wfds);
208 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
209 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
210 node->pfd.revents = 0;
211 if (FD_ISSET(node->pfd.fd, &rfds)) {
212 node->pfd.revents |= G_IO_IN;
213 have_select_revents = true;
216 if (FD_ISSET(node->pfd.fd, &wfds)) {
217 node->pfd.revents |= G_IO_OUT;
218 have_select_revents = true;
223 qemu_lockcnt_dec(&ctx->list_lock);
224 return have_select_revents;
227 bool aio_pending(AioContext *ctx)
233 * We have to walk very carefully in case aio_set_fd_handler is
234 * called while we're walking.
236 qemu_lockcnt_inc(&ctx->list_lock);
237 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
238 if (node->pfd.revents && node->io_notify) {
243 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
247 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
253 qemu_lockcnt_dec(&ctx->list_lock);
257 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
260 bool progress = false;
264 * We have to walk very carefully in case aio_set_fd_handler is
265 * called while we're walking.
267 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
268 int revents = node->pfd.revents;
270 if (!node->deleted &&
271 (revents || event_notifier_get_handle(node->e) == event) &&
273 node->pfd.revents = 0;
274 node->io_notify(node->e);
276 /* aio_notify() does not count as progress */
277 if (node->e != &ctx->notifier) {
282 if (!node->deleted &&
283 (node->io_read || node->io_write)) {
284 node->pfd.revents = 0;
285 if ((revents & G_IO_IN) && node->io_read) {
286 node->io_read(node->opaque);
289 if ((revents & G_IO_OUT) && node->io_write) {
290 node->io_write(node->opaque);
294 /* if the next select() will return an event, we have progressed */
295 if (event == event_notifier_get_handle(&ctx->notifier)) {
297 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
298 if (ev.lNetworkEvents) {
305 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
306 QLIST_REMOVE(node, node);
308 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
316 void aio_dispatch(AioContext *ctx)
318 qemu_lockcnt_inc(&ctx->list_lock);
320 aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
321 qemu_lockcnt_dec(&ctx->list_lock);
322 timerlistgroup_run_timers(&ctx->tlg);
325 bool aio_poll(AioContext *ctx, bool blocking)
328 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
329 bool progress, have_select_revents, first;
334 * There cannot be two concurrent aio_poll calls for the same AioContext (or
335 * an aio_poll concurrent with a GSource prepare/check/dispatch callback).
336 * We rely on this below to avoid slow locked accesses to ctx->notify_me.
338 * aio_poll() may only be called in the AioContext's thread. iohandler_ctx
339 * is special in that it runs in the main thread, but that thread's context
340 * is qemu_aio_context.
342 assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
343 qemu_get_aio_context() : ctx));
346 /* aio_notify can avoid the expensive event_notifier_set if
347 * everything (file descriptors, bottom halves, timers) will
348 * be re-evaluated before the next blocking poll(). This is
349 * already true when aio_poll is called with blocking == false;
350 * if blocking == true, it is only true after poll() returns,
351 * so disable the optimization now.
354 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
356 * Write ctx->notify_me before computing the timeout
357 * (reading bottom half flags, etc.). Pairs with
358 * smp_mb in aio_notify().
363 qemu_lockcnt_inc(&ctx->list_lock);
364 have_select_revents = aio_prepare(ctx);
368 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
369 if (!node->deleted && node->io_notify
370 && aio_node_check(ctx, node->is_external)) {
371 events[count++] = event_notifier_get_handle(node->e);
377 /* ctx->notifier is always registered. */
380 /* Multiple iterations, all of them non-blocking except the first,
381 * may be necessary to process all pending events. After the first
382 * WaitForMultipleObjects call ctx->notify_me will be decremented.
388 timeout = blocking && !have_select_revents
389 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
390 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
393 qatomic_store_release(&ctx->notify_me,
394 qatomic_read(&ctx->notify_me) - 2);
395 aio_notify_accept(ctx);
399 progress |= aio_bh_poll(ctx);
403 /* if we have any signaled events, dispatch event */
405 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
406 event = events[ret - WAIT_OBJECT_0];
407 events[ret - WAIT_OBJECT_0] = events[--count];
408 } else if (!have_select_revents) {
412 have_select_revents = false;
415 progress |= aio_dispatch_handlers(ctx, event);
418 qemu_lockcnt_dec(&ctx->list_lock);
420 progress |= timerlistgroup_run_timers(&ctx->tlg);
424 void aio_context_setup(AioContext *ctx)
428 void aio_context_destroy(AioContext *ctx)
432 void aio_context_use_g_source(AioContext *ctx)
436 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
437 int64_t grow, int64_t shrink, Error **errp)
440 error_setg(errp, "AioContext polling is not implemented on Windows");