Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::BasicPoolRunner< PoolT > Class Template Reference

Background runner that periodically measures NTP offsets using a pool. More...

#include <ntp_client_pool_runner.hpp>

Public Member Functions

 BasicPoolRunner (PoolT pool=PoolT{})
 Construct runner with a pool instance.
 
 ~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.
 
bool start (int interval_ms, bool measure_immediately=true)
 Start periodic measurements using milliseconds.
 
void stop ()
 Stop background measurements.
 
bool running () const noexcept
 Return true when background thread is running.
 
bool force_measure ()
 Wake the worker thread and request a measurement.
 
bool measure_now ()
 Perform one measurement immediately.
 
int64_t offset_us () const noexcept
 Return last estimated offset in microseconds.
 
int64_t utc_time_us () const noexcept
 Return current UTC time in microseconds using pool offset.
 
int64_t utc_time_ms () const noexcept
 Return current UTC time in milliseconds using pool offset.
 
int64_t utc_time_sec () const noexcept
 Return current UTC time in seconds using pool offset.
 
bool last_measure_ok () const noexcept
 Return whether last measurement updated the offset.
 
uint64_t measure_count () const noexcept
 Return total number of measurement attempts.
 
uint64_t fail_count () const noexcept
 Return number of failed measurement attempts.
 
int64_t last_update_realtime_us () const noexcept
 Return realtime timestamp of last measurement attempt.
 
int64_t last_success_realtime_us () const noexcept
 Return realtime timestamp of last successful measurement.
 
std::vector< NtpSamplelast_samples () const
 Return copy of the most recent samples.
 

Private Member Functions

void run_loop (std::chrono::milliseconds interval, bool measure_immediately)
 
bool do_measure ()
 

Private Attributes

PoolT m_pool
 
std::mutex m_pool_mtx
 
std::thread m_thread
 
std::condition_variable m_cv
 
std::mutex m_cv_mtx
 
std::atomic< bool > m_is_running {false}
 
std::atomic< bool > m_is_stop_requested {false}
 
std::atomic< bool > m_is_force_requested {false}
 
std::atomic< bool > m_last_measure_ok {false}
 
std::atomic< uint64_t > m_measure_count {0}
 
std::atomic< uint64_t > m_fail_count {0}
 
std::atomic< int64_t > m_last_update_realtime_us {0}
 
std::atomic< int64_t > m_last_success_realtime_us {0}
 

Detailed Description

template<class PoolT>
class time_shield::BasicPoolRunner< PoolT >

Background runner that periodically measures NTP offsets using a pool.

pool.set_default_servers();
time_shield::NtpClientPoolRunner runner(std::move(pool));
runner.start(std::chrono::seconds(30));
auto now_ms = runner.utc_time_ms();
auto offset = runner.offset_us();
runner.stop();

Definition at line 40 of file ntp_client_pool_runner.hpp.

Constructor & Destructor Documentation

◆ BasicPoolRunner()

template<class PoolT>
time_shield::BasicPoolRunner< PoolT >::BasicPoolRunner ( PoolT pool = PoolT{})
inlineexplicit

Construct runner with a pool instance.

Parameters
poolPool instance to use.

Definition at line 44 of file ntp_client_pool_runner.hpp.

◆ ~BasicPoolRunner()

template<class PoolT>
time_shield::BasicPoolRunner< PoolT >::~BasicPoolRunner ( )
inline

Stop background thread on destruction.

Definition at line 48 of file ntp_client_pool_runner.hpp.

Member Function Documentation

◆ do_measure()

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::do_measure ( )
inlineprivate

Definition at line 173 of file ntp_client_pool_runner.hpp.

◆ fail_count()

template<class PoolT>
uint64_t time_shield::BasicPoolRunner< PoolT >::fail_count ( ) const
inlinenoexcept

Return number of failed measurement attempts.

Returns
Number of failed measurement attempts.

Definition at line 139 of file ntp_client_pool_runner.hpp.

◆ force_measure()

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::force_measure ( )
inline

Wake the worker thread and request a measurement.

Returns
True when request accepted.

Definition at line 103 of file ntp_client_pool_runner.hpp.

◆ last_measure_ok()

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::last_measure_ok ( ) const
inlinenoexcept

Return whether last measurement updated the offset.

Returns
True when last measurement updated the offset.

Definition at line 133 of file ntp_client_pool_runner.hpp.

◆ last_samples()

template<class PoolT>
std::vector< NtpSample > time_shield::BasicPoolRunner< PoolT >::last_samples ( ) const
inline

Return copy of the most recent samples.

Returns
Copy of samples from the last measurement.

Definition at line 149 of file ntp_client_pool_runner.hpp.

◆ last_success_realtime_us()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::last_success_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last successful measurement.

Returns
Realtime microseconds timestamp for last successful measurement.

Definition at line 145 of file ntp_client_pool_runner.hpp.

◆ last_update_realtime_us()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::last_update_realtime_us ( ) const
inlinenoexcept

Return realtime timestamp of last measurement attempt.

Returns
Realtime microseconds timestamp for last measurement attempt.

Definition at line 142 of file ntp_client_pool_runner.hpp.

◆ measure_count()

template<class PoolT>
uint64_t time_shield::BasicPoolRunner< PoolT >::measure_count ( ) const
inlinenoexcept

Return total number of measurement attempts.

Returns
Number of measurement attempts.

Definition at line 136 of file ntp_client_pool_runner.hpp.

◆ measure_now()

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::measure_now ( )
inline

Perform one measurement immediately.

Returns
True when pool offset updated.

