3 #ifdef MADARA_FEATURE_SIMTIME 10 const double minrate = 0.0000000001;
12 std::mutex SimTime::mutex_{};
14 sim_time_callback_fn SimTime::callback_ =
nullptr;
16 uint64_t SimTime::last_realtime_ = SimTime::realtime();
17 uint64_t SimTime::last_simtime_ = -1;
18 double SimTime::last_rate_ = 1.0;
20 uint64_t SimTime::realtime() {
21 namespace sc = std::chrono;
22 auto now = sc::steady_clock::now();
23 auto dur = now.time_since_epoch();
24 return sc::duration_cast<sc::nanoseconds>(dur).count();
27 uint64_t SimTime::time() {
33 sim_time_callback_fn callback;
35 uint64_t now = realtime();
37 std::lock_guard<std::mutex> guard{mutex_};
53 if (pst == (uint64_t)-1) {
60 int64_t offset = now - prt;
66 double delta = offset * pr;
67 st = pst + (int64_t)delta;
73 double SimTime::rate() {
75 sim_time_callback_fn callback;
78 std::lock_guard<std::mutex> guard{mutex_};
82 callback(
nullptr, &r);
91 uint64_t SimTime::duration(uint64_t sim_duration) {
98 return sim_duration / r;
101 uint64_t SimTime::future(uint64_t sim_offset) {
102 uint64_t now = realtime();
103 uint64_t offset = duration(sim_offset);
105 if (offset == (uint64_t)-1) {
112 sim_time_callback_fn set_sim_time_callback(sim_time_callback_fn fn) {
113 std::lock_guard<std::mutex> guard{SimTime::mutex_};
115 swap (fn, SimTime::callback_);
119 void sim_time_notify(uint64_t time,
double rate) {
120 bool update_time = time != (uint64_t)-1;
121 bool update_rate = !std::isnan(rate);
123 if (!update_time && !update_rate) {
127 uint64_t now = SimTime::realtime();
128 std::lock_guard<std::mutex> guard{SimTime::mutex_};
131 SimTime::last_realtime_ = now;
132 SimTime::last_simtime_ = time;
133 }
else if (SimTime::last_simtime_ == (uint64_t)-1) {
134 SimTime::last_realtime_ = now;
135 SimTime::last_simtime_ = now;
139 SimTime::last_rate_ = rate;
Provides utility functions and classes for common tasks and needs.
Copyright (c) 2015 Carnegie Mellon University.