This commit is contained in:
trung09122002
2026-09-18 18:14:42 +07:00
commit 48bd5377dc
224 changed files with 37685 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
#include "vslam/clock.h"
#include <mutex>
#include <thread>
#include "vslam/log.h"
namespace vslam {
namespace {
class SystemClock : public Clock {
public:
UnixTime Now() const override { return UnixTime::now(); }
SteadyPoint Steady() const override { return std::chrono::steady_clock::now(); }
void SleepUntil(SteadyPoint tp) override {
std::this_thread::sleep_until(tp);
}
bool is_virtual() const override { return false; }
};
} // namespace
ClockPtr MakeSystemClock() { return std::make_shared<SystemClock>(); }
// ---------------------------------------------------------------------------
struct ReplayClock::Impl {
mutable std::mutex mu;
UnixTime now;
SteadyPoint steady_origin;
std::chrono::nanoseconds steady_offset{0};
double speed = 0.0; // 0 = chay nhanh nhat, khong ngu that
};
ReplayClock::ReplayClock(UnixTime start) : impl_(std::make_unique<Impl>()) {
impl_->now = start;
impl_->steady_origin = std::chrono::steady_clock::now();
}
ReplayClock::~ReplayClock() = default;
UnixTime ReplayClock::Now() const {
std::lock_guard<std::mutex> lk(impl_->mu);
return impl_->now;
}
Clock::SteadyPoint ReplayClock::Steady() const {
std::lock_guard<std::mutex> lk(impl_->mu);
return impl_->steady_origin + impl_->steady_offset;
}
void ReplayClock::SleepUntil(SteadyPoint tp) {
double speed;
SteadyPoint current;
{
std::lock_guard<std::mutex> lk(impl_->mu);
speed = impl_->speed;
current = impl_->steady_origin + impl_->steady_offset;
if (tp > current) {
// Thoi gian ao luon tien toi moc yeu cau, ke ca khi khong ngu that.
impl_->steady_offset += (tp - current);
}
}
if (speed > 0.0 && tp > current) {
const auto wall = std::chrono::duration_cast<std::chrono::nanoseconds>(
(tp - current) / speed);
std::this_thread::sleep_for(wall);
}
}
void ReplayClock::SetNow(UnixTime t, bool allow_backward) {
std::lock_guard<std::mutex> lk(impl_->mu);
if (!allow_backward && t < impl_->now) {
// Lui thoi gian la dau hieu ban ghi bi xao tron thu tu. Bao chu khong
// im lang chap nhan -- neu im lang thi moi phep timeout sau do deu sai.
VSLAM_WARN("ReplayClock: tu choi lui thoi gian " << impl_->now.toString()
<< " -> " << t.toString());
return;
}
impl_->now = t;
}
void ReplayClock::Advance(Duration d) {
std::lock_guard<std::mutex> lk(impl_->mu);
impl_->now = impl_->now + d;
impl_->steady_offset += std::chrono::nanoseconds(d.toNSec());
}
void ReplayClock::SetSpeed(double speed) {
std::lock_guard<std::mutex> lk(impl_->mu);
impl_->speed = speed < 0.0 ? 0.0 : speed;
}
std::shared_ptr<ReplayClock> MakeReplayClock(UnixTime start) {
return std::make_shared<ReplayClock>(start);
}
} // namespace vslam