AioContext: introduce aio_prepare
[qemu/ar7.git] / aio-win32.c
blob45422704a466b7bf9807c50b1df730c8b6e78f71
1 /*
2 * QEMU aio implementation
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
7 * Authors:
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"
23 struct AioHandler {
24 EventNotifier *e;
25 EventNotifierHandler *io_notify;
26 GPollFD pfd;
27 int deleted;
28 QLIST_ENTRY(AioHandler) node;
31 void aio_set_event_notifier(AioContext *ctx,
32 EventNotifier *e,
33 EventNotifierHandler *io_notify)
35 AioHandler *node;
37 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
38 if (node->e == e && !node->deleted) {
39 break;
43 /* Are we deleting the fd handler? */
44 if (!io_notify) {
45 if (node) {
46 g_source_remove_poll(&ctx->source, &node->pfd);
48 /* If the lock is held, just mark the node as deleted */
49 if (ctx->walking_handlers) {
50 node->deleted = 1;
51 node->pfd.revents = 0;
52 } else {
53 /* Otherwise, delete it for real. We can't just mark it as
54 * deleted because deleted nodes are only cleaned up after
55 * releasing the walking_handlers lock.
57 QLIST_REMOVE(node, node);
58 g_free(node);
61 } else {
62 if (node == NULL) {
63 /* Alloc and insert if it's not already there */
64 node = g_malloc0(sizeof(AioHandler));
65 node->e = e;
66 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
67 node->pfd.events = G_IO_IN;
68 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
70 g_source_add_poll(&ctx->source, &node->pfd);
72 /* Update handler with latest information */
73 node->io_notify = io_notify;
76 aio_notify(ctx);
79 bool aio_prepare(AioContext *ctx)
81 return false;
84 bool aio_pending(AioContext *ctx)
86 AioHandler *node;
88 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
89 if (node->pfd.revents && node->io_notify) {
90 return true;
94 return false;
97 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
99 AioHandler *node;
100 bool progress = false;
103 * We have to walk very carefully in case aio_set_fd_handler is
104 * called while we're walking.
106 node = QLIST_FIRST(&ctx->aio_handlers);
107 while (node) {
108 AioHandler *tmp;
110 ctx->walking_handlers++;
112 if (!node->deleted &&
113 (node->pfd.revents || event_notifier_get_handle(node->e) == event) &&
114 node->io_notify) {
115 node->pfd.revents = 0;
116 node->io_notify(node->e);
118 /* aio_notify() does not count as progress */
119 if (node->e != &ctx->notifier) {
120 progress = true;
124 tmp = node;
125 node = QLIST_NEXT(node, node);
127 ctx->walking_handlers--;
129 if (!ctx->walking_handlers && tmp->deleted) {
130 QLIST_REMOVE(tmp, node);
131 g_free(tmp);
135 return progress;
138 bool aio_dispatch(AioContext *ctx)
140 bool progress;
142 progress = aio_bh_poll(ctx);
143 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
144 progress |= timerlistgroup_run_timers(&ctx->tlg);
145 return progress;
148 bool aio_poll(AioContext *ctx, bool blocking)
150 AioHandler *node;
151 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
152 bool was_dispatching, progress, first;
153 int count;
154 int timeout;
156 was_dispatching = ctx->dispatching;
157 progress = false;
159 /* aio_notify can avoid the expensive event_notifier_set if
160 * everything (file descriptors, bottom halves, timers) will
161 * be re-evaluated before the next blocking poll(). This is
162 * already true when aio_poll is called with blocking == false;
163 * if blocking == true, it is only true after poll() returns.
165 * If we're in a nested event loop, ctx->dispatching might be true.
166 * In that case we can restore it just before returning, but we
167 * have to clear it now.
169 aio_set_dispatching(ctx, !blocking);
171 ctx->walking_handlers++;
173 /* fill fd sets */
174 count = 0;
175 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
176 if (!node->deleted && node->io_notify) {
177 events[count++] = event_notifier_get_handle(node->e);
181 ctx->walking_handlers--;
182 first = true;
184 /* wait until next event */
185 while (count > 0) {
186 int ret;
188 timeout = blocking
189 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
190 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
191 aio_set_dispatching(ctx, true);
193 if (first && aio_bh_poll(ctx)) {
194 progress = true;
196 first = false;
198 /* if we have any signaled events, dispatch event */
199 if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
200 break;
203 blocking = false;
205 progress |= aio_dispatch_handlers(ctx, events[ret - WAIT_OBJECT_0]);
207 /* Try again, but only call each handler once. */
208 events[ret - WAIT_OBJECT_0] = events[--count];
211 progress |= timerlistgroup_run_timers(&ctx->tlg);
213 aio_set_dispatching(ctx, was_dispatching);
214 return progress;