block/qcow2: refactor encryption code
[qemu/ar7.git] / util / qemu-coroutine-sleep.c
blob4bfdd30cbf1384fa865924e53d36d35996ba0ee7
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 void co_sleep_cb(void *opaque)
22 Coroutine *co = opaque;
24 /* Write of schedule protected by barrier write in aio_co_schedule */
25 atomic_set(&co->scheduled, NULL);
26 aio_co_wake(co);
29 void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns)
31 AioContext *ctx = qemu_get_current_aio_context();
32 QEMUTimer *ts;
33 Coroutine *co = qemu_coroutine_self();
35 const char *scheduled = atomic_cmpxchg(&co->scheduled, NULL, __func__);
36 if (scheduled) {
37 fprintf(stderr,
38 "%s: Co-routine was already scheduled in '%s'\n",
39 __func__, scheduled);
40 abort();
42 ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, co);
43 timer_mod(ts, qemu_clock_get_ns(type) + ns);
44 qemu_coroutine_yield();
45 timer_del(ts);
46 timer_free(ts);