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/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 static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
40 /* If aio_poll is in progress, just mark the node as deleted */
41 if (qemu_lockcnt_count(&ctx->list_lock)) {
43 node->pfd.revents = 0;
45 /* Otherwise, delete it for real. We can't just mark it as
46 * deleted because deleted nodes are only cleaned up after
47 * releasing the list_lock.
49 QLIST_REMOVE(node, node);
54 void aio_set_fd_handler(AioContext *ctx,
62 /* fd is a SOCKET in our case */
64 AioHandler *node = NULL;
66 qemu_lockcnt_lock(&ctx->list_lock);
67 QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {
68 if (old_node->pfd.fd == fd && !old_node->deleted) {
73 if (io_read || io_write) {
77 /* Alloc and insert if it's not already there */
78 node = g_new0(AioHandler, 1);
83 node->pfd.events |= G_IO_IN;
86 node->pfd.events |= G_IO_OUT;
89 node->e = &ctx->notifier;
91 /* Update handler with latest information */
92 node->opaque = opaque;
93 node->io_read = io_read;
94 node->io_write = io_write;
95 node->is_external = is_external;
98 bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
102 bitmask |= FD_WRITE | FD_CONNECT;
105 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
106 event = event_notifier_get_handle(&ctx->notifier);
107 WSAEventSelect(node->pfd.fd, event, bitmask);
110 aio_remove_fd_handler(ctx, old_node);
113 qemu_lockcnt_unlock(&ctx->list_lock);
117 void aio_set_fd_poll(AioContext *ctx, int fd,
118 IOHandler *io_poll_begin,
119 IOHandler *io_poll_end)
121 /* Not implemented */
124 void aio_set_event_notifier(AioContext *ctx,
127 EventNotifierHandler *io_notify,
132 qemu_lockcnt_lock(&ctx->list_lock);
133 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
134 if (node->e == e && !node->deleted) {
139 /* Are we deleting the fd handler? */
142 g_source_remove_poll(&ctx->source, &node->pfd);
144 aio_remove_fd_handler(ctx, node);
148 /* Alloc and insert if it's not already there */
149 node = g_new0(AioHandler, 1);
151 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
152 node->pfd.events = G_IO_IN;
153 node->is_external = is_external;
154 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
156 g_source_add_poll(&ctx->source, &node->pfd);
158 /* Update handler with latest information */
159 node->io_notify = io_notify;
162 qemu_lockcnt_unlock(&ctx->list_lock);
166 void aio_set_event_notifier_poll(AioContext *ctx,
167 EventNotifier *notifier,
168 EventNotifierHandler *io_poll_begin,
169 EventNotifierHandler *io_poll_end)
171 /* Not implemented */
174 bool aio_prepare(AioContext *ctx)
176 static struct timeval tv0;
178 bool have_select_revents = false;
182 * We have to walk very carefully in case aio_set_fd_handler is
183 * called while we're walking.
185 qemu_lockcnt_inc(&ctx->list_lock);
190 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
192 FD_SET ((SOCKET)node->pfd.fd, &rfds);
194 if (node->io_write) {
195 FD_SET ((SOCKET)node->pfd.fd, &wfds);
199 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
200 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
201 node->pfd.revents = 0;
202 if (FD_ISSET(node->pfd.fd, &rfds)) {
203 node->pfd.revents |= G_IO_IN;
204 have_select_revents = true;
207 if (FD_ISSET(node->pfd.fd, &wfds)) {
208 node->pfd.revents |= G_IO_OUT;
209 have_select_revents = true;
214 qemu_lockcnt_dec(&ctx->list_lock);
215 return have_select_revents;
218 bool aio_pending(AioContext *ctx)
224 * We have to walk very carefully in case aio_set_fd_handler is
225 * called while we're walking.
227 qemu_lockcnt_inc(&ctx->list_lock);
228 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
229 if (node->pfd.revents && node->io_notify) {
234 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
238 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
244 qemu_lockcnt_dec(&ctx->list_lock);
248 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
251 bool progress = false;
255 * We have to walk very carefully in case aio_set_fd_handler is
256 * called while we're walking.
258 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
259 int revents = node->pfd.revents;
261 if (!node->deleted &&
262 (revents || event_notifier_get_handle(node->e) == event) &&
264 node->pfd.revents = 0;
265 node->io_notify(node->e);
267 /* aio_notify() does not count as progress */
268 if (node->e != &ctx->notifier) {
273 if (!node->deleted &&
274 (node->io_read || node->io_write)) {
275 node->pfd.revents = 0;
276 if ((revents & G_IO_IN) && node->io_read) {
277 node->io_read(node->opaque);
280 if ((revents & G_IO_OUT) && node->io_write) {
281 node->io_write(node->opaque);
285 /* if the next select() will return an event, we have progressed */
286 if (event == event_notifier_get_handle(&ctx->notifier)) {
288 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
289 if (ev.lNetworkEvents) {
296 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
297 QLIST_REMOVE(node, node);
299 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
307 void aio_dispatch(AioContext *ctx)
309 qemu_lockcnt_inc(&ctx->list_lock);
311 aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
312 qemu_lockcnt_dec(&ctx->list_lock);
313 timerlistgroup_run_timers(&ctx->tlg);
316 bool aio_poll(AioContext *ctx, bool blocking)
319 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
320 bool progress, have_select_revents, first;
326 /* aio_notify can avoid the expensive event_notifier_set if
327 * everything (file descriptors, bottom halves, timers) will
328 * be re-evaluated before the next blocking poll(). This is
329 * already true when aio_poll is called with blocking == false;
330 * if blocking == true, it is only true after poll() returns,
331 * so disable the optimization now.
334 atomic_add(&ctx->notify_me, 2);
337 qemu_lockcnt_inc(&ctx->list_lock);
338 have_select_revents = aio_prepare(ctx);
342 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
343 if (!node->deleted && node->io_notify
344 && aio_node_check(ctx, node->is_external)) {
345 events[count++] = event_notifier_get_handle(node->e);
351 /* ctx->notifier is always registered. */
354 /* Multiple iterations, all of them non-blocking except the first,
355 * may be necessary to process all pending events. After the first
356 * WaitForMultipleObjects call ctx->notify_me will be decremented.
362 timeout = blocking && !have_select_revents
363 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
364 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
367 assert(in_aio_context_home_thread(ctx));
368 atomic_sub(&ctx->notify_me, 2);
369 aio_notify_accept(ctx);
373 progress |= aio_bh_poll(ctx);
377 /* if we have any signaled events, dispatch event */
379 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
380 event = events[ret - WAIT_OBJECT_0];
381 events[ret - WAIT_OBJECT_0] = events[--count];
382 } else if (!have_select_revents) {
386 have_select_revents = false;
389 progress |= aio_dispatch_handlers(ctx, event);
392 qemu_lockcnt_dec(&ctx->list_lock);
394 progress |= timerlistgroup_run_timers(&ctx->tlg);
398 void aio_context_setup(AioContext *ctx)
402 void aio_context_destroy(AioContext *ctx)
406 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
407 int64_t grow, int64_t shrink, Error **errp)
410 error_setg(errp, "AioContext polling is not implemented on Windows");