virtio-gpu: add support to enable/disable command processing
[qemu/kevin.git] / aio-win32.c
blobcdc445608baddeea4ed1499a827052a454ddbced
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-common.h"
19 #include "block/block.h"
20 #include "qemu/queue.h"
21 #include "qemu/sockets.h"
23 struct AioHandler {
24 EventNotifier *e;
25 IOHandler *io_read;
26 IOHandler *io_write;
27 EventNotifierHandler *io_notify;
28 GPollFD pfd;
29 int deleted;
30 void *opaque;
31 bool is_external;
32 QLIST_ENTRY(AioHandler) node;
35 void aio_set_fd_handler(AioContext *ctx,
36 int fd,
37 bool is_external,
38 IOHandler *io_read,
39 IOHandler *io_write,
40 void *opaque)
42 /* fd is a SOCKET in our case */
43 AioHandler *node;
45 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
46 if (node->pfd.fd == fd && !node->deleted) {
47 break;
51 /* Are we deleting the fd handler? */
52 if (!io_read && !io_write) {
53 if (node) {
54 /* If the lock is held, just mark the node as deleted */
55 if (ctx->walking_handlers) {
56 node->deleted = 1;
57 node->pfd.revents = 0;
58 } else {
59 /* Otherwise, delete it for real. We can't just mark it as
60 * deleted because deleted nodes are only cleaned up after
61 * releasing the walking_handlers lock.
63 QLIST_REMOVE(node, node);
64 g_free(node);
67 } else {
68 HANDLE event;
70 if (node == NULL) {
71 /* Alloc and insert if it's not already there */
72 node = g_new0(AioHandler, 1);
73 node->pfd.fd = fd;
74 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
77 node->pfd.events = 0;
78 if (node->io_read) {
79 node->pfd.events |= G_IO_IN;
81 if (node->io_write) {
82 node->pfd.events |= G_IO_OUT;
85 node->e = &ctx->notifier;
87 /* Update handler with latest information */
88 node->opaque = opaque;
89 node->io_read = io_read;
90 node->io_write = io_write;
91 node->is_external = is_external;
93 event = event_notifier_get_handle(&ctx->notifier);
94 WSAEventSelect(node->pfd.fd, event,
95 FD_READ | FD_ACCEPT | FD_CLOSE |
96 FD_CONNECT | FD_WRITE | FD_OOB);
99 aio_notify(ctx);
102 void aio_set_event_notifier(AioContext *ctx,
103 EventNotifier *e,
104 bool is_external,
105 EventNotifierHandler *io_notify)
107 AioHandler *node;
109 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
110 if (node->e == e && !node->deleted) {
111 break;
115 /* Are we deleting the fd handler? */
116 if (!io_notify) {
117 if (node) {
118 g_source_remove_poll(&ctx->source, &node->pfd);
120 /* If the lock is held, just mark the node as deleted */
121 if (ctx->walking_handlers) {
122 node->deleted = 1;
123 node->pfd.revents = 0;
124 } else {
125 /* Otherwise, delete it for real. We can't just mark it as
126 * deleted because deleted nodes are only cleaned up after
127 * releasing the walking_handlers lock.
129 QLIST_REMOVE(node, node);
130 g_free(node);
133 } else {
134 if (node == NULL) {
135 /* Alloc and insert if it's not already there */
136 node = g_new0(AioHandler, 1);
137 node->e = e;
138 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
139 node->pfd.events = G_IO_IN;
140 node->is_external = is_external;
141 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
143 g_source_add_poll(&ctx->source, &node->pfd);
145 /* Update handler with latest information */
146 node->io_notify = io_notify;
149 aio_notify(ctx);
152 bool aio_prepare(AioContext *ctx)
154 static struct timeval tv0;
155 AioHandler *node;
156 bool have_select_revents = false;
157 fd_set rfds, wfds;
159 /* fill fd sets */
160 FD_ZERO(&rfds);
161 FD_ZERO(&wfds);
162 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
163 if (node->io_read) {
164 FD_SET ((SOCKET)node->pfd.fd, &rfds);
166 if (node->io_write) {
167 FD_SET ((SOCKET)node->pfd.fd, &wfds);
171 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
172 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
173 node->pfd.revents = 0;
174 if (FD_ISSET(node->pfd.fd, &rfds)) {
175 node->pfd.revents |= G_IO_IN;
176 have_select_revents = true;
179 if (FD_ISSET(node->pfd.fd, &wfds)) {
180 node->pfd.revents |= G_IO_OUT;
181 have_select_revents = true;
186 return have_select_revents;
189 bool aio_pending(AioContext *ctx)
191 AioHandler *node;
193 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
194 if (node->pfd.revents && node->io_notify) {
195 return true;
198 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
199 return true;
201 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
202 return true;
206 return false;
209 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
211 AioHandler *node;
212 bool progress = false;
215 * We have to walk very carefully in case aio_set_fd_handler is
216 * called while we're walking.
218 node = QLIST_FIRST(&ctx->aio_handlers);
219 while (node) {
220 AioHandler *tmp;
221 int revents = node->pfd.revents;
223 ctx->walking_handlers++;
225 if (!node->deleted &&
226 (revents || event_notifier_get_handle(node->e) == event) &&
227 node->io_notify) {
228 node->pfd.revents = 0;
229 node->io_notify(node->e);
231 /* aio_notify() does not count as progress */
232 if (node->e != &ctx->notifier) {
233 progress = true;
237 if (!node->deleted &&
238 (node->io_read || node->io_write)) {
239 node->pfd.revents = 0;
240 if ((revents & G_IO_IN) && node->io_read) {
241 node->io_read(node->opaque);
242 progress = true;
244 if ((revents & G_IO_OUT) && node->io_write) {
245 node->io_write(node->opaque);
246 progress = true;
249 /* if the next select() will return an event, we have progressed */
250 if (event == event_notifier_get_handle(&ctx->notifier)) {
251 WSANETWORKEVENTS ev;
252 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
253 if (ev.lNetworkEvents) {
254 progress = true;
259 tmp = node;
260 node = QLIST_NEXT(node, node);
262 ctx->walking_handlers--;
264 if (!ctx->walking_handlers && tmp->deleted) {
265 QLIST_REMOVE(tmp, node);
266 g_free(tmp);
270 return progress;
273 bool aio_dispatch(AioContext *ctx)
275 bool progress;
277 progress = aio_bh_poll(ctx);
278 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
279 progress |= timerlistgroup_run_timers(&ctx->tlg);
280 return progress;
283 bool aio_poll(AioContext *ctx, bool blocking)
285 AioHandler *node;
286 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
287 bool progress, have_select_revents, first;
288 int count;
289 int timeout;
291 aio_context_acquire(ctx);
292 progress = false;
294 /* aio_notify can avoid the expensive event_notifier_set if
295 * everything (file descriptors, bottom halves, timers) will
296 * be re-evaluated before the next blocking poll(). This is
297 * already true when aio_poll is called with blocking == false;
298 * if blocking == true, it is only true after poll() returns,
299 * so disable the optimization now.
301 if (blocking) {
302 atomic_add(&ctx->notify_me, 2);
305 have_select_revents = aio_prepare(ctx);
307 ctx->walking_handlers++;
309 /* fill fd sets */
310 count = 0;
311 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
312 if (!node->deleted && node->io_notify
313 && aio_node_check(ctx, node->is_external)) {
314 events[count++] = event_notifier_get_handle(node->e);
318 ctx->walking_handlers--;
319 first = true;
321 /* ctx->notifier is always registered. */
322 assert(count > 0);
324 /* Multiple iterations, all of them non-blocking except the first,
325 * may be necessary to process all pending events. After the first
326 * WaitForMultipleObjects call ctx->notify_me will be decremented.
328 do {
329 HANDLE event;
330 int ret;
332 timeout = blocking && !have_select_revents
333 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
334 if (timeout) {
335 aio_context_release(ctx);
337 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
338 if (blocking) {
339 assert(first);
340 atomic_sub(&ctx->notify_me, 2);
342 if (timeout) {
343 aio_context_acquire(ctx);
346 if (first) {
347 aio_notify_accept(ctx);
348 progress |= aio_bh_poll(ctx);
349 first = false;
352 /* if we have any signaled events, dispatch event */
353 event = NULL;
354 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
355 event = events[ret - WAIT_OBJECT_0];
356 events[ret - WAIT_OBJECT_0] = events[--count];
357 } else if (!have_select_revents) {
358 break;
361 have_select_revents = false;
362 blocking = false;
364 progress |= aio_dispatch_handlers(ctx, event);
365 } while (count > 0);
367 progress |= timerlistgroup_run_timers(&ctx->tlg);
369 aio_context_release(ctx);
370 return progress;
373 void aio_context_setup(AioContext *ctx, Error **errp)