AioContext: run bottom halves after polling
[sdk/emulator/qemu.git] / aio-win32.c
1 /*
2  * QEMU aio implementation
3  *
4  * Copyright IBM Corp., 2008
5  * Copyright Red Hat Inc., 2012
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Paolo Bonzini     <pbonzini@redhat.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
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.
16  */
17
18 #include "qemu-common.h"
19 #include "block/block.h"
20 #include "qemu/queue.h"
21 #include "qemu/sockets.h"
22
23 struct AioHandler {
24     EventNotifier *e;
25     EventNotifierHandler *io_notify;
26     GPollFD pfd;
27     int deleted;
28     QLIST_ENTRY(AioHandler) node;
29 };
30
31 void aio_set_event_notifier(AioContext *ctx,
32                             EventNotifier *e,
33                             EventNotifierHandler *io_notify)
34 {
35     AioHandler *node;
36
37     QLIST_FOREACH(node, &ctx->aio_handlers, node) {
38         if (node->e == e && !node->deleted) {
39             break;
40         }
41     }
42
43     /* Are we deleting the fd handler? */
44     if (!io_notify) {
45         if (node) {
46             g_source_remove_poll(&ctx->source, &node->pfd);
47
48             /* If the lock is held, just mark the node as deleted */
49             if (ctx->walking_handlers) {
50                 node->deleted = 1;
51                 node->pfd.revents = 0;
52             } else {
53                 /* Otherwise, delete it for real.  We can't just mark it as
54                  * deleted because deleted nodes are only cleaned up after
55                  * releasing the walking_handlers lock.
56                  */
57                 QLIST_REMOVE(node, node);
58                 g_free(node);
59             }
60         }
61     } else {
62         if (node == NULL) {
63             /* Alloc and insert if it's not already there */
64             node = g_malloc0(sizeof(AioHandler));
65             node->e = e;
66             node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
67             node->pfd.events = G_IO_IN;
68             QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
69
70             g_source_add_poll(&ctx->source, &node->pfd);
71         }
72         /* Update handler with latest information */
73         node->io_notify = io_notify;
74     }
75
76     aio_notify(ctx);
77 }
78
79 bool aio_pending(AioContext *ctx)
80 {
81     AioHandler *node;
82
83     QLIST_FOREACH(node, &ctx->aio_handlers, node) {
84         if (node->pfd.revents && node->io_notify) {
85             return true;
86         }
87     }
88
89     return false;
90 }
91
92 static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
93 {
94     AioHandler *node;
95     bool progress = false;
96
97     /*
98      * We have to walk very carefully in case aio_set_fd_handler is
99      * called while we're walking.
100      */
101     node = QLIST_FIRST(&ctx->aio_handlers);
102     while (node) {
103         AioHandler *tmp;
104
105         ctx->walking_handlers++;
106
107         if (!node->deleted &&
108             (node->pfd.revents || event_notifier_get_handle(node->e) == event) &&
109             node->io_notify) {
110             node->pfd.revents = 0;
111             node->io_notify(node->e);
112
113             /* aio_notify() does not count as progress */
114             if (node->e != &ctx->notifier) {
115                 progress = true;
116             }
117         }
118
119         tmp = node;
120         node = QLIST_NEXT(node, node);
121
122         ctx->walking_handlers--;
123
124         if (!ctx->walking_handlers && tmp->deleted) {
125             QLIST_REMOVE(tmp, node);
126             g_free(tmp);
127         }
128     }
129
130     return progress;
131 }
132
133 static bool aio_dispatch(AioContext *ctx)
134 {
135     bool progress;
136
137     progress = aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
138     progress |= timerlistgroup_run_timers(&ctx->tlg);
139     return progress;
140 }
141
142 bool aio_poll(AioContext *ctx, bool blocking)
143 {
144     AioHandler *node;
145     HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
146     bool progress, first;
147     int count;
148     int timeout;
149
150     progress = false;
151
152     /*
153      * If there are callbacks left that have been queued, we need to call then.
154      * Do not call select in this case, because it is possible that the caller
155      * does not need a complete flush (as is the case for aio_poll loops).
156      */
157     if (aio_bh_poll(ctx)) {
158         blocking = false;
159         progress = true;
160     }
161
162     /* Dispatch any pending callbacks from the GSource.  */
163     progress |= aio_dispatch(ctx);
164
165     if (progress && !blocking) {
166         return true;
167     }
168
169     ctx->walking_handlers++;
170
171     /* fill fd sets */
172     count = 0;
173     QLIST_FOREACH(node, &ctx->aio_handlers, node) {
174         if (!node->deleted && node->io_notify) {
175             events[count++] = event_notifier_get_handle(node->e);
176         }
177     }
178
179     ctx->walking_handlers--;
180     first = true;
181
182     /* wait until next event */
183     while (count > 0) {
184         int ret;
185
186         timeout = blocking
187             ? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
188         ret = WaitForMultipleObjects(count, events, FALSE, timeout);
189
190         if (first && aio_bh_poll(ctx)) {
191             progress = true;
192         }
193         first = false;
194
195         /* if we have any signaled events, dispatch event */
196         if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
197             break;
198         }
199
200         blocking = false;
201
202         progress |= aio_dispatch_handlers(ctx, events[ret - WAIT_OBJECT_0]);
203
204         /* Try again, but only call each handler once.  */
205         events[ret - WAIT_OBJECT_0] = events[--count];
206     }
207
208     if (blocking) {
209         /* Run the timers a second time. We do this because otherwise aio_wait
210          * will not note progress - and will stop a drain early - if we have
211          * a timer that was not ready to run entering g_poll but is ready
212          * after g_poll. This will only do anything if a timer has expired.
213          */
214         progress |= timerlistgroup_run_timers(&ctx->tlg);
215     }
216
217     return progress;
218 }