2 * QEMU aio implementation
4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "block/block.h"
18 #include "qemu/queue.h"
19 #include "qemu/sockets.h"
26 AioFlushHandler *io_flush;
30 QLIST_ENTRY(AioHandler) node;
33 static AioHandler *find_aio_handler(AioContext *ctx, int fd)
37 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
38 if (node->pfd.fd == fd)
46 void aio_set_fd_handler(AioContext *ctx,
50 AioFlushHandler *io_flush,
55 node = find_aio_handler(ctx, fd);
57 /* Are we deleting the fd handler? */
58 if (!io_read && !io_write) {
60 g_source_remove_poll(&ctx->source, &node->pfd);
62 /* If the lock is held, just mark the node as deleted */
63 if (ctx->walking_handlers) {
65 node->pfd.revents = 0;
67 /* Otherwise, delete it for real. We can't just mark it as
68 * deleted because deleted nodes are only cleaned up after
69 * releasing the walking_handlers lock.
71 QLIST_REMOVE(node, node);
77 /* Alloc and insert if it's not already there */
78 node = g_malloc0(sizeof(AioHandler));
80 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
82 g_source_add_poll(&ctx->source, &node->pfd);
84 /* Update handler with latest information */
85 node->io_read = io_read;
86 node->io_write = io_write;
87 node->io_flush = io_flush;
88 node->opaque = opaque;
89 node->pollfds_idx = -1;
91 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
92 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
98 void aio_set_event_notifier(AioContext *ctx,
99 EventNotifier *notifier,
100 EventNotifierHandler *io_read,
101 AioFlushEventNotifierHandler *io_flush)
103 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
104 (IOHandler *)io_read, NULL,
105 (AioFlushHandler *)io_flush, notifier);
108 bool aio_pending(AioContext *ctx)
112 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
115 revents = node->pfd.revents & node->pfd.events;
116 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
119 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
127 static bool aio_dispatch(AioContext *ctx)
130 bool progress = false;
133 * We have to walk very carefully in case qemu_aio_set_fd_handler is
134 * called while we're walking.
136 node = QLIST_FIRST(&ctx->aio_handlers);
141 ctx->walking_handlers++;
143 revents = node->pfd.revents & node->pfd.events;
144 node->pfd.revents = 0;
146 if (!node->deleted &&
147 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
149 node->io_read(node->opaque);
152 if (!node->deleted &&
153 (revents & (G_IO_OUT | G_IO_ERR)) &&
155 node->io_write(node->opaque);
160 node = QLIST_NEXT(node, node);
162 ctx->walking_handlers--;
164 if (!ctx->walking_handlers && tmp->deleted) {
165 QLIST_REMOVE(tmp, node);
172 bool aio_poll(AioContext *ctx, bool blocking)
181 * If there are callbacks left that have been queued, we need to call them.
182 * Do not call select in this case, because it is possible that the caller
183 * does not need a complete flush (as is the case for qemu_aio_wait loops).
185 if (aio_bh_poll(ctx)) {
190 if (aio_dispatch(ctx)) {
194 if (progress && !blocking) {
198 ctx->walking_handlers++;
200 g_array_set_size(ctx->pollfds, 0);
204 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
205 node->pollfds_idx = -1;
207 /* If there aren't pending AIO operations, don't invoke callbacks.
208 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
211 if (!node->deleted && node->io_flush) {
212 if (node->io_flush(node->opaque) == 0) {
217 if (!node->deleted && node->pfd.events) {
220 .events = node->pfd.events,
222 node->pollfds_idx = ctx->pollfds->len;
223 g_array_append_val(ctx->pollfds, pfd);
227 ctx->walking_handlers--;
229 /* No AIO operations? Get us out of here */
234 /* wait until next event */
235 ret = g_poll((GPollFD *)ctx->pollfds->data,
239 /* if we have any readable fds, dispatch event */
241 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
242 if (node->pollfds_idx != -1) {
243 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
245 node->pfd.revents = pfd->revents;
248 if (aio_dispatch(ctx)) {
253 assert(progress || busy);