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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright (c) 2017 Richard Braun.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* Timekeeping module.
*/
#ifndef _KERN_CLOCK_H
#define _KERN_CLOCK_H
#include <stdbool.h>
#include <stdint.h>
#include <kern/atomic.h>
#include <kern/clock_i.h>
#include <kern/init.h>
#include <kern/macros.h>
/*
* Clock frequency.
*/
#define CLOCK_FREQ CONFIG_CLOCK_FREQ
#if (CLOCK_FREQ < 100) || (CLOCK_FREQ > 1000) || (1000 % CLOCK_FREQ) != 0
#error "invalid clock frequency"
#endif /* (1000 % CLOCK_FREQ) != 0 */
/*
* Arbitrary value used to determine if a time is in the past or the future.
*
* Time is represented as 64-bits unsigned integers counting ticks. The
* global time currently starts from 0 but this isn't a strong assumption
* users should rely on. Instead, all time checks involve a time reference
* against which to compare. The result of that comparison, done by
* substraction, is either in the future, i.e. the difference is less
* than the expire threshold, or in the past, i.e. the difference is
* greater (keep in mind the result is unsigned). The threshold must be
* large enough to allow both a wide range of possible times in the future,
* but also enough time in the past for reliable timeout detection. Note
* that using signed integers would be equivalent to dividing the range
* in two (almost) equal past and future halves.
*/
#define CLOCK_EXPIRE_THRESHOLD (-(1ULL << 60))
static inline uint64_t
clock_get_time(void)
{
extern union clock_global_time clock_global_time;
#ifdef ATOMIC_HAVE_64B_OPS
/*
* Don't enforce a stronger memory order, since :
* 1/ it's useless as long as the reader remains on the same processor
* 2/ thread migration enforces sequential consistency
*/
return atomic_load(&clock_global_time.ticks, ATOMIC_RELAXED);
#else /* ATOMIC_HAVE_64B_OPS */
uint32_t high1, low, high2;
/*
* For machines with no 64-bits atomic accessors, this implementation uses
* a variant of the two-digit monotonic-clock algorithm, described in the
* paper "Concurrent Reading and Writing of Clocks" by Leslie Lamport.
*/
do {
high1 = atomic_load_acquire(&clock_global_time.high1);
low = atomic_load_acquire(&clock_global_time.low);
high2 = atomic_load(&clock_global_time.high2, ATOMIC_RELAXED);
} while (high1 != high2);
return ((uint64_t)high2 << 32) | low;
#endif /* ATOMIC_HAVE_64B_OPS */
}
static inline uint64_t
clock_ticks_to_ms(uint64_t ticks)
{
return ticks * (1000 / CLOCK_FREQ);
}
static inline uint64_t
clock_ticks_from_ms(uint64_t ms)
{
return DIV_CEIL(ms, (1000 / CLOCK_FREQ));
}
static inline bool
clock_time_expired(uint64_t t, uint64_t ref)
{
return (t - ref) > CLOCK_EXPIRE_THRESHOLD;
}
static inline bool
clock_time_occurred(uint64_t t, uint64_t ref)
{
return (t == ref) || clock_time_expired(t, ref);
}
void clock_tick_intr(void);
#endif /* _KERN_CLOCK_H */
|