Baselib_Timer.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. // Defines the conversion ratio from Baselib_Timer_Ticks to nanoseconds as a fraction.
  26. typedef struct Baselib_Timer_TickToNanosecondConversionRatio
  27. {
  28. uint64_t ticksToNanosecondsNumerator;
  29. uint64_t ticksToNanosecondsDenominator;
  30. } Baselib_Timer_TickToNanosecondConversionRatio;
  31. // Returns the conversion ratio between ticks and nanoseconds.
  32. //
  33. // The conversion factor is guaranteed to be constant for the entire application for its entire lifetime.
  34. // However, it may be different on every start of the application.
  35. //
  36. // \returns The conversion factor from ticks to nanoseconds as an integer fraction.
  37. BASELIB_API Baselib_Timer_TickToNanosecondConversionRatio Baselib_Timer_GetTicksToNanosecondsConversionRatio(void);
  38. // The fraction of Baselib_Timer_GetTicksToNanosecondsConversionRatio as a precomputed double value. It is subject to precision loss.
  39. //
  40. // Attention:
  41. // 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.
  42. // Prefer Baselib_Timer_GetTicksToNanosecondsConversionRatio when in doubt.
  43. extern BASELIB_API const double Baselib_Timer_TickToNanosecondsConversionFactor;
  44. // Get the current tick count of the high precision timer.
  45. //
  46. // Accuracy:
  47. // It is assumed that the accuracy corresponds to the granularity of Baselib_Timer_Ticks (which is determined by Baselib_Timer_GetTicksToNanosecondsConversionRatio).
  48. // However, there are no strict guarantees on the accuracy of the timer.
  49. //
  50. // Monotony:
  51. // ATTENTION: On some platforms this clock is suspended during application/device sleep states.
  52. // The timer is not susceptible to wall clock time changes by the user.
  53. // Different threads are guaranteed to be on the same timeline.
  54. //
  55. // Known issues:
  56. // * Some web browsers impose Spectre mitigation which can introduce jitter in this timer.
  57. // * 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)
  58. //
  59. // \returns Current tick value of the high precision timer.
  60. BASELIB_API Baselib_Timer_Ticks Baselib_Timer_GetHighPrecisionTimerTicks(void);
  61. // This function will wait for at least the requested amount of time before returning.
  62. //
  63. // 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.
  64. //
  65. // \param timeInMilliseconds Time to wait in milliseconds
  66. BASELIB_API void Baselib_Timer_WaitForAtLeast(uint32_t timeInMilliseconds);
  67. // Time since application startup in seconds.
  68. //
  69. // Disregarding potential rounding errors, all threads are naturally on the same timeline (i.e. time since process start).
  70. BASELIB_API double Baselib_Timer_GetTimeSinceStartupInSeconds(void);
  71. #ifdef __cplusplus
  72. } // extern "C"
  73. #endif