You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

waiter.h 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. {
  52. ABSL_NAMESPACE_BEGIN
  53. namespace synchronization_internal
  54. {
  55. // Waiter is an OS-specific semaphore.
  56. class Waiter
  57. {
  58. public:
  59. // Prepare any data to track waits.
  60. Waiter();
  61. // Not copyable or movable
  62. Waiter(const Waiter&) = delete;
  63. Waiter& operator=(const Waiter&) = delete;
  64. // Blocks the calling thread until a matching call to `Post()` or
  65. // `t` has passed. Returns `true` if woken (`Post()` called),
  66. // `false` on timeout.
  67. bool Wait(KernelTimeout t);
  68. // Restart the caller of `Wait()` as with a normal semaphore.
  69. void Post();
  70. // If anyone is waiting, wake them up temporarily and cause them to
  71. // call `MaybeBecomeIdle()`. They will then return to waiting for a
  72. // `Post()` or timeout.
  73. void Poke();
  74. // Returns the Waiter associated with the identity.
  75. static Waiter* GetWaiter(base_internal::ThreadIdentity* identity)
  76. {
  77. static_assert(
  78. sizeof(Waiter) <= sizeof(base_internal::ThreadIdentity::WaiterState),
  79. "Insufficient space for Waiter"
  80. );
  81. return reinterpret_cast<Waiter*>(identity->waiter_state.data);
  82. }
  83. // How many periods to remain idle before releasing resources
  84. #ifndef ABSL_HAVE_THREAD_SANITIZER
  85. static constexpr int kIdlePeriods = 60;
  86. #else
  87. // Memory consumption under ThreadSanitizer is a serious concern,
  88. // so we release resources sooner. The value of 1 leads to 1 to 2 second
  89. // delay before marking a thread as idle.
  90. static const int kIdlePeriods = 1;
  91. #endif
  92. private:
  93. // The destructor must not be called since Mutex/CondVar
  94. // can use PerThreadSem/Waiter after the thread exits.
  95. // Waiter objects are embedded in ThreadIdentity objects,
  96. // which are reused via a freelist and are never destroyed.
  97. ~Waiter() = delete;
  98. #if ABSL_WAITER_MODE == ABSL_WAITER_MODE_FUTEX
  99. // Futexes are defined by specification to be 32-bits.
  100. // Thus std::atomic<int32_t> must be just an int32_t with lockfree methods.
  101. std::atomic<int32_t> futex_;
  102. static_assert(sizeof(int32_t) == sizeof(futex_), "Wrong size for futex");
  103. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_CONDVAR
  104. // REQUIRES: mu_ must be held.
  105. void InternalCondVarPoke();
  106. pthread_mutex_t mu_;
  107. pthread_cond_t cv_;
  108. int waiter_count_;
  109. int wakeup_count_; // Unclaimed wakeups.
  110. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_SEM
  111. sem_t sem_;
  112. // This seems superfluous, but for Poke() we need to cause spurious
  113. // wakeups on the semaphore. Hence we can't actually use the
  114. // semaphore's count.
  115. std::atomic<int> wakeups_;
  116. #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_WIN32
  117. // WinHelper - Used to define utilities for accessing the lock and
  118. // condition variable storage once the types are complete.
  119. class WinHelper;
  120. // REQUIRES: WinHelper::GetLock(this) must be held.
  121. void InternalCondVarPoke();
  122. // We can't include Windows.h in our headers, so we use aligned character
  123. // buffers to define the storage of SRWLOCK and CONDITION_VARIABLE.
  124. // SRW locks and condition variables do not need to be explicitly destroyed.
  125. // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock
  126. // https://stackoverflow.com/questions/28975958/why-does-windows-have-no-deleteconditionvariable-function-to-go-together-with
  127. alignas(void*) unsigned char mu_storage_[sizeof(void*)];
  128. alignas(void*) unsigned char cv_storage_[sizeof(void*)];
  129. int waiter_count_;
  130. int wakeup_count_;
  131. #else
  132. #error Unknown ABSL_WAITER_MODE
  133. #endif
  134. };
  135. } // namespace synchronization_internal
  136. ABSL_NAMESPACE_END
  137. } // namespace absl
  138. #endif // ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_