blob: d945a2c4f23a6d1e102705cbb6bb8df236619a16 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Timer module */
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "task.h"
#include "timer.h"
/*
* For test that need to test for longer than 10 seconds, adjust
* its time scale in test/build.mk by specifying
* <test_name>-scale=<new scale>.
*/
#ifndef TEST_TIME_SCALE
#define TEST_TIME_SCALE 1
#endif
static timestamp_t boot_time;
static int time_set;
void usleep(unsigned us)
{
task_wait_event(us);
}
timestamp_t _get_time(void)
{
struct timespec ts;
timestamp_t ret;
clock_gettime(CLOCK_MONOTONIC, &ts);
ret.val = (1000000000 * (uint64_t)ts.tv_sec + ts.tv_nsec) *
TEST_TIME_SCALE / 1000;
return ret;
}
timestamp_t get_time(void)
{
timestamp_t ret = _get_time();
ret.val -= boot_time.val;
return ret;
}
void force_time(timestamp_t ts)
{
timestamp_t now = _get_time();
boot_time.val = now.val - ts.val;
time_set = 1;
}
void udelay(unsigned us)
{
timestamp_t deadline = get_time();
deadline.val += us;
while (get_time().val < deadline.val)
;
}
int timestamp_expired(timestamp_t deadline, const timestamp_t *now)
{
timestamp_t now_val;
if (!now) {
now_val = get_time();
now = &now_val;
}
return ((int64_t)(now->val - deadline.val) >= 0);
}
void timer_init(void)
{
if (!time_set)
boot_time = _get_time();
}
|