xref: /aosp_15_r20/external/abseil-cpp/absl/synchronization/internal/win32_waiter.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2023 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/internal/win32_waiter.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_HAVE_WIN32_WAITER
18*9356374aSAndroid Build Coastguard Worker 
19*9356374aSAndroid Build Coastguard Worker #include <windows.h>
20*9356374aSAndroid Build Coastguard Worker 
21*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
22*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/raw_logging.h"
23*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/thread_identity.h"
24*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
25*9356374aSAndroid Build Coastguard Worker #include "absl/synchronization/internal/kernel_timeout.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker namespace absl {
28*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
29*9356374aSAndroid Build Coastguard Worker namespace synchronization_internal {
30*9356374aSAndroid Build Coastguard Worker 
31*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
32*9356374aSAndroid Build Coastguard Worker constexpr char Win32Waiter::kName[];
33*9356374aSAndroid Build Coastguard Worker #endif
34*9356374aSAndroid Build Coastguard Worker 
35*9356374aSAndroid Build Coastguard Worker class Win32Waiter::WinHelper {
36*9356374aSAndroid Build Coastguard Worker  public:
GetLock(Win32Waiter * w)37*9356374aSAndroid Build Coastguard Worker   static SRWLOCK *GetLock(Win32Waiter *w) {
38*9356374aSAndroid Build Coastguard Worker     return reinterpret_cast<SRWLOCK *>(&w->mu_storage_);
39*9356374aSAndroid Build Coastguard Worker   }
40*9356374aSAndroid Build Coastguard Worker 
GetCond(Win32Waiter * w)41*9356374aSAndroid Build Coastguard Worker   static CONDITION_VARIABLE *GetCond(Win32Waiter *w) {
42*9356374aSAndroid Build Coastguard Worker     return reinterpret_cast<CONDITION_VARIABLE *>(&w->cv_storage_);
43*9356374aSAndroid Build Coastguard Worker   }
44*9356374aSAndroid Build Coastguard Worker 
45*9356374aSAndroid Build Coastguard Worker   static_assert(sizeof(SRWLOCK) == sizeof(void *),
46*9356374aSAndroid Build Coastguard Worker                 "`mu_storage_` does not have the same size as SRWLOCK");
47*9356374aSAndroid Build Coastguard Worker   static_assert(alignof(SRWLOCK) == alignof(void *),
48*9356374aSAndroid Build Coastguard Worker                 "`mu_storage_` does not have the same alignment as SRWLOCK");
49*9356374aSAndroid Build Coastguard Worker 
50*9356374aSAndroid Build Coastguard Worker   static_assert(sizeof(CONDITION_VARIABLE) == sizeof(void *),
51*9356374aSAndroid Build Coastguard Worker                 "`ABSL_CONDITION_VARIABLE_STORAGE` does not have the same size "
52*9356374aSAndroid Build Coastguard Worker                 "as `CONDITION_VARIABLE`");
53*9356374aSAndroid Build Coastguard Worker   static_assert(
54*9356374aSAndroid Build Coastguard Worker       alignof(CONDITION_VARIABLE) == alignof(void *),
55*9356374aSAndroid Build Coastguard Worker       "`cv_storage_` does not have the same alignment as `CONDITION_VARIABLE`");
56*9356374aSAndroid Build Coastguard Worker 
57*9356374aSAndroid Build Coastguard Worker   // The SRWLOCK and CONDITION_VARIABLE types must be trivially constructible
58*9356374aSAndroid Build Coastguard Worker   // and destructible because we never call their constructors or destructors.
59*9356374aSAndroid Build Coastguard Worker   static_assert(std::is_trivially_constructible<SRWLOCK>::value,
60*9356374aSAndroid Build Coastguard Worker                 "The `SRWLOCK` type must be trivially constructible");
61*9356374aSAndroid Build Coastguard Worker   static_assert(
62*9356374aSAndroid Build Coastguard Worker       std::is_trivially_constructible<CONDITION_VARIABLE>::value,
63*9356374aSAndroid Build Coastguard Worker       "The `CONDITION_VARIABLE` type must be trivially constructible");
64*9356374aSAndroid Build Coastguard Worker   static_assert(std::is_trivially_destructible<SRWLOCK>::value,
65*9356374aSAndroid Build Coastguard Worker                 "The `SRWLOCK` type must be trivially destructible");
66*9356374aSAndroid Build Coastguard Worker   static_assert(std::is_trivially_destructible<CONDITION_VARIABLE>::value,
67*9356374aSAndroid Build Coastguard Worker                 "The `CONDITION_VARIABLE` type must be trivially destructible");
68*9356374aSAndroid Build Coastguard Worker };
69*9356374aSAndroid Build Coastguard Worker 
70*9356374aSAndroid Build Coastguard Worker class LockHolder {
71*9356374aSAndroid Build Coastguard Worker  public:
LockHolder(SRWLOCK * mu)72*9356374aSAndroid Build Coastguard Worker   explicit LockHolder(SRWLOCK* mu) : mu_(mu) {
73*9356374aSAndroid Build Coastguard Worker     AcquireSRWLockExclusive(mu_);
74*9356374aSAndroid Build Coastguard Worker   }
75*9356374aSAndroid Build Coastguard Worker 
76*9356374aSAndroid Build Coastguard Worker   LockHolder(const LockHolder&) = delete;
77*9356374aSAndroid Build Coastguard Worker   LockHolder& operator=(const LockHolder&) = delete;
78*9356374aSAndroid Build Coastguard Worker 
~LockHolder()79*9356374aSAndroid Build Coastguard Worker   ~LockHolder() {
80*9356374aSAndroid Build Coastguard Worker     ReleaseSRWLockExclusive(mu_);
81*9356374aSAndroid Build Coastguard Worker   }
82*9356374aSAndroid Build Coastguard Worker 
83*9356374aSAndroid Build Coastguard Worker  private:
84*9356374aSAndroid Build Coastguard Worker   SRWLOCK* mu_;
85*9356374aSAndroid Build Coastguard Worker };
86*9356374aSAndroid Build Coastguard Worker 
Win32Waiter()87*9356374aSAndroid Build Coastguard Worker Win32Waiter::Win32Waiter() {
88*9356374aSAndroid Build Coastguard Worker   auto *mu = ::new (static_cast<void *>(&mu_storage_)) SRWLOCK;
89*9356374aSAndroid Build Coastguard Worker   auto *cv = ::new (static_cast<void *>(&cv_storage_)) CONDITION_VARIABLE;
90*9356374aSAndroid Build Coastguard Worker   InitializeSRWLock(mu);
91*9356374aSAndroid Build Coastguard Worker   InitializeConditionVariable(cv);
92*9356374aSAndroid Build Coastguard Worker   waiter_count_ = 0;
93*9356374aSAndroid Build Coastguard Worker   wakeup_count_ = 0;
94*9356374aSAndroid Build Coastguard Worker }
95*9356374aSAndroid Build Coastguard Worker 
Wait(KernelTimeout t)96*9356374aSAndroid Build Coastguard Worker bool Win32Waiter::Wait(KernelTimeout t) {
97*9356374aSAndroid Build Coastguard Worker   SRWLOCK *mu = WinHelper::GetLock(this);
98*9356374aSAndroid Build Coastguard Worker   CONDITION_VARIABLE *cv = WinHelper::GetCond(this);
99*9356374aSAndroid Build Coastguard Worker 
100*9356374aSAndroid Build Coastguard Worker   LockHolder h(mu);
101*9356374aSAndroid Build Coastguard Worker   ++waiter_count_;
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker   // Loop until we find a wakeup to consume or timeout.
104*9356374aSAndroid Build Coastguard Worker   // Note that, since the thread ticker is just reset, we don't need to check
105*9356374aSAndroid Build Coastguard Worker   // whether the thread is idle on the very first pass of the loop.
106*9356374aSAndroid Build Coastguard Worker   bool first_pass = true;
107*9356374aSAndroid Build Coastguard Worker   while (wakeup_count_ == 0) {
108*9356374aSAndroid Build Coastguard Worker     if (!first_pass) MaybeBecomeIdle();
109*9356374aSAndroid Build Coastguard Worker     // No wakeups available, time to wait.
110*9356374aSAndroid Build Coastguard Worker     if (!SleepConditionVariableSRW(cv, mu, t.InMillisecondsFromNow(), 0)) {
111*9356374aSAndroid Build Coastguard Worker       // GetLastError() returns a Win32 DWORD, but we assign to
112*9356374aSAndroid Build Coastguard Worker       // unsigned long to simplify the ABSL_RAW_LOG case below.  The uniform
113*9356374aSAndroid Build Coastguard Worker       // initialization guarantees this is not a narrowing conversion.
114*9356374aSAndroid Build Coastguard Worker       const unsigned long err{GetLastError()};  // NOLINT(runtime/int)
115*9356374aSAndroid Build Coastguard Worker       if (err == ERROR_TIMEOUT) {
116*9356374aSAndroid Build Coastguard Worker         --waiter_count_;
117*9356374aSAndroid Build Coastguard Worker         return false;
118*9356374aSAndroid Build Coastguard Worker       } else {
119*9356374aSAndroid Build Coastguard Worker         ABSL_RAW_LOG(FATAL, "SleepConditionVariableSRW failed: %lu", err);
120*9356374aSAndroid Build Coastguard Worker       }
121*9356374aSAndroid Build Coastguard Worker     }
122*9356374aSAndroid Build Coastguard Worker     first_pass = false;
123*9356374aSAndroid Build Coastguard Worker   }
124*9356374aSAndroid Build Coastguard Worker   // Consume a wakeup and we're done.
125*9356374aSAndroid Build Coastguard Worker   --wakeup_count_;
126*9356374aSAndroid Build Coastguard Worker   --waiter_count_;
127*9356374aSAndroid Build Coastguard Worker   return true;
128*9356374aSAndroid Build Coastguard Worker }
129*9356374aSAndroid Build Coastguard Worker 
Post()130*9356374aSAndroid Build Coastguard Worker void Win32Waiter::Post() {
131*9356374aSAndroid Build Coastguard Worker   LockHolder h(WinHelper::GetLock(this));
132*9356374aSAndroid Build Coastguard Worker   ++wakeup_count_;
133*9356374aSAndroid Build Coastguard Worker   InternalCondVarPoke();
134*9356374aSAndroid Build Coastguard Worker }
135*9356374aSAndroid Build Coastguard Worker 
Poke()136*9356374aSAndroid Build Coastguard Worker void Win32Waiter::Poke() {
137*9356374aSAndroid Build Coastguard Worker   LockHolder h(WinHelper::GetLock(this));
138*9356374aSAndroid Build Coastguard Worker   InternalCondVarPoke();
139*9356374aSAndroid Build Coastguard Worker }
140*9356374aSAndroid Build Coastguard Worker 
InternalCondVarPoke()141*9356374aSAndroid Build Coastguard Worker void Win32Waiter::InternalCondVarPoke() {
142*9356374aSAndroid Build Coastguard Worker   if (waiter_count_ != 0) {
143*9356374aSAndroid Build Coastguard Worker     WakeConditionVariable(WinHelper::GetCond(this));
144*9356374aSAndroid Build Coastguard Worker   }
145*9356374aSAndroid Build Coastguard Worker }
146*9356374aSAndroid Build Coastguard Worker 
147*9356374aSAndroid Build Coastguard Worker }  // namespace synchronization_internal
148*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
149*9356374aSAndroid Build Coastguard Worker }  // namespace absl
150*9356374aSAndroid Build Coastguard Worker 
151*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_INTERNAL_HAVE_WIN32_WAITER
152