Skip to content
Snippets Groups Projects
Commit b130a11c authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Allow 10ms idle timer jobs

parent d18854d5
No related branches found
No related tags found
1 merge request!97Resolves #160 by resync of clock
Checking pipeline status
...@@ -55,6 +55,8 @@ struct TimerHandle { ...@@ -55,6 +55,8 @@ struct TimerHandle {
TimerHandle &operator=(const TimerHandle &h) { const_cast<int&>(id) = h.id; return *this; } TimerHandle &operator=(const TimerHandle &h) { const_cast<int&>(id) = h.id; return *this; }
}; };
int64_t get_time();
/** /**
* Milliseconds between calls. * Milliseconds between calls.
*/ */
......
...@@ -38,7 +38,7 @@ struct TimerJob { ...@@ -38,7 +38,7 @@ struct TimerJob {
static list<TimerJob> jobs[kTimerMAXLEVEL]; static list<TimerJob> jobs[kTimerMAXLEVEL];
static inline int64_t get_time() { int64_t ftl::timer::get_time() {
return time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch().count()+clock_adjust; return time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch().count()+clock_adjust;
} }
...@@ -60,6 +60,15 @@ static void waitTimePoint() { ...@@ -60,6 +60,15 @@ static void waitTimePoint() {
msdelay = mspf - (now % mspf); msdelay = mspf - (now % mspf);
} }
// Still lots of time so do some idle jobs
while (msdelay >= 10 && sincelast != mspf) {
for (auto &j : jobs[kTimerIdle10]) {
j.job(now);
}
now = get_time();
msdelay = mspf - (now % mspf);
}
// Spin loop until exact grab time // Spin loop until exact grab time
//LOG(INFO) << "Spin Delay: " << (now / 40) << " = " << (40 - (now%40)); //LOG(INFO) << "Spin Delay: " << (now / 40) << " = " << (40 - (now%40));
...@@ -82,7 +91,7 @@ int ftl::timer::getInterval() { ...@@ -82,7 +91,7 @@ int ftl::timer::getInterval() {
} }
void ftl::timer::setClockAdjustment(int64_t ms) { void ftl::timer::setClockAdjustment(int64_t ms) {
clock_adjust = ms; clock_adjust += ms;
} }
const TimerHandle ftl::timer::add(timerlevel_t l, const std::function<void(int64_t ts)> &f) { const TimerHandle ftl::timer::add(timerlevel_t l, const std::function<void(int64_t ts)> &f) {
......
...@@ -172,16 +172,21 @@ void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID ...@@ -172,16 +172,21 @@ void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID
if (time_peer_ == ftl::UUID(0)) { if (time_peer_ == ftl::UUID(0)) {
time_peer_ = peer; time_peer_ = peer;
// Also do a time sync (but should be repeated periodically) ftl::timer::add(ftl::timer::kTimerIdle10, [peer,this](int id) {
auto start = std::chrono::high_resolution_clock::now(); // Also do a time sync (but should be repeated periodically)
int64_t mastertime = net_->call<int64_t>(peer, "__ping__"); auto start = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::high_resolution_clock::now() - start; int64_t mastertime = net_->call<int64_t>(peer, "__ping__");
int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(); auto elapsed = std::chrono::high_resolution_clock::now() - start;
clock_adjust_ = mastertime - (std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() + (latency/2)); int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
LOG(INFO) << "Clock adjustment: " << clock_adjust_; auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2));
LOG(INFO) << "Latency: " << (latency / 2);
LOG(INFO) << "Local: " << std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() << ", master: " << mastertime; //if (clock_adjust > 0) {
ftl::timer::setClockAdjustment(clock_adjust_); LOG(INFO) << "Clock adjustment: " << clock_adjust;
//LOG(INFO) << "Latency: " << (latency / 2);
//LOG(INFO) << "Local: " << std::chrono::time_point_cast<std::chrono::milliseconds>(start).time_since_epoch().count() << ", master: " << mastertime;
ftl::timer::setClockAdjustment(clock_adjust);
//}
});
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment