diff --git a/components/common/cpp/src/timer.cpp b/components/common/cpp/src/timer.cpp index 23de9476169b932b9da9927c76b8b57f960e44af..6064c1f744225c0c1541d0a617d2dc6a5cf0ee33 100644 --- a/components/common/cpp/src/timer.cpp +++ b/components/common/cpp/src/timer.cpp @@ -61,11 +61,13 @@ static void waitTimePoint() { } // Still lots of time so do some idle jobs - { + if (msdelay >= 10 && sincelast != mspf) { UNIQUE_LOCK(mtx, lk); auto idle_job = jobs[kTimerIdle10].begin(); while (idle_job != jobs[kTimerIdle10].end() && msdelay >= 10 && sincelast != mspf) { - (*idle_job++).job(now); + (*idle_job).active = true; + (*idle_job).job(now); + (*idle_job++).active = false; now = get_time(); msdelay = mspf - (now % mspf); } diff --git a/components/rgbd-sources/src/streamer.cpp b/components/rgbd-sources/src/streamer.cpp index 962da5a373073c1928135154ba36a3c7a838af41..311b69021504b5c6bf4f6366a17e5026b262df9c 100644 --- a/components/rgbd-sources/src/streamer.cpp +++ b/components/rgbd-sources/src/streamer.cpp @@ -173,10 +173,18 @@ void Streamer::_addClient(const string &source, int N, int rate, const ftl::UUID if (time_peer_ == ftl::UUID(0)) { time_peer_ = peer; + // Do a time sync whenever the CPU is idle for 10ms or more. + // FIXME: Could be starved timer_job_ = ftl::timer::add(ftl::timer::kTimerIdle10, [peer,this](int id) { - // Also do a time sync (but should be repeated periodically) auto start = std::chrono::high_resolution_clock::now(); - int64_t mastertime = net_->call<int64_t>(peer, "__ping__"); + int64_t mastertime; + + try { + mastertime = net_->call<int64_t>(peer, "__ping__"); + } catch (...) { + //timer_job_.cancel(); + } + auto elapsed = std::chrono::high_resolution_clock::now() - start; int64_t latency = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count(); auto clock_adjust = mastertime - (ftl::timer::get_time() + (latency/2));