]> git.ipfire.org Git - thirdparty/qemu.git/blame - async.c
mac_dbdma: always initialize channel field in DBDMA_channel
[thirdparty/qemu.git] / async.c
CommitLineData
4f999d05
KW
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu-common.h"
737e150e 26#include "block/aio.h"
9b34277d 27#include "block/thread-pool.h"
1de7afc9 28#include "qemu/main-loop.h"
0ceb849b 29#include "qemu/atomic.h"
9a1e9481 30
4f999d05
KW
31/***********************************************************/
32/* bottom halves (can be seen as timers which expire ASAP) */
33
34struct QEMUBH {
2f4dc3c1 35 AioContext *ctx;
4f999d05
KW
36 QEMUBHFunc *cb;
37 void *opaque;
4f999d05 38 QEMUBH *next;
9b47b17e
SW
39 bool scheduled;
40 bool idle;
41 bool deleted;
4f999d05
KW
42};
43
f627aab1 44QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
4f999d05
KW
45{
46 QEMUBH *bh;
ee82310f
PB
47 bh = g_new(QEMUBH, 1);
48 *bh = (QEMUBH){
49 .ctx = ctx,
50 .cb = cb,
51 .opaque = opaque,
52 };
dcc772e2 53 qemu_mutex_lock(&ctx->bh_lock);
f627aab1 54 bh->next = ctx->first_bh;
dcc772e2
LPF
55 /* Make sure that the members are ready before putting bh into list */
56 smp_wmb();
f627aab1 57 ctx->first_bh = bh;
dcc772e2 58 qemu_mutex_unlock(&ctx->bh_lock);
4f999d05
KW
59 return bh;
60}
61
df281b80
PD
62void aio_bh_call(QEMUBH *bh)
63{
64 bh->cb(bh->opaque);
65}
66
dcc772e2 67/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
f627aab1 68int aio_bh_poll(AioContext *ctx)
4f999d05 69{
7887f620 70 QEMUBH *bh, **bhp, *next;
4f999d05 71 int ret;
648fb0ea 72
f627aab1 73 ctx->walking_bh++;
4f999d05
KW
74
75 ret = 0;
f627aab1 76 for (bh = ctx->first_bh; bh; bh = next) {
dcc772e2
LPF
77 /* Make sure that fetching bh happens before accessing its members */
78 smp_read_barrier_depends();
7887f620 79 next = bh->next;
e8d3b1a2
PB
80 /* The atomic_xchg is paired with the one in qemu_bh_schedule. The
81 * implicit memory barrier ensures that the callback sees all writes
82 * done by the scheduling thread. It also ensures that the scheduling
83 * thread sees the zero before bh->cb has run, and thus will call
84 * aio_notify again if necessary.
85 */
86 if (!bh->deleted && atomic_xchg(&bh->scheduled, 0)) {
ca96ac44
SH
87 /* Idle BHs and the notify BH don't count as progress */
88 if (!bh->idle && bh != ctx->notify_dummy_bh) {
4f999d05 89 ret = 1;
ca96ac44 90 }
4f999d05 91 bh->idle = 0;
df281b80 92 aio_bh_call(bh);
4f999d05
KW
93 }
94 }
95
f627aab1 96 ctx->walking_bh--;
648fb0ea 97
4f999d05 98 /* remove deleted bhs */
f627aab1 99 if (!ctx->walking_bh) {
dcc772e2 100 qemu_mutex_lock(&ctx->bh_lock);
f627aab1 101 bhp = &ctx->first_bh;
648fb0ea
KW
102 while (*bhp) {
103 bh = *bhp;
104 if (bh->deleted) {
105 *bhp = bh->next;
106 g_free(bh);
107 } else {
108 bhp = &bh->next;
109 }
110 }
dcc772e2 111 qemu_mutex_unlock(&ctx->bh_lock);
4f999d05
KW
112 }
113
114 return ret;
115}
116
117void qemu_bh_schedule_idle(QEMUBH *bh)
118{
4f999d05 119 bh->idle = 1;
dcc772e2
LPF
120 /* Make sure that idle & any writes needed by the callback are done
121 * before the locations are read in the aio_bh_poll.
122 */
e8d3b1a2 123 atomic_mb_set(&bh->scheduled, 1);
4f999d05
KW
124}
125
126void qemu_bh_schedule(QEMUBH *bh)
127{
924fe129
SH
128 AioContext *ctx;
129
924fe129 130 ctx = bh->ctx;
4f999d05 131 bh->idle = 0;
e8d3b1a2 132 /* The memory barrier implicit in atomic_xchg makes sure that:
924fe129
SH
133 * 1. idle & any writes needed by the callback are done before the
134 * locations are read in the aio_bh_poll.
135 * 2. ctx is loaded before scheduled is set and the callback has a chance
136 * to execute.
dcc772e2 137 */
e8d3b1a2
PB
138 if (atomic_xchg(&bh->scheduled, 1) == 0) {
139 aio_notify(ctx);
140 }
4f999d05
KW
141}
142
dcc772e2
LPF
143
144/* This func is async.
145 */
4f999d05
KW
146void qemu_bh_cancel(QEMUBH *bh)
147{
148 bh->scheduled = 0;
149}
150
dcc772e2
LPF
151/* This func is async.The bottom half will do the delete action at the finial
152 * end.
153 */
4f999d05
KW
154void qemu_bh_delete(QEMUBH *bh)
155{
156 bh->scheduled = 0;
157 bh->deleted = 1;
158}
159
845ca10d
PB
160int64_t
161aio_compute_timeout(AioContext *ctx)
4f999d05 162{
845ca10d
PB
163 int64_t deadline;
164 int timeout = -1;
4f999d05
KW
165 QEMUBH *bh;
166
f627aab1 167 for (bh = ctx->first_bh; bh; bh = bh->next) {
4f999d05
KW
168 if (!bh->deleted && bh->scheduled) {
169 if (bh->idle) {
170 /* idle bottom halves will be polled at least
171 * every 10ms */
845ca10d 172 timeout = 10000000;
4f999d05
KW
173 } else {
174 /* non-idle bottom halves will be executed
175 * immediately */
845ca10d 176 return 0;
4f999d05
KW
177 }
178 }
179 }
e3713e00 180
845ca10d 181 deadline = timerlistgroup_deadline_ns(&ctx->tlg);
533a8cf3 182 if (deadline == 0) {
845ca10d 183 return 0;
533a8cf3 184 } else {
845ca10d 185 return qemu_soonest_timeout(timeout, deadline);
533a8cf3 186 }
845ca10d 187}
533a8cf3 188
845ca10d
PB
189static gboolean
190aio_ctx_prepare(GSource *source, gint *timeout)
191{
192 AioContext *ctx = (AioContext *) source;
193
eabc9779
PB
194 atomic_or(&ctx->notify_me, 1);
195
845ca10d
PB
196 /* We assume there is no timeout already supplied */
197 *timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
a3462c65
PB
198
199 if (aio_prepare(ctx)) {
200 *timeout = 0;
201 }
202
845ca10d 203 return *timeout == 0;
e3713e00
PB
204}
205
206static gboolean
207aio_ctx_check(GSource *source)
208{
209 AioContext *ctx = (AioContext *) source;
210 QEMUBH *bh;
211
eabc9779 212 atomic_and(&ctx->notify_me, ~1);
05e514b1 213 aio_notify_accept(ctx);
21a03d17 214
e3713e00
PB
215 for (bh = ctx->first_bh; bh; bh = bh->next) {
216 if (!bh->deleted && bh->scheduled) {
217 return true;
218 }
219 }
533a8cf3 220 return aio_pending(ctx) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
e3713e00
PB
221}
222
223static gboolean
224aio_ctx_dispatch(GSource *source,
225 GSourceFunc callback,
226 gpointer user_data)
227{
228 AioContext *ctx = (AioContext *) source;
229
230 assert(callback == NULL);
e4c7e2d1 231 aio_dispatch(ctx);
e3713e00
PB
232 return true;
233}
234
2f4dc3c1
PB
235static void
236aio_ctx_finalize(GSource *source)
237{
238 AioContext *ctx = (AioContext *) source;
239
ca96ac44 240 qemu_bh_delete(ctx->notify_dummy_bh);
9b34277d 241 thread_pool_free(ctx->thread_pool);
a076972a
SH
242
243 qemu_mutex_lock(&ctx->bh_lock);
244 while (ctx->first_bh) {
245 QEMUBH *next = ctx->first_bh->next;
246
247 /* qemu_bh_delete() must have been called on BHs in this AioContext */
248 assert(ctx->first_bh->deleted);
249
250 g_free(ctx->first_bh);
251 ctx->first_bh = next;
252 }
253 qemu_mutex_unlock(&ctx->bh_lock);
254
dca21ef2 255 aio_set_event_notifier(ctx, &ctx->notifier, false, NULL);
2f4dc3c1 256 event_notifier_cleanup(&ctx->notifier);
98563fc3 257 rfifolock_destroy(&ctx->lock);
dcc772e2 258 qemu_mutex_destroy(&ctx->bh_lock);
dae21b98 259 timerlistgroup_deinit(&ctx->tlg);
2f4dc3c1
PB
260}
261
e3713e00
PB
262static GSourceFuncs aio_source_funcs = {
263 aio_ctx_prepare,
264 aio_ctx_check,
265 aio_ctx_dispatch,
2f4dc3c1 266 aio_ctx_finalize
e3713e00
PB
267};
268
269GSource *aio_get_g_source(AioContext *ctx)
270{
271 g_source_ref(&ctx->source);
272 return &ctx->source;
273}
a915f4bc 274
9b34277d
SH
275ThreadPool *aio_get_thread_pool(AioContext *ctx)
276{
277 if (!ctx->thread_pool) {
278 ctx->thread_pool = thread_pool_new(ctx);
279 }
280 return ctx->thread_pool;
281}
282
2f4dc3c1
PB
283void aio_notify(AioContext *ctx)
284{
eabc9779
PB
285 /* Write e.g. bh->scheduled before reading ctx->notify_me. Pairs
286 * with atomic_or in aio_ctx_prepare or atomic_add in aio_poll.
287 */
0ceb849b 288 smp_mb();
eabc9779 289 if (ctx->notify_me) {
0ceb849b 290 event_notifier_set(&ctx->notifier);
05e514b1
PB
291 atomic_mb_set(&ctx->notified, true);
292 }
293}
294
295void aio_notify_accept(AioContext *ctx)
296{
297 if (atomic_xchg(&ctx->notified, false)) {
298 event_notifier_test_and_clear(&ctx->notifier);
0ceb849b 299 }
2f4dc3c1
PB
300}
301
d5541d86
AB
302static void aio_timerlist_notify(void *opaque)
303{
304 aio_notify(opaque);
305}
306
da5e1de9
SH
307static void aio_rfifolock_cb(void *opaque)
308{
ca96ac44
SH
309 AioContext *ctx = opaque;
310
da5e1de9 311 /* Kick owner thread in case they are blocked in aio_poll() */
ca96ac44
SH
312 qemu_bh_schedule(ctx->notify_dummy_bh);
313}
314
315static void notify_dummy_bh(void *opaque)
316{
317 /* Do nothing, we were invoked just to force the event loop to iterate */
da5e1de9
SH
318}
319
21a03d17
PB
320static void event_notifier_dummy_cb(EventNotifier *e)
321{
322}
323
2f78e491 324AioContext *aio_context_new(Error **errp)
f627aab1 325{
2f78e491 326 int ret;
2f4dc3c1 327 AioContext *ctx;
37fcee5d
FZ
328 Error *local_err = NULL;
329
2f4dc3c1 330 ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
37fcee5d
FZ
331 aio_context_setup(ctx, &local_err);
332 if (local_err) {
333 error_propagate(errp, local_err);
334 goto fail;
335 }
2f78e491
CN
336 ret = event_notifier_init(&ctx->notifier, false);
337 if (ret < 0) {
2f78e491 338 error_setg_errno(errp, -ret, "Failed to initialize event notifier");
37fcee5d 339 goto fail;
2f78e491 340 }
fcf5def1 341 g_source_set_can_recurse(&ctx->source, true);
2f78e491 342 aio_set_event_notifier(ctx, &ctx->notifier,
dca21ef2 343 false,
2f78e491 344 (EventNotifierHandler *)
21a03d17 345 event_notifier_dummy_cb);
9b34277d 346 ctx->thread_pool = NULL;
dcc772e2 347 qemu_mutex_init(&ctx->bh_lock);
da5e1de9 348 rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
d5541d86 349 timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
2f4dc3c1 350
ca96ac44
SH
351 ctx->notify_dummy_bh = aio_bh_new(ctx, notify_dummy_bh, NULL);
352
2f4dc3c1 353 return ctx;
37fcee5d
FZ
354fail:
355 g_source_destroy(&ctx->source);
356 return NULL;
e3713e00
PB
357}
358
359void aio_context_ref(AioContext *ctx)
360{
361 g_source_ref(&ctx->source);
362}
363
364void aio_context_unref(AioContext *ctx)
365{
366 g_source_unref(&ctx->source);
f627aab1 367}
98563fc3
SH
368
369void aio_context_acquire(AioContext *ctx)
370{
371 rfifolock_lock(&ctx->lock);
372}
373
374void aio_context_release(AioContext *ctx)
375{
376 rfifolock_unlock(&ctx->lock);
377}