85 #include "time_core.h"
89 using namespace std::chrono;
93 thread_ret thread_function_call TimeCore::Run(
void *args) {
95 TimeCore *_this = ((TimeCore *)args);
103 if (!j->is_alive()) {
109 Timestamp target = j->target_time_;
110 Timestamp next_target(microseconds(0));
111 if (target.time_since_epoch().count() == 0)
112 run = j->update(next_target);
115 auto time_to_wait = duration_cast<microseconds>(target - Now());
116 if (time_to_wait.count() == 0)
117 run = j->update(next_target);
118 else if (time_to_wait.count() > 0) {
120 DelegatedCore *d =
new DelegatedCore(j->target_time_, time_to_wait, j);
121 d->start(DelegatedCore::Wait);
122 _Mem::Get()->register_time_job_latency(time_to_wait);
123 next_target = Timestamp(seconds(0));
126 run = j->update(next_target);
127 j->report(-time_to_wait);
131 while (next_target.time_since_epoch().count() && run) {
136 auto time_to_wait = duration_cast<microseconds>(next_target - Now());
137 next_target = Timestamp(seconds(0));
138 if (time_to_wait.count() == 0)
139 run = j->update(next_target);
140 else if (time_to_wait.count() > 0) {
142 DelegatedCore *d =
new DelegatedCore(next_target, time_to_wait, j);
143 d->start(DelegatedCore::Wait);
146 run = j->update(next_target);
147 j->report(-time_to_wait);
158 TimeCore::TimeCore() :
Thread() {
161 TimeCore::~TimeCore() {
166 thread_ret thread_function_call DelegatedCore::Wait(
void *args) {
168 _Mem::Get()->start_core();
169 DelegatedCore *_this = ((DelegatedCore *)args);
171 auto time_to_wait = _this->time_to_wait_;
172 auto target_time = _this->target_time_;
174 wait: _this->timer_.start(time_to_wait);
175 _this->timer_.wait();
177 if (!_this->job_->is_alive())
180 if (_Mem::Get()->check_state() == _Mem::RUNNING) {
182 while (Now() < target_time);
183 target_time = Timestamp(seconds(0));
184 _this->job_->update(target_time);
187 redo:
if (target_time.time_since_epoch().count()) {
189 if (!_this->job_->is_alive())
191 if (_Mem::Get()->check_state() != _Mem::RUNNING)
194 time_to_wait = duration_cast<microseconds>(target_time - Now());
195 if (time_to_wait.count() == 0) {
197 _this->job_->update(target_time);
199 }
else if (time_to_wait.count() < 0) {
201 _this->job_->update(target_time);
202 _this->job_->report(-time_to_wait);
208 end: _Mem::Get()->shutdown_core();
215 DelegatedCore::DelegatedCore(Timestamp target_time, microseconds time_to_wait, TimeJob *j) :
Thread(), target_time_(target_time), time_to_wait_(time_to_wait), job_(j) {
218 DelegatedCore::~DelegatedCore() {