1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "qemu/osdep.h"
19#include "block/block.h"
20#include "qemu/main-loop.h"
21#include "qemu/queue.h"
22#include "qemu/sockets.h"
23#include "qapi/error.h"
24#include "qemu/rcu_queue.h"
25#include "qemu/error-report.h"
26
27struct AioHandler {
28 EventNotifier *e;
29 IOHandler *io_read;
30 IOHandler *io_write;
31 EventNotifierHandler *io_notify;
32 GPollFD pfd;
33 int deleted;
34 void *opaque;
35 bool is_external;
36 QLIST_ENTRY(AioHandler) node;
37};
38
39static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
40{
41
42
43
44
45
46
47 if (!g_source_is_destroyed(&ctx->source)) {
48 g_source_remove_poll(&ctx->source, &node->pfd);
49 }
50
51
52 if (qemu_lockcnt_count(&ctx->list_lock)) {
53 node->deleted = 1;
54 node->pfd.revents = 0;
55 } else {
56
57
58
59
60 QLIST_REMOVE(node, node);
61 g_free(node);
62 }
63}
64
65void aio_set_fd_handler(AioContext *ctx,
66 int fd,
67 bool is_external,
68 IOHandler *io_read,
69 IOHandler *io_write,
70 AioPollFn *io_poll,
71 IOHandler *io_poll_ready,
72 void *opaque)
73{
74 AioHandler *old_node;
75 AioHandler *node = NULL;
76 SOCKET s;
77
78 if (!fd_is_socket(fd)) {
79 error_report("fd=%d is not a socket, AIO implementation is missing", fd);
80 return;
81 }
82
83 s = _get_osfhandle(fd);
84
85 qemu_lockcnt_lock(&ctx->list_lock);
86 QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {
87 if (old_node->pfd.fd == s && !old_node->deleted) {
88 break;
89 }
90 }
91
92 if (io_read || io_write) {
93 HANDLE event;
94 long bitmask = 0;
95
96
97 node = g_new0(AioHandler, 1);
98 node->pfd.fd = s;
99
100 node->pfd.events = 0;
101 if (node->io_read) {
102 node->pfd.events |= G_IO_IN;
103 }
104 if (node->io_write) {
105 node->pfd.events |= G_IO_OUT;
106 }
107
108 node->e = &ctx->notifier;
109
110
111 node->opaque = opaque;
112 node->io_read = io_read;
113 node->io_write = io_write;
114 node->is_external = is_external;
115
116 if (io_read) {
117 bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
118 }
119
120 if (io_write) {
121 bitmask |= FD_WRITE | FD_CONNECT;
122 }
123
124 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
125 event = event_notifier_get_handle(&ctx->notifier);
126 qemu_socket_select(fd, event, bitmask, NULL);
127 }
128 if (old_node) {
129 aio_remove_fd_handler(ctx, old_node);
130 }
131
132 qemu_lockcnt_unlock(&ctx->list_lock);
133 aio_notify(ctx);
134}
135
136void aio_set_event_notifier(AioContext *ctx,
137 EventNotifier *e,
138 bool is_external,
139 EventNotifierHandler *io_notify,
140 AioPollFn *io_poll,
141 EventNotifierHandler *io_poll_ready)
142{
143 AioHandler *node;
144
145 qemu_lockcnt_lock(&ctx->list_lock);
146 QLIST_FOREACH(node, &ctx->aio_handlers, node) {
147 if (node->e == e && !node->deleted) {
148 break;
149 }
150 }
151
152
153 if (!io_notify) {
154 if (node) {
155 aio_remove_fd_handler(ctx, node);
156 }
157 } else {
158 if (node == NULL) {
159
160 node = g_new0(AioHandler, 1);
161 node->e = e;
162 node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
163 node->pfd.events = G_IO_IN;
164 node->is_external = is_external;
165 QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
166
167 g_source_add_poll(&ctx->source, &node->pfd);
168 }
169
170 node->io_notify = io_notify;
171 }
172
173 qemu_lockcnt_unlock(&ctx->list_lock);
174 aio_notify(ctx);
175}
176
177void aio_set_event_notifier_poll(AioContext *ctx,
178 EventNotifier *notifier,
179 EventNotifierHandler *io_poll_begin,
180 EventNotifierHandler *io_poll_end)
181{
182
183}
184
185bool aio_prepare(AioContext *ctx)
186{
187 static struct timeval tv0;
188 AioHandler *node;
189 bool have_select_revents = false;
190 fd_set rfds, wfds;
191
192
193
194
195
196 qemu_lockcnt_inc(&ctx->list_lock);
197
198
199 FD_ZERO(&rfds);
200 FD_ZERO(&wfds);
201 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
202 if (node->io_read) {
203 FD_SET ((SOCKET)node->pfd.fd, &rfds);
204 }
205 if (node->io_write) {
206 FD_SET ((SOCKET)node->pfd.fd, &wfds);
207 }
208 }
209
210 if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
211 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
212 node->pfd.revents = 0;
213 if (FD_ISSET(node->pfd.fd, &rfds)) {
214 node->pfd.revents |= G_IO_IN;
215 have_select_revents = true;
216 }
217
218 if (FD_ISSET(node->pfd.fd, &wfds)) {
219 node->pfd.revents |= G_IO_OUT;
220 have_select_revents = true;
221 }
222 }
223 }
224
225 qemu_lockcnt_dec(&ctx->list_lock);
226 return have_select_revents;
227}
228
229bool aio_pending(AioContext *ctx)
230{
231 AioHandler *node;
232 bool result = false;
233
234
235
236
237
238 qemu_lockcnt_inc(&ctx->list_lock);
239 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
240 if (node->pfd.revents && node->io_notify) {
241 result = true;
242 break;
243 }
244
245 if ((node->pfd.revents & G_IO_IN) && node->io_read) {
246 result = true;
247 break;
248 }
249 if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
250 result = true;
251 break;
252 }
253 }
254
255 qemu_lockcnt_dec(&ctx->list_lock);
256 return result;
257}
258
259static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
260{
261 AioHandler *node;
262 bool progress = false;
263 AioHandler *tmp;
264
265
266
267
268
269 QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
270 int revents = node->pfd.revents;
271
272 if (!node->deleted &&
273 (revents || event_notifier_get_handle(node->e) == event) &&
274 node->io_notify) {
275 node->pfd.revents = 0;
276 node->io_notify(node->e);
277
278
279 if (node->e != &ctx->notifier) {
280 progress = true;
281 }
282 }
283
284 if (!node->deleted &&
285 (node->io_read || node->io_write)) {
286 node->pfd.revents = 0;
287 if ((revents & G_IO_IN) && node->io_read) {
288 node->io_read(node->opaque);
289 progress = true;
290 }
291 if ((revents & G_IO_OUT) && node->io_write) {
292 node->io_write(node->opaque);
293 progress = true;
294 }
295
296
297 if (event == event_notifier_get_handle(&ctx->notifier)) {
298 WSANETWORKEVENTS ev;
299 WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
300 if (ev.lNetworkEvents) {
301 progress = true;
302 }
303 }
304 }
305
306 if (node->deleted) {
307 if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
308 QLIST_REMOVE(node, node);
309 g_free(node);
310 qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
311 }
312 }
313 }
314
315 return progress;
316}
317
318void aio_dispatch(AioContext *ctx)
319{
320 qemu_lockcnt_inc(&ctx->list_lock);
321 aio_bh_poll(ctx);
322 aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
323 qemu_lockcnt_dec(&ctx->list_lock);
324 timerlistgroup_run_timers(&ctx->tlg);
325}
326
327bool aio_poll(AioContext *ctx, bool blocking)
328{
329 AioHandler *node;
330 HANDLE events[MAXIMUM_WAIT_OBJECTS];
331 bool progress, have_select_revents, first;
332 unsigned count;
333 int timeout;
334
335
336
337
338
339
340
341
342
343
344 assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
345 qemu_get_aio_context() : ctx));
346 progress = false;
347
348
349
350
351
352
353
354
355 if (blocking) {
356 qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
357
358
359
360
361
362 smp_mb();
363 }
364
365 qemu_lockcnt_inc(&ctx->list_lock);
366 have_select_revents = aio_prepare(ctx);
367
368
369 count = 0;
370 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
371 if (!node->deleted && node->io_notify
372 && aio_node_check(ctx, node->is_external)) {
373 assert(count < MAXIMUM_WAIT_OBJECTS);
374 events[count++] = event_notifier_get_handle(node->e);
375 }
376 }
377
378 first = true;
379
380
381 assert(count > 0);
382
383
384
385
386
387 do {
388 HANDLE event;
389 int ret;
390
391 timeout = blocking && !have_select_revents
392 ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
393 ret = WaitForMultipleObjects(count, events, FALSE, timeout);
394 if (blocking) {
395 assert(first);
396 qatomic_store_release(&ctx->notify_me,
397 qatomic_read(&ctx->notify_me) - 2);
398 aio_notify_accept(ctx);
399 }
400
401 if (first) {
402 progress |= aio_bh_poll(ctx);
403 first = false;
404 }
405
406
407 event = NULL;
408 if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
409 event = events[ret - WAIT_OBJECT_0];
410 events[ret - WAIT_OBJECT_0] = events[--count];
411 } else if (!have_select_revents) {
412 break;
413 }
414
415 have_select_revents = false;
416 blocking = false;
417
418 progress |= aio_dispatch_handlers(ctx, event);
419 } while (count > 0);
420
421 qemu_lockcnt_dec(&ctx->list_lock);
422
423 progress |= timerlistgroup_run_timers(&ctx->tlg);
424 return progress;
425}
426
427void aio_context_setup(AioContext *ctx)
428{
429}
430
431void aio_context_destroy(AioContext *ctx)
432{
433}
434
435void aio_context_use_g_source(AioContext *ctx)
436{
437}
438
439void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
440 int64_t grow, int64_t shrink, Error **errp)
441{
442 if (max_ns) {
443 error_setg(errp, "AioContext polling is not implemented on Windows");
444 }
445}
446
447void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch,
448 Error **errp)
449{
450}
451