Definition at line 114 of file ntp_client_pool_runner.hpp.

◆ offset_us()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::offset_us ( ) const
inlinenoexcept

Return last estimated offset in microseconds.

Returns
Offset in microseconds (UTC - local realtime).

Definition at line 120 of file ntp_client_pool_runner.hpp.

◆ run_loop()

template<class PoolT>
void time_shield::BasicPoolRunner< PoolT >::run_loop ( std::chrono::milliseconds interval,
bool measure_immediately )
inlineprivate

Definition at line 152 of file ntp_client_pool_runner.hpp.

◆ running()

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::running ( ) const
inlinenoexcept

Return true when background thread is running.

Returns
True when background measurements are active.

Definition at line 99 of file ntp_client_pool_runner.hpp.

◆ start() [1/2]

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::start ( int interval_ms,
bool measure_immediately = true )
inline

Start periodic measurements using milliseconds.

Parameters
interval_msMeasurement interval in milliseconds.
measure_immediatelyMeasure before first sleep if true.
Returns
True when background thread started.

Definition at line 83 of file ntp_client_pool_runner.hpp.

◆ start() [2/2]

template<class PoolT>
bool time_shield::BasicPoolRunner< PoolT >::start ( std::chrono::milliseconds interval = std::chrono::seconds(30),
bool measure_immediately = true )
inline

Start periodic measurements on a background thread.

Parameters
intervalMeasurement interval.
measure_immediatelyMeasure before first sleep if true.
Returns
True when background thread started.

Definition at line 56 of file ntp_client_pool_runner.hpp.

◆ stop()

template<class PoolT>
void time_shield::BasicPoolRunner< PoolT >::stop ( )
inline

Stop background measurements.

Definition at line 88 of file ntp_client_pool_runner.hpp.

◆ utc_time_ms()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::utc_time_ms ( ) const
inlinenoexcept

Return current UTC time in milliseconds using pool offset.

Returns
UTC time in milliseconds using pool offset.

Definition at line 126 of file ntp_client_pool_runner.hpp.

◆ utc_time_sec()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::utc_time_sec ( ) const
inlinenoexcept

Return current UTC time in seconds using pool offset.

Returns
UTC time in seconds using pool offset.

Definition at line 129 of file ntp_client_pool_runner.hpp.

◆ utc_time_us()

template<class PoolT>
int64_t time_shield::BasicPoolRunner< PoolT >::utc_time_us ( ) const
inlinenoexcept

Return current UTC time in microseconds using pool offset.

Returns
UTC time in microseconds using pool offset.

Definition at line 123 of file ntp_client_pool_runner.hpp.

Member Data Documentation

◆ m_cv

template<class PoolT>
std::condition_variable time_shield::BasicPoolRunner< PoolT >::m_cv
private

Definition at line 203 of file ntp_client_pool_runner.hpp.

◆ m_cv_mtx

template<class PoolT>
std::mutex time_shield::BasicPoolRunner< PoolT >::m_cv_mtx
private

Definition at line 204 of file ntp_client_pool_runner.hpp.

◆ m_fail_count

template<class PoolT>
std::atomic<uint64_t> time_shield::BasicPoolRunner< PoolT >::m_fail_count {0}
private

Definition at line 212 of file ntp_client_pool_runner.hpp.

◆ m_is_force_requested

template<class PoolT>
std::atomic<bool> time_shield::BasicPoolRunner< PoolT >::m_is_force_requested {false}
private

Definition at line 208 of file ntp_client_pool_runner.hpp.

◆ m_is_running

template<class PoolT>
std::atomic<bool> time_shield::BasicPoolRunner< PoolT >::m_is_running {false}
private

Definition at line 206 of file ntp_client_pool_runner.hpp.

◆ m_is_stop_requested

template<class PoolT>
std::atomic<bool> time_shield::BasicPoolRunner< PoolT >::m_is_stop_requested {false}
private

Definition at line 207 of file ntp_client_pool_runner.hpp.

◆ m_last_measure_ok

template<class PoolT>
std::atomic<bool> time_shield::BasicPoolRunner< PoolT >::m_last_measure_ok {false}
private

Definition at line 210 of file ntp_client_pool_runner.hpp.

◆ m_last_success_realtime_us

template<class PoolT>
std::atomic<int64_t> time_shield::BasicPoolRunner< PoolT >::m_last_success_realtime_us {0}
private

Definition at line 214 of file ntp_client_pool_runner.hpp.

◆ m_last_update_realtime_us

template<class PoolT>
std::atomic<int64_t> time_shield::BasicPoolRunner< PoolT >::m_last_update_realtime_us {0}
private

Definition at line 213 of file ntp_client_pool_runner.hpp.

◆ m_measure_count

template<class PoolT>
std::atomic<uint64_t> time_shield::BasicPoolRunner< PoolT >::m_measure_count {0}
private

Definition at line 211 of file ntp_client_pool_runner.hpp.

◆ m_pool

template<class PoolT>
PoolT time_shield::BasicPoolRunner< PoolT >::m_pool
private

Definition at line 199 of file ntp_client_pool_runner.hpp.

◆ m_pool_mtx

template<class PoolT>
std::mutex time_shield::BasicPoolRunner< PoolT >::m_pool_mtx
mutableprivate

Definition at line 200 of file ntp_client_pool_runner.hpp.

◆ m_thread

template<class PoolT>
std::thread time_shield::BasicPoolRunner< PoolT >::m_thread
private

Definition at line 202 of file ntp_client_pool_runner.hpp.


The documentation for this class was generated from the following file: