waiter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2017 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. //
  15. #ifndef ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_
  16. #define ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_
  17. #include "absl/base/config.h"
  18. #ifdef _WIN32
  19. #include <sdkddkver.h>
  20. #else
  21. #include <pthread.h>
  22. #endif
  23. #ifdef __linux__
  24. #include <linux/futex.h>
  25. #endif
  26. #ifdef ABSL_HAVE_SEMAPHORE_H
  27. #include <semaphore.h>
  28. #endif
  29. #include <atomic>
  30. #include <cstdint>
  31. #include "absl/base/internal/thread_identity.h"
  32. #include "absl/synchronization/internal/kernel_timeout.h"
  33. // May be chosen at compile time via -DABSL_FORCE_WAITER_MODE=<index>
  34. #define ABSL_WAITER_MODE_FUTEX 0
  35. #define ABSL_WAITER_MODE_SEM 1
  36. #define ABSL_WAITER_MODE_CONDVAR 2
  37. #define ABSL_WAITER_MODE_WIN32 3
  38. #if defined(ABSL_FORCE_WAITER_MODE)
  39. #define ABSL_WAITER_MODE ABSL_FORCE_WAITER_MODE
  40. #elif defined(_WIN32) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  41. #define ABSL_WAITER_MODE ABSL_WAITER_MODE_WIN32
  42. #elif defined(__BIONIC__)
  43. // Bionic supports all the futex operations we need even when some of the futex
  44. // definitions are missing.
  45. #define ABSL_WAITER_MODE ABSL_WAITER_MODE_FUTEX
  46. #elif defined(__linux__) && defined(FUTEX_CLOCK_REALTIME)
  47. // FUTEX_CLOCK_REALTIME requires Linux >= 2.6.28.
  48. #define ABSL_WAITER_MODE ABSL_WAITER_MODE_FUTEX
  49. #elif defined(ABSL_HAVE_SEMAPHORE_H)
  50. #define ABSL_WAITER_MODE ABSL_WAITER_MODE_SEM
  51. #else
  52. #define ABSL_WAITER_MODE ABSL_WAITER_MODE_CONDVAR
  53. #endif
  54. namespace absl {
  55. ABSL_NAMESPACE_BEGIN
  56. namespace synchronization_internal {
  57. // Waiter is an OS-specific semaphore.
  58. class Waiter {
  59. public:
  60. // Prepare any data to track waits.
  61. Waiter();
  62. // Not copyable or movable
  63. Waiter(const Waiter&) = delete;
  64. Waiter& operator=(const Waiter&) = delete;
  65. // Destroy any data to track waits.
  66. ~Waiter();
  67. // Blocks the calling thread until a matching call to `Post()` or
  68. // `t` has passed. Returns `true` if woken (`Post()` called),
  69. // `false` on timeout.
  70. bool Wait(KernelTimeout t);
  71. // Restart the caller of `Wait()` as with a normal semaphore.
  72. void Post();
  73. // If anyone is waiting, wake them up temporarily and cause them to
  74. // call `MaybeBecomeIdle()`. They will then return to waiting for a
  75. // `Post()` or timeout.
  76. void Poke();
  77. // Returns the Waiter associated with the identity.
  78. static Waiter* GetWaiter(base_internal::ThreadIdentity* identity) {
  79. static_assert(
  80. sizeof(Waiter) <= sizeof(base_internal::ThreadIdentity::WaiterState),
  81. "Insufficient space for Waiter");
  82. return reinterpret_cast<Waiter*>(identity->waiter_state.data);
  83. }
  84. // How many periods to remain idle before releasing resources
  85. #ifndef ABSL_HAVE_THREAD_SANITIZER
  86. static constexpr int kIdlePeriods = 60;
  87. #else
  88. // Memory consumption under ThreadSanitizer is a serious concern,
  89. // so we release resources sooner. The value of 1 leads to 1 to 2 second
  90. // delay before marking a thread as idle.
  91. static const int kIdlePeriods = 1;
  92. #endif
  93. private:
  94. #if ABSL_WAITER_MODE == ABSL_WAITER_MODE_FUTEX
  95. // Futexes are defined by specification to be 32-bits.
  96. // Thus std::atomic<int32_t> must be just an int32_t with lockfree methods.
  97. std::atomic<int32_t> futex_;
  98. static_assert(sizeof(int32_t) == sizeof(futex_), "Wrong size for futex");
  99. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_CONDVAR
  100. // REQUIRES: mu_ must be held.
  101. void InternalCondVarPoke();
  102. pthread_mutex_t mu_;
  103. pthread_cond_t cv_;
  104. int waiter_count_;
  105. int wakeup_count_; // Unclaimed wakeups.
  106. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_SEM
  107. sem_t sem_;
  108. // This seems superfluous, but for Poke() we need to cause spurious
  109. // wakeups on the semaphore. Hence we can't actually use the
  110. // semaphore's count.
  111. std::atomic<int> wakeups_;
  112. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_WIN32
  113. // WinHelper - Used to define utilities for accessing the lock and
  114. // condition variable storage once the types are complete.
  115. class WinHelper;
  116. // REQUIRES: WinHelper::GetLock(this) must be held.
  117. void InternalCondVarPoke();
  118. // We can't include Windows.h in our headers, so we use aligned charachter
  119. // buffers to define the storage of SRWLOCK and CONDITION_VARIABLE.
  120. alignas(void*) unsigned char mu_storage_[sizeof(void*)];
  121. alignas(void*) unsigned char cv_storage_[sizeof(void*)];
  122. int waiter_count_;
  123. int wakeup_count_;
  124. #else
  125. #error Unknown ABSL_WAITER_MODE
  126. #endif
  127. };
  128. } // namespace synchronization_internal
  129. ABSL_NAMESPACE_END
  130. } // namespace absl
  131. #endif // ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_