1 /*
2     Copyright (c) 2021-2023 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>
adaptive_wait_on_address(void * address,Predicate wakeup_condition,std::uintptr_t context)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 
waitable_atomic(T value)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 
load(std::memory_order order)52     T load(std::memory_order order) const noexcept {
53         return my_atomic.load(order);
54     }
55 
exchange(T desired)56     T exchange(T desired) noexcept {
57         return my_atomic.exchange(desired);
58     }
59 
wait(T old,std::uintptr_t context,std::memory_order order)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 
notify_one_relaxed()72     void notify_one_relaxed() {
73         r1::notify_by_address_one(this);
74     }
75 
76     // TODO: consider adding following interfaces:
77     // store(desired, memory_order)
78     // notify_all_relaxed()
79     // wait_until(T, std::uintptr_t, std::memory_order)
80     // notify_relaxed(std::uintptr_t context)
81 
82 private:
83     std::atomic<T> my_atomic{};
84 };
85 
86 } // namespace d1
87 } // namespace detail
88 } // namespace tbb
89 
90 #endif // __TBB_detail__address_waiters_H
91