3 #if defined(MADARA_FEATURE_SIMTIME) && MADARA_FEATURE_SIMTIME==1 9 const double minrate = 0.0000000001;
11 std::mutex SimTime::mutex_{};
13 sim_time_callback_fn SimTime::callback_ =
nullptr;
15 uint64_t SimTime::last_realtime_ = SimTime::realtime();
16 uint64_t SimTime::last_simtime_ = 0;
17 double SimTime::last_rate_ = 1.0;
19 uint64_t SimTime::realtime() {
20 namespace sc = std::chrono;
21 auto now = sc::system_clock::now();
22 auto dur = now.time_since_epoch();
23 return sc::duration_cast<sc::nanoseconds>(dur).count();
26 uint64_t SimTime::time() {
32 sim_time_callback_fn callback;
34 uint64_t now = realtime();
36 std::lock_guard<std::mutex> guard{mutex_};
56 int64_t offset = now - prt;
62 double delta = offset * pr;
63 st = pst + (int64_t)delta;
69 double SimTime::rate() {
71 sim_time_callback_fn callback;
74 std::lock_guard<std::mutex> guard{mutex_};
78 callback(
nullptr, &r);
87 uint64_t SimTime::duration(uint64_t sim_duration) {
94 return sim_duration / r;
97 uint64_t SimTime::future(uint64_t sim_offset) {
98 uint64_t now = realtime();
99 uint64_t offset = duration(sim_offset);
108 sim_time_callback_fn set_sim_time_callback(sim_time_callback_fn fn) {
109 std::lock_guard<std::mutex> guard{SimTime::mutex_};
110 sim_time_callback_fn ret = SimTime::callback_;
111 SimTime::callback_ = fn;
115 void sim_time_notify(uint64_t time,
double rate) {
116 uint64_t now = SimTime::realtime();
118 std::lock_guard<std::mutex> guard{SimTime::mutex_};
120 SimTime::last_realtime_ = now;
121 SimTime::last_simtime_ = time;
122 SimTime::last_rate_ = rate;
Provides utility functions and classes for common tasks and needs.
Copyright (c) 2015 Carnegie Mellon University.