1 /*
2 * Tvheadend - clock support
3 * Copyright (C) 2016 Jaroslav Kysela
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef TVHEADEND_CLOCK_H
20 #define TVHEADEND_CLOCK_H
21
22 #include <time.h>
23
24 #ifndef CLOCK_MONOTONIC_COARSE
25 #define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
26 #endif
27
28 #if defined(PLATFORM_DARWIN) && !defined(CLOCK_MONOTONIC)
29 #error "Platforms without monotonic clocks are not supported!"
30 #define CLOCK_MONOTONIC 0
31 #define CLOCK_REALTIME 0
32
clock_gettime(int clk_id,struct timespec * t)33 static inline int clock_gettime(int clk_id, struct timespec* t) {
34 struct timeval now;
35 int rv = gettimeofday(&now, NULL);
36 if (rv) return rv;
37 t->tv_sec = now.tv_sec;
38 t->tv_nsec = now.tv_usec * 1000;
39 return 0;
40 }
41 #endif
42
43 extern int64_t __mdispatch_clock;
44 extern time_t __gdispatch_clock;
45
mclk(void)46 static inline int64_t mclk(void)
47 {
48 return atomic_get_s64(&__mdispatch_clock);
49 }
50
gclk(void)51 static inline time_t gclk(void)
52 {
53 return atomic_get_time_t(&__gdispatch_clock);
54 }
55
56 #define MONOCLOCK_RESOLUTION 1000000LL /* microseconds */
57 #define MONOCLOCK_FASTSEC 0xfffffLL /* 1048575 */
58
59 static inline int64_t
sec2mono(int64_t sec)60 sec2mono(int64_t sec)
61 {
62 return sec * MONOCLOCK_RESOLUTION;
63 }
64
65 static inline int64_t
mono2sec(int64_t monosec)66 mono2sec(int64_t monosec)
67 {
68 return monosec / MONOCLOCK_RESOLUTION;
69 }
70
71 static inline int64_t
ms2mono(int64_t ms)72 ms2mono(int64_t ms)
73 {
74 return ms * (MONOCLOCK_RESOLUTION / 1000LL);
75 }
76
77 static inline int64_t
mono2ms(int64_t monosec)78 mono2ms(int64_t monosec)
79 {
80 return monosec / (MONOCLOCK_RESOLUTION / 1000LL);
81 }
82
83 static inline int64_t
getmonoclock(void)84 getmonoclock(void)
85 {
86 struct timespec tp;
87
88 clock_gettime(CLOCK_MONOTONIC, &tp);
89
90 return tp.tv_sec * MONOCLOCK_RESOLUTION +
91 (tp.tv_nsec / (1000000000LL/MONOCLOCK_RESOLUTION));
92 }
93
94 static inline int64_t
getfastmonoclock(void)95 getfastmonoclock(void)
96 {
97 struct timespec tp;
98
99 clock_gettime(CLOCK_MONOTONIC_COARSE, &tp);
100
101 return tp.tv_sec * MONOCLOCK_RESOLUTION +
102 (tp.tv_nsec / (1000000000LL/MONOCLOCK_RESOLUTION));
103 }
104
105 static inline int
monocmpfastsec(int64_t m1,int64_t m2)106 monocmpfastsec(int64_t m1, int64_t m2)
107 {
108 return (m1 & ~MONOCLOCK_FASTSEC) == (m2 & ~MONOCLOCK_FASTSEC);
109 }
110
111 void time_t_out_of_range_notify(int64_t val);
112
time_t_out_of_range(uint64_t val)113 static inline time_t time_t_out_of_range(uint64_t val)
114 {
115 time_t r = val;
116 if ((int64_t)r != val) {
117 time_t_out_of_range_notify(val);
118 r = INT32_MAX;
119 }
120 return r;
121 }
122
123 void tvh_safe_usleep(int64_t us);
124
125 int64_t tvh_usleep(int64_t us);
126 int64_t tvh_usleep_abs(int64_t us);
127
128 #endif /* TVHEADEND_CLOCK_H */
129