monotonic.cpp 833 B

1234567891011121314151617181920212223
  1. #include "monotonic.h"
  2. #include <chrono>
  3. namespace NActors {
  4. namespace {
  5. // Unfortunately time_since_epoch() is sometimes negative on wine
  6. // Remember initial time point at program start and use offsets from that
  7. std::chrono::steady_clock::time_point MonotonicOffset = std::chrono::steady_clock::now();
  8. }
  9. ui64 GetMonotonicMicroSeconds() {
  10. auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - MonotonicOffset).count();
  11. // Steady clock is supposed to never jump backwards, but it's better to be safe in case of buggy implementations
  12. if (Y_UNLIKELY(microseconds < 0)) {
  13. microseconds = 0;
  14. }
  15. // Add one so we never return zero
  16. return microseconds + 1;
  17. }
  18. } // namespace NActors