diff options
author | John Stultz <john.stultz@linaro.org> | 2014-07-16 17:03:58 -0400 |
---|---|---|
committer | John Stultz <john.stultz@linaro.org> | 2014-07-23 13:17:53 -0400 |
commit | 361a3bf00582469877f8d18ff20f1efa6b781274 (patch) | |
tree | 8ee505002fe5b839728b16a054590d2b44b8be1b /include | |
parent | b17b20d70dcbe48dd1aa6aba073a60ddfce5d7db (diff) |
time64: Add time64.h header and define struct timespec64
Define the timespec64 structure and standard helper functions.
[ tglx: Make it 32bit only. 64bit really can map timespec to timespec64 ]
Signed-off-by: John Stultz <john.stultz@linaro.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <john.stultz@linaro.org>
Diffstat (limited to 'include')
-rw-r--r-- | include/linux/time.h | 15 | ||||
-rw-r--r-- | include/linux/time64.h | 162 |
2 files changed, 163 insertions, 14 deletions
diff --git a/include/linux/time.h b/include/linux/time.h index 129f0bd36a8d..234feac7f1c3 100644 --- a/include/linux/time.h +++ b/include/linux/time.h | |||
@@ -4,25 +4,12 @@ | |||
4 | # include <linux/cache.h> | 4 | # include <linux/cache.h> |
5 | # include <linux/seqlock.h> | 5 | # include <linux/seqlock.h> |
6 | # include <linux/math64.h> | 6 | # include <linux/math64.h> |
7 | #include <uapi/linux/time.h> | 7 | # include <linux/time64.h> |
8 | 8 | ||
9 | extern struct timezone sys_tz; | 9 | extern struct timezone sys_tz; |
10 | 10 | ||
11 | /* Parameters used to convert the timespec values: */ | ||
12 | #define MSEC_PER_SEC 1000L | ||
13 | #define USEC_PER_MSEC 1000L | ||
14 | #define NSEC_PER_USEC 1000L | ||
15 | #define NSEC_PER_MSEC 1000000L | ||
16 | #define USEC_PER_SEC 1000000L | ||
17 | #define NSEC_PER_SEC 1000000000L | ||
18 | #define FSEC_PER_SEC 1000000000000000LL | ||
19 | |||
20 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) | 11 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) |
21 | 12 | ||
22 | /* Located here for timespec_valid_strict */ | ||
23 | #define KTIME_MAX ((s64)~((u64)1 << 63)) | ||
24 | #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) | ||
25 | |||
26 | static inline int timespec_equal(const struct timespec *a, | 13 | static inline int timespec_equal(const struct timespec *a, |
27 | const struct timespec *b) | 14 | const struct timespec *b) |
28 | { | 15 | { |
diff --git a/include/linux/time64.h b/include/linux/time64.h new file mode 100644 index 000000000000..e7b499e1cd79 --- /dev/null +++ b/include/linux/time64.h | |||
@@ -0,0 +1,162 @@ | |||
1 | #ifndef _LINUX_TIME64_H | ||
2 | #define _LINUX_TIME64_H | ||
3 | |||
4 | #include <uapi/linux/time.h> | ||
5 | |||
6 | typedef __s64 time64_t; | ||
7 | |||
8 | /* | ||
9 | * This wants to go into uapi/linux/time.h once we agreed about the | ||
10 | * userspace interfaces. | ||
11 | */ | ||
12 | #if __BITS_PER_LONG == 64 | ||
13 | # define timespec64 timespec | ||
14 | #else | ||
15 | struct timespec64 { | ||
16 | time64_t tv_sec; /* seconds */ | ||
17 | long tv_nsec; /* nanoseconds */ | ||
18 | }; | ||
19 | #endif | ||
20 | |||
21 | /* Parameters used to convert the timespec values: */ | ||
22 | #define MSEC_PER_SEC 1000L | ||
23 | #define USEC_PER_MSEC 1000L | ||
24 | #define NSEC_PER_USEC 1000L | ||
25 | #define NSEC_PER_MSEC 1000000L | ||
26 | #define USEC_PER_SEC 1000000L | ||
27 | #define NSEC_PER_SEC 1000000000L | ||
28 | #define FSEC_PER_SEC 1000000000000000LL | ||
29 | |||
30 | /* Located here for timespec[64]_valid_strict */ | ||
31 | #define KTIME_MAX ((s64)~((u64)1 << 63)) | ||
32 | #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) | ||
33 | |||
34 | #if __BITS_PER_LONG == 64 | ||
35 | |||
36 | # define timespec64_equal timespec_equal | ||
37 | # define timespec64_compare timespec_compare | ||
38 | # define set_normalized_timespec64 set_normalized_timespec | ||
39 | # define timespec64_add_safe timespec_add_safe | ||
40 | # define timespec64_add timespec_add | ||
41 | # define timespec64_sub timespec_sub | ||
42 | # define timespec64_valid timespec_valid | ||
43 | # define timespec64_valid_strict timespec_valid_strict | ||
44 | # define timespec64_to_ns timespec_to_ns | ||
45 | # define ns_to_timespec64 ns_to_timespec | ||
46 | # define timespec64_add_ns timespec_add_ns | ||
47 | |||
48 | #else | ||
49 | |||
50 | static inline int timespec64_equal(const struct timespec64 *a, | ||
51 | const struct timespec64 *b) | ||
52 | { | ||
53 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); | ||
54 | } | ||
55 | |||
56 | /* | ||
57 | * lhs < rhs: return <0 | ||
58 | * lhs == rhs: return 0 | ||
59 | * lhs > rhs: return >0 | ||
60 | */ | ||
61 | static inline int timespec64_compare(const struct timespec64 *lhs, const struct timespec64 *rhs) | ||
62 | { | ||
63 | if (lhs->tv_sec < rhs->tv_sec) | ||
64 | return -1; | ||
65 | if (lhs->tv_sec > rhs->tv_sec) | ||
66 | return 1; | ||
67 | return lhs->tv_nsec - rhs->tv_nsec; | ||
68 | } | ||
69 | |||
70 | extern void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec); | ||
71 | |||
72 | /* | ||
73 | * timespec64_add_safe assumes both values are positive and checks for | ||
74 | * overflow. It will return TIME_T_MAX if the returned value would be | ||
75 | * smaller then either of the arguments. | ||
76 | */ | ||
77 | extern struct timespec64 timespec64_add_safe(const struct timespec64 lhs, | ||
78 | const struct timespec64 rhs); | ||
79 | |||
80 | |||
81 | static inline struct timespec64 timespec64_add(struct timespec64 lhs, | ||
82 | struct timespec64 rhs) | ||
83 | { | ||
84 | struct timespec64 ts_delta; | ||
85 | set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, | ||
86 | lhs.tv_nsec + rhs.tv_nsec); | ||
87 | return ts_delta; | ||
88 | } | ||
89 | |||
90 | /* | ||
91 | * sub = lhs - rhs, in normalized form | ||
92 | */ | ||
93 | static inline struct timespec64 timespec64_sub(struct timespec64 lhs, | ||
94 | struct timespec64 rhs) | ||
95 | { | ||
96 | struct timespec64 ts_delta; | ||
97 | set_normalized_timespec64(&ts_delta, lhs.tv_sec - rhs.tv_sec, | ||
98 | lhs.tv_nsec - rhs.tv_nsec); | ||
99 | return ts_delta; | ||
100 | } | ||
101 | |||
102 | /* | ||
103 | * Returns true if the timespec64 is norm, false if denorm: | ||
104 | */ | ||
105 | static inline bool timespec64_valid(const struct timespec64 *ts) | ||
106 | { | ||
107 | /* Dates before 1970 are bogus */ | ||
108 | if (ts->tv_sec < 0) | ||
109 | return false; | ||
110 | /* Can't have more nanoseconds then a second */ | ||
111 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) | ||
112 | return false; | ||
113 | return true; | ||
114 | } | ||
115 | |||
116 | static inline bool timespec64_valid_strict(const struct timespec64 *ts) | ||
117 | { | ||
118 | if (!timespec64_valid(ts)) | ||
119 | return false; | ||
120 | /* Disallow values that could overflow ktime_t */ | ||
121 | if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) | ||
122 | return false; | ||
123 | return true; | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * timespec64_to_ns - Convert timespec64 to nanoseconds | ||
128 | * @ts: pointer to the timespec64 variable to be converted | ||
129 | * | ||
130 | * Returns the scalar nanosecond representation of the timespec64 | ||
131 | * parameter. | ||
132 | */ | ||
133 | static inline s64 timespec64_to_ns(const struct timespec64 *ts) | ||
134 | { | ||
135 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * ns_to_timespec64 - Convert nanoseconds to timespec64 | ||
140 | * @nsec: the nanoseconds value to be converted | ||
141 | * | ||
142 | * Returns the timespec64 representation of the nsec parameter. | ||
143 | */ | ||
144 | extern struct timespec64 ns_to_timespec64(const s64 nsec); | ||
145 | |||
146 | /** | ||
147 | * timespec64_add_ns - Adds nanoseconds to a timespec64 | ||
148 | * @a: pointer to timespec64 to be incremented | ||
149 | * @ns: unsigned nanoseconds value to be added | ||
150 | * | ||
151 | * This must always be inlined because its used from the x86-64 vdso, | ||
152 | * which cannot call other kernel functions. | ||
153 | */ | ||
154 | static __always_inline void timespec64_add_ns(struct timespec64 *a, u64 ns) | ||
155 | { | ||
156 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); | ||
157 | a->tv_nsec = ns; | ||
158 | } | ||
159 | |||
160 | #endif | ||
161 | |||
162 | #endif /* _LINUX_TIME64_H */ | ||