2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
22 constexpr mutex() noexcept;
25 mutex(const mutex&) = delete;
26 mutex& operator=(const mutex&) = delete;
32 typedef pthread_mutex_t* native_handle_type;
33 native_handle_type native_handle();
42 recursive_mutex(const recursive_mutex&) = delete;
43 recursive_mutex& operator=(const recursive_mutex&) = delete;
46 bool try_lock() noexcept;
49 typedef pthread_mutex_t* native_handle_type;
50 native_handle_type native_handle();
59 timed_mutex(const timed_mutex&) = delete;
60 timed_mutex& operator=(const timed_mutex&) = delete;
64 template <class Rep, class Period>
65 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
66 template <class Clock, class Duration>
67 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
71 class recursive_timed_mutex
74 recursive_timed_mutex();
75 ~recursive_timed_mutex();
77 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
78 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
81 bool try_lock() noexcept;
82 template <class Rep, class Period>
83 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
84 template <class Clock, class Duration>
85 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
89 struct defer_lock_t { explicit defer_lock_t() = default; };
90 struct try_to_lock_t { explicit try_to_lock_t() = default; };
91 struct adopt_lock_t { explicit adopt_lock_t() = default; };
93 inline constexpr defer_lock_t defer_lock{};
94 inline constexpr try_to_lock_t try_to_lock{};
95 inline constexpr adopt_lock_t adopt_lock{};
97 template <class Mutex>
101 typedef Mutex mutex_type;
103 explicit lock_guard(mutex_type& m);
104 lock_guard(mutex_type& m, adopt_lock_t);
107 lock_guard(lock_guard const&) = delete;
108 lock_guard& operator=(lock_guard const&) = delete;
111 template <class... MutexTypes>
112 class scoped_lock // C++17
115 using mutex_type = Mutex; // Only if sizeof...(MutexTypes) == 1
117 explicit scoped_lock(MutexTypes&... m);
118 scoped_lock(adopt_lock_t, MutexTypes&... m);
120 scoped_lock(scoped_lock const&) = delete;
121 scoped_lock& operator=(scoped_lock const&) = delete;
123 tuple<MutexTypes&...> pm; // exposition only
126 template <class Mutex>
130 typedef Mutex mutex_type;
131 unique_lock() noexcept;
132 explicit unique_lock(mutex_type& m);
133 unique_lock(mutex_type& m, defer_lock_t) noexcept;
134 unique_lock(mutex_type& m, try_to_lock_t);
135 unique_lock(mutex_type& m, adopt_lock_t);
136 template <class Clock, class Duration>
137 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
138 template <class Rep, class Period>
139 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
142 unique_lock(unique_lock const&) = delete;
143 unique_lock& operator=(unique_lock const&) = delete;
145 unique_lock(unique_lock&& u) noexcept;
146 unique_lock& operator=(unique_lock&& u) noexcept;
151 template <class Rep, class Period>
152 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
153 template <class Clock, class Duration>
154 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
158 void swap(unique_lock& u) noexcept;
159 mutex_type* release() noexcept;
161 bool owns_lock() const noexcept;
162 explicit operator bool () const noexcept;
163 mutex_type* mutex() const noexcept;
166 template <class Mutex>
167 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
169 template <class L1, class L2, class... L3>
170 int try_lock(L1&, L2&, L3&...);
171 template <class L1, class L2, class... L3>
172 void lock(L1&, L2&, L3&...);
176 constexpr once_flag() noexcept;
178 once_flag(const once_flag&) = delete;
179 once_flag& operator=(const once_flag&) = delete;
182 template<class Callable, class ...Args>
183 void call_once(once_flag& flag, Callable&& func, Args&&... args);
189 #include <__cxx03/__chrono/steady_clock.h>
190 #include <__cxx03/__chrono/time_point.h>
191 #include <__cxx03/__condition_variable/condition_variable.h>
192 #include <__cxx03/__config>
193 #include <__cxx03/__memory/shared_ptr.h>
194 #include <__cxx03/__mutex/lock_guard.h>
195 #include <__cxx03/__mutex/mutex.h>
196 #include <__cxx03/__mutex/once_flag.h>
197 #include <__cxx03/__mutex/tag_types.h>
198 #include <__cxx03/__mutex/unique_lock.h>
199 #include <__cxx03/__thread/id.h>
200 #include <__cxx03/__thread/support.h>
201 #include <__cxx03/__utility/forward.h>
202 #include <__cxx03/cstddef>
203 #include <__cxx03/limits>
204 #ifndef _LIBCPP_CXX03_LANG
205 # include <__cxx03/tuple>
207 #include <__cxx03/version>
209 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
210 # pragma GCC system_header
214 #include <__cxx03/__undef_macros>
216 _LIBCPP_BEGIN_NAMESPACE_STD
218 #ifndef _LIBCPP_HAS_NO_THREADS
220 class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex {
221 __libcpp_recursive_mutex_t __m_;
227 recursive_mutex(const recursive_mutex&) = delete;
228 recursive_mutex& operator=(const recursive_mutex&) = delete;
231 bool try_lock() _NOEXCEPT;
232 void unlock() _NOEXCEPT;
234 typedef __libcpp_recursive_mutex_t* native_handle_type;
236 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
239 class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {
241 condition_variable __cv_;
248 timed_mutex(const timed_mutex&) = delete;
249 timed_mutex& operator=(const timed_mutex&) = delete;
253 bool try_lock() _NOEXCEPT;
254 template <class _Rep, class _Period>
255 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
256 return try_lock_until(chrono::steady_clock::now() + __d);
258 template <class _Clock, class _Duration>
259 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
260 try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
261 void unlock() _NOEXCEPT;
264 template <class _Clock, class _Duration>
265 bool timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
266 using namespace chrono;
267 unique_lock<mutex> __lk(__m_);
268 bool __no_timeout = _Clock::now() < __t;
269 while (__no_timeout && __locked_)
270 __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
278 class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex {
280 condition_variable __cv_;
285 recursive_timed_mutex();
286 ~recursive_timed_mutex();
288 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
289 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
292 bool try_lock() _NOEXCEPT;
293 template <class _Rep, class _Period>
294 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
295 return try_lock_until(chrono::steady_clock::now() + __d);
297 template <class _Clock, class _Duration>
298 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
299 try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
300 void unlock() _NOEXCEPT;
303 template <class _Clock, class _Duration>
304 bool recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
305 using namespace chrono;
306 __thread_id __id = this_thread::get_id();
307 unique_lock<mutex> __lk(__m_);
309 if (__count_ == numeric_limits<size_t>::max())
314 bool __no_timeout = _Clock::now() < __t;
315 while (__no_timeout && __count_ != 0)
316 __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
325 template <class _L0, class _L1>
326 _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
327 unique_lock<_L0> __u0(__l0, try_to_lock_t());
328 if (__u0.owns_lock()) {
329 if (__l1.try_lock()) {
338 # ifndef _LIBCPP_CXX03_LANG
340 template <class _L0, class _L1, class _L2, class... _L3>
341 _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
343 unique_lock<_L0> __u0(__l0, try_to_lock);
344 if (__u0.owns_lock()) {
345 __r = std::try_lock(__l1, __l2, __l3...);
354 # endif // _LIBCPP_CXX03_LANG
356 template <class _L0, class _L1>
357 _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1) {
360 unique_lock<_L0> __u0(__l0);
361 if (__l1.try_lock()) {
366 __libcpp_thread_yield();
368 unique_lock<_L1> __u1(__l1);
369 if (__l0.try_lock()) {
374 __libcpp_thread_yield();
378 # ifndef _LIBCPP_CXX03_LANG
380 template <class _L0, class _L1, class _L2, class... _L3>
381 void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
385 unique_lock<_L0> __u0(__l0);
386 __i = std::try_lock(__l1, __l2, __l3...);
393 __libcpp_thread_yield();
396 unique_lock<_L1> __u1(__l1);
397 __i = std::try_lock(__l2, __l3..., __l0);
403 if (__i == sizeof...(_L3) + 1)
407 __libcpp_thread_yield();
410 std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
416 template <class _L0, class _L1, class _L2, class... _L3>
417 inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
418 std::__lock_first(0, __l0, __l1, __l2, __l3...);
421 # endif // _LIBCPP_CXX03_LANG
423 # if _LIBCPP_STD_VER >= 17
424 template <class... _Mutexes>
425 class _LIBCPP_TEMPLATE_VIS scoped_lock;
428 class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
430 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock() {}
431 ~scoped_lock() = default;
433 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t) {}
435 scoped_lock(scoped_lock const&) = delete;
436 scoped_lock& operator=(scoped_lock const&) = delete;
439 template <class _Mutex>
440 class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
442 typedef _Mutex mutex_type;
449 _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
454 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) { __m_.unlock(); }
456 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t, mutex_type& __m)
457 _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
460 scoped_lock(scoped_lock const&) = delete;
461 scoped_lock& operator=(scoped_lock const&) = delete;
464 template <class... _MArgs>
465 class _LIBCPP_TEMPLATE_VIS scoped_lock {
466 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
467 typedef tuple<_MArgs&...> _MutexTuple;
470 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(_MArgs&... __margs) : __t_(__margs...) {
471 std::lock(__margs...);
474 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI scoped_lock(adopt_lock_t, _MArgs&... __margs) : __t_(__margs...) {}
476 _LIBCPP_HIDE_FROM_ABI ~scoped_lock() {
477 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
478 __unlock_unpack(_Indices{}, __t_);
481 scoped_lock(scoped_lock const&) = delete;
482 scoped_lock& operator=(scoped_lock const&) = delete;
485 template <size_t... _Indx>
486 _LIBCPP_HIDE_FROM_ABI static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
487 (std::get<_Indx>(__mt).unlock(), ...);
492 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
494 # endif // _LIBCPP_STD_VER >= 17
495 #endif // !_LIBCPP_HAS_NO_THREADS
497 _LIBCPP_END_NAMESPACE_STD
501 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
502 # include <__cxx03/atomic>
503 # include <__cxx03/concepts>
504 # include <__cxx03/cstdlib>
505 # include <__cxx03/cstring>
506 # include <__cxx03/ctime>
507 # include <__cxx03/initializer_list>
508 # include <__cxx03/iosfwd>
509 # include <__cxx03/new>
510 # include <__cxx03/stdexcept>
511 # include <__cxx03/system_error>
512 # include <__cxx03/type_traits>
513 # include <__cxx03/typeinfo>
516 #endif // _LIBCPP_MUTEX