blob: 9abb7fdf31a96118c6c2b822225fa3d7074cecc7 [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"
MORITA Kazutaka3ab7bd12013-10-24 16:01:14 +090016#include "block/aio.h"
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000017
18typedef struct CoSleepCB {
19 QEMUTimer *ts;
20 Coroutine *co;
21} CoSleepCB;
22
23static void co_sleep_cb(void *opaque)
24{
25 CoSleepCB *sleep_cb = opaque;
26
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000027 qemu_coroutine_enter(sleep_cb->co, NULL);
28}
29
MORITA Kazutaka3ab7bd12013-10-24 16:01:14 +090030void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
31 int64_t ns)
32{
33 CoSleepCB sleep_cb = {
34 .co = qemu_coroutine_self(),
35 };
36 sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb);
37 timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns);
38 qemu_coroutine_yield();
39 timer_del(sleep_cb.ts);
40 timer_free(sleep_cb.ts);
41}