win32_waiter.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "absl/synchronization/internal/win32_waiter.h"
  15. #ifdef ABSL_INTERNAL_HAVE_WIN32_WAITER
  16. #include <windows.h>
  17. #include "absl/base/config.h"
  18. #include "absl/base/internal/raw_logging.h"
  19. #include "absl/base/internal/thread_identity.h"
  20. #include "absl/base/optimization.h"
  21. #include "absl/synchronization/internal/kernel_timeout.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace synchronization_internal {
  25. #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  26. constexpr char Win32Waiter::kName[];
  27. #endif
  28. class Win32Waiter::WinHelper {
  29. public:
  30. static SRWLOCK *GetLock(Win32Waiter *w) {
  31. return reinterpret_cast<SRWLOCK *>(&w->mu_storage_);
  32. }
  33. static CONDITION_VARIABLE *GetCond(Win32Waiter *w) {
  34. return reinterpret_cast<CONDITION_VARIABLE *>(&w->cv_storage_);
  35. }
  36. static_assert(sizeof(SRWLOCK) == sizeof(void *),
  37. "`mu_storage_` does not have the same size as SRWLOCK");
  38. static_assert(alignof(SRWLOCK) == alignof(void *),
  39. "`mu_storage_` does not have the same alignment as SRWLOCK");
  40. static_assert(sizeof(CONDITION_VARIABLE) == sizeof(void *),
  41. "`ABSL_CONDITION_VARIABLE_STORAGE` does not have the same size "
  42. "as `CONDITION_VARIABLE`");
  43. static_assert(
  44. alignof(CONDITION_VARIABLE) == alignof(void *),
  45. "`cv_storage_` does not have the same alignment as `CONDITION_VARIABLE`");
  46. // The SRWLOCK and CONDITION_VARIABLE types must be trivially constructible
  47. // and destructible because we never call their constructors or destructors.
  48. static_assert(std::is_trivially_constructible<SRWLOCK>::value,
  49. "The `SRWLOCK` type must be trivially constructible");
  50. static_assert(
  51. std::is_trivially_constructible<CONDITION_VARIABLE>::value,
  52. "The `CONDITION_VARIABLE` type must be trivially constructible");
  53. static_assert(std::is_trivially_destructible<SRWLOCK>::value,
  54. "The `SRWLOCK` type must be trivially destructible");
  55. static_assert(std::is_trivially_destructible<CONDITION_VARIABLE>::value,
  56. "The `CONDITION_VARIABLE` type must be trivially destructible");
  57. };
  58. class LockHolder {
  59. public:
  60. explicit LockHolder(SRWLOCK* mu) : mu_(mu) {
  61. AcquireSRWLockExclusive(mu_);
  62. }
  63. LockHolder(const LockHolder&) = delete;
  64. LockHolder& operator=(const LockHolder&) = delete;
  65. ~LockHolder() {
  66. ReleaseSRWLockExclusive(mu_);
  67. }
  68. private:
  69. SRWLOCK* mu_;
  70. };
  71. Win32Waiter::Win32Waiter() {
  72. auto *mu = ::new (static_cast<void *>(&mu_storage_)) SRWLOCK;
  73. auto *cv = ::new (static_cast<void *>(&cv_storage_)) CONDITION_VARIABLE;
  74. InitializeSRWLock(mu);
  75. InitializeConditionVariable(cv);
  76. waiter_count_ = 0;
  77. wakeup_count_ = 0;
  78. }
  79. bool Win32Waiter::Wait(KernelTimeout t) {
  80. SRWLOCK *mu = WinHelper::GetLock(this);
  81. CONDITION_VARIABLE *cv = WinHelper::GetCond(this);
  82. LockHolder h(mu);
  83. ++waiter_count_;
  84. // Loop until we find a wakeup to consume or timeout.
  85. // Note that, since the thread ticker is just reset, we don't need to check
  86. // whether the thread is idle on the very first pass of the loop.
  87. bool first_pass = true;
  88. while (wakeup_count_ == 0) {
  89. if (!first_pass) MaybeBecomeIdle();
  90. // No wakeups available, time to wait.
  91. if (!SleepConditionVariableSRW(cv, mu, t.InMillisecondsFromNow(), 0)) {
  92. // GetLastError() returns a Win32 DWORD, but we assign to
  93. // unsigned long to simplify the ABSL_RAW_LOG case below. The uniform
  94. // initialization guarantees this is not a narrowing conversion.
  95. const unsigned long err{GetLastError()}; // NOLINT(runtime/int)
  96. if (err == ERROR_TIMEOUT) {
  97. --waiter_count_;
  98. return false;
  99. } else {
  100. ABSL_RAW_LOG(FATAL, "SleepConditionVariableSRW failed: %lu", err);
  101. }
  102. }
  103. first_pass = false;
  104. }
  105. // Consume a wakeup and we're done.
  106. --wakeup_count_;
  107. --waiter_count_;
  108. return true;
  109. }
  110. void Win32Waiter::Post() {
  111. LockHolder h(WinHelper::GetLock(this));
  112. ++wakeup_count_;
  113. InternalCondVarPoke();
  114. }
  115. void Win32Waiter::Poke() {
  116. LockHolder h(WinHelper::GetLock(this));
  117. InternalCondVarPoke();
  118. }
  119. void Win32Waiter::InternalCondVarPoke() {
  120. if (waiter_count_ != 0) {
  121. WakeConditionVariable(WinHelper::GetCond(this));
  122. }
  123. }
  124. } // namespace synchronization_internal
  125. ABSL_NAMESPACE_END
  126. } // namespace absl
  127. #endif // ABSL_INTERNAL_HAVE_WIN32_WAITER