]> git.proxmox.com Git - qemu.git/blob - aio.c
aio: add non-blocking variant of aio_wait
[qemu.git] / aio.c
1 /*
2 * QEMU aio implementation
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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.
14 */
15
16 #include "qemu-common.h"
17 #include "block.h"
18 #include "qemu-queue.h"
19 #include "qemu_socket.h"
20
21 struct AioHandler
22 {
23 int fd;
24 IOHandler *io_read;
25 IOHandler *io_write;
26 AioFlushHandler *io_flush;
27 int deleted;
28 void *opaque;
29 QLIST_ENTRY(AioHandler) node;
30 };
31
32 static AioHandler *find_aio_handler(AioContext *ctx, int fd)
33 {
34 AioHandler *node;
35
36 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
37 if (node->fd == fd)
38 if (!node->deleted)
39 return node;
40 }
41
42 return NULL;
43 }
44
45 void aio_set_fd_handler(AioContext *ctx,
46 int fd,
47 IOHandler *io_read,
48 IOHandler *io_write,
49 AioFlushHandler *io_flush,
50 void *opaque)
51 {
52 AioHandler *node;
53
54 node = find_aio_handler(ctx, fd);
55
56 /* Are we deleting the fd handler? */
57 if (!io_read && !io_write) {
58 if (node) {
59 /* If the lock is held, just mark the node as deleted */
60 if (ctx->walking_handlers)
61 node->deleted = 1;
62 else {
63 /* Otherwise, delete it for real. We can't just mark it as
64 * deleted because deleted nodes are only cleaned up after
65 * releasing the walking_handlers lock.
66 */
67 QLIST_REMOVE(node, node);
68 g_free(node);
69 }
70 }
71 } else {
72 if (node == NULL) {
73 /* Alloc and insert if it's not already there */
74 node = g_malloc0(sizeof(AioHandler));
75 node->fd = fd;
76 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
77 }
78 /* Update handler with latest information */
79 node->io_read = io_read;
80 node->io_write = io_write;
81 node->io_flush = io_flush;
82 node->opaque = opaque;
83 }
84 }
85
86 void aio_set_event_notifier(AioContext *ctx,
87 EventNotifier *notifier,
88 EventNotifierHandler *io_read,
89 AioFlushEventNotifierHandler *io_flush)
90 {
91 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
92 (IOHandler *)io_read, NULL,
93 (AioFlushHandler *)io_flush, notifier);
94 }
95
96 bool aio_poll(AioContext *ctx, bool blocking)
97 {
98 static struct timeval tv0;
99 AioHandler *node;
100 fd_set rdfds, wrfds;
101 int max_fd = -1;
102 int ret;
103 bool busy, progress;
104
105 progress = false;
106
107 /*
108 * If there are callbacks left that have been queued, we need to call then.
109 * Do not call select in this case, because it is possible that the caller
110 * does not need a complete flush (as is the case for qemu_aio_wait loops).
111 */
112 if (aio_bh_poll(ctx)) {
113 blocking = false;
114 progress = true;
115 }
116
117 if (progress && !blocking) {
118 return true;
119 }
120
121 ctx->walking_handlers++;
122
123 FD_ZERO(&rdfds);
124 FD_ZERO(&wrfds);
125
126 /* fill fd sets */
127 busy = false;
128 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
129 /* If there aren't pending AIO operations, don't invoke callbacks.
130 * Otherwise, if there are no AIO requests, qemu_aio_wait() would
131 * wait indefinitely.
132 */
133 if (!node->deleted && node->io_flush) {
134 if (node->io_flush(node->opaque) == 0) {
135 continue;
136 }
137 busy = true;
138 }
139 if (!node->deleted && node->io_read) {
140 FD_SET(node->fd, &rdfds);
141 max_fd = MAX(max_fd, node->fd + 1);
142 }
143 if (!node->deleted && node->io_write) {
144 FD_SET(node->fd, &wrfds);
145 max_fd = MAX(max_fd, node->fd + 1);
146 }
147 }
148
149 ctx->walking_handlers--;
150
151 /* No AIO operations? Get us out of here */
152 if (!busy) {
153 return progress;
154 }
155
156 /* wait until next event */
157 ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
158
159 /* if we have any readable fds, dispatch event */
160 if (ret > 0) {
161 /* we have to walk very carefully in case
162 * qemu_aio_set_fd_handler is called while we're walking */
163 node = QLIST_FIRST(&ctx->aio_handlers);
164 while (node) {
165 AioHandler *tmp;
166
167 ctx->walking_handlers++;
168
169 if (!node->deleted &&
170 FD_ISSET(node->fd, &rdfds) &&
171 node->io_read) {
172 progress = true;
173 node->io_read(node->opaque);
174 }
175 if (!node->deleted &&
176 FD_ISSET(node->fd, &wrfds) &&
177 node->io_write) {
178 progress = true;
179 node->io_write(node->opaque);
180 }
181
182 tmp = node;
183 node = QLIST_NEXT(node, node);
184
185 ctx->walking_handlers--;
186
187 if (!ctx->walking_handlers && tmp->deleted) {
188 QLIST_REMOVE(tmp, node);
189 g_free(tmp);
190 }
191 }
192 }
193
194 return progress;
195 }