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
22#if defined(__x86_64__)
23static uint64_t tscperms;
24#endif
25
26static bool initialised = false;
27
28void sleep_init(void)
29{
30    if (!initialised) {
31        bench_init();
32#if defined(__x86_64__)
33        errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
34        assert(err_is_ok(err));
35#endif
36        initialised = true;
37    }
38}
39
40
41void cycle_sleep(uint64_t cycles)
42{
43    if (!initialised) {
44        sleep_init();
45    }
46
47    uint64_t start = bench_tsc();
48    uint64_t stop = bench_tsc();
49    while ((stop - start) < cycles) {
50        //        sys_yield(CPTR_NULL);
51        thread_yield_dispatcher(NULL_CAP);
52        stop = bench_tsc();
53    }
54}
55
56// FIXME: this should be determined at runtime from the kernel...
57// #define TSC_PER_MS 2513385
58
59void milli_sleep(uint64_t ms)
60{
61#if defined(__x86_64__)
62    uint64_t cycles = ms * tscperms;
63    cycle_sleep(cycles);
64#else
65    USER_PANIC("milli_sleep NYI for non-x86_64");
66#endif
67}
68
69