Changeset 34:2b0db3816e23 in rrlib_time


Ignore:
Timestamp:
04.10.2020 22:10:20 (2 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Phase:
public
Message:

Adds new time mode 'steady initial system time' - and makes tTimestamp a system time stamp again (to avoid the necessity to adapt driver modules now - and mostly revert this when the full planned upgrade to time handling is ready)

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • time.cpp

    r33 r34  
    8181//---------------------------------------------------------------------- 
    8282 
    83 /*! Current time mode (FIXME: we use int as template parameter, because enum support is not available until gcc 4.7) */ 
    84 static std::atomic<int> mode((int)tTimeMode::SYSTEM_TIME); 
     83namespace 
     84{ 
     85 
     86/*! Current time mode */ 
     87std::atomic<tTimeMode> mode(tTimeMode::SYSTEM_TIME); 
    8588 
    8689/*! Time stretching parameters: application time = application_start + time_stretching_factor * (system time - application_start - time_diff); */ 
     
    9295 
    9396/*! Helper struct for storing parameters in two int64 atomic variables */ 
    94 static const uint cNUMERATOR_MASK = 0xFFFFF; // 20 bit 
    95 static const uint cSTAMP_MASK = 0xFFF; // 12 bit 
     97const uint cNUMERATOR_MASK = 0xFFFFF; // 20 bit 
     98const uint cSTAMP_MASK = 0xFFF; // 12 bit 
    9699union tParameterStoreHelper 
    97100{ 
     
    108111 
    109112/*! Parameter storage - two buffers to avoid live-locks */ 
    110 static std::atomic<uint64_t> time_stretching_parameters1(1LL << 32); 
    111 static std::atomic<uint64_t> time_stretching_parameters2(1LL << 32); 
    112 static std::atomic<uint64_t> time_stretching_parameters1_copy(1LL << 32); 
    113 static std::atomic<uint64_t> time_stretching_parameters2_copy(1LL << 32); 
    114 static const tTimestamp application_start = Now(); 
     113std::atomic<uint64_t> time_stretching_parameters1(1LL << 32); 
     114std::atomic<uint64_t> time_stretching_parameters2(1LL << 32); 
     115std::atomic<uint64_t> time_stretching_parameters1_copy(1LL << 32); 
     116std::atomic<uint64_t> time_stretching_parameters2_copy(1LL << 32); 
     117const tTimestamp application_start = Now(); 
    115118 
    116119/*! Current time - in non-linear clock mode */ 
    117 static tAtomicTimestamp current_time; 
     120tAtomicTimestamp current_time; 
    118121 
    119122/*! Current time source */ 
    120 static const tCustomClock* current_clock = NULL; 
     123const tCustomClock* current_clock = nullptr; 
     124 
     125/*! Offset between steady and system clock at startup time */ 
     126const tDuration cINITIAL_SYSTEM_TO_STEADY_CLOCK_OFFSET = GetCurrentSystemToSteadyClockOffset(); 
    121127 
    122128/*! Load time stretching parameters from two valid atomic variables */ 
    123 static void LoadParameters(tTimeStretchingParameters& params) 
     129void LoadParameters(tTimeStretchingParameters& params) 
    124130{ 
    125131  // (Live-)Lock-free loading of parameters 
     
    147153 
    148154/*! Store time stretching parameters to atomic variables */ 
    149 static void StoreParameters(const tTimeStretchingParameters& params) 
     155void StoreParameters(const tTimeStretchingParameters& params) 
    150156{ 
    151157  static int64_t counter = 0; 
     
    162168 
    163169 
    164 static tTimestamp ToApplicationTime(const tTimestamp& system_time) 
     170inline tTimestamp GetApplicationTime(const tTimestamp* optional_system_time = nullptr) 
    165171{ 
    166172  switch (GetTimeMode()) 
    167173  { 
    168174  case tTimeMode::SYSTEM_TIME: 
    169     return system_time; 
     175    return optional_system_time ? *optional_system_time : std::chrono::system_clock::now(); 
     176  case tTimeMode::STEADY_INITIAL_SYSTEM_TIME: 
     177    return ToSystemTime(std::chrono::steady_clock::now(), cINITIAL_SYSTEM_TO_STEADY_CLOCK_OFFSET); 
    170178  case tTimeMode::CUSTOM_CLOCK: 
    171179    return current_time.Load(); 
     
    173181    tTimeStretchingParameters params; 
    174182    LoadParameters(params); 
     183    auto system_time = optional_system_time ? *optional_system_time : std::chrono::system_clock::now(); 
    175184    std::chrono::nanoseconds tmp((system_time - application_start) - params.time_diff); 
    176185    auto ticks = tmp.count(); 
     
    182191} 
    183192 
     193} 
     194 
    184195tTimestamp Now(bool precise) 
    185196{ 
    186   return ToApplicationTime(tBaseClock::now()); 
     197  return GetApplicationTime(); 
    187198} 
    188199 
    189200tTimeMode GetTimeMode() 
    190201{ 
    191   return (tTimeMode)mode.load(std::memory_order_relaxed); 
     202  return mode.load(std::memory_order_relaxed); 
     203} 
     204 
     205void SetTimeMode(tTimeMode new_mode) 
     206{ 
     207  mode.store(new_mode); 
    192208} 
    193209 
     
    201217      current_clock = clock; 
    202218      current_time.Store(initial_time); 
    203       if (mode.load() != (int)tTimeMode::CUSTOM_CLOCK) 
     219      if (mode.load() != tTimeMode::CUSTOM_CLOCK) 
    204220      { 
    205         mode.store((int)tTimeMode::CUSTOM_CLOCK); 
     221        mode.store(tTimeMode::CUSTOM_CLOCK); 
    206222        tTimeStretchingListener::NotifyListeners(tTimeMode::CUSTOM_CLOCK); 
    207223      } 
     
    210226    else 
    211227    { 
    212       if (mode.load() != (int)tTimeMode::STRETCHED_SYSTEM_TIME) 
     228      if (mode.load() != tTimeMode::STRETCHED_SYSTEM_TIME) 
    213229      { 
    214         mode.store((int)tTimeMode::STRETCHED_SYSTEM_TIME); 
     230        mode.store(tTimeMode::STRETCHED_SYSTEM_TIME); 
    215231        tTimeStretchingListener::NotifyListeners(tTimeMode::STRETCHED_SYSTEM_TIME); 
    216232      } 
     
    242258    { 
    243259      tTimestamp system_time = tBaseClock::now(); 
    244       tTimestamp app_time = ToApplicationTime(system_time); 
     260      tTimestamp app_time = GetApplicationTime(&system_time); 
    245261 
    246262      params.time_diff = system_time - app_time; 
     
    249265      StoreParameters(params); 
    250266 
    251       if (mode.load() != (int)tTimeMode::STRETCHED_SYSTEM_TIME) 
     267      if (mode.load() != tTimeMode::STRETCHED_SYSTEM_TIME) 
    252268      { 
    253         mode.store((int)tTimeMode::STRETCHED_SYSTEM_TIME); 
     269        mode.store(tTimeMode::STRETCHED_SYSTEM_TIME); 
    254270        tTimeStretchingListener::NotifyListeners(tTimeMode::STRETCHED_SYSTEM_TIME); 
    255271      } 
     
    269285  case tTimeMode::SYSTEM_TIME: 
    270286  case tTimeMode::CUSTOM_CLOCK: 
     287  case tTimeMode::STEADY_INITIAL_SYSTEM_TIME: 
    271288    return app_duration; 
    272289  case tTimeMode::STRETCHED_SYSTEM_TIME: 
     
    632649tDuration GetCurrentSystemToSteadyClockOffset() 
    633650{ 
    634   tSteadyTimestamp steady_time_1 = std::chrono::steady_clock::now(); 
     651  tSteadyTimestamp steady_time = std::chrono::steady_clock::now(); 
    635652  tSystemTimestamp system_time = std::chrono::system_clock::now(); 
    636   tSteadyTimestamp steady_time_2 = std::chrono::steady_clock::now(); 
    637   tDuration steady_delta = steady_time_2 - steady_time_1; 
    638   tSteadyTimestamp steady_time = steady_time_1 + steady_delta / 2; 
    639653  return system_time.time_since_epoch() - steady_time.time_since_epoch(); 
    640654} 
     
    643657bool tCustomClock::IsCurrentTimeSource() const 
    644658{ 
    645   return mode.load() == static_cast<int>(tTimeMode::CUSTOM_CLOCK) && current_clock == this; 
     659  return mode.load() == tTimeMode::CUSTOM_CLOCK && current_clock == this; 
    646660} 
    647661 
     
    651665  { 
    652666    std::lock_guard<std::mutex> lock(internal::tTimeMutex::Instance()); 
    653     if (this == current_clock && mode.load() == (int)tTimeMode::CUSTOM_CLOCK) 
     667    if (this == current_clock && mode.load() == tTimeMode::CUSTOM_CLOCK) 
    654668    { 
    655669      current_time.Store(new_time); 
  • time.h

    r33 r34  
    6363 
    6464/*! 
    65  * Clock that "application time" is derived from 
    66  */ 
    67 typedef std::chrono::steady_clock tBaseClock; 
     65 * Clock whose timestamp type to use to represent "application time" 
     66 */ 
     67typedef std::chrono::system_clock tBaseClock; 
    6868 
    6969//! Time stamp type 
     
    9595enum class tTimeMode 
    9696{ 
    97   SYSTEM_TIME,            //!< "application time" is identical to system time 
    98   STRETCHED_SYSTEM_TIME,  //!< "application time" is system time with some sort of time-stretching applied 
    99   CUSTOM_CLOCK            //!< "application time" is set by an external entity ("custom clock") 
     97  SYSTEM_TIME,                 //!< "application time" is identical to system time 
     98  STEADY_INITIAL_SYSTEM_TIME,  //!< "application time" is derived from steady clock (and is therewith steady). On startup, the offset between steady and system clock is computed. This is used for the entire runtime of the program/process. 
     99  STRETCHED_SYSTEM_TIME,       //!< "application time" is system time with some sort of time-stretching applied. 
     100  CUSTOM_CLOCK                 //!< "application time" is set by an external entity ("custom clock") 
    100101}; 
    101102 
     
    122123 
    123124/*! 
     125 * Sets time mode. 
     126 * 
     127 * \param new_mode New Time Mode 
     128 */ 
     129void SetTimeMode(tTimeMode new_mode); 
     130 
     131/*! 
    124132 * Sets specified non-linear clock as active time source for "application time". 
    125133 * Time mode is set to CUSTOM_CLOCK. 
Note: See TracChangeset for help on using the changeset viewer.