Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:57

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___MUTEX_UNIQUE_LOCK_H
0010 #define _LIBCPP___MUTEX_UNIQUE_LOCK_H
0011 
0012 #include <__chrono/duration.h>
0013 #include <__chrono/time_point.h>
0014 #include <__config>
0015 #include <__memory/addressof.h>
0016 #include <__mutex/tag_types.h>
0017 #include <__system_error/throw_system_error.h>
0018 #include <__utility/swap.h>
0019 #include <cerrno>
0020 
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 #  pragma GCC system_header
0023 #endif
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 template <class _Mutex>
0028 class _LIBCPP_TEMPLATE_VIS unique_lock {
0029 public:
0030   typedef _Mutex mutex_type;
0031 
0032 private:
0033   mutex_type* __m_;
0034   bool __owns_;
0035 
0036 public:
0037   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
0038   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI explicit unique_lock(mutex_type& __m)
0039       : __m_(std::addressof(__m)), __owns_(true) {
0040     __m_->lock();
0041   }
0042 
0043   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
0044       : __m_(std::addressof(__m)),
0045         __owns_(false) {}
0046 
0047   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, try_to_lock_t)
0048       : __m_(std::addressof(__m)), __owns_(__m.try_lock()) {}
0049 
0050   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, adopt_lock_t)
0051       : __m_(std::addressof(__m)), __owns_(true) {}
0052 
0053   template <class _Clock, class _Duration>
0054   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
0055       : __m_(std::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
0056 
0057   template <class _Rep, class _Period>
0058   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
0059       : __m_(std::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
0060 
0061   _LIBCPP_HIDE_FROM_ABI ~unique_lock() {
0062     if (__owns_)
0063       __m_->unlock();
0064   }
0065 
0066   unique_lock(unique_lock const&)            = delete;
0067   unique_lock& operator=(unique_lock const&) = delete;
0068 
0069   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(unique_lock&& __u) _NOEXCEPT
0070       : __m_(__u.__m_),
0071         __owns_(__u.__owns_) {
0072     __u.__m_    = nullptr;
0073     __u.__owns_ = false;
0074   }
0075 
0076   _LIBCPP_HIDE_FROM_ABI unique_lock& operator=(unique_lock&& __u) _NOEXCEPT {
0077     if (__owns_)
0078       __m_->unlock();
0079 
0080     __m_        = __u.__m_;
0081     __owns_     = __u.__owns_;
0082     __u.__m_    = nullptr;
0083     __u.__owns_ = false;
0084     return *this;
0085   }
0086 
0087   _LIBCPP_HIDE_FROM_ABI void lock();
0088   _LIBCPP_HIDE_FROM_ABI bool try_lock();
0089 
0090   template <class _Rep, class _Period>
0091   _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
0092 
0093   template <class _Clock, class _Duration>
0094   _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
0095 
0096   _LIBCPP_HIDE_FROM_ABI void unlock();
0097 
0098   _LIBCPP_HIDE_FROM_ABI void swap(unique_lock& __u) _NOEXCEPT {
0099     std::swap(__m_, __u.__m_);
0100     std::swap(__owns_, __u.__owns_);
0101   }
0102 
0103   _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
0104     mutex_type* __m = __m_;
0105     __m_            = nullptr;
0106     __owns_         = false;
0107     return __m;
0108   }
0109 
0110   _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
0111   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
0112   _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
0113 };
0114 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(unique_lock);
0115 
0116 template <class _Mutex>
0117 _LIBCPP_HIDE_FROM_ABI void unique_lock<_Mutex>::lock() {
0118   if (__m_ == nullptr)
0119     __throw_system_error(EPERM, "unique_lock::lock: references null mutex");
0120   if (__owns_)
0121     __throw_system_error(EDEADLK, "unique_lock::lock: already locked");
0122   __m_->lock();
0123   __owns_ = true;
0124 }
0125 
0126 template <class _Mutex>
0127 _LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock() {
0128   if (__m_ == nullptr)
0129     __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex");
0130   if (__owns_)
0131     __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked");
0132   __owns_ = __m_->try_lock();
0133   return __owns_;
0134 }
0135 
0136 template <class _Mutex>
0137 template <class _Rep, class _Period>
0138 _LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
0139   if (__m_ == nullptr)
0140     __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex");
0141   if (__owns_)
0142     __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked");
0143   __owns_ = __m_->try_lock_for(__d);
0144   return __owns_;
0145 }
0146 
0147 template <class _Mutex>
0148 template <class _Clock, class _Duration>
0149 _LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
0150   if (__m_ == nullptr)
0151     __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex");
0152   if (__owns_)
0153     __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked");
0154   __owns_ = __m_->try_lock_until(__t);
0155   return __owns_;
0156 }
0157 
0158 template <class _Mutex>
0159 _LIBCPP_HIDE_FROM_ABI void unique_lock<_Mutex>::unlock() {
0160   if (!__owns_)
0161     __throw_system_error(EPERM, "unique_lock::unlock: not locked");
0162   __m_->unlock();
0163   __owns_ = false;
0164 }
0165 
0166 template <class _Mutex>
0167 inline _LIBCPP_HIDE_FROM_ABI void swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT {
0168   __x.swap(__y);
0169 }
0170 
0171 _LIBCPP_END_NAMESPACE_STD
0172 
0173 #endif // _LIBCPP___MUTEX_UNIQUE_LOCK_H