3#ifndef _TIME_SHIELD_NTP_CLIENT_POOL_RUNNER_HPP_INCLUDED
4#define _TIME_SHIELD_NTP_CLIENT_POOL_RUNNER_HPP_INCLUDED
8#if TIME_SHIELD_ENABLE_NTP_CLIENT
15#include <condition_variable>
39 template <
class PoolT>
45 :
m_pool(std::move(pool)) {}
56 bool start(std::chrono::milliseconds interval = std::chrono::seconds(30),
57 bool measure_immediately =
true) {
61 if (interval.count() <= 0) {
62 interval = std::chrono::milliseconds(1);
83 bool start(
int interval_ms,
bool measure_immediately =
true) {
84 return start(std::chrono::milliseconds(interval_ms), measure_immediately);
152 void run_loop(std::chrono::milliseconds interval,
bool measure_immediately) {
153 bool is_first = measure_immediately;
159 std::unique_lock<std::mutex> lk(
m_cv_mtx);
160 m_cv.wait_for(lk, interval, [
this]() {
227 static_assert(
sizeof(
void*) == 0,
"NtpClientPoolRunner is disabled by configuration.");
Background runner that periodically measures NTP offsets using a pool.
std::atomic< bool > m_last_measure_ok
bool start(int interval_ms, bool measure_immediately=true)
Start periodic measurements using milliseconds.
std::atomic< bool > m_is_stop_requested
std::atomic< int64_t > m_last_update_realtime_us
std::atomic< bool > m_is_force_requested
uint64_t fail_count() const noexcept
Return number of failed measurement attempts.
~BasicPoolRunner()
Stop background thread on destruction.
bool start(std::chrono::milliseconds interval=std::chrono::seconds(30), bool measure_immediately=true)
Start periodic measurements on a background thread.
std::atomic< bool > m_is_running
uint64_t measure_count() const noexcept
Return total number of measurement attempts.
std::condition_variable m_cv
int64_t utc_time_us() const noexcept
Return current UTC time in microseconds using pool offset.
std::atomic< uint64_t > m_measure_count
int64_t utc_time_sec() const noexcept
Return current UTC time in seconds using pool offset.
bool force_measure()
Wake the worker thread and request a measurement.
void run_loop(std::chrono::milliseconds interval, bool measure_immediately)
bool measure_now()
Perform one measurement immediately.
bool last_measure_ok() const noexcept
Return whether last measurement updated the offset.
bool running() const noexcept
Return true when background thread is running.
int64_t offset_us() const noexcept
Return last estimated offset in microseconds.
std::atomic< uint64_t > m_fail_count
BasicPoolRunner(PoolT pool=PoolT{})
Construct runner with a pool instance.
int64_t utc_time_ms() const noexcept
Return current UTC time in milliseconds using pool offset.
int64_t last_update_realtime_us() const noexcept
Return realtime timestamp of last measurement attempt.
std::atomic< int64_t > m_last_success_realtime_us
void stop()
Stop background measurements.
std::vector< NtpSample > last_samples() const
Return copy of the most recent samples.
int64_t last_success_realtime_us() const noexcept
Return realtime timestamp of last successful measurement.
Configuration macros for the library.
ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
Main namespace for the Time Shield library.
Header file with time-related utility functions.