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__mutex_common_H
18 #define __TBB_detail__mutex_common_H
19 
20 #include "_config.h"
21 #include "_utils.h"
22 
23 #if __TBB_CPP20_CONCEPTS_PRESENT
24 #include <concepts>
25 
26 namespace tbb {
27 namespace detail {
28 inline namespace d0 {
29 
30 template <typename Lock, typename Mutex>
31 concept mutex_scoped_lock = std::default_initializable<Lock> &&
32                             std::constructible_from<Lock, Mutex&> &&
requires(Lock & lock,Mutex & mutex)33                             requires( Lock& lock, Mutex& mutex ) {
34                                 lock.acquire(mutex);
35                                 { lock.try_acquire(mutex) } -> adaptive_same_as<bool>;
36                                 lock.release();
37                             };
38 
39 template <typename Lock, typename Mutex>
40 concept rw_mutex_scoped_lock = mutex_scoped_lock<Lock, Mutex> &&
41                                std::constructible_from<Lock, Mutex&, bool> &&
requires(Lock & lock,Mutex & mutex)42                                requires( Lock& lock, Mutex& mutex ) {
43                                    lock.acquire(mutex, false);
44                                    { lock.try_acquire(mutex, false) } -> adaptive_same_as<bool>;
45                                    { lock.upgrade_to_writer() } -> adaptive_same_as<bool>;
46                                    { lock.downgrade_to_reader() } -> adaptive_same_as<bool>;
47                                };
48 
49 template <typename Mutex>
50 concept scoped_lockable = mutex_scoped_lock<typename Mutex::scoped_lock, Mutex>;
51 
52 template <typename Mutex>
53 concept rw_scoped_lockable = scoped_lockable<Mutex> &&
54                              rw_mutex_scoped_lock<typename Mutex::scoped_lock, Mutex>;
55 
56 } // namespace d0
57 } // namespace detail
58 } // namespace tbb
59 
60 #endif // __TBB_CPP20_CONCEPTS_PRESENT
61 #endif // __TBB_detail__mutex_common_H
62