1/** \file
2 *  \brief Simple sleep call
3 */
4
5/*
6 * Copyright (c) 2010-2011, ETH Zurich.
7 * All rights reserved.
8 *
9 * This file is distributed under the terms in the attached LICENSE file.
10 * If you do not find this file, copies can be found by writing to:
11 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
12 */
13
14#include <barrelfish/barrelfish.h>
15#include <barrelfish/sys_debug.h>
16
17#include <bench/bench.h>
18
19#include "sleep.h"
20
21#if defined(__x86_64__)
22static uint64_t tscperms;
23#endif
24
25static bool initialised = false;
26
27void sleep_init(void)
28{
29    if (!initialised) {
30        bench_init();
31#if defined(__x86_64__)
32        errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
33        assert(err_is_ok(err));
34#endif
35        initialised = true;
36    }
37}
38
39
40void cycle_sleep(uint64_t cycles)
41{
42    if (!initialised) {
43        sleep_init();
44    }
45
46    uint64_t start = bench_tsc();
47    uint64_t stop = bench_tsc();
48    while ((stop - start) < cycles) {
49        //        sys_yield(CPTR_NULL);
50        thread_yield_dispatcher(NULL_CAP);
51        stop = bench_tsc();
52    }
53}
54
55// FIXME: this should be determined at runtime from the kernel...
56// #define TSC_PER_MS 2513385
57
58void milli_sleep(uint64_t ms)
59{
60#if defined(__x86_64__)
61    uint64_t cycles = ms * tscperms;
62    cycle_sleep(cycles);
63#else
64    USER_PANIC("milli_sleep NYI for non-x86_64");
65#endif
66}
67
68