clock_posix.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "util/misc/clock.h"
  15. #include <time.h>
  16. #include "base/logging.h"
  17. #include "base/posix/eintr_wrapper.h"
  18. #include "build/build_config.h"
  19. namespace {
  20. constexpr uint64_t kNanosecondsPerSecond = 1E9;
  21. } // namespace
  22. namespace crashpad {
  23. #if !defined(OS_MACOSX)
  24. uint64_t ClockMonotonicNanoseconds() {
  25. timespec now;
  26. int rv = clock_gettime(CLOCK_MONOTONIC, &now);
  27. DPCHECK(rv == 0) << "clock_gettime";
  28. return now.tv_sec * kNanosecondsPerSecond + now.tv_nsec;
  29. }
  30. #endif
  31. void SleepNanoseconds(uint64_t nanoseconds) {
  32. timespec sleep_time;
  33. sleep_time.tv_sec = nanoseconds / kNanosecondsPerSecond;
  34. sleep_time.tv_nsec = nanoseconds % kNanosecondsPerSecond;
  35. int rv = HANDLE_EINTR(nanosleep(&sleep_time, &sleep_time));
  36. DPCHECK(rv == 0) << "nanosleep";
  37. }
  38. } // namespace crashpad