lsi: check ssid versus sdid only if ssid is valid
[qemu/ar7.git] / aio-win32.c
blobf9cfbb75aca8ad9af7b50fc0338065f286dc81f8
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_pending(AioContext *ctx)
81 AioHandler *node;
83 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
84 if (node->pfd.revents && node->io_notify) {
85 return true;
89 return false;
92 bool aio_poll(AioContext *ctx, bool blocking)
94 AioHandler *node;
95 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
96 bool progress;
97 int count;
98 int timeout;
100 progress = false;
103 * If there are callbacks left that have been queued, we need to call then.
104 * Do not call select in this case, because it is possible that the caller
105 * does not need a complete flush (as is the case for qemu_aio_wait loops).
107 if (aio_bh_poll(ctx)) {
108 blocking = false;
109 progress = true;
112 /* Run timers */
113 progress |= timerlistgroup_run_timers(&ctx->tlg);
116 * Then dispatch any pending callbacks from the GSource.
118 * We have to walk very carefully in case qemu_aio_set_fd_handler is
119 * called while we're walking.
121 node = QLIST_FIRST(&ctx->aio_handlers);
122 while (node) {
123 AioHandler *tmp;
125 ctx->walking_handlers++;
127 if (node->pfd.revents && node->io_notify) {
128 node->pfd.revents = 0;
129 node->io_notify(node->e);
131 /* aio_notify() does not count as progress */
132 if (node->e != &ctx->notifier) {
133 progress = true;
137 tmp = node;
138 node = QLIST_NEXT(node, node);
140 ctx->walking_handlers--;
142 if (!ctx->walking_handlers && tmp->deleted) {
143 QLIST_REMOVE(tmp, node);
144 g_free(tmp);
148 if (progress && !blocking) {
149 return true;
152 ctx->walking_handlers++;
154 /* fill fd sets */
155 count = 0;
156 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
157 if (!node->deleted && node->io_notify) {
158 events[count++] = event_notifier_get_handle(node->e);
162 ctx->walking_handlers--;
164 /* early return if we only have the aio_notify() fd */
165 if (count == 1) {
166 return progress;
169 /* wait until next event */
170 while (count > 0) {
171 int ret;
173 timeout = blocking ?
174 qemu_timeout_ns_to_ms(timerlistgroup_deadline_ns(&ctx->tlg)) : 0;
175 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
177 /* if we have any signaled events, dispatch event */
178 if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
179 break;
182 blocking = false;
184 /* we have to walk very carefully in case
185 * qemu_aio_set_fd_handler is called while we're walking */
186 node = QLIST_FIRST(&ctx->aio_handlers);
187 while (node) {
188 AioHandler *tmp;
190 ctx->walking_handlers++;
192 if (!node->deleted &&
193 event_notifier_get_handle(node->e) == events[ret - WAIT_OBJECT_0] &&
194 node->io_notify) {
195 node->io_notify(node->e);
197 /* aio_notify() does not count as progress */
198 if (node->e != &ctx->notifier) {
199 progress = true;
203 tmp = node;
204 node = QLIST_NEXT(node, node);
206 ctx->walking_handlers--;
208 if (!ctx->walking_handlers && tmp->deleted) {
209 QLIST_REMOVE(tmp, node);
210 g_free(tmp);
214 /* Try again, but only call each handler once. */
215 events[ret - WAIT_OBJECT_0] = events[--count];
218 if (blocking) {
219 /* Run the timers a second time. We do this because otherwise aio_wait
220 * will not note progress - and will stop a drain early - if we have
221 * a timer that was not ready to run entering g_poll but is ready
222 * after g_poll. This will only do anything if a timer has expired.
224 progress |= timerlistgroup_run_timers(&ctx->tlg);
227 return progress;