Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 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 | |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 14 | #include "block/coroutine.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 15 | #include "qemu/timer.h" |
MORITA Kazutaka | 3ab7bd1 | 2013-10-24 16:01:14 +0900 | [diff] [blame] | 16 | #include "block/aio.h" |
Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 17 | |
| 18 | typedef struct CoSleepCB { |
| 19 | QEMUTimer *ts; |
| 20 | Coroutine *co; |
| 21 | } CoSleepCB; |
| 22 | |
| 23 | static void co_sleep_cb(void *opaque) |
| 24 | { |
| 25 | CoSleepCB *sleep_cb = opaque; |
| 26 | |
Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 27 | qemu_coroutine_enter(sleep_cb->co, NULL); |
| 28 | } |
| 29 | |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 30 | void coroutine_fn co_sleep_ns(QEMUClockType type, int64_t ns) |
Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 31 | { |
| 32 | CoSleepCB sleep_cb = { |
| 33 | .co = qemu_coroutine_self(), |
| 34 | }; |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 35 | sleep_cb.ts = timer_new(type, SCALE_NS, co_sleep_cb, &sleep_cb); |
| 36 | timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns); |
Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 37 | qemu_coroutine_yield(); |
Alex Bligh | 7483d1e | 2013-08-21 16:03:05 +0100 | [diff] [blame] | 38 | timer_del(sleep_cb.ts); |
| 39 | timer_free(sleep_cb.ts); |
Stefan Hajnoczi | 7e62466 | 2012-01-18 14:40:40 +0000 | [diff] [blame] | 40 | } |
MORITA Kazutaka | 3ab7bd1 | 2013-10-24 16:01:14 +0900 | [diff] [blame] | 41 | |
| 42 | void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type, |
| 43 | int64_t ns) |
| 44 | { |
| 45 | CoSleepCB sleep_cb = { |
| 46 | .co = qemu_coroutine_self(), |
| 47 | }; |
| 48 | sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb); |
| 49 | timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns); |
| 50 | qemu_coroutine_yield(); |
| 51 | timer_del(sleep_cb.ts); |
| 52 | timer_free(sleep_cb.ts); |
| 53 | } |