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 | |
16 | #ifndef ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_ |
17 | #define ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_ |
18 | |
19 | #include "absl/base/config.h" |
20 | |
21 | #ifndef _WIN32 |
22 | #include <pthread.h> |
23 | #endif |
24 | |
25 | #ifdef ABSL_HAVE_SEMAPHORE_H |
26 | #include <semaphore.h> |
27 | #endif |
28 | |
29 | #include <atomic> |
30 | #include <cstdint> |
31 | |
32 | #include "absl/base/internal/thread_identity.h" |
33 | #include "absl/synchronization/internal/kernel_timeout.h" |
34 | |
35 | // May be chosen at compile time via -DABSL_FORCE_WAITER_MODE=<index> |
36 | #define ABSL_WAITER_MODE_FUTEX 0 |
37 | #define ABSL_WAITER_MODE_SEM 1 |
38 | #define ABSL_WAITER_MODE_CONDVAR 2 |
39 | #define ABSL_WAITER_MODE_WIN32 3 |
40 | |
41 | #if defined(ABSL_FORCE_WAITER_MODE) |
42 | #define ABSL_WAITER_MODE ABSL_FORCE_WAITER_MODE |
43 | #elif defined(_WIN32) |
44 | #define ABSL_WAITER_MODE ABSL_WAITER_MODE_WIN32 |
45 | #elif defined(__linux__) |
46 | #define ABSL_WAITER_MODE ABSL_WAITER_MODE_FUTEX |
47 | #elif defined(ABSL_HAVE_SEMAPHORE_H) |
48 | #define ABSL_WAITER_MODE ABSL_WAITER_MODE_SEM |
49 | #else |
50 | #define ABSL_WAITER_MODE ABSL_WAITER_MODE_CONDVAR |
51 | #endif |
52 | |
53 | namespace absl { |
54 | namespace synchronization_internal { |
55 | |
56 | // Waiter is an OS-specific semaphore. |
57 | class Waiter { |
58 | public: |
59 | // No constructor, instances use the reserved space in ThreadIdentity. |
60 | // All initialization logic belongs in `Init()`. |
61 | Waiter() = delete; |
62 | Waiter(const Waiter&) = delete; |
63 | Waiter& operator=(const Waiter&) = delete; |
64 | |
65 | // Prepare any data to track waits. |
66 | void Init(); |
67 | |
68 | // Blocks the calling thread until a matching call to `Post()` or |
69 | // `t` has passed. Returns `true` if woken (`Post()` called), |
70 | // `false` on timeout. |
71 | bool Wait(KernelTimeout t); |
72 | |
73 | // Restart the caller of `Wait()` as with a normal semaphore. |
74 | void Post(); |
75 | |
76 | // If anyone is waiting, wake them up temporarily and cause them to |
77 | // call `MaybeBecomeIdle()`. They will then return to waiting for a |
78 | // `Post()` or timeout. |
79 | void Poke(); |
80 | |
81 | // Returns the Waiter associated with the identity. |
82 | static Waiter* GetWaiter(base_internal::ThreadIdentity* identity) { |
83 | static_assert( |
84 | sizeof(Waiter) <= sizeof(base_internal::ThreadIdentity::WaiterState), |
85 | "Insufficient space for Waiter" ); |
86 | return reinterpret_cast<Waiter*>(identity->waiter_state.data); |
87 | } |
88 | |
89 | // How many periods to remain idle before releasing resources |
90 | #ifndef THREAD_SANITIZER |
91 | static const int kIdlePeriods = 60; |
92 | #else |
93 | // Memory consumption under ThreadSanitizer is a serious concern, |
94 | // so we release resources sooner. The value of 1 leads to 1 to 2 second |
95 | // delay before marking a thread as idle. |
96 | static const int kIdlePeriods = 1; |
97 | #endif |
98 | |
99 | private: |
100 | #if ABSL_WAITER_MODE == ABSL_WAITER_MODE_FUTEX |
101 | // Futexes are defined by specification to be 32-bits. |
102 | // Thus std::atomic<int32_t> must be just an int32_t with lockfree methods. |
103 | std::atomic<int32_t> futex_; |
104 | static_assert(sizeof(int32_t) == sizeof(futex_), "Wrong size for futex" ); |
105 | |
106 | #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_CONDVAR |
107 | pthread_mutex_t mu_; |
108 | pthread_cond_t cv_; |
109 | std::atomic<int> waiter_count_; |
110 | std::atomic<int> wakeup_count_; // Unclaimed wakeups, written under lock. |
111 | |
112 | #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_SEM |
113 | sem_t sem_; |
114 | // This seems superfluous, but for Poke() we need to cause spurious |
115 | // wakeups on the semaphore. Hence we can't actually use the |
116 | // semaphore's count. |
117 | std::atomic<int> wakeups_; |
118 | |
119 | #elif ABSL_WAITER_MODE == ABSL_WAITER_MODE_WIN32 |
120 | // The Windows API has lots of choices for synchronization |
121 | // primivitives. We are using SRWLOCK and CONDITION_VARIABLE |
122 | // because they don't require a destructor to release system |
123 | // resources. |
124 | // |
125 | // However, we can't include Windows.h in our headers, so we use aligned |
126 | // storage buffers to define the storage. |
127 | using SRWLockStorage = |
128 | typename std::aligned_storage<sizeof(void*), alignof(void*)>::type; |
129 | using ConditionVariableStorage = |
130 | typename std::aligned_storage<sizeof(void*), alignof(void*)>::type; |
131 | |
132 | // WinHelper - Used to define utilities for accessing the lock and |
133 | // condition variable storage once the types are complete. |
134 | class WinHelper; |
135 | |
136 | SRWLockStorage mu_storage_; |
137 | ConditionVariableStorage cv_storage_; |
138 | std::atomic<int> waiter_count_; |
139 | std::atomic<int> wakeup_count_; |
140 | |
141 | #else |
142 | #error Unknown ABSL_WAITER_MODE |
143 | #endif |
144 | }; |
145 | |
146 | } // namespace synchronization_internal |
147 | } // namespace absl |
148 | |
149 | #endif // ABSL_SYNCHRONIZATION_INTERNAL_WAITER_H_ |
150 | |