disas/nanomips: Remove NMD class
[qemu/armbru.git] / util / qemu-coroutine-sleep.c
blob571ab521ff0b7b9e2f7aa69f20468a6c11a7db01
1 /*
2 * QEMU coroutine sleep
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/coroutine.h"
16 #include "qemu/coroutine_int.h"
17 #include "qemu/timer.h"
18 #include "block/aio.h"
20 static const char *qemu_co_sleep_ns__scheduled = "qemu_co_sleep_ns";
22 void qemu_co_sleep_wake(QemuCoSleep *w)
24 Coroutine *co;
26 co = w->to_wake;
27 w->to_wake = NULL;
28 if (co) {
29 /* Write of schedule protected by barrier write in aio_co_schedule */
30 const char *scheduled = qatomic_cmpxchg(&co->scheduled,
31 qemu_co_sleep_ns__scheduled, NULL);
33 assert(scheduled == qemu_co_sleep_ns__scheduled);
34 aio_co_wake(co);
38 static void co_sleep_cb(void *opaque)
40 QemuCoSleep *w = opaque;
41 qemu_co_sleep_wake(w);
44 void coroutine_fn qemu_co_sleep(QemuCoSleep *w)
46 Coroutine *co = qemu_coroutine_self();
48 const char *scheduled = qatomic_cmpxchg(&co->scheduled, NULL,
49 qemu_co_sleep_ns__scheduled);
50 if (scheduled) {
51 fprintf(stderr,
52 "%s: Co-routine was already scheduled in '%s'\n",
53 __func__, scheduled);
54 abort();
57 w->to_wake = co;
58 qemu_coroutine_yield();
60 /* w->to_wake is cleared before resuming this coroutine. */
61 assert(w->to_wake == NULL);
64 void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w,
65 QEMUClockType type, int64_t ns)
67 AioContext *ctx = qemu_get_current_aio_context();
68 QEMUTimer ts;
70 aio_timer_init(ctx, &ts, type, SCALE_NS, co_sleep_cb, w);
71 timer_mod(&ts, qemu_clock_get_ns(type) + ns);
74 * The timer will fire in the current AiOContext, so the callback
75 * must happen after qemu_co_sleep yields and there is no race
76 * between timer_mod and qemu_co_sleep.
78 qemu_co_sleep(w);
79 timer_del(&ts);