/src/abseil-cpp/absl/synchronization/internal/futex_waiter.h
Line  | Count  | Source  | 
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_FUTEX_WAITER_H_  | 
17  |  | #define ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_WAITER_H_  | 
18  |  |  | 
19  |  | #include <atomic>  | 
20  |  | #include <cstdint>  | 
21  |  |  | 
22  |  | #include "absl/base/config.h"  | 
23  |  | #include "absl/synchronization/internal/kernel_timeout.h"  | 
24  |  | #include "absl/synchronization/internal/futex.h"  | 
25  |  | #include "absl/synchronization/internal/waiter_base.h"  | 
26  |  |  | 
27  |  | #ifdef ABSL_INTERNAL_HAVE_FUTEX  | 
28  |  |  | 
29  |  | namespace absl { | 
30  |  | ABSL_NAMESPACE_BEGIN  | 
31  |  | namespace synchronization_internal { | 
32  |  |  | 
33  |  | #define ABSL_INTERNAL_HAVE_FUTEX_WAITER 1  | 
34  |  |  | 
35  |  | class FutexWaiter : public WaiterCrtp<FutexWaiter> { | 
36  |  |  public:  | 
37  | 2  |   FutexWaiter() : futex_(0) {} | 
38  |  |  | 
39  |  |   bool Wait(KernelTimeout t);  | 
40  |  |   void Post();  | 
41  |  |   void Poke();  | 
42  |  |  | 
43  |  |   static constexpr char kName[] = "FutexWaiter";  | 
44  |  |  | 
45  |  |  private:  | 
46  |  |   // Atomically check that `*v == val`, and if it is, then sleep until the  | 
47  |  |   // timeout `t` has been reached, or until woken by `Wake()`.  | 
48  |  |   static int WaitUntil(std::atomic<int32_t>* v, int32_t val,  | 
49  |  |                        KernelTimeout t);  | 
50  |  |  | 
51  |  |   // Futexes are defined by specification to be 32-bits.  | 
52  |  |   // Thus std::atomic<int32_t> must be just an int32_t with lockfree methods.  | 
53  |  |   std::atomic<int32_t> futex_;  | 
54  |  |   static_assert(sizeof(int32_t) == sizeof(futex_), "Wrong size for futex");  | 
55  |  | };  | 
56  |  |  | 
57  |  | }  // namespace synchronization_internal  | 
58  |  | ABSL_NAMESPACE_END  | 
59  |  | }  // namespace absl  | 
60  |  |  | 
61  |  | #endif  // ABSL_INTERNAL_HAVE_FUTEX  | 
62  |  |  | 
63  |  | #endif  // ABSL_SYNCHRONIZATION_INTERNAL_FUTEX_WAITER_H_  |