iothread: add polling parameters
[qemu/kevin.git] / aio-win32.c
blob0a6e91b0c37799d4b0f272aad8f59d31cfdd24e4
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_event_notifier(AioContext *ctx,
106 EventNotifier *e,
107 bool is_external,
108 EventNotifierHandler *io_notify,
109 AioPollFn *io_poll)
111 AioHandler *node;
113 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
114 if (node->e == e && !node->deleted) {
115 break;
119 /* Are we deleting the fd handler? */
120 if (!io_notify) {
121 if (node) {
122 g_source_remove_poll(&ctx->source, &node->pfd);
124 /* If the lock is held, just mark the node as deleted */
125 if (ctx->walking_handlers) {
126 node->deleted = 1;
127 node->pfd.revents = 0;
128 } else {
129 /* Otherwise, delete it for real. We can't just mark it as
130 * deleted because deleted nodes are only cleaned up after
131 * releasing the walking_handlers lock.
133 QLIST_REMOVE(node, node);
134 g_free(node);
137 } else {
138 if (node == NULL) {
139 /* Alloc and insert if it's not already there */
140 node = g_new0(AioHandler, 1);
141 node->e = e;
142 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
143 node->pfd.events = G_IO_IN;
144 node->is_external = is_external;
145 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
147 g_source_add_poll(&ctx->source, &node->pfd);
149 /* Update handler with latest information */
150 node->io_notify = io_notify;
153 aio_notify(ctx);
156 bool aio_prepare(AioContext *ctx)
158 static struct timeval tv0;
159 AioHandler *node;
160 bool have_select_revents = false;
161 fd_set rfds, wfds;
163 /* fill fd sets */
164 FD_ZERO(&rfds);
165 FD_ZERO(&wfds);
166 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
167 if (node->io_read) {
168 FD_SET ((SOCKET)node->pfd.fd, &rfds);
170 if (node->io_write) {
171 FD_SET ((SOCKET)node->pfd.fd, &wfds);
175 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
176 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
177 node->pfd.revents = 0;
178 if (FD_ISSET(node->pfd.fd, &rfds)) {
179 node->pfd.revents |= G_IO_IN;
180 have_select_revents = true;
183 if (FD_ISSET(node->pfd.fd, &wfds)) {
184 node->pfd.revents |= G_IO_OUT;
185 have_select_revents = true;
190 return have_select_revents;
193 bool aio_pending(AioContext *ctx)
195 AioHandler *node;
197 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
198 if (node->pfd.revents && node->io_notify) {
199 return true;
202 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
203 return true;
205 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
206 return true;
210 return false;
213 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
215 AioHandler *node;
216 bool progress = false;
219 * We have to walk very carefully in case aio_set_fd_handler is
220 * called while we're walking.
222 node = QLIST_FIRST(&ctx->aio_handlers);
223 while (node) {
224 AioHandler *tmp;
225 int revents = node->pfd.revents;
227 ctx->walking_handlers++;
229 if (!node->deleted &&
230 (revents || event_notifier_get_handle(node->e) == event) &&
231 node->io_notify) {
232 node->pfd.revents = 0;
233 node->io_notify(node->e);
235 /* aio_notify() does not count as progress */
236 if (node->e != &ctx->notifier) {
237 progress = true;
241 if (!node->deleted &&
242 (node->io_read || node->io_write)) {
243 node->pfd.revents = 0;
244 if ((revents & G_IO_IN) && node->io_read) {
245 node->io_read(node->opaque);
246 progress = true;
248 if ((revents & G_IO_OUT) && node->io_write) {
249 node->io_write(node->opaque);
250 progress = true;
253 /* if the next select() will return an event, we have progressed */
254 if (event == event_notifier_get_handle(&ctx->notifier)) {
255 WSANETWORKEVENTS ev;
256 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
257 if (ev.lNetworkEvents) {
258 progress = true;
263 tmp = node;
264 node = QLIST_NEXT(node, node);
266 ctx->walking_handlers--;
268 if (!ctx->walking_handlers && tmp->deleted) {
269 QLIST_REMOVE(tmp, node);
270 g_free(tmp);
274 return progress;
277 bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
279 bool progress;
281 progress = aio_bh_poll(ctx);
282 if (dispatch_fds) {
283 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
285 progress |= timerlistgroup_run_timers(&ctx->tlg);
286 return progress;
289 bool aio_poll(AioContext *ctx, bool blocking)
291 AioHandler *node;
292 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
293 bool progress, have_select_revents, first;
294 int count;
295 int timeout;
297 aio_context_acquire(ctx);
298 progress = false;
300 /* aio_notify can avoid the expensive event_notifier_set if
301 * everything (file descriptors, bottom halves, timers) will
302 * be re-evaluated before the next blocking poll(). This is
303 * already true when aio_poll is called with blocking == false;
304 * if blocking == true, it is only true after poll() returns,
305 * so disable the optimization now.
307 if (blocking) {
308 atomic_add(&ctx->notify_me, 2);
311 have_select_revents = aio_prepare(ctx);
313 ctx->walking_handlers++;
315 /* fill fd sets */
316 count = 0;
317 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
318 if (!node->deleted && node->io_notify
319 && aio_node_check(ctx, node->is_external)) {
320 events[count++] = event_notifier_get_handle(node->e);
324 ctx->walking_handlers--;
325 first = true;
327 /* ctx->notifier is always registered. */
328 assert(count > 0);
330 /* Multiple iterations, all of them non-blocking except the first,
331 * may be necessary to process all pending events. After the first
332 * WaitForMultipleObjects call ctx->notify_me will be decremented.
334 do {
335 HANDLE event;
336 int ret;
338 timeout = blocking && !have_select_revents
339 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
340 if (timeout) {
341 aio_context_release(ctx);
343 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
344 if (blocking) {
345 assert(first);
346 atomic_sub(&ctx->notify_me, 2);
348 if (timeout) {
349 aio_context_acquire(ctx);
352 if (first) {
353 aio_notify_accept(ctx);
354 progress |= aio_bh_poll(ctx);
355 first = false;
358 /* if we have any signaled events, dispatch event */
359 event = NULL;
360 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
361 event = events[ret - WAIT_OBJECT_0];
362 events[ret - WAIT_OBJECT_0] = events[--count];
363 } else if (!have_select_revents) {
364 break;
367 have_select_revents = false;
368 blocking = false;
370 progress |= aio_dispatch_handlers(ctx, event);
371 } while (count > 0);
373 progress |= timerlistgroup_run_timers(&ctx->tlg);
375 aio_context_release(ctx);
376 return progress;
379 void aio_context_setup(AioContext *ctx)
383 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns, Error **errp)
385 error_setg(errp, "AioContext polling is not implemented on Windows");