1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu-common.h"
17#include "block/block.h"
18#include "qemu/queue.h"
19#include "qemu/sockets.h"
20
21struct AioHandler
22{
23 GPollFD pfd;
24 IOHandler *io_read;
25 IOHandler *io_write;
26 int deleted;
27 int pollfds_idx;
28 void *opaque;
29 QLIST_ENTRY(AioHandler) node;
30};
31
32static AioHandler *find_aio_handler(AioContext *ctx, int fd)
33{
34 AioHandler *node;
35
36 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
37 if (node->pfd.fd == fd)
38 if (!node->deleted)
39 return node;
40 }
41
42 return NULL;
43}
44
45void aio_set_fd_handler(AioContext *ctx,
46 int fd,
47 IOHandler *io_read,
48 IOHandler *io_write,
49 void *opaque)
50{
51 AioHandler *node;
52
53 node = find_aio_handler(ctx, fd);
54
55
56 if (!io_read && !io_write) {
57 if (node) {
58 g_source_remove_poll(&ctx->source, &node->pfd);
59
60
61 if (ctx->walking_handlers) {
62 node->deleted = 1;
63 node->pfd.revents = 0;
64 } else {
65
66
67
68
69 QLIST_REMOVE(node, node);
70 g_free(node);
71 }
72 }
73 } else {
74 if (node == NULL) {
75
76 node = g_new0(AioHandler, 1);
77 node->pfd.fd = fd;
78 QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
79
80 g_source_add_poll(&ctx->source, &node->pfd);
81 }
82
83 node->io_read = io_read;
84 node->io_write = io_write;
85 node->opaque = opaque;
86 node->pollfds_idx = -1;
87
88 node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
89 node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
90 }
91
92 aio_notify(ctx);
93}
94
95void aio_set_event_notifier(AioContext *ctx,
96 EventNotifier *notifier,
97 EventNotifierHandler *io_read)
98{
99 aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
100 (IOHandler *)io_read, NULL, notifier);
101}
102
103bool aio_prepare(AioContext *ctx)
104{
105 return false;
106}
107
108bool aio_pending(AioContext *ctx)
109{
110 AioHandler *node;
111
112 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
113 int revents;
114
115 revents = node->pfd.revents & node->pfd.events;
116 if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
117 return true;
118 }
119 if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
120 return true;
121 }
122 }
123
124 return false;
125}
126
127bool aio_dispatch(AioContext *ctx)
128{
129 AioHandler *node;
130 bool progress = false;
131
132
133
134
135
136
137 if (aio_bh_poll(ctx)) {
138 progress = true;
139 }
140
141
142
143
144
145 node = QLIST_FIRST(&ctx->aio_handlers);
146 while (node) {
147 AioHandler *tmp;
148 int revents;
149
150 ctx->walking_handlers++;
151
152 revents = node->pfd.revents & node->pfd.events;
153 node->pfd.revents = 0;
154
155 if (!node->deleted &&
156 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
157 node->io_read) {
158 node->io_read(node->opaque);
159
160
161 if (node->opaque != &ctx->notifier) {
162 progress = true;
163 }
164 }
165 if (!node->deleted &&
166 (revents & (G_IO_OUT | G_IO_ERR)) &&
167 node->io_write) {
168 node->io_write(node->opaque);
169 progress = true;
170 }
171
172 tmp = node;
173 node = QLIST_NEXT(node, node);
174
175 ctx->walking_handlers--;
176
177 if (!ctx->walking_handlers && tmp->deleted) {
178 QLIST_REMOVE(tmp, node);
179 g_free(tmp);
180 }
181 }
182
183
184 progress |= timerlistgroup_run_timers(&ctx->tlg);
185
186 return progress;
187}
188
189bool aio_poll(AioContext *ctx, bool blocking)
190{
191 AioHandler *node;
192 bool was_dispatching;
193 int ret;
194 bool progress;
195
196 was_dispatching = ctx->dispatching;
197 progress = false;
198
199
200
201
202
203
204
205
206
207
208
209 aio_set_dispatching(ctx, !blocking);
210
211 ctx->walking_handlers++;
212
213 g_array_set_size(ctx->pollfds, 0);
214
215
216 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
217 node->pollfds_idx = -1;
218 if (!node->deleted && node->pfd.events) {
219 GPollFD pfd = {
220 .fd = node->pfd.fd,
221 .events = node->pfd.events,
222 };
223 node->pollfds_idx = ctx->pollfds->len;
224 g_array_append_val(ctx->pollfds, pfd);
225 }
226 }
227
228 ctx->walking_handlers--;
229
230
231 ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
232 ctx->pollfds->len,
233 blocking ? aio_compute_timeout(ctx) : 0);
234
235
236 if (ret > 0) {
237 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
238 if (node->pollfds_idx != -1) {
239 GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
240 node->pollfds_idx);
241 node->pfd.revents = pfd->revents;
242 }
243 }
244 }
245
246
247 aio_set_dispatching(ctx, true);
248 if (aio_dispatch(ctx)) {
249 progress = true;
250 }
251
252 aio_set_dispatching(ctx, was_dispatching);
253 return progress;
254}
255