blob: 169ce5ccc921e9ec9705b1e8409da0fba9fbff20 [file] [log] [blame]
Stefan Hajnoczi7e624662012-01-18 14:40:40 +00001/*
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 Bonzini737e1502012-12-17 18:19:44 +010014#include "block/coroutine.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010015#include "qemu/timer.h"
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000016
17typedef struct CoSleepCB {
18 QEMUTimer *ts;
19 Coroutine *co;
20} CoSleepCB;
21
22static void co_sleep_cb(void *opaque)
23{
24 CoSleepCB *sleep_cb = opaque;
25
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000026 qemu_coroutine_enter(sleep_cb->co, NULL);
27}
28
29void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns)
30{
31 CoSleepCB sleep_cb = {
32 .co = qemu_coroutine_self(),
33 };
34 sleep_cb.ts = qemu_new_timer(clock, SCALE_NS, co_sleep_cb, &sleep_cb);
35 qemu_mod_timer(sleep_cb.ts, qemu_get_clock_ns(clock) + ns);
36 qemu_coroutine_yield();
Paolo Bonzini3ed99022012-04-18 15:12:01 +020037 qemu_del_timer(sleep_cb.ts);
38 qemu_free_timer(sleep_cb.ts);
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000039}