]>
Commit | Line | Data |
---|---|---|
a76bab49 AL |
1 | /* |
2 | * QEMU aio implementation | |
3 | * | |
f42b2207 PB |
4 | * Copyright IBM Corp., 2008 |
5 | * Copyright Red Hat Inc., 2012 | |
a76bab49 AL |
6 | * |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
f42b2207 | 9 | * Paolo Bonzini <pbonzini@redhat.com> |
a76bab49 AL |
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 | * | |
6b620ca3 PB |
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. | |
a76bab49 AL |
16 | */ |
17 | ||
18 | #include "qemu-common.h" | |
737e150e | 19 | #include "block/block.h" |
1de7afc9 PB |
20 | #include "qemu/queue.h" |
21 | #include "qemu/sockets.h" | |
a76bab49 | 22 | |
f42b2207 PB |
23 | struct AioHandler { |
24 | EventNotifier *e; | |
25 | EventNotifierHandler *io_notify; | |
cd9ba1eb | 26 | GPollFD pfd; |
a76bab49 | 27 | int deleted; |
72cf2d4f | 28 | QLIST_ENTRY(AioHandler) node; |
a76bab49 AL |
29 | }; |
30 | ||
f42b2207 PB |
31 | void aio_set_event_notifier(AioContext *ctx, |
32 | EventNotifier *e, | |
f2e5dca4 | 33 | EventNotifierHandler *io_notify) |
a76bab49 AL |
34 | { |
35 | AioHandler *node; | |
36 | ||
a915f4bc | 37 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
f42b2207 PB |
38 | if (node->e == e && !node->deleted) { |
39 | break; | |
40 | } | |
a76bab49 AL |
41 | } |
42 | ||
a76bab49 | 43 | /* Are we deleting the fd handler? */ |
f42b2207 | 44 | if (!io_notify) { |
a76bab49 | 45 | if (node) { |
e3713e00 PB |
46 | g_source_remove_poll(&ctx->source, &node->pfd); |
47 | ||
a76bab49 | 48 | /* If the lock is held, just mark the node as deleted */ |
cd9ba1eb | 49 | if (ctx->walking_handlers) { |
a76bab49 | 50 | node->deleted = 1; |
cd9ba1eb PB |
51 | node->pfd.revents = 0; |
52 | } else { | |
a76bab49 AL |
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 | */ | |
72cf2d4f | 57 | QLIST_REMOVE(node, node); |
7267c094 | 58 | g_free(node); |
a76bab49 AL |
59 | } |
60 | } | |
61 | } else { | |
62 | if (node == NULL) { | |
63 | /* Alloc and insert if it's not already there */ | |
7267c094 | 64 | node = g_malloc0(sizeof(AioHandler)); |
f42b2207 PB |
65 | node->e = e; |
66 | node->pfd.fd = (uintptr_t)event_notifier_get_handle(e); | |
67 | node->pfd.events = G_IO_IN; | |
a915f4bc | 68 | QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node); |
e3713e00 PB |
69 | |
70 | g_source_add_poll(&ctx->source, &node->pfd); | |
a76bab49 AL |
71 | } |
72 | /* Update handler with latest information */ | |
f42b2207 | 73 | node->io_notify = io_notify; |
a76bab49 | 74 | } |
7ed2b24c PB |
75 | |
76 | aio_notify(ctx); | |
9958c351 PB |
77 | } |
78 | ||
cd9ba1eb PB |
79 | bool aio_pending(AioContext *ctx) |
80 | { | |
81 | AioHandler *node; | |
82 | ||
83 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { | |
f42b2207 | 84 | if (node->pfd.revents && node->io_notify) { |
cd9ba1eb PB |
85 | return true; |
86 | } | |
87 | } | |
88 | ||
89 | return false; | |
90 | } | |
91 | ||
7c0628b2 | 92 | bool aio_poll(AioContext *ctx, bool blocking) |
a76bab49 | 93 | { |
9eb0bfca | 94 | AioHandler *node; |
f42b2207 | 95 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
164a101f | 96 | bool progress; |
f42b2207 | 97 | int count; |
7c0628b2 PB |
98 | |
99 | progress = false; | |
a76bab49 | 100 | |
8febfa26 KW |
101 | /* |
102 | * If there are callbacks left that have been queued, we need to call then. | |
bcdc1857 PB |
103 | * Do not call select in this case, because it is possible that the caller |
104 | * does not need a complete flush (as is the case for qemu_aio_wait loops). | |
8febfa26 | 105 | */ |
a915f4bc | 106 | if (aio_bh_poll(ctx)) { |
7c0628b2 PB |
107 | blocking = false; |
108 | progress = true; | |
109 | } | |
110 | ||
cd9ba1eb PB |
111 | /* |
112 | * Then dispatch any pending callbacks from the GSource. | |
113 | * | |
114 | * We have to walk very carefully in case qemu_aio_set_fd_handler is | |
115 | * called while we're walking. | |
116 | */ | |
117 | node = QLIST_FIRST(&ctx->aio_handlers); | |
118 | while (node) { | |
119 | AioHandler *tmp; | |
cd9ba1eb PB |
120 | |
121 | ctx->walking_handlers++; | |
122 | ||
f42b2207 PB |
123 | if (node->pfd.revents && node->io_notify) { |
124 | node->pfd.revents = 0; | |
125 | node->io_notify(node->e); | |
164a101f SH |
126 | |
127 | /* aio_notify() does not count as progress */ | |
128 | if (node->opaque != &ctx->notifier) { | |
129 | progress = true; | |
130 | } | |
cd9ba1eb PB |
131 | } |
132 | ||
133 | tmp = node; | |
134 | node = QLIST_NEXT(node, node); | |
135 | ||
136 | ctx->walking_handlers--; | |
137 | ||
138 | if (!ctx->walking_handlers && tmp->deleted) { | |
139 | QLIST_REMOVE(tmp, node); | |
140 | g_free(tmp); | |
141 | } | |
142 | } | |
143 | ||
7c0628b2 | 144 | if (progress && !blocking) { |
bcdc1857 | 145 | return true; |
bafbd6a1 | 146 | } |
8febfa26 | 147 | |
a915f4bc | 148 | ctx->walking_handlers++; |
a76bab49 | 149 | |
9eb0bfca | 150 | /* fill fd sets */ |
f42b2207 | 151 | count = 0; |
a915f4bc | 152 | QLIST_FOREACH(node, &ctx->aio_handlers, node) { |
f42b2207 PB |
153 | if (!node->deleted && node->io_notify) { |
154 | events[count++] = event_notifier_get_handle(node->e); | |
9eb0bfca PB |
155 | } |
156 | } | |
a76bab49 | 157 | |
a915f4bc | 158 | ctx->walking_handlers--; |
a76bab49 | 159 | |
164a101f SH |
160 | /* early return if we only have the aio_notify() fd */ |
161 | if (count == 1) { | |
7c0628b2 | 162 | return progress; |
9eb0bfca | 163 | } |
a76bab49 | 164 | |
9eb0bfca | 165 | /* wait until next event */ |
b022b4a4 | 166 | while (count > 0) { |
f42b2207 PB |
167 | int timeout = blocking ? INFINITE : 0; |
168 | int ret = WaitForMultipleObjects(count, events, FALSE, timeout); | |
169 | ||
170 | /* if we have any signaled events, dispatch event */ | |
171 | if ((DWORD) (ret - WAIT_OBJECT_0) >= count) { | |
172 | break; | |
173 | } | |
174 | ||
175 | blocking = false; | |
9eb0bfca | 176 | |
9eb0bfca PB |
177 | /* we have to walk very carefully in case |
178 | * qemu_aio_set_fd_handler is called while we're walking */ | |
a915f4bc | 179 | node = QLIST_FIRST(&ctx->aio_handlers); |
9eb0bfca PB |
180 | while (node) { |
181 | AioHandler *tmp; | |
182 | ||
a915f4bc | 183 | ctx->walking_handlers++; |
2db2bfc0 | 184 | |
9eb0bfca | 185 | if (!node->deleted && |
f42b2207 PB |
186 | event_notifier_get_handle(node->e) == events[ret - WAIT_OBJECT_0] && |
187 | node->io_notify) { | |
188 | node->io_notify(node->e); | |
164a101f SH |
189 | |
190 | /* aio_notify() does not count as progress */ | |
191 | if (node->opaque != &ctx->notifier) { | |
192 | progress = true; | |
193 | } | |
a76bab49 AL |
194 | } |
195 | ||
9eb0bfca PB |
196 | tmp = node; |
197 | node = QLIST_NEXT(node, node); | |
198 | ||
a915f4bc | 199 | ctx->walking_handlers--; |
2db2bfc0 | 200 | |
a915f4bc | 201 | if (!ctx->walking_handlers && tmp->deleted) { |
9eb0bfca PB |
202 | QLIST_REMOVE(tmp, node); |
203 | g_free(tmp); | |
204 | } | |
a76bab49 | 205 | } |
b022b4a4 PB |
206 | |
207 | /* Try again, but only call each handler once. */ | |
208 | events[ret - WAIT_OBJECT_0] = events[--count]; | |
9eb0bfca | 209 | } |
bcdc1857 | 210 | |
164a101f | 211 | return progress; |
a76bab49 | 212 | } |