Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/__cxx03/shared_mutex is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03_SHARED_MUTEX
0011 #define _LIBCPP___CXX03_SHARED_MUTEX
0012 
0013 /*
0014     shared_mutex synopsis
0015 
0016 // C++1y
0017 
0018 namespace std
0019 {
0020 
0021 class shared_mutex      // C++17
0022 {
0023 public:
0024     shared_mutex();
0025     ~shared_mutex();
0026 
0027     shared_mutex(const shared_mutex&) = delete;
0028     shared_mutex& operator=(const shared_mutex&) = delete;
0029 
0030     // Exclusive ownership
0031     void lock(); // blocking
0032     bool try_lock();
0033     void unlock();
0034 
0035     // Shared ownership
0036     void lock_shared(); // blocking
0037     bool try_lock_shared();
0038     void unlock_shared();
0039 
0040     typedef implementation-defined native_handle_type; // See 30.2.3
0041     native_handle_type native_handle(); // See 30.2.3
0042 };
0043 
0044 class shared_timed_mutex
0045 {
0046 public:
0047     shared_timed_mutex();
0048     ~shared_timed_mutex();
0049 
0050     shared_timed_mutex(const shared_timed_mutex&) = delete;
0051     shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
0052 
0053     // Exclusive ownership
0054     void lock(); // blocking
0055     bool try_lock();
0056     template <class Rep, class Period>
0057         bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
0058     template <class Clock, class Duration>
0059         bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
0060     void unlock();
0061 
0062     // Shared ownership
0063     void lock_shared(); // blocking
0064     bool try_lock_shared();
0065     template <class Rep, class Period>
0066         bool
0067         try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
0068     template <class Clock, class Duration>
0069         bool
0070         try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
0071     void unlock_shared();
0072 };
0073 
0074 template <class Mutex>
0075 class shared_lock
0076 {
0077 public:
0078     typedef Mutex mutex_type;
0079 
0080     // Shared locking
0081     shared_lock() noexcept;
0082     explicit shared_lock(mutex_type& m); // blocking
0083     shared_lock(mutex_type& m, defer_lock_t) noexcept;
0084     shared_lock(mutex_type& m, try_to_lock_t);
0085     shared_lock(mutex_type& m, adopt_lock_t);
0086     template <class Clock, class Duration>
0087         shared_lock(mutex_type& m,
0088                     const chrono::time_point<Clock, Duration>& abs_time);
0089     template <class Rep, class Period>
0090         shared_lock(mutex_type& m,
0091                     const chrono::duration<Rep, Period>& rel_time);
0092     ~shared_lock();
0093 
0094     shared_lock(shared_lock const&) = delete;
0095     shared_lock& operator=(shared_lock const&) = delete;
0096 
0097     shared_lock(shared_lock&& u) noexcept;
0098     shared_lock& operator=(shared_lock&& u) noexcept;
0099 
0100     void lock(); // blocking
0101     bool try_lock();
0102     template <class Rep, class Period>
0103         bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
0104     template <class Clock, class Duration>
0105         bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
0106     void unlock();
0107 
0108     // Setters
0109     void swap(shared_lock& u) noexcept;
0110     mutex_type* release() noexcept;
0111 
0112     // Getters
0113     bool owns_lock() const noexcept;
0114     explicit operator bool () const noexcept;
0115     mutex_type* mutex() const noexcept;
0116 };
0117 
0118 template <class Mutex>
0119     void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
0120 
0121 }  // std
0122 
0123 */
0124 
0125 #include <__cxx03/__config>
0126 
0127 #if !defined(_LIBCPP_HAS_NO_THREADS)
0128 
0129 #  include <__cxx03/__chrono/duration.h>
0130 #  include <__cxx03/__chrono/steady_clock.h>
0131 #  include <__cxx03/__chrono/time_point.h>
0132 #  include <__cxx03/__condition_variable/condition_variable.h>
0133 #  include <__cxx03/__memory/addressof.h>
0134 #  include <__cxx03/__mutex/mutex.h>
0135 #  include <__cxx03/__mutex/tag_types.h>
0136 #  include <__cxx03/__mutex/unique_lock.h>
0137 #  include <__cxx03/__system_error/system_error.h>
0138 #  include <__cxx03/__utility/swap.h>
0139 #  include <__cxx03/cerrno>
0140 #  include <__cxx03/version>
0141 
0142 _LIBCPP_PUSH_MACROS
0143 #  include <__cxx03/__undef_macros>
0144 
0145 #  if _LIBCPP_STD_VER >= 14
0146 
0147 #    if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0148 #      pragma GCC system_header
0149 #    endif
0150 
0151 _LIBCPP_BEGIN_NAMESPACE_STD
0152 
0153 struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
0154   mutex __mut_;
0155   condition_variable __gate1_;
0156   condition_variable __gate2_;
0157   unsigned __state_;
0158 
0159   static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
0160   static const unsigned __n_readers_     = ~__write_entered_;
0161 
0162   __shared_mutex_base();
0163   _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
0164 
0165   __shared_mutex_base(const __shared_mutex_base&)            = delete;
0166   __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
0167 
0168   // Exclusive ownership
0169   void lock(); // blocking
0170   bool try_lock();
0171   void unlock();
0172 
0173   // Shared ownership
0174   void lock_shared(); // blocking
0175   bool try_lock_shared();
0176   void unlock_shared();
0177 
0178   //     typedef implementation-defined native_handle_type; // See 30.2.3
0179   //     native_handle_type native_handle(); // See 30.2.3
0180 };
0181 
0182 #    if _LIBCPP_STD_VER >= 17
0183 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex {
0184   __shared_mutex_base __base_;
0185 
0186 public:
0187   _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
0188   _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
0189 
0190   shared_mutex(const shared_mutex&)            = delete;
0191   shared_mutex& operator=(const shared_mutex&) = delete;
0192 
0193   // Exclusive ownership
0194   _LIBCPP_HIDE_FROM_ABI void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()) {
0195     return __base_.lock();
0196   }
0197   _LIBCPP_HIDE_FROM_ABI bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
0198     return __base_.try_lock();
0199   }
0200   _LIBCPP_HIDE_FROM_ABI void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()) {
0201     return __base_.unlock();
0202   }
0203 
0204   // Shared ownership
0205   _LIBCPP_HIDE_FROM_ABI void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()) {
0206     return __base_.lock_shared();
0207   }
0208   _LIBCPP_HIDE_FROM_ABI bool try_lock_shared()
0209       _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
0210     return __base_.try_lock_shared();
0211   }
0212   _LIBCPP_HIDE_FROM_ABI void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()) {
0213     return __base_.unlock_shared();
0214   }
0215 
0216   //     typedef __shared_mutex_base::native_handle_type native_handle_type;
0217   //     _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
0218 };
0219 #    endif
0220 
0221 class _LIBCPP_EXPORTED_FROM_ABI
0222 _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_timed_mutex")) shared_timed_mutex {
0223   __shared_mutex_base __base_;
0224 
0225 public:
0226   shared_timed_mutex();
0227   _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
0228 
0229   shared_timed_mutex(const shared_timed_mutex&)            = delete;
0230   shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
0231 
0232   // Exclusive ownership
0233   void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__());
0234   bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
0235   template <class _Rep, class _Period>
0236   _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
0237       _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
0238     return try_lock_until(chrono::steady_clock::now() + __rel_time);
0239   }
0240   template <class _Clock, class _Duration>
0241   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0242   try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
0243       _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
0244   void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__());
0245 
0246   // Shared ownership
0247   void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__());
0248   bool try_lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
0249   template <class _Rep, class _Period>
0250   _LIBCPP_HIDE_FROM_ABI bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
0251       _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
0252     return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
0253   }
0254   template <class _Clock, class _Duration>
0255   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0256   try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
0257       _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
0258   void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__());
0259 };
0260 
0261 template <class _Clock, class _Duration>
0262 bool shared_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
0263   unique_lock<mutex> __lk(__base_.__mut_);
0264   if (__base_.__state_ & __base_.__write_entered_) {
0265     while (true) {
0266       cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
0267       if ((__base_.__state_ & __base_.__write_entered_) == 0)
0268         break;
0269       if (__status == cv_status::timeout)
0270         return false;
0271     }
0272   }
0273   __base_.__state_ |= __base_.__write_entered_;
0274   if (__base_.__state_ & __base_.__n_readers_) {
0275     while (true) {
0276       cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
0277       if ((__base_.__state_ & __base_.__n_readers_) == 0)
0278         break;
0279       if (__status == cv_status::timeout) {
0280         __base_.__state_ &= ~__base_.__write_entered_;
0281         __base_.__gate1_.notify_all();
0282         return false;
0283       }
0284     }
0285   }
0286   return true;
0287 }
0288 
0289 template <class _Clock, class _Duration>
0290 bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
0291   unique_lock<mutex> __lk(__base_.__mut_);
0292   if ((__base_.__state_ & __base_.__write_entered_) ||
0293       (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
0294     while (true) {
0295       cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
0296       if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
0297           (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
0298         break;
0299       if (__status == cv_status::timeout)
0300         return false;
0301     }
0302   }
0303   unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
0304   __base_.__state_ &= ~__base_.__n_readers_;
0305   __base_.__state_ |= __num_readers;
0306   return true;
0307 }
0308 
0309 template <class _Mutex>
0310 class shared_lock {
0311 public:
0312   typedef _Mutex mutex_type;
0313 
0314 private:
0315   mutex_type* __m_;
0316   bool __owns_;
0317 
0318 public:
0319   _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
0320 
0321   _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(std::addressof(__m)), __owns_(true) {
0322     __m_->lock_shared();
0323   }
0324 
0325   _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
0326       : __m_(std::addressof(__m)),
0327         __owns_(false) {}
0328 
0329   _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
0330       : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {}
0331 
0332   _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(std::addressof(__m)), __owns_(true) {}
0333 
0334   template <class _Clock, class _Duration>
0335   _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
0336       : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
0337 
0338   template <class _Rep, class _Period>
0339   _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
0340       : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
0341 
0342   _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
0343     if (__owns_)
0344       __m_->unlock_shared();
0345   }
0346 
0347   shared_lock(shared_lock const&)            = delete;
0348   shared_lock& operator=(shared_lock const&) = delete;
0349 
0350   _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) {
0351     __u.__m_    = nullptr;
0352     __u.__owns_ = false;
0353   }
0354 
0355   _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
0356     if (__owns_)
0357       __m_->unlock_shared();
0358     __m_        = nullptr;
0359     __owns_     = false;
0360     __m_        = __u.__m_;
0361     __owns_     = __u.__owns_;
0362     __u.__m_    = nullptr;
0363     __u.__owns_ = false;
0364     return *this;
0365   }
0366 
0367   _LIBCPP_HIDE_FROM_ABI void lock();
0368   _LIBCPP_HIDE_FROM_ABI bool try_lock();
0369   template <class _Rep, class _Period>
0370   _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
0371   template <class _Clock, class _Duration>
0372   _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
0373   _LIBCPP_HIDE_FROM_ABI void unlock();
0374 
0375   // Setters
0376   _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
0377     std::swap(__m_, __u.__m_);
0378     std::swap(__owns_, __u.__owns_);
0379   }
0380 
0381   _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
0382     mutex_type* __m = __m_;
0383     __m_            = nullptr;
0384     __owns_         = false;
0385     return __m;
0386   }
0387 
0388   // Getters
0389   _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
0390 
0391   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
0392 
0393   _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
0394 };
0395 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
0396 
0397 template <class _Mutex>
0398 void shared_lock<_Mutex>::lock() {
0399   if (__m_ == nullptr)
0400     __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
0401   if (__owns_)
0402     __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
0403   __m_->lock_shared();
0404   __owns_ = true;
0405 }
0406 
0407 template <class _Mutex>
0408 bool shared_lock<_Mutex>::try_lock() {
0409   if (__m_ == nullptr)
0410     __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
0411   if (__owns_)
0412     __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
0413   __owns_ = __m_->try_lock_shared();
0414   return __owns_;
0415 }
0416 
0417 template <class _Mutex>
0418 template <class _Rep, class _Period>
0419 bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
0420   if (__m_ == nullptr)
0421     __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
0422   if (__owns_)
0423     __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
0424   __owns_ = __m_->try_lock_shared_for(__d);
0425   return __owns_;
0426 }
0427 
0428 template <class _Mutex>
0429 template <class _Clock, class _Duration>
0430 bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
0431   if (__m_ == nullptr)
0432     __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
0433   if (__owns_)
0434     __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
0435   __owns_ = __m_->try_lock_shared_until(__t);
0436   return __owns_;
0437 }
0438 
0439 template <class _Mutex>
0440 void shared_lock<_Mutex>::unlock() {
0441   if (!__owns_)
0442     __throw_system_error(EPERM, "shared_lock::unlock: not locked");
0443   __m_->unlock_shared();
0444   __owns_ = false;
0445 }
0446 
0447 template <class _Mutex>
0448 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
0449   __x.swap(__y);
0450 }
0451 
0452 _LIBCPP_END_NAMESPACE_STD
0453 
0454 #  endif // _LIBCPP_STD_VER >= 14
0455 
0456 _LIBCPP_POP_MACROS
0457 
0458 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
0459 
0460 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0461 #  include <__cxx03/system_error>
0462 #endif
0463 
0464 #endif // _LIBCPP___CXX03_SHARED_MUTEX