futex_waiter.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/futex_waiter.h"
  15. #ifdef Y_ABSL_INTERNAL_HAVE_FUTEX_WAITER
  16. #include <atomic>
  17. #include <cstdint>
  18. #include <cerrno>
  19. #include "y_absl/base/config.h"
  20. #include "y_absl/base/internal/raw_logging.h"
  21. #include "y_absl/base/internal/thread_identity.h"
  22. #include "y_absl/base/optimization.h"
  23. #include "y_absl/synchronization/internal/kernel_timeout.h"
  24. #include "y_absl/synchronization/internal/futex.h"
  25. namespace y_absl {
  26. Y_ABSL_NAMESPACE_BEGIN
  27. namespace synchronization_internal {
  28. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  29. constexpr char FutexWaiter::kName[];
  30. #endif
  31. int FutexWaiter::WaitUntil(std::atomic<int32_t>* v, int32_t val,
  32. KernelTimeout t) {
  33. #ifdef CLOCK_MONOTONIC
  34. constexpr bool kHasClockMonotonic = true;
  35. #else
  36. constexpr bool kHasClockMonotonic = false;
  37. #endif
  38. // We can't call Futex::WaitUntil() here because the prodkernel implementation
  39. // does not know about KernelTimeout::SupportsSteadyClock().
  40. if (!t.has_timeout()) {
  41. return Futex::Wait(v, val);
  42. } else if (kHasClockMonotonic && KernelTimeout::SupportsSteadyClock() &&
  43. t.is_relative_timeout()) {
  44. auto rel_timespec = t.MakeRelativeTimespec();
  45. return Futex::WaitRelativeTimeout(v, val, &rel_timespec);
  46. } else {
  47. auto abs_timespec = t.MakeAbsTimespec();
  48. return Futex::WaitAbsoluteTimeout(v, val, &abs_timespec);
  49. }
  50. }
  51. bool FutexWaiter::Wait(KernelTimeout t) {
  52. // Loop until we can atomically decrement futex from a positive
  53. // value, waiting on a futex while we believe it is zero.
  54. // Note that, since the thread ticker is just reset, we don't need to check
  55. // whether the thread is idle on the very first pass of the loop.
  56. bool first_pass = true;
  57. while (true) {
  58. int32_t x = futex_.load(std::memory_order_relaxed);
  59. while (x != 0) {
  60. if (!futex_.compare_exchange_weak(x, x - 1,
  61. std::memory_order_acquire,
  62. std::memory_order_relaxed)) {
  63. continue; // Raced with someone, retry.
  64. }
  65. return true; // Consumed a wakeup, we are done.
  66. }
  67. if (!first_pass) MaybeBecomeIdle();
  68. const int err = WaitUntil(&futex_, 0, t);
  69. if (err != 0) {
  70. if (err == -EINTR || err == -EWOULDBLOCK || err == -512 /* ERESTARTSYS */ || err == -516 /* ERESTART_RESTARTBLOCK */) {
  71. // Do nothing, the loop will retry.
  72. } else if (err == -ETIMEDOUT) {
  73. return false;
  74. } else {
  75. Y_ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
  76. }
  77. }
  78. first_pass = false;
  79. }
  80. }
  81. void FutexWaiter::Post() {
  82. if (futex_.fetch_add(1, std::memory_order_release) == 0) {
  83. // We incremented from 0, need to wake a potential waiter.
  84. Poke();
  85. }
  86. }
  87. void FutexWaiter::Poke() {
  88. // Wake one thread waiting on the futex.
  89. const int err = Futex::Wake(&futex_, 1);
  90. if (Y_ABSL_PREDICT_FALSE(err < 0)) {
  91. Y_ABSL_RAW_LOG(FATAL, "Futex operation failed with error %d\n", err);
  92. }
  93. }
  94. } // namespace synchronization_internal
  95. Y_ABSL_NAMESPACE_END
  96. } // namespace y_absl
  97. #endif // Y_ABSL_INTERNAL_HAVE_FUTEX_WAITER