sync_cxx11.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2020 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. #include "platform_sys.h"
  11. #include <iomanip>
  12. #include <math.h>
  13. #include <stdexcept>
  14. #include "sync.h"
  15. #include "srt_compat.h"
  16. #include "common.h"
  17. ////////////////////////////////////////////////////////////////////////////////
  18. //
  19. // Clock frequency helpers
  20. //
  21. ////////////////////////////////////////////////////////////////////////////////
  22. namespace {
  23. template <int val>
  24. int pow10();
  25. template <>
  26. int pow10<10>()
  27. {
  28. return 1;
  29. }
  30. template <int val>
  31. int pow10()
  32. {
  33. return 1 + pow10<val / 10>();
  34. }
  35. }
  36. int srt::sync::clockSubsecondPrecision()
  37. {
  38. const int64_t ticks_per_sec = (srt::sync::steady_clock::period::den / srt::sync::steady_clock::period::num);
  39. const int decimals = pow10<ticks_per_sec>();
  40. return decimals;
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////
  43. //
  44. // SyncCond (based on stl chrono C++11)
  45. //
  46. ////////////////////////////////////////////////////////////////////////////////
  47. srt::sync::Condition::Condition() {}
  48. srt::sync::Condition::~Condition() {}
  49. void srt::sync::Condition::init() {}
  50. void srt::sync::Condition::destroy() {}
  51. void srt::sync::Condition::wait(UniqueLock& lock)
  52. {
  53. m_cv.wait(lock);
  54. }
  55. bool srt::sync::Condition::wait_for(UniqueLock& lock, const steady_clock::duration& rel_time)
  56. {
  57. // Another possible implementation is wait_until(steady_clock::now() + timeout);
  58. return m_cv.wait_for(lock, rel_time) != std::cv_status::timeout;
  59. }
  60. bool srt::sync::Condition::wait_until(UniqueLock& lock, const steady_clock::time_point& timeout_time)
  61. {
  62. return m_cv.wait_until(lock, timeout_time) != std::cv_status::timeout;
  63. }
  64. void srt::sync::Condition::notify_one()
  65. {
  66. m_cv.notify_one();
  67. }
  68. void srt::sync::Condition::notify_all()
  69. {
  70. m_cv.notify_all();
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////
  73. //
  74. // CThreadError class - thread local storage error wrapper
  75. //
  76. ////////////////////////////////////////////////////////////////////////////////
  77. // Threal local error will be used by CUDTUnited
  78. // with a static scope, therefore static thread_local
  79. static thread_local srt::CUDTException s_thErr;
  80. void srt::sync::SetThreadLocalError(const srt::CUDTException& e)
  81. {
  82. s_thErr = e;
  83. }
  84. srt::CUDTException& srt::sync::GetThreadLocalError()
  85. {
  86. return s_thErr;
  87. }