xref: /aosp_15_r20/external/abseil-cpp/absl/synchronization/internal/win32_waiter.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 //
15 
16 #ifndef ABSL_SYNCHRONIZATION_INTERNAL_WIN32_WAITER_H_
17 #define ABSL_SYNCHRONIZATION_INTERNAL_WIN32_WAITER_H_
18 
19 #ifdef _WIN32
20 #include <sdkddkver.h>
21 #endif
22 
23 #if defined(_WIN32) && !defined(__MINGW32__) && \
24     _WIN32_WINNT >= _WIN32_WINNT_VISTA
25 
26 #include "absl/base/config.h"
27 #include "absl/synchronization/internal/kernel_timeout.h"
28 #include "absl/synchronization/internal/waiter_base.h"
29 
30 namespace absl {
31 ABSL_NAMESPACE_BEGIN
32 namespace synchronization_internal {
33 
34 #define ABSL_INTERNAL_HAVE_WIN32_WAITER 1
35 
36 class Win32Waiter : public WaiterCrtp<Win32Waiter> {
37  public:
38   Win32Waiter();
39 
40   bool Wait(KernelTimeout t);
41   void Post();
42   void Poke();
43 
44   static constexpr char kName[] = "Win32Waiter";
45 
46  private:
47   // WinHelper - Used to define utilities for accessing the lock and
48   // condition variable storage once the types are complete.
49   class WinHelper;
50 
51   // REQUIRES: WinHelper::GetLock(this) must be held.
52   void InternalCondVarPoke();
53 
54   // We can't include Windows.h in our headers, so we use aligned character
55   // buffers to define the storage of SRWLOCK and CONDITION_VARIABLE.
56   // SRW locks and condition variables do not need to be explicitly destroyed.
57   // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock
58   // https://stackoverflow.com/questions/28975958/why-does-windows-have-no-deleteconditionvariable-function-to-go-together-with
59   alignas(void*) unsigned char mu_storage_[sizeof(void*)];
60   alignas(void*) unsigned char cv_storage_[sizeof(void*)];
61   int waiter_count_;
62   int wakeup_count_;
63 };
64 
65 }  // namespace synchronization_internal
66 ABSL_NAMESPACE_END
67 }  // namespace absl
68 
69 #endif  // defined(_WIN32) && !defined(__MINGW32__) &&
70         // _WIN32_WINNT >= _WIN32_WINNT_VISTA
71 
72 #endif  // ABSL_SYNCHRONIZATION_INTERNAL_WIN32_WAITER_H_
73