1/*-
2 * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <bmk-core/errno.h>
27#include <bmk-core/memalloc.h>
28#include <bmk-core/null.h>
29#include <bmk-core/platform.h>
30#include <bmk-core/sched.h>
31#include <bmk-core/string.h>
32
33#include <bmk-rumpuser/core_types.h>
34#include <bmk-rumpuser/rumpuser.h>
35
36int
37rumpuser_clock_gettime(int which, int64_t *sec, long *nsec)
38{
39	bmk_time_t time;
40
41	time = bmk_platform_cpu_clock_monotonic();
42
43	switch (which) {
44	case RUMPUSER_CLOCK_RELWALL:
45		time += bmk_platform_cpu_clock_epochoffset();
46		break;
47	case RUMPUSER_CLOCK_ABSMONO:
48		break;
49	}
50
51	*sec  = time / (1000*1000*1000ULL);
52	*nsec = time % (1000*1000*1000ULL);
53
54	return 0;
55}
56
57int
58rumpuser_clock_sleep(int enum_rumpclock, int64_t sec, long nsec)
59{
60	enum rumpclock rclk = enum_rumpclock;
61	bmk_time_t deadline = 0;
62	int nlocks;
63
64	rumpkern_unsched(&nlocks, NULL);
65	switch (rclk) {
66	case RUMPUSER_CLOCK_RELWALL:
67		deadline = bmk_platform_cpu_clock_monotonic();
68		break;
69	case RUMPUSER_CLOCK_ABSMONO:
70		break;
71	}
72	deadline += sec * 1000*1000*1000 + nsec;
73	bmk_sched_blockprepare_timeout(deadline);
74	bmk_sched_block();
75	rumpkern_sched(nlocks, NULL);
76
77	return 0;
78}
79