aio: tweak walking in dispatch phase
[qemu/kevin.git] / aio-win32.c
blob1ad459d7ec0919ba718544db725cb9b02afe0cf3
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/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"
25 struct AioHandler {
26 EventNotifier *e;
27 IOHandler *io_read;
28 IOHandler *io_write;
29 EventNotifierHandler *io_notify;
30 GPollFD pfd;
31 int deleted;
32 void *opaque;
33 bool is_external;
34 QLIST_ENTRY(AioHandler) node;
37 void aio_set_fd_handler(AioContext *ctx,
38 int fd,
39 bool is_external,
40 IOHandler *io_read,
41 IOHandler *io_write,
42 AioPollFn *io_poll,
43 void *opaque)
45 /* fd is a SOCKET in our case */
46 AioHandler *node;
48 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
49 if (node->pfd.fd == fd && !node->deleted) {
50 break;
54 /* Are we deleting the fd handler? */
55 if (!io_read && !io_write) {
56 if (node) {
57 /* If the lock is held, just mark the node as deleted */
58 if (ctx->walking_handlers) {
59 node->deleted = 1;
60 node->pfd.revents = 0;
61 } else {
62 /* Otherwise, delete it for real. We can't just mark it as
63 * deleted because deleted nodes are only cleaned up after
64 * releasing the walking_handlers lock.
66 QLIST_REMOVE(node, node);
67 g_free(node);
70 } else {
71 HANDLE event;
73 if (node == NULL) {
74 /* Alloc and insert if it's not already there */
75 node = g_new0(AioHandler, 1);
76 node->pfd.fd = fd;
77 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
80 node->pfd.events = 0;
81 if (node->io_read) {
82 node->pfd.events |= G_IO_IN;
84 if (node->io_write) {
85 node->pfd.events |= G_IO_OUT;
88 node->e = &ctx->notifier;
90 /* Update handler with latest information */
91 node->opaque = opaque;
92 node->io_read = io_read;
93 node->io_write = io_write;
94 node->is_external = is_external;
96 event = event_notifier_get_handle(&ctx->notifier);
97 WSAEventSelect(node->pfd.fd, event,
98 FD_READ | FD_ACCEPT | FD_CLOSE |
99 FD_CONNECT | FD_WRITE | FD_OOB);
102 aio_notify(ctx);
105 void aio_set_fd_poll(AioContext *ctx, int fd,
106 IOHandler *io_poll_begin,
107 IOHandler *io_poll_end)
109 /* Not implemented */
112 void aio_set_event_notifier(AioContext *ctx,
113 EventNotifier *e,
114 bool is_external,
115 EventNotifierHandler *io_notify,
116 AioPollFn *io_poll)
118 AioHandler *node;
120 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
121 if (node->e == e && !node->deleted) {
122 break;
126 /* Are we deleting the fd handler? */
127 if (!io_notify) {
128 if (node) {
129 g_source_remove_poll(&ctx->source, &node->pfd);
131 /* If the lock is held, just mark the node as deleted */
132 if (ctx->walking_handlers) {
133 node->deleted = 1;
134 node->pfd.revents = 0;
135 } else {
136 /* Otherwise, delete it for real. We can't just mark it as
137 * deleted because deleted nodes are only cleaned up after
138 * releasing the walking_handlers lock.
140 QLIST_REMOVE(node, node);
141 g_free(node);
144 } else {
145 if (node == NULL) {
146 /* Alloc and insert if it's not already there */
147 node = g_new0(AioHandler, 1);
148 node->e = e;
149 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
150 node->pfd.events = G_IO_IN;
151 node->is_external = is_external;
152 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
154 g_source_add_poll(&ctx->source, &node->pfd);
156 /* Update handler with latest information */
157 node->io_notify = io_notify;
160 aio_notify(ctx);
163 void aio_set_event_notifier_poll(AioContext *ctx,
164 EventNotifier *notifier,
165 EventNotifierHandler *io_poll_begin,
166 EventNotifierHandler *io_poll_end)
168 /* Not implemented */
171 bool aio_prepare(AioContext *ctx)
173 static struct timeval tv0;
174 AioHandler *node;
175 bool have_select_revents = false;
176 fd_set rfds, wfds;
178 /* fill fd sets */
179 FD_ZERO(&rfds);
180 FD_ZERO(&wfds);
181 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
182 if (node->io_read) {
183 FD_SET ((SOCKET)node->pfd.fd, &rfds);
185 if (node->io_write) {
186 FD_SET ((SOCKET)node->pfd.fd, &wfds);
190 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
191 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
192 node->pfd.revents = 0;
193 if (FD_ISSET(node->pfd.fd, &rfds)) {
194 node->pfd.revents |= G_IO_IN;
195 have_select_revents = true;
198 if (FD_ISSET(node->pfd.fd, &wfds)) {
199 node->pfd.revents |= G_IO_OUT;
200 have_select_revents = true;
205 return have_select_revents;
208 bool aio_pending(AioContext *ctx)
210 AioHandler *node;
212 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
213 if (node->pfd.revents && node->io_notify) {
214 return true;
217 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
218 return true;
220 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
221 return true;
225 return false;
228 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
230 AioHandler *node, *tmp;
231 bool progress = false;
233 ctx->walking_handlers++;
236 * We have to walk very carefully in case aio_set_fd_handler is
237 * called while we're walking.
239 QLIST_FOREACH_SAFE(node, &ctx->aio_handlers, node, tmp) {
240 int revents = node->pfd.revents;
242 if (!node->deleted &&
243 (revents || event_notifier_get_handle(node->e) == event) &&
244 node->io_notify) {
245 node->pfd.revents = 0;
246 node->io_notify(node->e);
248 /* aio_notify() does not count as progress */
249 if (node->e != &ctx->notifier) {
250 progress = true;
254 if (!node->deleted &&
255 (node->io_read || node->io_write)) {
256 node->pfd.revents = 0;
257 if ((revents & G_IO_IN) && node->io_read) {
258 node->io_read(node->opaque);
259 progress = true;
261 if ((revents & G_IO_OUT) && node->io_write) {
262 node->io_write(node->opaque);
263 progress = true;
266 /* if the next select() will return an event, we have progressed */
267 if (event == event_notifier_get_handle(&ctx->notifier)) {
268 WSANETWORKEVENTS ev;
269 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
270 if (ev.lNetworkEvents) {
271 progress = true;
276 if (node->deleted) {
277 ctx->walking_handlers--;
278 if (!ctx->walking_handlers) {
279 QLIST_REMOVE(node, node);
280 g_free(node);
282 ctx->walking_handlers++;
286 ctx->walking_handlers--;
287 return progress;
290 bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
292 bool progress;
294 progress = aio_bh_poll(ctx);
295 if (dispatch_fds) {
296 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
298 progress |= timerlistgroup_run_timers(&ctx->tlg);
299 return progress;
302 bool aio_poll(AioContext *ctx, bool blocking)
304 AioHandler *node;
305 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
306 bool progress, have_select_revents, first;
307 int count;
308 int timeout;
310 aio_context_acquire(ctx);
311 progress = false;
313 /* aio_notify can avoid the expensive event_notifier_set if
314 * everything (file descriptors, bottom halves, timers) will
315 * be re-evaluated before the next blocking poll(). This is
316 * already true when aio_poll is called with blocking == false;
317 * if blocking == true, it is only true after poll() returns,
318 * so disable the optimization now.
320 if (blocking) {
321 atomic_add(&ctx->notify_me, 2);
324 have_select_revents = aio_prepare(ctx);
326 ctx->walking_handlers++;
328 /* fill fd sets */
329 count = 0;
330 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
331 if (!node->deleted && node->io_notify
332 && aio_node_check(ctx, node->is_external)) {
333 events[count++] = event_notifier_get_handle(node->e);
337 ctx->walking_handlers--;
338 first = true;
340 /* ctx->notifier is always registered. */
341 assert(count > 0);
343 /* Multiple iterations, all of them non-blocking except the first,
344 * may be necessary to process all pending events. After the first
345 * WaitForMultipleObjects call ctx->notify_me will be decremented.
347 do {
348 HANDLE event;
349 int ret;
351 timeout = blocking && !have_select_revents
352 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
353 if (timeout) {
354 aio_context_release(ctx);
356 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
357 if (blocking) {
358 assert(first);
359 atomic_sub(&ctx->notify_me, 2);
361 if (timeout) {
362 aio_context_acquire(ctx);
365 if (first) {
366 aio_notify_accept(ctx);
367 progress |= aio_bh_poll(ctx);
368 first = false;
371 /* if we have any signaled events, dispatch event */
372 event = NULL;
373 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
374 event = events[ret - WAIT_OBJECT_0];
375 events[ret - WAIT_OBJECT_0] = events[--count];
376 } else if (!have_select_revents) {
377 break;
380 have_select_revents = false;
381 blocking = false;
383 progress |= aio_dispatch_handlers(ctx, event);
384 } while (count > 0);
386 progress |= timerlistgroup_run_timers(&ctx->tlg);
388 aio_context_release(ctx);
389 return progress;
392 void aio_context_setup(AioContext *ctx)
396 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
397 int64_t grow, int64_t shrink, Error **errp)
399 error_setg(errp, "AioContext polling is not implemented on Windows");