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, Universitaetstrasse 6, 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    uint64_t start = bench_tsc();
46    uint64_t stop = bench_tsc();
47    while ((stop - start) < cycles) {
48        //        sys_yield(CPTR_NULL);
49        thread_yield_dispatcher(NULL_CAP);
50        stop = bench_tsc();
51    }
52}
53
54// FIXME: this should be determined at runtime from the kernel...
55// #define TSC_PER_MS 2513385
56
57void milli_sleep(uint64_t ms)
58{
59    if (!initialised) {
60        sleep_init();
61    }
62
63#if defined(__x86_64__)
64    uint64_t cycles = ms * tscperms;
65    cycle_sleep(cycles);
66#else
67    USER_PANIC("milli_sleep NYI for non-x86_64");
68#endif
69}
70
71