blob: 6966831d370e60f7e707e36205e2ceae5757c2dd [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
Peter Maydellaafd7582016-01-29 17:49:55 +000014#include "qemu/osdep.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010015#include "qemu/coroutine.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/timer.h"
MORITA Kazutaka3ab7bd12013-10-24 16:01:14 +090017#include "block/aio.h"
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000018
19typedef struct CoSleepCB {
20 QEMUTimer *ts;
21 Coroutine *co;
22} CoSleepCB;
23
24static void co_sleep_cb(void *opaque)
25{
26 CoSleepCB *sleep_cb = opaque;
27
Stefan Hajnoczi7e624662012-01-18 14:40:40 +000028 qemu_coroutine_enter(sleep_cb->co, NULL);
29}
30
MORITA Kazutaka3ab7bd12013-10-24 16:01:14 +090031void coroutine_fn co_aio_sleep_ns(AioContext *ctx, QEMUClockType type,
32 int64_t ns)
33{
34 CoSleepCB sleep_cb = {
35 .co = qemu_coroutine_self(),
36 };
37 sleep_cb.ts = aio_timer_new(ctx, type, SCALE_NS, co_sleep_cb, &sleep_cb);
38 timer_mod(sleep_cb.ts, qemu_clock_get_ns(type) + ns);
39 qemu_coroutine_yield();
40 timer_del(sleep_cb.ts);
41 timer_free(sleep_cb.ts);
42}