Back to home page

EIC code displayed by LXR

 
 

    


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

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