2 * QEMU aio implementation
4 * Copyright IBM Corp., 2008
5 * Copyright Red Hat Inc., 2012
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"
29 EventNotifierHandler *io_notify;
34 QLIST_ENTRY(AioHandler) node;
37 void aio_set_fd_handler(AioContext *ctx,
45 /* fd is a SOCKET in our case */
48 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
49 if (node->pfd.fd == fd && !node->deleted) {
54 /* Are we deleting the fd handler? */
55 if (!io_read && !io_write) {
57 /* If the lock is held, just mark the node as deleted */
58 if (ctx->walking_handlers) {
60 node->pfd.revents = 0;
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);
74 /* Alloc and insert if it's not already there */
75 node = g_new0(AioHandler, 1);
77 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
82 node->pfd.events |= G_IO_IN;
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);
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,
115 EventNotifierHandler *io_notify,
120 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
121 if (node->e == e && !node->deleted) {
126 /* Are we deleting the fd handler? */
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) {
134 node->pfd.revents = 0;
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);
146 /* Alloc and insert if it's not already there */
147 node = g_new0(AioHandler, 1);
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;
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;
175 bool have_select_revents = false;
181 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
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)
212 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
213 if (node->pfd.revents && node->io_notify) {
217 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
220 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
228 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
231 bool progress = false;
234 * We have to walk very carefully in case aio_set_fd_handler is
235 * called while we're walking.
237 node = QLIST_FIRST(&ctx->aio_handlers);
240 int revents = node->pfd.revents;
242 ctx->walking_handlers++;
244 if (!node->deleted &&
245 (revents || event_notifier_get_handle(node->e) == event) &&
247 node->pfd.revents = 0;
248 node->io_notify(node->e);
250 /* aio_notify() does not count as progress */
251 if (node->e != &ctx->notifier) {
256 if (!node->deleted &&
257 (node->io_read || node->io_write)) {
258 node->pfd.revents = 0;
259 if ((revents & G_IO_IN) && node->io_read) {
260 node->io_read(node->opaque);
263 if ((revents & G_IO_OUT) && node->io_write) {
264 node->io_write(node->opaque);
268 /* if the next select() will return an event, we have progressed */
269 if (event == event_notifier_get_handle(&ctx->notifier)) {
271 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
272 if (ev.lNetworkEvents) {
279 node = QLIST_NEXT(node, node);
281 ctx->walking_handlers--;
283 if (!ctx->walking_handlers && tmp->deleted) {
284 QLIST_REMOVE(tmp, node);
292 bool aio_dispatch(AioContext *ctx, bool dispatch_fds)
296 progress = aio_bh_poll(ctx);
298 progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
300 progress |= timerlistgroup_run_timers(&ctx->tlg);
304 bool aio_poll(AioContext *ctx, bool blocking)
307 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
308 bool progress, have_select_revents, first;
312 aio_context_acquire(ctx);
315 /* aio_notify can avoid the expensive event_notifier_set if
316 * everything (file descriptors, bottom halves, timers) will
317 * be re-evaluated before the next blocking poll(). This is
318 * already true when aio_poll is called with blocking == false;
319 * if blocking == true, it is only true after poll() returns,
320 * so disable the optimization now.
323 atomic_add(&ctx->notify_me, 2);
326 have_select_revents = aio_prepare(ctx);
328 ctx->walking_handlers++;
332 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
333 if (!node->deleted && node->io_notify
334 && aio_node_check(ctx, node->is_external)) {
335 events[count++] = event_notifier_get_handle(node->e);
339 ctx->walking_handlers--;
342 /* ctx->notifier is always registered. */
345 /* Multiple iterations, all of them non-blocking except the first,
346 * may be necessary to process all pending events. After the first
347 * WaitForMultipleObjects call ctx->notify_me will be decremented.
353 timeout = blocking && !have_select_revents
354 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
356 aio_context_release(ctx);
358 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
361 atomic_sub(&ctx->notify_me, 2);
364 aio_context_acquire(ctx);
368 aio_notify_accept(ctx);
369 progress |= aio_bh_poll(ctx);
373 /* if we have any signaled events, dispatch event */
375 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
376 event = events[ret - WAIT_OBJECT_0];
377 events[ret - WAIT_OBJECT_0] = events[--count];
378 } else if (!have_select_revents) {
382 have_select_revents = false;
385 progress |= aio_dispatch_handlers(ctx, event);
388 progress |= timerlistgroup_run_timers(&ctx->tlg);
390 aio_context_release(ctx);
394 void aio_context_setup(AioContext *ctx)
398 void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
399 int64_t grow, int64_t shrink, Error **errp)
401 error_setg(errp, "AioContext polling is not implemented on Windows");