|
Time Shield Library
C++ library for working with time
|
Singleton service for background NTP measurements. More...
#include <ntp_time_service.hpp>
Public Member Functions | |
| NtpTimeServiceT (const NtpTimeServiceT &)=delete | |
| NtpTimeServiceT & | operator= (const NtpTimeServiceT &)=delete |
| bool | init () |
| Start background measurements using stored interval. | |
| bool | init (std::chrono::milliseconds interval, bool measure_immediately=true) |
| Start background measurements with interval and immediate flag. | |
| bool | init (int interval_ms, bool measure_immediately=true) |
| Start background measurements using milliseconds. | |
| void | shutdown () |
| Stop background measurements and release resources. | |
| bool | running () const noexcept |
| Return true when background runner is active. | |
| void | ensure_started () noexcept |
| Ensure background runner is started with current config. | |
| int64_t | offset_us () noexcept |
| Return last estimated offset in microseconds. | |
| int64_t | utc_time_us () noexcept |
| Return current UTC time in microseconds based on offset. | |
| int64_t | utc_time_ms () noexcept |
| Return current UTC time in milliseconds based on offset. | |
| int64_t | utc_time_sec () noexcept |
| Return current UTC time in seconds based on 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. | |
| bool | stale (std::chrono::milliseconds max_age) const noexcept |
| Return true when last measurement is older than max_age. | |
| bool | stale (int max_age_ms) const noexcept |
| Return true when last measurement is older than max_age_ms. | |
| bool | set_servers (std::vector< NtpServerConfig > servers) |
| Replace server list used for new runner instances. | |
| bool | set_default_servers () |
| Use conservative default servers for new runner instances. | |
| bool | clear_servers () |
| Clear custom server list and return to default behavior. | |
| bool | set_pool_config (NtpPoolConfig cfg) |
| Override pool configuration for new runner instances. | |
| NtpPoolConfig | pool_config () const |
| Return current pool configuration. | |
| std::vector< NtpSample > | last_samples () const |
| Return copy of last measurement samples. | |
| NtpTimeServiceT ()=default | |
| Construct service. | |
| ~NtpTimeServiceT ()=default | |
| Immortal singleton is stopped via process-shutdown handler. | |
| bool | apply_config_now () |
| Apply current config by rebuilding the runner. | |
Static Public Member Functions | |
| static NtpTimeServiceT & | instance () noexcept |
| Return the singleton instance. | |
Private Types | |
| enum class | ProcessState : uint8_t { alive , shutting_down } |
| Global lifetime state of the immortal singleton. More... | |
| enum class | State : uint8_t { stopped , starting , running , stopping } |
| Lifecycle state of the singleton runner. More... | |
Private Member Functions | |
| void | register_process_shutdown_handler () noexcept |
| Register one process-shutdown handler for this specialization. | |
| void | begin_process_shutdown () noexcept |
| Mark the singleton as shutting down and stop the runner. | |
| bool | is_process_shutting_down () const noexcept |
| Return true when process shutdown has started. | |
| bool | is_process_shutting_down_locked () const noexcept |
| Return true when process shutdown has started. | |
| uint32_t | atexit_registration_count () const noexcept |
| Return number of successful atexit registrations. | |
| bool | is_running_locked () const noexcept |
| Check runner status under lock. | |
| bool | is_transitioning_locked () const noexcept |
| Return true when a start or stop transition is in progress. | |
| bool | is_reconfigurable_locked () const noexcept |
| Return true when configuration can be changed safely. | |
| std::unique_ptr< RunnerT > | build_runner_locked () |
| Build a runner with current server list and pool config. | |
Private Attributes | |
| std::mutex | m_mtx |
| std::condition_variable | m_cv |
| State | m_state {State::stopped} |
| std::atomic< ProcessState > | m_process_state {ProcessState::alive} |
| std::atomic< int64_t > | m_last_offset_us {0} |
| std::atomic< uint32_t > | m_atexit_registration_count {0} |
| std::chrono::milliseconds | m_interval {std::chrono::seconds(30)} |
| bool | m_measure_immediately {true} |
| bool | m_has_custom_servers {false} |
| std::vector< NtpServerConfig > | m_servers |
| bool | m_has_custom_pool_cfg {false} |
| NtpPoolConfig | m_pool_cfg {} |
| std::unique_ptr< RunnerT > | m_runner |
Friends | |
| struct | detail::NtpTimeServiceSingleton< RunnerT > |
| struct | detail::NtpTimeServiceTestAccess< RunnerT > |
Singleton service for background NTP measurements.
Uses an internal runner to keep offset updated. It exposes UTC time computed as realtime clock plus the latest offset. Configure pool servers and sampling before starting the service. During process shutdown, the singleton stops background work and falls back to the last cached offset without restarting the runner.
Definition at line 199 of file ntp_time_service.hpp.
|
strongprivate |
Global lifetime state of the immortal singleton.
| Enumerator | |
|---|---|
| alive | |
| shutting_down | |
Definition at line 602 of file ntp_time_service.hpp.
|
strongprivate |
Lifecycle state of the singleton runner.
| Enumerator | |
|---|---|
| stopped | |
| starting | |
| running | |
| stopping | |
Definition at line 608 of file ntp_time_service.hpp.
|
delete |
|
default |
Construct service.
|
default |
Immortal singleton is stopped via process-shutdown handler.
|
inline |
Apply current config by rebuilding the runner.
Definition at line 524 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Return number of successful atexit registrations.
Definition at line 667 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Mark the singleton as shutting down and stop the runner.
Definition at line 623 of file ntp_time_service.hpp.
|
inlineprivate |
Build a runner with current server list and pool config.
Definition at line 687 of file ntp_time_service.hpp.
|
inline |
Clear custom server list and return to default behavior.
Definition at line 476 of file ntp_time_service.hpp.
|
inlinenoexcept |
Ensure background runner is started with current config.
Definition at line 337 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return number of failed measurement attempts.
Definition at line 408 of file ntp_time_service.hpp.
|
inline |
Start background measurements using stored interval.
Definition at line 215 of file ntp_time_service.hpp.
|
inline |
Start background measurements using milliseconds.
| interval_ms | Measurement interval in milliseconds. |
| measure_immediately | Measure before first sleep if true. |
Definition at line 299 of file ntp_time_service.hpp.
|
inline |
Start background measurements with interval and immediate flag.
| interval | Measurement interval. |
| measure_immediately | Measure before first sleep if true. |
Definition at line 223 of file ntp_time_service.hpp.
|
inlinestaticnoexcept |
Return the singleton instance.
Definition at line 206 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Return true when process shutdown has started.
Definition at line 657 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Return true when process shutdown has started.
Definition at line 662 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Return true when configuration can be changed safely.
Definition at line 682 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Check runner status under lock.
Definition at line 672 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Return true when a start or stop transition is in progress.
Definition at line 677 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return whether last measurement updated the offset.
Definition at line 392 of file ntp_time_service.hpp.
|
inline |
Return copy of last measurement samples.
Definition at line 511 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return realtime timestamp of last successful measurement.
Definition at line 424 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return realtime timestamp of last measurement attempt.
Definition at line 416 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return total number of measurement attempts.
Definition at line 400 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return last estimated offset in microseconds.
Definition at line 351 of file ntp_time_service.hpp.
|
delete |
|
inline |
Return current pool configuration.
Definition at line 501 of file ntp_time_service.hpp.
|
inlineprivatenoexcept |
Register one process-shutdown handler for this specialization.
Definition at line 616 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return true when background runner is active.
Definition at line 331 of file ntp_time_service.hpp.
|
inline |
Use conservative default servers for new runner instances.
Definition at line 464 of file ntp_time_service.hpp.
|
inline |
Override pool configuration for new runner instances.
| cfg | Pool configuration to apply. |
Definition at line 489 of file ntp_time_service.hpp.
|
inline |
Replace server list used for new runner instances.
| servers | Server configurations to use. |
Definition at line 452 of file ntp_time_service.hpp.
|
inline |
Stop background measurements and release resources.
Definition at line 304 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return true when last measurement is older than max_age_ms.
| max_age_ms | Maximum allowed age in milliseconds. |
Definition at line 445 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return true when last measurement is older than max_age.
| max_age | Maximum allowed age. |
Definition at line 433 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in milliseconds based on offset.
Definition at line 380 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in seconds based on offset.
Definition at line 386 of file ntp_time_service.hpp.
|
inlinenoexcept |
Return current UTC time in microseconds based on offset.
Definition at line 367 of file ntp_time_service.hpp.
|
friend |
Definition at line 185 of file ntp_time_service.hpp.
|
friend |
Definition at line 185 of file ntp_time_service.hpp.
|
private |
Definition at line 714 of file ntp_time_service.hpp.
|
private |
Definition at line 710 of file ntp_time_service.hpp.
|
private |
Definition at line 721 of file ntp_time_service.hpp.
|
private |
Definition at line 718 of file ntp_time_service.hpp.
|
private |
Definition at line 715 of file ntp_time_service.hpp.
|
private |
Definition at line 713 of file ntp_time_service.hpp.
|
private |
Definition at line 716 of file ntp_time_service.hpp.
|
mutableprivate |
Definition at line 709 of file ntp_time_service.hpp.
|
private |
Definition at line 722 of file ntp_time_service.hpp.
|
private |
Definition at line 712 of file ntp_time_service.hpp.
|
private |
Definition at line 724 of file ntp_time_service.hpp.
|
private |
Definition at line 719 of file ntp_time_service.hpp.
|
private |
Definition at line 711 of file ntp_time_service.hpp.