1 /*
2     Copyright (c) 2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_detail__address_waiters_H
18 #define __TBB_detail__address_waiters_H
19 
20 #include "_utils.h"
21 
22 namespace tbb {
23 namespace detail {
24 
25 namespace r1 {
26 TBB_EXPORT void __TBB_EXPORTED_FUNC wait_on_address(void* address, d1::delegate_base& wakeup_condition, std::uintptr_t context);
27 TBB_EXPORT void __TBB_EXPORTED_FUNC notify_by_address(void* address, std::uintptr_t context);
28 TBB_EXPORT void __TBB_EXPORTED_FUNC notify_by_address_one(void* address);
29 TBB_EXPORT void __TBB_EXPORTED_FUNC notify_by_address_all(void* address);
30 } // namespace r1
31 
32 namespace d1 {
33 
34 template <typename Predicate>
35 void adaptive_wait_on_address(void* address, Predicate wakeup_condition, std::uintptr_t context) {
36     if (!timed_spin_wait_until(wakeup_condition)) {
37         d1::delegated_function<Predicate> pred(wakeup_condition);
38         r1::wait_on_address(address, pred, context);
39     }
40 }
41 
42 template <typename T>
43 class waitable_atomic {
44 public:
45     waitable_atomic() = default;
46 
47     explicit waitable_atomic(T value) : my_atomic(value) {}
48 
49     waitable_atomic(const waitable_atomic&) = delete;
50     waitable_atomic& operator=(const waitable_atomic&) = delete;
51 
52     T load(std::memory_order order) const noexcept {
53         return my_atomic.load(order);
54     }
55 
56     T exchange(T desired) noexcept {
57         return my_atomic.exchange(desired);
58     }
59 
60     void wait(T old, std::uintptr_t context, std::memory_order order) {
61         auto wakeup_condition = [&] { return my_atomic.load(order) != old; };
62         if (!timed_spin_wait_until(wakeup_condition)) {
63             // We need to use while here, because notify_all() will wake up all threads
64             // But predicate for them might be false
65             d1::delegated_function<decltype(wakeup_condition)> pred(wakeup_condition);
66             do {
67                 r1::wait_on_address(this, pred, context);
68             } while (!wakeup_condition());
69         }
70     }
71 
72     void wait_until(T expected, std::uintptr_t context, std::memory_order order) {
73         auto wakeup_condition = [&] { return my_atomic.load(order) == expected; };
74         if (!timed_spin_wait_until(wakeup_condition)) {
75             // We need to use while here, because notify_all() will wake up all threads
76             // But predicate for them might be false
77             d1::delegated_function<decltype(wakeup_condition)> pred(wakeup_condition);
78             do {
79                 r1::wait_on_address(this, pred, context);
80             } while (!wakeup_condition());
81         }
82     }
83 
84     void notify_relaxed(std::uintptr_t context) {
85         r1::notify_by_address(this, context);
86     }
87 
88     void notify_one_relaxed() {
89         r1::notify_by_address_one(this);
90     }
91 
92     // TODO: consider adding following interfaces:
93     // store(desired, memory_order)
94     // notify_all_relaxed()
95 
96 private:
97     std::atomic<T> my_atomic{};
98 };
99 
100 } // namespace d1
101 } // namespace detail
102 } // namespace tbb
103 
104 #endif // __TBB_detail__address_waiters_H
105