]> git.proxmox.com Git - qemu.git/blame - aio-win32.c
qdev-properties-system.c: Allow vlan or netdev for -device, not both
[qemu.git] / aio-win32.c
CommitLineData
a76bab49
AL
1/*
2 * QEMU aio implementation
3 *
f42b2207
PB
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
a76bab49
AL
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
f42b2207 9 * Paolo Bonzini <pbonzini@redhat.com>
a76bab49
AL
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
6b620ca3
PB
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.
a76bab49
AL
16 */
17
18#include "qemu-common.h"
737e150e 19#include "block/block.h"
1de7afc9
PB
20#include "qemu/queue.h"
21#include "qemu/sockets.h"
a76bab49 22
f42b2207
PB
23struct AioHandler {
24 EventNotifier *e;
25 EventNotifierHandler *io_notify;
cd9ba1eb 26 GPollFD pfd;
a76bab49 27 int deleted;
72cf2d4f 28 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
29};
30
f42b2207
PB
31void aio_set_event_notifier(AioContext *ctx,
32 EventNotifier *e,
f2e5dca4 33 EventNotifierHandler *io_notify)
a76bab49
AL
34{
35 AioHandler *node;
36
a915f4bc 37 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
f42b2207
PB
38 if (node->e == e && !node->deleted) {
39 break;
40 }
a76bab49
AL
41 }
42
a76bab49 43 /* Are we deleting the fd handler? */
f42b2207 44 if (!io_notify) {
a76bab49 45 if (node) {
e3713e00
PB
46 g_source_remove_poll(&ctx->source, &node->pfd);
47
a76bab49 48 /* If the lock is held, just mark the node as deleted */
cd9ba1eb 49 if (ctx->walking_handlers) {
a76bab49 50 node->deleted = 1;
cd9ba1eb
PB
51 node->pfd.revents = 0;
52 } else {
a76bab49
AL
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.
56 */
72cf2d4f 57 QLIST_REMOVE(node, node);
7267c094 58 g_free(node);
a76bab49
AL
59 }
60 }
61 } else {
62 if (node == NULL) {
63 /* Alloc and insert if it's not already there */
7267c094 64 node = g_malloc0(sizeof(AioHandler));
f42b2207
PB
65 node->e = e;
66 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
67 node->pfd.events = G_IO_IN;
a915f4bc 68 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
e3713e00
PB
69
70 g_source_add_poll(&ctx->source, &node->pfd);
a76bab49
AL
71 }
72 /* Update handler with latest information */
f42b2207 73 node->io_notify = io_notify;
a76bab49 74 }
7ed2b24c
PB
75
76 aio_notify(ctx);
9958c351
PB
77}
78
cd9ba1eb
PB
79bool aio_pending(AioContext *ctx)
80{
81 AioHandler *node;
82
83 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
f42b2207 84 if (node->pfd.revents && node->io_notify) {
cd9ba1eb
PB
85 return true;
86 }
87 }
88
89 return false;
90}
91
7c0628b2 92bool aio_poll(AioContext *ctx, bool blocking)
a76bab49 93{
9eb0bfca 94 AioHandler *node;
f42b2207 95 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
164a101f 96 bool progress;
f42b2207 97 int count;
438e1f47 98 int timeout;
7c0628b2
PB
99
100 progress = false;
a76bab49 101
8febfa26
KW
102 /*
103 * If there are callbacks left that have been queued, we need to call then.
bcdc1857
PB
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).
8febfa26 106 */
a915f4bc 107 if (aio_bh_poll(ctx)) {
7c0628b2
PB
108 blocking = false;
109 progress = true;
110 }
111
438e1f47
AB
112 /* Run timers */
113 progress |= timerlistgroup_run_timers(&ctx->tlg);
114
cd9ba1eb
PB
115 /*
116 * Then dispatch any pending callbacks from the GSource.
117 *
118 * We have to walk very carefully in case qemu_aio_set_fd_handler is
119 * called while we're walking.
120 */
121 node = QLIST_FIRST(&ctx->aio_handlers);
122 while (node) {
123 AioHandler *tmp;
cd9ba1eb
PB
124
125 ctx->walking_handlers++;
126
f42b2207
PB
127 if (node->pfd.revents && node->io_notify) {
128 node->pfd.revents = 0;
129 node->io_notify(node->e);
164a101f
SH
130
131 /* aio_notify() does not count as progress */
8b2d42d2 132 if (node->e != &ctx->notifier) {
164a101f
SH
133 progress = true;
134 }
cd9ba1eb
PB
135 }
136
137 tmp = node;
138 node = QLIST_NEXT(node, node);
139
140 ctx->walking_handlers--;
141
142 if (!ctx->walking_handlers && tmp->deleted) {
143 QLIST_REMOVE(tmp, node);
144 g_free(tmp);
145 }
146 }
147
7c0628b2 148 if (progress && !blocking) {
bcdc1857 149 return true;
bafbd6a1 150 }
8febfa26 151
a915f4bc 152 ctx->walking_handlers++;
a76bab49 153
9eb0bfca 154 /* fill fd sets */
f42b2207 155 count = 0;
a915f4bc 156 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
f42b2207
PB
157 if (!node->deleted && node->io_notify) {
158 events[count++] = event_notifier_get_handle(node->e);
9eb0bfca
PB
159 }
160 }
a76bab49 161
a915f4bc 162 ctx->walking_handlers--;
a76bab49 163
164a101f
SH
164 /* early return if we only have the aio_notify() fd */
165 if (count == 1) {
7c0628b2 166 return progress;
9eb0bfca 167 }
a76bab49 168
9eb0bfca 169 /* wait until next event */
b022b4a4 170 while (count > 0) {
438e1f47
AB
171 int ret;
172
173 timeout = blocking ?
174 qemu_timeout_ns_to_ms(timerlistgroup_deadline_ns(&ctx->tlg)) : 0;
175 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
f42b2207
PB
176
177 /* if we have any signaled events, dispatch event */
178 if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
179 break;
180 }
181
182 blocking = false;
9eb0bfca 183
9eb0bfca
PB
184 /* we have to walk very carefully in case
185 * qemu_aio_set_fd_handler is called while we're walking */
a915f4bc 186 node = QLIST_FIRST(&ctx->aio_handlers);
9eb0bfca
PB
187 while (node) {
188 AioHandler *tmp;
189
a915f4bc 190 ctx->walking_handlers++;
2db2bfc0 191
9eb0bfca 192 if (!node->deleted &&
f42b2207
PB
193 event_notifier_get_handle(node->e) == events[ret - WAIT_OBJECT_0] &&
194 node->io_notify) {
195 node->io_notify(node->e);
164a101f
SH
196
197 /* aio_notify() does not count as progress */
8b2d42d2 198 if (node->e != &ctx->notifier) {
164a101f
SH
199 progress = true;
200 }
a76bab49
AL
201 }
202
9eb0bfca
PB
203 tmp = node;
204 node = QLIST_NEXT(node, node);
205
a915f4bc 206 ctx->walking_handlers--;
2db2bfc0 207
a915f4bc 208 if (!ctx->walking_handlers && tmp->deleted) {
9eb0bfca
PB
209 QLIST_REMOVE(tmp, node);
210 g_free(tmp);
211 }
a76bab49 212 }
b022b4a4
PB
213
214 /* Try again, but only call each handler once. */
215 events[ret - WAIT_OBJECT_0] = events[--count];
9eb0bfca 216 }
bcdc1857 217
438e1f47
AB
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.
223 */
224 progress |= timerlistgroup_run_timers(&ctx->tlg);
225 }
226
164a101f 227 return progress;
a76bab49 228}