sem_waiter.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2023 The Abseil Authors.
  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. // https://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 "y_absl/synchronization/internal/sem_waiter.h"
  15. #ifdef Y_ABSL_INTERNAL_HAVE_SEM_WAITER
  16. #include <semaphore.h>
  17. #include <atomic>
  18. #include <cassert>
  19. #include <cstdint>
  20. #include <cerrno>
  21. #include "y_absl/base/config.h"
  22. #include "y_absl/base/internal/raw_logging.h"
  23. #include "y_absl/base/internal/thread_identity.h"
  24. #include "y_absl/base/optimization.h"
  25. #include "y_absl/synchronization/internal/kernel_timeout.h"
  26. namespace y_absl {
  27. Y_ABSL_NAMESPACE_BEGIN
  28. namespace synchronization_internal {
  29. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  30. constexpr char SemWaiter::kName[];
  31. #endif
  32. SemWaiter::SemWaiter() : wakeups_(0) {
  33. if (sem_init(&sem_, 0, 0) != 0) {
  34. Y_ABSL_RAW_LOG(FATAL, "sem_init failed with errno %d\n", errno);
  35. }
  36. }
  37. #if defined(__GLIBC__) && \
  38. (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30))
  39. #define Y_ABSL_INTERNAL_HAVE_SEM_CLOCKWAIT 1
  40. #elif defined(__ANDROID_API__) && __ANDROID_API__ >= 30
  41. #define Y_ABSL_INTERNAL_HAVE_SEM_CLOCKWAIT 1
  42. #endif
  43. // Calls sem_timedwait() or possibly something else like
  44. // sem_clockwait() depending on the platform and
  45. // KernelTimeout requested. The return value is the same as a call to the return
  46. // value to a call to sem_timedwait().
  47. int SemWaiter::TimedWait(KernelTimeout t) {
  48. if (KernelTimeout::SupportsSteadyClock() && t.is_relative_timeout()) {
  49. #if defined(Y_ABSL_INTERNAL_HAVE_SEM_CLOCKWAIT) && defined(CLOCK_MONOTONIC)
  50. const auto abs_clock_timeout = t.MakeClockAbsoluteTimespec(CLOCK_MONOTONIC);
  51. return sem_clockwait(&sem_, CLOCK_MONOTONIC, &abs_clock_timeout);
  52. #endif
  53. }
  54. const auto abs_timeout = t.MakeAbsTimespec();
  55. return sem_timedwait(&sem_, &abs_timeout);
  56. }
  57. bool SemWaiter::Wait(KernelTimeout t) {
  58. // Loop until we timeout or consume a wakeup.
  59. // Note that, since the thread ticker is just reset, we don't need to check
  60. // whether the thread is idle on the very first pass of the loop.
  61. bool first_pass = true;
  62. while (true) {
  63. int x = wakeups_.load(std::memory_order_relaxed);
  64. while (x != 0) {
  65. if (!wakeups_.compare_exchange_weak(x, x - 1,
  66. std::memory_order_acquire,
  67. std::memory_order_relaxed)) {
  68. continue; // Raced with someone, retry.
  69. }
  70. // Successfully consumed a wakeup, we're done.
  71. return true;
  72. }
  73. if (!first_pass) MaybeBecomeIdle();
  74. // Nothing to consume, wait (looping on EINTR).
  75. while (true) {
  76. if (!t.has_timeout()) {
  77. if (sem_wait(&sem_) == 0) break;
  78. if (errno == EINTR) continue;
  79. Y_ABSL_RAW_LOG(FATAL, "sem_wait failed: %d", errno);
  80. } else {
  81. if (TimedWait(t) == 0) break;
  82. if (errno == EINTR) continue;
  83. if (errno == ETIMEDOUT) return false;
  84. Y_ABSL_RAW_LOG(FATAL, "SemWaiter::TimedWait() failed: %d", errno);
  85. }
  86. }
  87. first_pass = false;
  88. }
  89. }
  90. void SemWaiter::Post() {
  91. // Post a wakeup.
  92. if (wakeups_.fetch_add(1, std::memory_order_release) == 0) {
  93. // We incremented from 0, need to wake a potential waiter.
  94. Poke();
  95. }
  96. }
  97. void SemWaiter::Poke() {
  98. if (sem_post(&sem_) != 0) { // Wake any semaphore waiter.
  99. Y_ABSL_RAW_LOG(FATAL, "sem_post failed with errno %d\n", errno);
  100. }
  101. }
  102. } // namespace synchronization_internal
  103. Y_ABSL_NAMESPACE_END
  104. } // namespace y_absl
  105. #endif // Y_ABSL_INTERNAL_HAVE_SEM_WAITER