waiter.h 4.7 KB

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