1 /* 2 * QEMU coroutine sleep 3 * 4 * Copyright IBM, Corp. 2011 5 * 6 * Authors: 7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 8 * 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. 11 * 12 */ 13 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" 19 20 static void co_sleep_cb(void *opaque) 21 { 22 Coroutine *co = opaque; 23 24 /* Write of schedule protected by barrier write in aio_co_schedule */ 25 atomic_set(&co->scheduled, NULL); 26 aio_co_wake(co); 27 } 28 29 void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) 30 { 31 AioContext *ctx = qemu_get_current_aio_context(); 32 QEMUTimer *ts; 33 Coroutine *co = qemu_coroutine_self(); 34 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(); 41 } 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); 47 } 48