Warning, /include/c++/v1/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_SHARED_MUTEX
0011 #define _LIBCPP_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 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0126 # include <__cxx03/shared_mutex>
0127 #else
0128 # include <__config>
0129
0130 # if _LIBCPP_HAS_THREADS
0131
0132 # include <__chrono/duration.h>
0133 # include <__chrono/steady_clock.h>
0134 # include <__chrono/time_point.h>
0135 # include <__condition_variable/condition_variable.h>
0136 # include <__memory/addressof.h>
0137 # include <__mutex/mutex.h>
0138 # include <__mutex/tag_types.h>
0139 # include <__mutex/unique_lock.h>
0140 # include <__system_error/throw_system_error.h>
0141 # include <__utility/swap.h>
0142 # include <cerrno>
0143 # include <version>
0144
0145 _LIBCPP_PUSH_MACROS
0146 # include <__undef_macros>
0147
0148 # if _LIBCPP_STD_VER >= 14
0149
0150 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0151 # pragma GCC system_header
0152 # endif
0153
0154 _LIBCPP_BEGIN_NAMESPACE_STD
0155
0156 struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
0157 mutex __mut_;
0158 condition_variable __gate1_;
0159 condition_variable __gate2_;
0160 unsigned __state_;
0161
0162 static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
0163 static const unsigned __n_readers_ = ~__write_entered_;
0164
0165 __shared_mutex_base();
0166 _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
0167
0168 __shared_mutex_base(const __shared_mutex_base&) = delete;
0169 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
0170
0171 // Exclusive ownership
0172 void lock(); // blocking
0173 bool try_lock();
0174 void unlock();
0175
0176 // Shared ownership
0177 void lock_shared(); // blocking
0178 bool try_lock_shared();
0179 void unlock_shared();
0180
0181 // typedef implementation-defined native_handle_type; // See 30.2.3
0182 // native_handle_type native_handle(); // See 30.2.3
0183 };
0184
0185 # if _LIBCPP_STD_VER >= 17
0186 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex {
0187 __shared_mutex_base __base_;
0188
0189 public:
0190 _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
0191 _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
0192
0193 shared_mutex(const shared_mutex&) = delete;
0194 shared_mutex& operator=(const shared_mutex&) = delete;
0195
0196 // Exclusive ownership
0197 _LIBCPP_HIDE_FROM_ABI void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()) {
0198 return __base_.lock();
0199 }
0200 _LIBCPP_HIDE_FROM_ABI bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
0201 return __base_.try_lock();
0202 }
0203 _LIBCPP_HIDE_FROM_ABI void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()) {
0204 return __base_.unlock();
0205 }
0206
0207 // Shared ownership
0208 _LIBCPP_HIDE_FROM_ABI void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()) {
0209 return __base_.lock_shared();
0210 }
0211 _LIBCPP_HIDE_FROM_ABI bool try_lock_shared()
0212 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
0213 return __base_.try_lock_shared();
0214 }
0215 _LIBCPP_HIDE_FROM_ABI void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()) {
0216 return __base_.unlock_shared();
0217 }
0218
0219 // typedef __shared_mutex_base::native_handle_type native_handle_type;
0220 // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
0221 };
0222 # endif
0223
0224 class _LIBCPP_EXPORTED_FROM_ABI
0225 _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_timed_mutex")) shared_timed_mutex {
0226 __shared_mutex_base __base_;
0227
0228 public:
0229 shared_timed_mutex();
0230 _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
0231
0232 shared_timed_mutex(const shared_timed_mutex&) = delete;
0233 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
0234
0235 // Exclusive ownership
0236 void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__());
0237 bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
0238 template <class _Rep, class _Period>
0239 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
0240 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) {
0241 return try_lock_until(chrono::steady_clock::now() + __rel_time);
0242 }
0243 template <class _Clock, class _Duration>
0244 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0245 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
0246 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true));
0247 void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__());
0248
0249 // Shared ownership
0250 void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__());
0251 bool try_lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
0252 template <class _Rep, class _Period>
0253 _LIBCPP_HIDE_FROM_ABI bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
0254 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) {
0255 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
0256 }
0257 template <class _Clock, class _Duration>
0258 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0259 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
0260 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true));
0261 void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__());
0262 };
0263
0264 template <class _Clock, class _Duration>
0265 bool shared_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
0266 unique_lock<mutex> __lk(__base_.__mut_);
0267 if (__base_.__state_ & __base_.__write_entered_) {
0268 while (true) {
0269 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
0270 if ((__base_.__state_ & __base_.__write_entered_) == 0)
0271 break;
0272 if (__status == cv_status::timeout)
0273 return false;
0274 }
0275 }
0276 __base_.__state_ |= __base_.__write_entered_;
0277 if (__base_.__state_ & __base_.__n_readers_) {
0278 while (true) {
0279 cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
0280 if ((__base_.__state_ & __base_.__n_readers_) == 0)
0281 break;
0282 if (__status == cv_status::timeout) {
0283 __base_.__state_ &= ~__base_.__write_entered_;
0284 __base_.__gate1_.notify_all();
0285 return false;
0286 }
0287 }
0288 }
0289 return true;
0290 }
0291
0292 template <class _Clock, class _Duration>
0293 bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
0294 unique_lock<mutex> __lk(__base_.__mut_);
0295 if ((__base_.__state_ & __base_.__write_entered_) ||
0296 (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
0297 while (true) {
0298 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
0299 if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
0300 (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
0301 break;
0302 if (__status == cv_status::timeout)
0303 return false;
0304 }
0305 }
0306 unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
0307 __base_.__state_ &= ~__base_.__n_readers_;
0308 __base_.__state_ |= __num_readers;
0309 return true;
0310 }
0311
0312 template <class _Mutex>
0313 class shared_lock {
0314 public:
0315 typedef _Mutex mutex_type;
0316
0317 private:
0318 mutex_type* __m_;
0319 bool __owns_;
0320
0321 public:
0322 _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
0323
0324 _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(std::addressof(__m)), __owns_(true) {
0325 __m_->lock_shared();
0326 }
0327
0328 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
0329 : __m_(std::addressof(__m)),
0330 __owns_(false) {}
0331
0332 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
0333 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {}
0334
0335 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(std::addressof(__m)), __owns_(true) {}
0336
0337 template <class _Clock, class _Duration>
0338 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
0339 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
0340
0341 template <class _Rep, class _Period>
0342 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
0343 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
0344
0345 _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
0346 if (__owns_)
0347 __m_->unlock_shared();
0348 }
0349
0350 shared_lock(shared_lock const&) = delete;
0351 shared_lock& operator=(shared_lock const&) = delete;
0352
0353 _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) {
0354 __u.__m_ = nullptr;
0355 __u.__owns_ = false;
0356 }
0357
0358 _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
0359 if (__owns_)
0360 __m_->unlock_shared();
0361 __m_ = nullptr;
0362 __owns_ = false;
0363 __m_ = __u.__m_;
0364 __owns_ = __u.__owns_;
0365 __u.__m_ = nullptr;
0366 __u.__owns_ = false;
0367 return *this;
0368 }
0369
0370 _LIBCPP_HIDE_FROM_ABI void lock();
0371 _LIBCPP_HIDE_FROM_ABI bool try_lock();
0372 template <class _Rep, class _Period>
0373 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
0374 template <class _Clock, class _Duration>
0375 _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
0376 _LIBCPP_HIDE_FROM_ABI void unlock();
0377
0378 // Setters
0379 _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
0380 std::swap(__m_, __u.__m_);
0381 std::swap(__owns_, __u.__owns_);
0382 }
0383
0384 _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
0385 mutex_type* __m = __m_;
0386 __m_ = nullptr;
0387 __owns_ = false;
0388 return __m;
0389 }
0390
0391 // Getters
0392 _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
0393
0394 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
0395
0396 _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
0397 };
0398 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
0399
0400 template <class _Mutex>
0401 void shared_lock<_Mutex>::lock() {
0402 if (__m_ == nullptr)
0403 __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
0404 if (__owns_)
0405 __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
0406 __m_->lock_shared();
0407 __owns_ = true;
0408 }
0409
0410 template <class _Mutex>
0411 bool shared_lock<_Mutex>::try_lock() {
0412 if (__m_ == nullptr)
0413 __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
0414 if (__owns_)
0415 __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
0416 __owns_ = __m_->try_lock_shared();
0417 return __owns_;
0418 }
0419
0420 template <class _Mutex>
0421 template <class _Rep, class _Period>
0422 bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
0423 if (__m_ == nullptr)
0424 __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
0425 if (__owns_)
0426 __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
0427 __owns_ = __m_->try_lock_shared_for(__d);
0428 return __owns_;
0429 }
0430
0431 template <class _Mutex>
0432 template <class _Clock, class _Duration>
0433 bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
0434 if (__m_ == nullptr)
0435 __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
0436 if (__owns_)
0437 __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
0438 __owns_ = __m_->try_lock_shared_until(__t);
0439 return __owns_;
0440 }
0441
0442 template <class _Mutex>
0443 void shared_lock<_Mutex>::unlock() {
0444 if (!__owns_)
0445 __throw_system_error(EPERM, "shared_lock::unlock: not locked");
0446 __m_->unlock_shared();
0447 __owns_ = false;
0448 }
0449
0450 template <class _Mutex>
0451 inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
0452 __x.swap(__y);
0453 }
0454
0455 _LIBCPP_END_NAMESPACE_STD
0456
0457 # endif // _LIBCPP_STD_VER >= 14
0458
0459 _LIBCPP_POP_MACROS
0460
0461 # endif // _LIBCPP_HAS_THREADS
0462
0463 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0464 # include <system_error>
0465 # endif
0466 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0467
0468 #endif // _LIBCPP_SHARED_MUTEX