IntrOS
Loading...
Searching...
No Matches
osclock.h
1/******************************************************************************
2
3 @file IntrOS: osclock.h
4 @author Rajmund Szymanski
5 @date 10.07.2022
6 @brief This file implements steady clock for IntrOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __INTROS_CLK_H
33#define __INTROS_CLK_H
34
35#include "oskernel.h"
36
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
52
53cnt_t sys_time( void );
54
55#ifdef __cplusplus
56}
57#endif
58
59/* -------------------------------------------------------------------------- */
60
61#if defined(__cplusplus)
62
63#if __cplusplus >= 201402L
64#include <chrono>
65#endif
66
67namespace intros {
68
72
73struct Clock
74{
75 using rep = cnt_t;
76#if __cplusplus >= 201402L
77 using period = std::ratio<1, OS_FREQUENCY>;
78 using duration = std::chrono::duration<rep, period>;
79 using time_point = std::chrono::time_point<Clock>;
80
81 static constexpr bool is_steady = true;
82
84 static
86 {
88 }
89
90private:
91 // rounded up to whole ticks: a delay is never shorter than requested
92 template<class _Rep, class _Period> static constexpr
93 intmax_t ticks( const std::chrono::duration<_Rep, _Period>& _time )
94 {
95 return std::chrono::duration_cast<std::chrono::duration<intmax_t, period>>(_time).count() +
96 (std::chrono::duration_cast<std::chrono::duration<intmax_t, period>>(_time) < _time ? 1 : 0);
97 }
98
99public:
101 template<class _Rep, class _Period> static constexpr
102 rep count( const std::chrono::duration<_Rep, _Period>& _delay )
103 {
104 return ticks(_delay) <= 0 ? IMMEDIATE : static_cast<uintmax_t>(ticks(_delay)) >= INFINITE ? INFINITE : static_cast<rep>(ticks(_delay));
105 }
106
108 template<class _Duration> static constexpr
109 rep until( const std::chrono::time_point<Clock, _Duration>& _time )
110 {
111 return static_cast<rep>(ticks(_time.time_since_epoch()));
112 }
113#endif
114 static constexpr
116 rep count( const rep _delay ) { return _delay; }
117
118 static constexpr
120 rep until( const rep _time ) { return _time; }
121};
122
123} // namespace
124#endif//__cplusplus
125
126/* -------------------------------------------------------------------------- */
127
129
130#endif//__INTROS_CLK_H
#define IMMEDIATE
no waiting
Definition osbase.h:247
#define INFINITE
infinite waiting
Definition osbase.h:250
cnt_t sys_time(void)
return current value of system counter
steady clock
Definition osclock.h:74
cnt_t rep
type of the system time (cnt_t)
Definition osclock.h:75
static constexpr rep until(const rep _time)
time point in ticks
Definition osclock.h:120
std::ratio< 1, OS_FREQUENCY > period
tick of the system timer (1 / OS_FREQUENCY s)
Definition osclock.h:77
static time_point now()
current system time (sys_time())
Definition osclock.h:85
static constexpr rep count(const rep _delay)
delay in ticks
Definition osclock.h:116
std::chrono::duration< rep, period > duration
duration in ticks of the system timer
Definition osclock.h:78
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:109
static constexpr bool is_steady
the system time is monotonic
Definition osclock.h:81
std::chrono::time_point< Clock > time_point
time point of the system time
Definition osclock.h:79
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:102