xref: /oneTBB/include/oneapi/tbb/mutex.h (revision 0a2b3987)
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_mutex_H
18 #define __TBB_mutex_H
19 
20 #if __TBB_PREVIEW_MUTEXES
21 
22 #include "detail/_namespace_injection.h"
23 #include "detail/_utils.h"
24 #include "detail/_scoped_lock.h"
25 #include "detail/_waitable_atomic.h"
26 #include "detail/_mutex_common.h"
27 #include "profiling.h"
28 
29 namespace tbb {
30 namespace detail {
31 namespace d1 {
32 
33 class mutex {
34 public:
35     //! Constructors
36     mutex() {
37         create_itt_sync(this, "tbb::mutex", "");
38     };
39 
40     //! Destructor
41     ~mutex() {
42         __TBB_ASSERT(!my_flag.load(std::memory_order_relaxed), "destruction of an acquired mutex");
43     }
44 
45     //! No Copy
46     mutex(const mutex&) = delete;
47     mutex& operator=(const mutex&) = delete;
48 
49     using scoped_lock = unique_scoped_lock<mutex>;
50 
51     //! Mutex traits
52     static constexpr bool is_rw_mutex = false;
53     static constexpr bool is_recursive_mutex = false;
54     static constexpr bool is_fair_mutex = false;
55 
56     //! Acquire lock
57     /** Spin if the lock is taken */
58     void lock() {
59         call_itt_notify(prepare, this);
60         while (!try_lock()) {
61             my_flag.wait(true, /* context = */ 0, std::memory_order_relaxed);
62         }
63     }
64 
65     //! Try acquiring lock (non-blocking)
66     /** Return true if lock acquired; false otherwise. */
67     bool try_lock() {
68         bool result = !my_flag.load(std::memory_order_relaxed) && !my_flag.exchange(true);
69         if (result) {
70             call_itt_notify(acquired, this);
71         }
72         return result;
73     }
74 
75     //! Release lock
76     void unlock() {
77         call_itt_notify(releasing, this);
78         // We need Write Read memory barrier before notify that reads the waiter list.
79         // In C++ only full fence covers this type of barrier.
80         my_flag.exchange(false);
81         my_flag.notify_one_relaxed();
82     }
83 
84 private:
85     waitable_atomic<bool> my_flag{0};
86 }; // class mutex
87 
88 } // namespace d1
89 } // namespace detail
90 
91 inline namespace v1 {
92 using detail::d1::mutex;
93 } // namespace v1
94 
95 } // namespace tbb
96 
97 #endif /* __TBB_PREVIEW_MUTEXES */
98 
99 #endif // __TBB_mutex_H
100