1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef _LIBCPP___MUTEX_UNIQUE_LOCK_H |
10 | #define _LIBCPP___MUTEX_UNIQUE_LOCK_H |
11 | |
12 | #include <__chrono/duration.h> |
13 | #include <__chrono/time_point.h> |
14 | #include <__config> |
15 | #include <__memory/addressof.h> |
16 | #include <__mutex/tag_types.h> |
17 | #include <__system_error/system_error.h> |
18 | #include <__utility/swap.h> |
19 | #include <cerrno> |
20 | |
21 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
22 | # pragma GCC system_header |
23 | #endif |
24 | |
25 | #ifndef _LIBCPP_HAS_NO_THREADS |
26 | |
27 | _LIBCPP_BEGIN_NAMESPACE_STD |
28 | |
29 | template <class _Mutex> |
30 | class _LIBCPP_TEMPLATE_VIS unique_lock { |
31 | public: |
32 | typedef _Mutex mutex_type; |
33 | |
34 | private: |
35 | mutex_type* __m_; |
36 | bool __owns_; |
37 | |
38 | public: |
39 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {} |
40 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI explicit unique_lock(mutex_type& __m) |
41 | : __m_(std::addressof(__m)), __owns_(true) { |
42 | __m_->lock(); |
43 | } |
44 | |
45 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT |
46 | : __m_(std::addressof(__m)), |
47 | __owns_(false) {} |
48 | |
49 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, try_to_lock_t) |
50 | : __m_(std::addressof(__m)), __owns_(__m.try_lock()) {} |
51 | |
52 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, adopt_lock_t) |
53 | : __m_(std::addressof(__m)), __owns_(true) {} |
54 | |
55 | template <class _Clock, class _Duration> |
56 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t) |
57 | : __m_(std::addressof(__m)), __owns_(__m.try_lock_until(__t)) {} |
58 | |
59 | template <class _Rep, class _Period> |
60 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d) |
61 | : __m_(std::addressof(__m)), __owns_(__m.try_lock_for(__d)) {} |
62 | |
63 | _LIBCPP_HIDE_FROM_ABI ~unique_lock() { |
64 | if (__owns_) |
65 | __m_->unlock(); |
66 | } |
67 | |
68 | unique_lock(unique_lock const&) = delete; |
69 | unique_lock& operator=(unique_lock const&) = delete; |
70 | |
71 | _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI unique_lock(unique_lock&& __u) _NOEXCEPT |
72 | : __m_(__u.__m_), |
73 | __owns_(__u.__owns_) { |
74 | __u.__m_ = nullptr; |
75 | __u.__owns_ = false; |
76 | } |
77 | |
78 | _LIBCPP_HIDE_FROM_ABI unique_lock& operator=(unique_lock&& __u) _NOEXCEPT { |
79 | if (__owns_) |
80 | __m_->unlock(); |
81 | |
82 | __m_ = __u.__m_; |
83 | __owns_ = __u.__owns_; |
84 | __u.__m_ = nullptr; |
85 | __u.__owns_ = false; |
86 | return *this; |
87 | } |
88 | |
89 | void lock(); |
90 | bool try_lock(); |
91 | |
92 | template <class _Rep, class _Period> |
93 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); |
94 | |
95 | template <class _Clock, class _Duration> |
96 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
97 | |
98 | void unlock(); |
99 | |
100 | _LIBCPP_HIDE_FROM_ABI void swap(unique_lock& __u) _NOEXCEPT { |
101 | std::swap(__m_, __u.__m_); |
102 | std::swap(__owns_, __u.__owns_); |
103 | } |
104 | |
105 | _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT { |
106 | mutex_type* __m = __m_; |
107 | __m_ = nullptr; |
108 | __owns_ = false; |
109 | return __m; |
110 | } |
111 | |
112 | _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; } |
113 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; } |
114 | _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; } |
115 | }; |
116 | _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(unique_lock); |
117 | |
118 | template <class _Mutex> |
119 | void unique_lock<_Mutex>::lock() { |
120 | if (__m_ == nullptr) |
121 | __throw_system_error(EPERM, "unique_lock::lock: references null mutex" ); |
122 | if (__owns_) |
123 | __throw_system_error(EDEADLK, "unique_lock::lock: already locked" ); |
124 | __m_->lock(); |
125 | __owns_ = true; |
126 | } |
127 | |
128 | template <class _Mutex> |
129 | bool unique_lock<_Mutex>::try_lock() { |
130 | if (__m_ == nullptr) |
131 | __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex" ); |
132 | if (__owns_) |
133 | __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked" ); |
134 | __owns_ = __m_->try_lock(); |
135 | return __owns_; |
136 | } |
137 | |
138 | template <class _Mutex> |
139 | template <class _Rep, class _Period> |
140 | bool unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) { |
141 | if (__m_ == nullptr) |
142 | __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex" ); |
143 | if (__owns_) |
144 | __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked" ); |
145 | __owns_ = __m_->try_lock_for(__d); |
146 | return __owns_; |
147 | } |
148 | |
149 | template <class _Mutex> |
150 | template <class _Clock, class _Duration> |
151 | bool unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) { |
152 | if (__m_ == nullptr) |
153 | __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex" ); |
154 | if (__owns_) |
155 | __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked" ); |
156 | __owns_ = __m_->try_lock_until(__t); |
157 | return __owns_; |
158 | } |
159 | |
160 | template <class _Mutex> |
161 | void unique_lock<_Mutex>::unlock() { |
162 | if (!__owns_) |
163 | __throw_system_error(EPERM, "unique_lock::unlock: not locked" ); |
164 | __m_->unlock(); |
165 | __owns_ = false; |
166 | } |
167 | |
168 | template <class _Mutex> |
169 | inline _LIBCPP_HIDE_FROM_ABI void swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT { |
170 | __x.swap(__y); |
171 | } |
172 | |
173 | _LIBCPP_END_NAMESPACE_STD |
174 | |
175 | #endif // _LIBCPP_HAS_NO_THREADS |
176 | |
177 | #endif // _LIBCPP___MUTEX_UNIQUE_LOCK_H |
178 | |