-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtclock.h
138 lines (118 loc) · 3.14 KB
/
rtclock.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* -*- C++ -*-
* rtclock.h
*
* Realtime clock
*
* Copyright (C) 2015, 2016 Mikael Djurfeldt
*
* rtclock 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.
*
* libneurosim 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/>.
*
*/
#ifndef RTCLOCK_H
#define RTCLOCK_H
#include <time.h>
#include <sys/time.h>
class RTClock {
public:
/**
* Create a new RTClock and note the starting wallclock time which
* time () and sleepUntil (t) will use as a reference.
*
* If the optional interval is specified, this gives the sleeping
* time for the method sleepNext ().
*/
RTClock () : RTClock (0.) { }
RTClock (double interval);
/**
* Reset starting time.
*
* This resets the reference wallclock time for time () and
* sleepUntil (t).
*/
void reset ();
/**
* Return the wallclock time in seconds since starting time.
*/
double time () const;
/**
* Return the start of the current interval in seconds since starting time.
*/
double steppedTime () const;
/**
* Sleep t seconds.
*/
void sleep (double t) const;
/**
* Sleep until next interval counting from start time.
*
* If wallclock time has passed since last interval, sleep shorter
* time such that we will wake up on a multiple of intervals from
* start time.
*/
void sleepNext ();
/**
* Sleep until time t counting from start time.
*/
void sleepUntil (double t) const;
/**
* Convert a (relative) time t in seconds to a timeval
*/
struct timeval timevalFromSeconds (double t) const;
/**
* Convert a (relative) time tv to seconds
*/
double secondsFromTimeval (const struct timeval& tv) const;
/**
* Convert a (relative) time t in seconds to a timespec
*/
struct timespec timespecFromSeconds (double s) const;
/**
* Convert tv into a timespec struct
*/
struct timespec timespecFromTimeval (const struct timeval& tv) const;
private:
struct timeval start_;
struct timeval last_;
struct timeval interval_;
};
inline struct timeval
RTClock::timevalFromSeconds (double t) const
{
struct timeval tv;
tv.tv_sec = t;
tv.tv_usec = 1000000 * (t - tv.tv_sec);
return tv;
}
inline double
RTClock::secondsFromTimeval (const struct timeval& tv) const
{
return tv.tv_sec + 1e-6 * tv.tv_usec;
}
inline struct timespec
RTClock::timespecFromSeconds (double t) const
{
struct timespec ts;
ts.tv_sec = t;
ts.tv_nsec = 1e9 * (t - ts.tv_sec);
return ts;
}
inline struct timespec
RTClock::timespecFromTimeval (const struct timeval& tv) const
{
struct timespec ts;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = 1000 * tv.tv_usec;
return ts;
}
#endif /* RTCLOCK_H */