Baselib_Timer.h 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #ifdef __cplusplus
  3. BASELIB_C_INTERFACE
  4. {
  5. #endif
  6. // Time conversion factors.
  7. //
  8. // (not an enum since Int32 can't represent Baselib_NanosecondsPerMinute)
  9. static const uint64_t Baselib_SecondsPerMinute = 60ULL;
  10. static const uint64_t Baselib_MillisecondsPerSecond = 1000ULL;
  11. static const uint64_t Baselib_MillisecondsPerMinute = 60ULL * 1000ULL;
  12. static const uint64_t Baselib_MicrosecondsPerMillisecond = 1000ULL;
  13. static const uint64_t Baselib_MicrosecondsPerSecond = 1000ULL * 1000ULL;
  14. static const uint64_t Baselib_MicrosecondsPerMinute = 60ULL * 1000ULL * 1000ULL;
  15. static const uint64_t Baselib_NanosecondsPerMicrosecond = 1000ULL;
  16. static const uint64_t Baselib_NanosecondsPerMillisecond = 1000ULL * 1000ULL;
  17. static const uint64_t Baselib_NanosecondsPerSecond = 1000ULL * 1000ULL * 1000ULL;
  18. static const uint64_t Baselib_NanosecondsPerMinute = 60ULL * 1000ULL * 1000ULL * 1000ULL;
  19. // Timer specific representation of time progression
  20. typedef uint64_t Baselib_Timer_Ticks;
  21. // Baselib_Timer_Ticks are guaranteed to be more granular than this constant.
  22. static const uint64_t Baselib_Timer_MaxNumberOfNanosecondsPerTick = 1000ULL;
  23. // Baselib_Timer_Ticks are guaranteed to be less granular than this constant.
  24. static const double Baselib_Timer_MinNumberOfNanosecondsPerTick = 0.01;
  25. // When comparing results of Baselib_Timer_GetHighPrecisionTimerTicks on different threads, timestamps are strictly monotonous with a tolerance of this many nanoseconds.
  26. static const double Baselib_Timer_HighPrecisionTimerCrossThreadMontotonyTolerance_InNanoseconds = 100.0;
  27. // Defines the conversion ratio from Baselib_Timer_Ticks to nanoseconds as a fraction.
  28. typedef struct Baselib_Timer_TickToNanosecondConversionRatio
  29. {
  30. uint64_t ticksToNanosecondsNumerator;
  31. uint64_t ticksToNanosecondsDenominator;
  32. } Baselib_Timer_TickToNanosecondConversionRatio;
  33. // Returns the conversion ratio between ticks and nanoseconds.
  34. //
  35. // The conversion factor is guaranteed to be constant for the entire application for its entire lifetime.
  36. // However, it may be different on every start of the application.
  37. //
  38. // \returns The conversion factor from ticks to nanoseconds as an integer fraction.
  39. BASELIB_API Baselib_Timer_TickToNanosecondConversionRatio Baselib_Timer_GetTicksToNanosecondsConversionRatio(void);
  40. // The fraction of Baselib_Timer_GetTicksToNanosecondsConversionRatio as a precomputed double value. It is subject to precision loss.
  41. //
  42. // Attention:
  43. // This value is determined during static initialization of baselib. As such it should not be used if it is not guaranteed that baselib is fully loaded.
  44. // Prefer Baselib_Timer_GetTicksToNanosecondsConversionRatio when in doubt.
  45. extern BASELIB_API const double Baselib_Timer_TickToNanosecondsConversionFactor;
  46. // Get the current tick count of the high precision timer.
  47. //
  48. // Accuracy:
  49. // It is assumed that the accuracy corresponds to the granularity of Baselib_Timer_Ticks (which is determined by Baselib_Timer_GetTicksToNanosecondsConversionRatio).
  50. // However, there are no strict guarantees on the accuracy of the timer.
  51. //
  52. // Monotony:
  53. // * ATTENTION: On some platforms this clock is suspended during application/device sleep states.
  54. // * The timer is not susceptible to wall clock time changes by the user.
  55. // * The timer is strictly monotonous on a thread
  56. // * When comparing times from different threads, timestamps are strictly monotonous with a tolerance of Baselib_Timer_HighPrecisionTimerCrossThreadMontotonyTolerance_InNanoseconds.
  57. //
  58. // Known issues:
  59. // * Some web browsers impose Spectre mitigation which can introduce jitter in this timer.
  60. // * Some web browsers may have different timelines per thread/webworker if they are not spawned on startup (this is a bug according to newest W3C specification)
  61. //
  62. // \returns Current tick value of the high precision timer.
  63. BASELIB_API Baselib_Timer_Ticks Baselib_Timer_GetHighPrecisionTimerTicks(void);
  64. // This function will wait for at least the requested amount of time before returning.
  65. //
  66. // Unlike some implementations of 'sleep', passing 0 does NOT guarantee a thread yield and may return immediately! Use the corresponding functionality in Baselib_Thread instead.
  67. //
  68. // \param timeInMilliseconds Time to wait in milliseconds
  69. BASELIB_API void Baselib_Timer_WaitForAtLeast(uint32_t timeInMilliseconds);
  70. // Time since application startup in seconds.
  71. //
  72. // Disregarding potential rounding errors, all threads are naturally on the same timeline (i.e. time since process start).
  73. BASELIB_API double Baselib_Timer_GetTimeSinceStartupInSeconds(void);
  74. #ifdef __cplusplus
  75. } // extern "C"
  76. #endif