]> git.proxmox.com Git - mirror_qemu.git/blame - util/aio-win32.c
tests/tcg/s390x: Add rxsbg.c
[mirror_qemu.git] / util / 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
d38ea87a 18#include "qemu/osdep.h"
737e150e 19#include "block/block.h"
eada6d92 20#include "qemu/main-loop.h"
1de7afc9
PB
21#include "qemu/queue.h"
22#include "qemu/sockets.h"
4a1cba38 23#include "qapi/error.h"
b92d9a91 24#include "qemu/rcu_queue.h"
e2a3a219 25#include "qemu/error-report.h"
a76bab49 26
f42b2207
PB
27struct AioHandler {
28 EventNotifier *e;
b493317d
PB
29 IOHandler *io_read;
30 IOHandler *io_write;
f42b2207 31 EventNotifierHandler *io_notify;
cd9ba1eb 32 GPollFD pfd;
a76bab49 33 int deleted;
b493317d 34 void *opaque;
dca21ef2 35 bool is_external;
72cf2d4f 36 QLIST_ENTRY(AioHandler) node;
a76bab49
AL
37};
38
fef16601
RN
39static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
40{
da0652c0
YL
41 /*
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
45 * destruction anyway.
46 */
47 if (!g_source_is_destroyed(&ctx->source)) {
48 g_source_remove_poll(&ctx->source, &node->pfd);
49 }
50
fef16601
RN
51 /* If aio_poll is in progress, just mark the node as deleted */
52 if (qemu_lockcnt_count(&ctx->list_lock)) {
53 node->deleted = 1;
54 node->pfd.revents = 0;
55 } else {
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.
59 */
60 QLIST_REMOVE(node, node);
61 g_free(node);
62 }
63}
64
b493317d
PB
65void aio_set_fd_handler(AioContext *ctx,
66 int fd,
dca21ef2 67 bool is_external,
b493317d
PB
68 IOHandler *io_read,
69 IOHandler *io_write,
4a1cba38 70 AioPollFn *io_poll,
826cc324 71 IOHandler *io_poll_ready,
b493317d
PB
72 void *opaque)
73{
fef16601
RN
74 AioHandler *old_node;
75 AioHandler *node = NULL;
abe34282 76 SOCKET s;
b493317d 77
e2a3a219
MAL
78 if (!fd_is_socket(fd)) {
79 error_report("fd=%d is not a socket, AIO implementation is missing", fd);
80 return;
81 }
82
abe34282
MAL
83 s = _get_osfhandle(fd);
84
b92d9a91 85 qemu_lockcnt_lock(&ctx->list_lock);
fef16601 86 QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {
abe34282 87 if (old_node->pfd.fd == s && !old_node->deleted) {
b493317d
PB
88 break;
89 }
90 }
91
fef16601 92 if (io_read || io_write) {
b493317d 93 HANDLE event;
55d41b16 94 long bitmask = 0;
b493317d 95
fef16601
RN
96 /* Alloc and insert if it's not already there */
97 node = g_new0(AioHandler, 1);
abe34282 98 node->pfd.fd = s;
b493317d
PB
99
100 node->pfd.events = 0;
101 if (node->io_read) {
102 node->pfd.events |= G_IO_IN;
103 }
104 if (node->io_write) {
105 node->pfd.events |= G_IO_OUT;
106 }
107
108 node->e = &ctx->notifier;
109
110 /* Update handler with latest information */
111 node->opaque = opaque;
112 node->io_read = io_read;
113 node->io_write = io_write;
dca21ef2 114 node->is_external = is_external;
b493317d 115
55d41b16
AF
116 if (io_read) {
117 bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
118 }
119
120 if (io_write) {
121 bitmask |= FD_WRITE | FD_CONNECT;
122 }
123
fef16601 124 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
b493317d 125 event = event_notifier_get_handle(&ctx->notifier);
abe34282 126 qemu_socket_select(fd, event, bitmask, NULL);
b493317d 127 }
fef16601
RN
128 if (old_node) {
129 aio_remove_fd_handler(ctx, old_node);
130 }
b493317d 131
b92d9a91 132 qemu_lockcnt_unlock(&ctx->list_lock);
b493317d
PB
133 aio_notify(ctx);
134}
135
f42b2207
PB
136void aio_set_event_notifier(AioContext *ctx,
137 EventNotifier *e,
dca21ef2 138 bool is_external,
4a1cba38 139 EventNotifierHandler *io_notify,
826cc324
SH
140 AioPollFn *io_poll,
141 EventNotifierHandler *io_poll_ready)
a76bab49
AL
142{
143 AioHandler *node;
144
b92d9a91 145 qemu_lockcnt_lock(&ctx->list_lock);
a915f4bc 146 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
f42b2207
PB
147 if (node->e == e && !node->deleted) {
148 break;
149 }
a76bab49
AL
150 }
151
a76bab49 152 /* Are we deleting the fd handler? */
f42b2207 153 if (!io_notify) {
a76bab49 154 if (node) {
fef16601 155 aio_remove_fd_handler(ctx, node);
a76bab49
AL
156 }
157 } else {
158 if (node == NULL) {
159 /* Alloc and insert if it's not already there */
3ba235a0 160 node = g_new0(AioHandler, 1);
f42b2207
PB
161 node->e = e;
162 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
163 node->pfd.events = G_IO_IN;
dca21ef2 164 node->is_external = is_external;
b92d9a91 165 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
e3713e00
PB
166
167 g_source_add_poll(&ctx->source, &node->pfd);
a76bab49
AL
168 }
169 /* Update handler with latest information */
f42b2207 170 node->io_notify = io_notify;
a76bab49 171 }
7ed2b24c 172
b92d9a91 173 qemu_lockcnt_unlock(&ctx->list_lock);
7ed2b24c 174 aio_notify(ctx);
9958c351
PB
175}
176
684e508c
SH
177void aio_set_event_notifier_poll(AioContext *ctx,
178 EventNotifier *notifier,
179 EventNotifierHandler *io_poll_begin,
180 EventNotifierHandler *io_poll_end)
181{
182 /* Not implemented */
183}
184
a3462c65
PB
185bool aio_prepare(AioContext *ctx)
186{
b493317d
PB
187 static struct timeval tv0;
188 AioHandler *node;
189 bool have_select_revents = false;
190 fd_set rfds, wfds;
191
b92d9a91
PB
192 /*
193 * We have to walk very carefully in case aio_set_fd_handler is
194 * called while we're walking.
195 */
196 qemu_lockcnt_inc(&ctx->list_lock);
197
b493317d
PB
198 /* fill fd sets */
199 FD_ZERO(&rfds);
200 FD_ZERO(&wfds);
b92d9a91 201 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
b493317d
PB
202 if (node->io_read) {
203 FD_SET ((SOCKET)node->pfd.fd, &rfds);
204 }
205 if (node->io_write) {
206 FD_SET ((SOCKET)node->pfd.fd, &wfds);
207 }
208 }
209
210 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
b92d9a91 211 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
b493317d
PB
212 node->pfd.revents = 0;
213 if (FD_ISSET(node->pfd.fd, &rfds)) {
214 node->pfd.revents |= G_IO_IN;
215 have_select_revents = true;
216 }
217
218 if (FD_ISSET(node->pfd.fd, &wfds)) {
219 node->pfd.revents |= G_IO_OUT;
220 have_select_revents = true;
221 }
222 }
223 }
224
b92d9a91 225 qemu_lockcnt_dec(&ctx->list_lock);
b493317d 226 return have_select_revents;
a3462c65
PB
227}
228
cd9ba1eb
PB
229bool aio_pending(AioContext *ctx)
230{
231 AioHandler *node;
b92d9a91 232 bool result = false;
cd9ba1eb 233
b92d9a91
PB
234 /*
235 * We have to walk very carefully in case aio_set_fd_handler is
236 * called while we're walking.
237 */
238 qemu_lockcnt_inc(&ctx->list_lock);
239 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
f42b2207 240 if (node->pfd.revents && node->io_notify) {
b92d9a91
PB
241 result = true;
242 break;
cd9ba1eb 243 }
b493317d
PB
244
245 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
b92d9a91
PB
246 result = true;
247 break;
b493317d
PB
248 }
249 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
b92d9a91
PB
250 result = true;
251 break;
b493317d 252 }
cd9ba1eb
PB
253 }
254
b92d9a91
PB
255 qemu_lockcnt_dec(&ctx->list_lock);
256 return result;
cd9ba1eb
PB
257}
258
a398dea3 259static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
a76bab49 260{
b92d9a91 261 AioHandler *node;
a398dea3 262 bool progress = false;
b92d9a91 263 AioHandler *tmp;
7c0628b2 264
cd9ba1eb 265 /*
87f68d31 266 * We have to walk very carefully in case aio_set_fd_handler is
cd9ba1eb
PB
267 * called while we're walking.
268 */
b92d9a91 269 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
b493317d 270 int revents = node->pfd.revents;
cd9ba1eb 271
a398dea3 272 if (!node->deleted &&
b493317d 273 (revents || event_notifier_get_handle(node->e) == event) &&
a398dea3 274 node->io_notify) {
f42b2207
PB
275 node->pfd.revents = 0;
276 node->io_notify(node->e);
164a101f
SH
277
278 /* aio_notify() does not count as progress */
8b2d42d2 279 if (node->e != &ctx->notifier) {
164a101f
SH
280 progress = true;
281 }
cd9ba1eb
PB
282 }
283
b493317d
PB
284 if (!node->deleted &&
285 (node->io_read || node->io_write)) {
286 node->pfd.revents = 0;
287 if ((revents & G_IO_IN) && node->io_read) {
288 node->io_read(node->opaque);
289 progress = true;
290 }
291 if ((revents & G_IO_OUT) && node->io_write) {
292 node->io_write(node->opaque);
293 progress = true;
294 }
295
296 /* if the next select() will return an event, we have progressed */
297 if (event == event_notifier_get_handle(&ctx->notifier)) {
298 WSANETWORKEVENTS ev;
299 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
300 if (ev.lNetworkEvents) {
301 progress = true;
302 }
303 }
304 }
305
abf90d39 306 if (node->deleted) {
b92d9a91 307 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
abf90d39
PB
308 QLIST_REMOVE(node, node);
309 g_free(node);
b92d9a91 310 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
abf90d39 311 }
cd9ba1eb
PB
312 }
313 }
314
a398dea3
PB
315 return progress;
316}
317
a153bf52 318void aio_dispatch(AioContext *ctx)
a398dea3 319{
bd451435 320 qemu_lockcnt_inc(&ctx->list_lock);
a153bf52
PB
321 aio_bh_poll(ctx);
322 aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
bd451435 323 qemu_lockcnt_dec(&ctx->list_lock);
a153bf52 324 timerlistgroup_run_timers(&ctx->tlg);
a398dea3
PB
325}
326
327bool aio_poll(AioContext *ctx, bool blocking)
328{
329 AioHandler *node;
e0d034bb 330 HANDLE events[MAXIMUM_WAIT_OBJECTS];
eabc9779 331 bool progress, have_select_revents, first;
e0d034bb 332 unsigned count;
a398dea3
PB
333 int timeout;
334
5710a3e0
PB
335 /*
336 * There cannot be two concurrent aio_poll calls for the same AioContext (or
337 * an aio_poll concurrent with a GSource prepare/check/dispatch callback).
338 * We rely on this below to avoid slow locked accesses to ctx->notify_me.
eada6d92
VR
339 *
340 * aio_poll() may only be called in the AioContext's thread. iohandler_ctx
341 * is special in that it runs in the main thread, but that thread's context
342 * is qemu_aio_context.
5710a3e0 343 */
eada6d92
VR
344 assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
345 qemu_get_aio_context() : ctx));
a398dea3
PB
346 progress = false;
347
0a9dd166
PB
348 /* aio_notify can avoid the expensive event_notifier_set if
349 * everything (file descriptors, bottom halves, timers) will
350 * be re-evaluated before the next blocking poll(). This is
351 * already true when aio_poll is called with blocking == false;
eabc9779
PB
352 * if blocking == true, it is only true after poll() returns,
353 * so disable the optimization now.
0a9dd166 354 */
eabc9779 355 if (blocking) {
d73415a3 356 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
5710a3e0
PB
357 /*
358 * Write ctx->notify_me before computing the timeout
359 * (reading bottom half flags, etc.). Pairs with
360 * smp_mb in aio_notify().
361 */
362 smp_mb();
eabc9779 363 }
0a9dd166 364
b92d9a91 365 qemu_lockcnt_inc(&ctx->list_lock);
6493c975
PB
366 have_select_revents = aio_prepare(ctx);
367
9eb0bfca 368 /* fill fd sets */
f42b2207 369 count = 0;
b92d9a91 370 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
c1e1e5fa
FZ
371 if (!node->deleted && node->io_notify
372 && aio_node_check(ctx, node->is_external)) {
e0d034bb 373 assert(count < MAXIMUM_WAIT_OBJECTS);
f42b2207 374 events[count++] = event_notifier_get_handle(node->e);
9eb0bfca
PB
375 }
376 }
a76bab49 377
3672fa50 378 first = true;
a76bab49 379
6493c975
PB
380 /* ctx->notifier is always registered. */
381 assert(count > 0);
382
383 /* Multiple iterations, all of them non-blocking except the first,
384 * may be necessary to process all pending events. After the first
385 * WaitForMultipleObjects call ctx->notify_me will be decremented.
386 */
387 do {
b493317d 388 HANDLE event;
438e1f47
AB
389 int ret;
390
6493c975 391 timeout = blocking && !have_select_revents
845ca10d 392 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
438e1f47 393 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
eabc9779
PB
394 if (blocking) {
395 assert(first);
d73415a3
SH
396 qatomic_store_release(&ctx->notify_me,
397 qatomic_read(&ctx->notify_me) - 2);
b37548fc 398 aio_notify_accept(ctx);
eabc9779 399 }
f42b2207 400
21a03d17 401 if (first) {
21a03d17
PB
402 progress |= aio_bh_poll(ctx);
403 first = false;
3672fa50 404 }
3672fa50 405
f42b2207 406 /* if we have any signaled events, dispatch event */
b493317d
PB
407 event = NULL;
408 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
409 event = events[ret - WAIT_OBJECT_0];
a90d411e 410 events[ret - WAIT_OBJECT_0] = events[--count];
b493317d 411 } else if (!have_select_revents) {
f42b2207
PB
412 break;
413 }
414
b493317d 415 have_select_revents = false;
f42b2207 416 blocking = false;
9eb0bfca 417
b493317d 418 progress |= aio_dispatch_handlers(ctx, event);
6493c975 419 } while (count > 0);
bcdc1857 420
bd451435
PB
421 qemu_lockcnt_dec(&ctx->list_lock);
422
e4c7e2d1 423 progress |= timerlistgroup_run_timers(&ctx->tlg);
164a101f 424 return progress;
a76bab49 425}
37fcee5d 426
7e003465 427void aio_context_setup(AioContext *ctx)
37fcee5d
FZ
428{
429}
4a1cba38 430
cd0a6d2b
JW
431void aio_context_destroy(AioContext *ctx)
432{
433}
434
ba607ca8
SH
435void aio_context_use_g_source(AioContext *ctx)
436{
437}
438
82a41186
SH
439void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
440 int64_t grow, int64_t shrink, Error **errp)
4a1cba38 441{
90c558be
PX
442 if (max_ns) {
443 error_setg(errp, "AioContext polling is not implemented on Windows");
444 }
4a1cba38 445}
1793ad02
SG
446
447void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch,
448 Error **errp)
449{
450}