Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/oneapi/tbb/mutex.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     Copyright (c) 2021-2023 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
0015 */
0016 
0017 #ifndef __TBB_mutex_H
0018 #define __TBB_mutex_H
0019 
0020 #include "detail/_namespace_injection.h"
0021 #include "detail/_utils.h"
0022 #include "detail/_scoped_lock.h"
0023 #include "detail/_waitable_atomic.h"
0024 #include "detail/_mutex_common.h"
0025 #include "profiling.h"
0026 
0027 namespace tbb {
0028 namespace detail {
0029 namespace d1 {
0030 
0031 class mutex {
0032 public:
0033     //! Constructors
0034     mutex() {
0035         create_itt_sync(this, "tbb::mutex", "");
0036     };
0037 
0038     //! Destructor
0039     ~mutex() = default;
0040 
0041     //! No Copy
0042     mutex(const mutex&) = delete;
0043     mutex& operator=(const mutex&) = delete;
0044 
0045     using scoped_lock = unique_scoped_lock<mutex>;
0046 
0047     //! Mutex traits
0048     static constexpr bool is_rw_mutex = false;
0049     static constexpr bool is_recursive_mutex = false;
0050     static constexpr bool is_fair_mutex = false;
0051 
0052     //! Acquire lock
0053     /** Spin if the lock is taken */
0054     void lock() {
0055         call_itt_notify(prepare, this);
0056         while (!try_lock()) {
0057             my_flag.wait(true, /* context = */ 0, std::memory_order_relaxed);
0058         }
0059     }
0060 
0061     //! Try acquiring lock (non-blocking)
0062     /** Return true if lock acquired; false otherwise. */
0063     bool try_lock() {
0064         bool result = !my_flag.load(std::memory_order_relaxed) && !my_flag.exchange(true);
0065         if (result) {
0066             call_itt_notify(acquired, this);
0067         }
0068         return result;
0069     }
0070 
0071     //! Release lock
0072     void unlock() {
0073         call_itt_notify(releasing, this);
0074         // We need Write Read memory barrier before notify that reads the waiter list.
0075         // In C++ only full fence covers this type of barrier.
0076         my_flag.exchange(false);
0077         my_flag.notify_one_relaxed();
0078     }
0079 
0080 private:
0081     waitable_atomic<bool> my_flag{0};
0082 }; // class mutex
0083 
0084 } // namespace d1
0085 } // namespace detail
0086 
0087 inline namespace v1 {
0088 using detail::d1::mutex;
0089 } // namespace v1
0090 
0091 } // namespace tbb
0092 
0093 #endif // __TBB_mutex_H