SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimClock.h
Go to the documentation of this file.
1 #ifndef SENS_SIMCLOCK_H
2 #define SENS_SIMCLOCK_H
3 
4 #include <mutex>
5 #include <atomic>
6 #include <SENS.h>
7 
8 /*! SENSSimClock
9 Clock used for sensor simulation in SENSSimulator and SENSSimulated
10 */
12 {
13 public:
14  SENSSimClock(SENSTimePt startTimePt, SENSTimePt simStartTimePt)
15  : _startTimePt(startTimePt),
16  _simStartTimePt(simStartTimePt)
17  {
18  _pause = true;
19  _pauseTimePt = (SENSTimePt)SENSClock::now();
20  }
21 
22  //!pause simulation
23  void pause()
24  {
25  std::lock_guard<std::mutex> lock(_pauseMutex);
26  _pause = true;
27  _pauseTimePt = (SENSTimePt)SENSClock::now();
28  }
29 
30  bool isPaused() const
31  {
32  return _pause;
33  }
34 
35  //!resume simulation
36  void resume()
37  {
38  std::lock_guard<std::mutex> lock(_pauseMutex);
39  _pause = false;
40  _pauseTime = _pauseTime + std::chrono::duration_cast<SENSMicroseconds>((SENSTimePt)SENSClock::now() - _pauseTimePt);
41  }
42 
43  //!reset simulation
44  //!(we switch to pause when setting new start, in this way now becomes thread save and we increase performance of now() (no mutex in nomal use))
45  void reset()
46  {
47  pause();
48  _startTimePt = SENSClock::now();
50  resume();
51  }
52 
53  //!get passed simulation time
55  {
56  SENSMicroseconds passedSimTime;
57  if (_pause)
58  {
59  const std::lock_guard<std::mutex> lock(_pauseMutex);
60  passedSimTime = std::chrono::duration_cast<SENSMicroseconds>(_pauseTimePt - _startTimePt) - _pauseTime;
61  }
62  else
63  {
64  passedSimTime = std::chrono::duration_cast<SENSMicroseconds>((SENSTimePt)SENSClock::now() - _startTimePt) - _pauseTime;
65  }
66 
67  return passedSimTime;
68  }
69 
70  //!get current simulation time (const function which should be thread save)
71  SENSTimePt now() const
72  {
73  SENSMicroseconds passedSimTime;
74  if (_pause)
75  {
76  const std::lock_guard<std::mutex> lock(_pauseMutex);
77  passedSimTime = std::chrono::duration_cast<SENSMicroseconds>(_pauseTimePt - _startTimePt) - _pauseTime;
78  }
79  else
80  {
81  passedSimTime = std::chrono::duration_cast<SENSMicroseconds>((SENSTimePt)SENSClock::now() - _startTimePt) - _pauseTime;
82  }
83 
84  return _simStartTimePt + passedSimTime;
85  }
86 
87 private:
88  //!atomic, because we use it in now without mutex
89  std::atomic_bool _pause{true};
91  //(mutable as we want to use it in the const function now())
92  mutable std::mutex _pauseMutex;
93 
94  //!Offset from pause state: we have to accumulate pause times and subtract it from sim time
96  //!real start time point in the present (when SENSSimulation::start was called)
98  //!start time point of simulation in the past
100 };
101 
102 #endif //SENS_SIMCLOCK_H
std::chrono::microseconds SENSMicroseconds
Definition: SENS.h:58
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
SENSTimePt now() const
get current simulation time (const function which should be thread save)
Definition: SENSSimClock.h:71
bool isPaused() const
Definition: SENSSimClock.h:30
SENSTimePt _pauseTimePt
Definition: SENSSimClock.h:90
SENSTimePt _simStartTimePt
start time point of simulation in the past
Definition: SENSSimClock.h:99
void pause()
pause simulation
Definition: SENSSimClock.h:23
SENSTimePt _startTimePt
real start time point in the present (when SENSSimulation::start was called)
Definition: SENSSimClock.h:97
void resume()
resume simulation
Definition: SENSSimClock.h:36
SENSMicroseconds _pauseTime
Offset from pause state: we have to accumulate pause times and subtract it from sim time.
Definition: SENSSimClock.h:95
std::mutex _pauseMutex
Definition: SENSSimClock.h:92
void reset()
Definition: SENSSimClock.h:45
SENSMicroseconds passedTime() const
get passed simulation time
Definition: SENSSimClock.h:54
std::atomic_bool _pause
atomic, because we use it in now without mutex
Definition: SENSSimClock.h:89
SENSSimClock(SENSTimePt startTimePt, SENSTimePt simStartTimePt)
Definition: SENSSimClock.h:14