#ifndef _LIBCPP_MUTEX
#define _LIBCPP_MUTEX
#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
# include <__cxx03/mutex>
#else
# include <__chrono/steady_clock.h>
# include <__chrono/time_point.h>
# include <__condition_variable/condition_variable.h>
# include <__config>
# include <__mutex/lock_guard.h>
# include <__mutex/mutex.h>
# include <__mutex/once_flag.h>
# include <__mutex/tag_types.h>
# include <__mutex/unique_lock.h>
# include <__thread/id.h>
# include <__thread/support.h>
# include <__utility/forward.h>
# include <limits>
# ifndef _LIBCPP_CXX03_LANG
# include <tuple>
# endif
# include <version>
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
# endif
_LIBCPP_PUSH_MACROS
# include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
# if _LIBCPP_HAS_THREADS
class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex {
__libcpp_recursive_mutex_t __m_;
public:
recursive_mutex();
~recursive_mutex();
recursive_mutex(const recursive_mutex&) = delete;
recursive_mutex& operator=(const recursive_mutex&) = delete;
void lock();
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
void unlock() _NOEXCEPT;
typedef __libcpp_recursive_mutex_t* native_handle_type;
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
};
class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {
mutex __m_;
condition_variable __cv_;
bool __locked_;
public:
timed_mutex();
~timed_mutex();
timed_mutex(const timed_mutex&) = delete;
timed_mutex& operator=(const timed_mutex&) = delete;
public:
void lock();
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
template <class _Rep, class _Period>
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
return try_lock_until(chrono::steady_clock::now() + __d);
}
template <class _Clock, class _Duration>
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
using namespace chrono;
unique_lock<mutex> __lk(__m_);
bool __no_timeout = _Clock::now() < __t;
while (__no_timeout && __locked_)
__no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
if (!__locked_) {
__locked_ = true;
return true;
}
return false;
}
void unlock() _NOEXCEPT;
};
class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex {
mutex __m_;
condition_variable __cv_;
size_t __count_;
__thread_id __id_;
public:
recursive_timed_mutex();
~recursive_timed_mutex();
recursive_timed_mutex(const recursive_timed_mutex&) = delete;
recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
void lock();
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
template <class _Rep, class _Period>
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
return try_lock_until(chrono::steady_clock::now() + __d);
}
template <class _Clock, class _Duration>
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
using namespace chrono;
__thread_id __id = this_thread::get_id();
unique_lock<mutex> __lk(__m_);
if (__id == __id_) {
if (__count_ == numeric_limits<size_t>::max())
return false;
++__count_;
return true;
}
bool __no_timeout = _Clock::now() < __t;
while (__no_timeout && __count_ != 0)
__no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
if (__count_ == 0) {
__count_ = 1;
__id_ = __id;
return true;
}
return false;
}
void unlock() _NOEXCEPT;
};
template <class _L0, class _L1>
[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
unique_lock<_L0> __u0(__l0, try_to_lock_t());
if (__u0.owns_lock()) {
if (__l1.try_lock()) {
__u0.release();
return -1;
} else
return 1;
}
return 0;
}
# ifndef _LIBCPP_CXX03_LANG
template <class _L0, class _L1, class _L2, class... _L3>
[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
_LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
int __r = 0;
unique_lock<_L0> __u0(__l0, try_to_lock);
if (__u0.owns_lock()) {
__r = std::try_lock(__l1, __l2, __l3...);
if (__r == -1)
__u0.release();
else
++__r;
}
return __r;
}
# endif
template <class _L0, class _L1>
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1)
_LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1) {
while (true) {
{
unique_lock<_L0> __u0(__l0);
if (__l1.try_lock()) {
__u0.release();
break;
}
}
__libcpp_thread_yield();
{
unique_lock<_L1> __u1(__l1);
if (__l0.try_lock()) {
__u1.release();
break;
}
}
__libcpp_thread_yield();
}
}
# ifndef _LIBCPP_CXX03_LANG
template <class _L0, class _L1, class _L2, class... _L3>
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
while (true) {
switch (__i) {
case 0: {
unique_lock<_L0> __u0(__l0);
__i = std::try_lock(__l1, __l2, __l3...);
if (__i == -1) {
__u0.release();
return;
}
}
++__i;
__libcpp_thread_yield();
break;
case 1: {
unique_lock<_L1> __u1(__l1);
__i = std::try_lock(__l2, __l3..., __l0);
if (__i == -1) {
__u1.release();
return;
}
}
if (__i == sizeof...(_L3) + 1)
__i = 0;
else
__i += 2;
__libcpp_thread_yield();
break;
default:
std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
return;
}
}
}
template <class _L0, class _L1, class _L2, class... _L3>
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
# if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2101
_LIBCPP_ACQUIRE_CAPABILITY(__l0, __l1, __l2, __l3...)
# endif
{
std::__lock_first(0, __l0, __l1, __l2, __l3...);
}
# endif
# if _LIBCPP_STD_VER >= 17
template <class... _Mutexes>
class scoped_lock;
template <>
class scoped_lock<> {
public:
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock() {}
~scoped_lock() = default;
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t) {}
scoped_lock(scoped_lock const&) = delete;
scoped_lock& operator=(scoped_lock const&) = delete;
};
template <class _Mutex>
class _LIBCPP_SCOPED_LOCKABLE scoped_lock<_Mutex> {
public:
typedef _Mutex mutex_type;
private:
mutex_type& __m_;
public:
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(mutex_type& __m) _LIBCPP_ACQUIRE_CAPABILITY(__m)
: __m_(__m) {
__m_.lock();
}
_LIBCPP_RELEASE_CAPABILITY _LIBCPP_HIDE_FROM_ABI ~scoped_lock() { __m_.unlock(); }
[[nodiscard]]
_LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_REQUIRES_CAPABILITY(__m)
: __m_(__m) {}
scoped_lock(scoped_lock const&) = delete;
scoped_lock& operator=(scoped_lock const&) = delete;
};
template <class... _MArgs>
class scoped_lock {
static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
typedef tuple<_MArgs&...> _MutexTuple;
public:
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(_MArgs&... __margs) : __t_(__margs...) {
std::lock(__margs...);
}
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI scoped_lock(adopt_lock_t, _MArgs&... __margs) : __t_(__margs...) {}
_LIBCPP_HIDE_FROM_ABI ~scoped_lock() { __unlock_unpack(make_index_sequence<sizeof...(_MArgs)>(), __t_); }
scoped_lock(scoped_lock const&) = delete;
scoped_lock& operator=(scoped_lock const&) = delete;
private:
template <size_t... _Indx>
_LIBCPP_HIDE_FROM_ABI static void __unlock_unpack(index_sequence<_Indx...>, _MutexTuple& __mt) {
(std::get<_Indx>(__mt).unlock(), ...);
}
_MutexTuple __t_;
};
_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
# endif # endif
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
# include <typeinfo>
# endif
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
# include <atomic>
# include <concepts>
# include <cstdlib>
# include <cstring>
# include <ctime>
# include <initializer_list>
# include <iosfwd>
# include <new>
# include <optional>
# include <stdexcept>
# include <system_error>
# include <type_traits>
# endif
#endif
#endif