aio: add AioPollFn and io_poll() interface
[qemu/ar7.git] / aio-win32.c
blob3ef8ea4caa0b7a7b8e2e8e6c097f55a0fe98e31c
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"
24 struct AioHandler {
25 EventNotifier *e;
26 IOHandler *io_read;
27 IOHandler *io_write;
28 EventNotifierHandler *io_notify;
29 GPollFD pfd;
30 int deleted;
31 void *opaque;
32 bool is_external;
33 QLIST_ENTRY(AioHandler) node;
36 void aio_set_fd_handler(AioContext *ctx,
37 int fd,
38 bool is_external,
39 IOHandler *io_read,
40 IOHandler *io_write,
41 void *opaque)
43 /* fd is a SOCKET in our case */
44 AioHandler *node;
46 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
47 if (node->pfd.fd == fd && !node->deleted) {
48 break;
52 /* Are we deleting the fd handler? */
53 if (!io_read && !io_write) {
54 if (node) {
55 /* If the lock is held, just mark the node as deleted */
56 if (ctx->walking_handlers) {
57 node->deleted = 1;
58 node->pfd.revents = 0;
59 } else {
60 /* Otherwise, delete it for real. We can't just mark it as
61 * deleted because deleted nodes are only cleaned up after
62 * releasing the walking_handlers lock.
64 QLIST_REMOVE(node, node);
65 g_free(node);
68 } else {
69 HANDLE event;
71 if (node == NULL) {
72 /* Alloc and insert if it's not already there */
73 node = g_new0(AioHandler, 1);
74 node->pfd.fd = fd;
75 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
78 node->pfd.events = 0;
79 if (node->io_read) {
80 node->pfd.events |= G_IO_IN;
82 if (node->io_write) {
83 node->pfd.events |= G_IO_OUT;
86 node->e = &ctx->notifier;
88 /* Update handler with latest information */
89 node->opaque = opaque;
90 node->io_read = io_read;
91 node->io_write = io_write;
92 node->is_external = is_external;
94 event = event_notifier_get_handle(&ctx->notifier);
95 WSAEventSelect(node->pfd.fd, event,
96 FD_READ | FD_ACCEPT | FD_CLOSE |
97 FD_CONNECT | FD_WRITE | FD_OOB);
100 aio_notify(ctx);
103 void aio_set_event_notifier(AioContext *ctx,
104 EventNotifier *e,
105 bool is_external,
106 EventNotifierHandler *io_notify)
108 AioHandler *node;
110 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
111 if (node->e == e && !node->deleted) {
112 break;
116 /* Are we deleting the fd handler? */
117 if (!io_notify) {
118 if (node) {
119 g_source_remove_poll(&ctx->source, &node->pfd);
121 /* If the lock is held, just mark the node as deleted */
122 if (ctx->walking_handlers) {
123 node->deleted = 1;
124 node->pfd.revents = 0;
125 } else {
126 /* Otherwise, delete it for real. We can't just mark it as
127 * deleted because deleted nodes are only cleaned up after
128 * releasing the walking_handlers lock.
130 QLIST_REMOVE(node, node);
131 g_free(node);
134 } else {
135 if (node == NULL) {
136 /* Alloc and insert if it's not already there */
137 node = g_new0(AioHandler, 1);
138 node->e = e;
139 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
140 node->pfd.events = G_IO_IN;
141 node->is_external = is_external;
142 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
144 g_source_add_poll(&ctx->source, &node->pfd);
146 /* Update handler with latest information */
147 node->io_notify = io_notify;
150 aio_notify(ctx);
153 bool aio_prepare(AioContext *ctx)
155 static struct timeval tv0;
156 AioHandler *node;
157 bool have_select_revents = false;
158 fd_set rfds, wfds;
160 /* fill fd sets */
161 FD_ZERO(&rfds);
162 FD_ZERO(&wfds);
163 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
164 if (node->io_read) {
165 FD_SET ((SOCKET)node->pfd.fd, &rfds);
167 if (node->io_write) {
168 FD_SET ((SOCKET)node->pfd.fd, &wfds);
172 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
173 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
174 node->pfd.revents = 0;
175 if (FD_ISSET(node->pfd.fd, &rfds)) {
176 node->pfd.revents |= G_IO_IN;
177 have_select_revents = true;
180 if (FD_ISSET(node->pfd.fd, &wfds)) {
181 node->pfd.revents |= G_IO_OUT;
182 have_select_revents = true;
187 return have_select_revents;
190 bool aio_pending(AioContext *ctx)
192 AioHandler *node;
194 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
195 if (node->pfd.revents && node->io_notify) {
196 return true;
199 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
200 return true;
202 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
203 return true;
207 return false;
210 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
212 AioHandler *node;
213 bool progress = false;
216 * We have to walk very carefully in case aio_set_fd_handler is
217 * called while we're walking.
219 node = QLIST_FIRST(&ctx->aio_handlers);
220 while (node) {
221 AioHandler *tmp;
222 int revents = node->pfd.revents;
224 ctx->walking_handlers++;
226 if (!node->deleted &&
227 (revents || event_notifier_get_handle(node->e) == event) &&
228 node->io_notify) {
229 node->pfd.revents = 0;
230 node->io_notify(node->e);
232 /* aio_notify() does not count as progress */
233 if (node->e != &ctx->notifier) {
234 progress = true;
238 if (!node->deleted &&
239 (node->io_read || node->io_write)) {
240 node->pfd.revents = 0;
241 if ((revents & G_IO_IN) && node->io_read) {
242 node->io_read(node->opaque);
243 progress = true;
245 if ((revents & G_IO_OUT) && node->io_write) {
246 node->io_write(node->opaque);
247 progress = true;
250 /* if the next select() will return an event, we have progressed */
251 if (event == event_notifier_get_handle(&ctx->notifier)) {
252 WSANETWORKEVENTS ev;
253 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
254 if (ev.lNetworkEvents) {
255 progress = true;
260 tmp = node;
261 node = QLIST_NEXT(node, node);
263 ctx->walking_handlers--;
265 if (!ctx->walking_handlers && tmp->deleted) {
266 QLIST_REMOVE(tmp, node);
267 g_free(tmp);
271 return progress;
274 bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
276 bool progress;
278 progress = aio_bh_poll(ctx);
279 if (dispatch_fds) {
280 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
282 progress |= timerlistgroup_run_timers(&ctx->tlg);
283 return progress;
286 bool aio_poll(AioContext *ctx, bool blocking)
288 AioHandler *node;
289 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
290 bool progress, have_select_revents, first;
291 int count;
292 int timeout;
294 aio_context_acquire(ctx);
295 progress = false;
297 /* aio_notify can avoid the expensive event_notifier_set if
298 * everything (file descriptors, bottom halves, timers) will
299 * be re-evaluated before the next blocking poll(). This is
300 * already true when aio_poll is called with blocking == false;
301 * if blocking == true, it is only true after poll() returns,
302 * so disable the optimization now.
304 if (blocking) {
305 atomic_add(&ctx->notify_me, 2);
308 have_select_revents = aio_prepare(ctx);
310 ctx->walking_handlers++;
312 /* fill fd sets */
313 count = 0;
314 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
315 if (!node->deleted && node->io_notify
316 && aio_node_check(ctx, node->is_external)) {
317 events[count++] = event_notifier_get_handle(node->e);
321 ctx->walking_handlers--;
322 first = true;
324 /* ctx->notifier is always registered. */
325 assert(count > 0);
327 /* Multiple iterations, all of them non-blocking except the first,
328 * may be necessary to process all pending events. After the first
329 * WaitForMultipleObjects call ctx->notify_me will be decremented.
331 do {
332 HANDLE event;
333 int ret;
335 timeout = blocking && !have_select_revents
336 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
337 if (timeout) {
338 aio_context_release(ctx);
340 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
341 if (blocking) {
342 assert(first);
343 atomic_sub(&ctx->notify_me, 2);
345 if (timeout) {
346 aio_context_acquire(ctx);
349 if (first) {
350 aio_notify_accept(ctx);
351 progress |= aio_bh_poll(ctx);
352 first = false;
355 /* if we have any signaled events, dispatch event */
356 event = NULL;
357 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
358 event = events[ret - WAIT_OBJECT_0];
359 events[ret - WAIT_OBJECT_0] = events[--count];
360 } else if (!have_select_revents) {
361 break;
364 have_select_revents = false;
365 blocking = false;
367 progress |= aio_dispatch_handlers(ctx, event);
368 } while (count > 0);
370 progress |= timerlistgroup_run_timers(&ctx->tlg);
372 aio_context_release(ctx);
373 return progress;
376 void aio_context_setup(AioContext *ctx)