1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_SHARED_MUTEX 11#define _LIBCPP_SHARED_MUTEX 12 13/* 14 shared_mutex synopsis 15 16// C++1y 17 18namespace std 19{ 20 21class shared_mutex // C++17 22{ 23public: 24 shared_mutex(); 25 ~shared_mutex(); 26 27 shared_mutex(const shared_mutex&) = delete; 28 shared_mutex& operator=(const shared_mutex&) = delete; 29 30 // Exclusive ownership 31 void lock(); // blocking 32 bool try_lock(); 33 void unlock(); 34 35 // Shared ownership 36 void lock_shared(); // blocking 37 bool try_lock_shared(); 38 void unlock_shared(); 39 40 typedef implementation-defined native_handle_type; // See 30.2.3 41 native_handle_type native_handle(); // See 30.2.3 42}; 43 44class shared_timed_mutex 45{ 46public: 47 shared_timed_mutex(); 48 ~shared_timed_mutex(); 49 50 shared_timed_mutex(const shared_timed_mutex&) = delete; 51 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete; 52 53 // Exclusive ownership 54 void lock(); // blocking 55 bool try_lock(); 56 template <class Rep, class Period> 57 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 58 template <class Clock, class Duration> 59 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 60 void unlock(); 61 62 // Shared ownership 63 void lock_shared(); // blocking 64 bool try_lock_shared(); 65 template <class Rep, class Period> 66 bool 67 try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time); 68 template <class Clock, class Duration> 69 bool 70 try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time); 71 void unlock_shared(); 72}; 73 74template <class Mutex> 75class shared_lock 76{ 77public: 78 typedef Mutex mutex_type; 79 80 // Shared locking 81 shared_lock() noexcept; 82 explicit shared_lock(mutex_type& m); // blocking 83 shared_lock(mutex_type& m, defer_lock_t) noexcept; 84 shared_lock(mutex_type& m, try_to_lock_t); 85 shared_lock(mutex_type& m, adopt_lock_t); 86 template <class Clock, class Duration> 87 shared_lock(mutex_type& m, 88 const chrono::time_point<Clock, Duration>& abs_time); 89 template <class Rep, class Period> 90 shared_lock(mutex_type& m, 91 const chrono::duration<Rep, Period>& rel_time); 92 ~shared_lock(); 93 94 shared_lock(shared_lock const&) = delete; 95 shared_lock& operator=(shared_lock const&) = delete; 96 97 shared_lock(shared_lock&& u) noexcept; 98 shared_lock& operator=(shared_lock&& u) noexcept; 99 100 void lock(); // blocking 101 bool try_lock(); 102 template <class Rep, class Period> 103 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 104 template <class Clock, class Duration> 105 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 106 void unlock(); 107 108 // Setters 109 void swap(shared_lock& u) noexcept; 110 mutex_type* release() noexcept; 111 112 // Getters 113 bool owns_lock() const noexcept; 114 explicit operator bool () const noexcept; 115 mutex_type* mutex() const noexcept; 116}; 117 118template <class Mutex> 119 void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept; 120 121} // std 122 123*/ 124 125#include <__config> 126 127#ifdef _LIBCPP_HAS_NO_THREADS 128# error "<shared_mutex> is not supported since libc++ has been configured without support for threads." 129#endif 130 131#include <__chrono/duration.h> 132#include <__chrono/steady_clock.h> 133#include <__chrono/time_point.h> 134#include <__condition_variable/condition_variable.h> 135#include <__memory/addressof.h> 136#include <__mutex/mutex.h> 137#include <__mutex/tag_types.h> 138#include <__mutex/unique_lock.h> 139#include <__system_error/system_error.h> 140#include <__utility/swap.h> 141#include <cerrno> 142#include <version> 143 144_LIBCPP_PUSH_MACROS 145#include <__undef_macros> 146 147#if _LIBCPP_STD_VER >= 14 148 149# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 150# pragma GCC system_header 151# endif 152 153_LIBCPP_BEGIN_NAMESPACE_STD 154 155struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base { 156 mutex __mut_; 157 condition_variable __gate1_; 158 condition_variable __gate2_; 159 unsigned __state_; 160 161 static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1); 162 static const unsigned __n_readers_ = ~__write_entered_; 163 164 __shared_mutex_base(); 165 _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default; 166 167 __shared_mutex_base(const __shared_mutex_base&) = delete; 168 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete; 169 170 // Exclusive ownership 171 void lock(); // blocking 172 bool try_lock(); 173 void unlock(); 174 175 // Shared ownership 176 void lock_shared(); // blocking 177 bool try_lock_shared(); 178 void unlock_shared(); 179 180 // typedef implementation-defined native_handle_type; // See 30.2.3 181 // native_handle_type native_handle(); // See 30.2.3 182}; 183 184# if _LIBCPP_STD_VER >= 17 185class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_mutex")) shared_mutex { 186 __shared_mutex_base __base_; 187 188public: 189 _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {} 190 _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default; 191 192 shared_mutex(const shared_mutex&) = delete; 193 shared_mutex& operator=(const shared_mutex&) = delete; 194 195 // Exclusive ownership 196 _LIBCPP_HIDE_FROM_ABI void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()) { 197 return __base_.lock(); 198 } 199 _LIBCPP_HIDE_FROM_ABI bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) { 200 return __base_.try_lock(); 201 } 202 _LIBCPP_HIDE_FROM_ABI void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()) { 203 return __base_.unlock(); 204 } 205 206 // Shared ownership 207 _LIBCPP_HIDE_FROM_ABI void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()) { 208 return __base_.lock_shared(); 209 } 210 _LIBCPP_HIDE_FROM_ABI bool try_lock_shared() 211 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) { 212 return __base_.try_lock_shared(); 213 } 214 _LIBCPP_HIDE_FROM_ABI void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()) { 215 return __base_.unlock_shared(); 216 } 217 218 // typedef __shared_mutex_base::native_handle_type native_handle_type; 219 // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); } 220}; 221# endif 222 223class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_THREAD_SAFETY_ANNOTATION(__capability__("shared_timed_mutex")) 224 shared_timed_mutex { 225 __shared_mutex_base __base_; 226 227public: 228 shared_timed_mutex(); 229 _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default; 230 231 shared_timed_mutex(const shared_timed_mutex&) = delete; 232 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete; 233 234 // Exclusive ownership 235 void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_capability__()); 236 bool try_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)); 237 template <class _Rep, class _Period> 238 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time) 239 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)) { 240 return try_lock_until(chrono::steady_clock::now() + __rel_time); 241 } 242 template <class _Clock, class _Duration> 243 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool 244 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) 245 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_capability__(true)); 246 void unlock() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_capability__()); 247 248 // Shared ownership 249 void lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__acquire_shared_capability__()); 250 bool try_lock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)); 251 template <class _Rep, class _Period> 252 _LIBCPP_HIDE_FROM_ABI bool try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time) 253 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)) { 254 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time); 255 } 256 template <class _Clock, class _Duration> 257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool 258 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) 259 _LIBCPP_THREAD_SAFETY_ANNOTATION(__try_acquire_shared_capability__(true)); 260 void unlock_shared() _LIBCPP_THREAD_SAFETY_ANNOTATION(__release_shared_capability__()); 261}; 262 263template <class _Clock, class _Duration> 264bool shared_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) { 265 unique_lock<mutex> __lk(__base_.__mut_); 266 if (__base_.__state_ & __base_.__write_entered_) { 267 while (true) { 268 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time); 269 if ((__base_.__state_ & __base_.__write_entered_) == 0) 270 break; 271 if (__status == cv_status::timeout) 272 return false; 273 } 274 } 275 __base_.__state_ |= __base_.__write_entered_; 276 if (__base_.__state_ & __base_.__n_readers_) { 277 while (true) { 278 cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time); 279 if ((__base_.__state_ & __base_.__n_readers_) == 0) 280 break; 281 if (__status == cv_status::timeout) { 282 __base_.__state_ &= ~__base_.__write_entered_; 283 __base_.__gate1_.notify_all(); 284 return false; 285 } 286 } 287 } 288 return true; 289} 290 291template <class _Clock, class _Duration> 292bool shared_timed_mutex::try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) { 293 unique_lock<mutex> __lk(__base_.__mut_); 294 if ((__base_.__state_ & __base_.__write_entered_) || 295 (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) { 296 while (true) { 297 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time); 298 if ((__base_.__state_ & __base_.__write_entered_) == 0 && 299 (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_) 300 break; 301 if (__status == cv_status::timeout) 302 return false; 303 } 304 } 305 unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1; 306 __base_.__state_ &= ~__base_.__n_readers_; 307 __base_.__state_ |= __num_readers; 308 return true; 309} 310 311template <class _Mutex> 312class shared_lock { 313public: 314 typedef _Mutex mutex_type; 315 316private: 317 mutex_type* __m_; 318 bool __owns_; 319 320public: 321 _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {} 322 323 _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(std::addressof(__m)), __owns_(true) { 324 __m_->lock_shared(); 325 } 326 327 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT 328 : __m_(std::addressof(__m)), 329 __owns_(false) {} 330 331 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t) 332 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {} 333 334 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(std::addressof(__m)), __owns_(true) {} 335 336 template <class _Clock, class _Duration> 337 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time) 338 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {} 339 340 template <class _Rep, class _Period> 341 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time) 342 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {} 343 344 _LIBCPP_HIDE_FROM_ABI ~shared_lock() { 345 if (__owns_) 346 __m_->unlock_shared(); 347 } 348 349 shared_lock(shared_lock const&) = delete; 350 shared_lock& operator=(shared_lock const&) = delete; 351 352 _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) { 353 __u.__m_ = nullptr; 354 __u.__owns_ = false; 355 } 356 357 _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT { 358 if (__owns_) 359 __m_->unlock_shared(); 360 __m_ = nullptr; 361 __owns_ = false; 362 __m_ = __u.__m_; 363 __owns_ = __u.__owns_; 364 __u.__m_ = nullptr; 365 __u.__owns_ = false; 366 return *this; 367 } 368 369 _LIBCPP_HIDE_FROM_ABI void lock(); 370 _LIBCPP_HIDE_FROM_ABI bool try_lock(); 371 template <class _Rep, class _Period> 372 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time); 373 template <class _Clock, class _Duration> 374 _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time); 375 _LIBCPP_HIDE_FROM_ABI void unlock(); 376 377 // Setters 378 _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT { 379 std::swap(__m_, __u.__m_); 380 std::swap(__owns_, __u.__owns_); 381 } 382 383 _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT { 384 mutex_type* __m = __m_; 385 __m_ = nullptr; 386 __owns_ = false; 387 return __m; 388 } 389 390 // Getters 391 _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; } 392 393 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; } 394 395 _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; } 396}; 397_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock); 398 399template <class _Mutex> 400void shared_lock<_Mutex>::lock() { 401 if (__m_ == nullptr) 402 __throw_system_error(EPERM, "shared_lock::lock: references null mutex"); 403 if (__owns_) 404 __throw_system_error(EDEADLK, "shared_lock::lock: already locked"); 405 __m_->lock_shared(); 406 __owns_ = true; 407} 408 409template <class _Mutex> 410bool shared_lock<_Mutex>::try_lock() { 411 if (__m_ == nullptr) 412 __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex"); 413 if (__owns_) 414 __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked"); 415 __owns_ = __m_->try_lock_shared(); 416 return __owns_; 417} 418 419template <class _Mutex> 420template <class _Rep, class _Period> 421bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) { 422 if (__m_ == nullptr) 423 __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex"); 424 if (__owns_) 425 __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked"); 426 __owns_ = __m_->try_lock_shared_for(__d); 427 return __owns_; 428} 429 430template <class _Mutex> 431template <class _Clock, class _Duration> 432bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) { 433 if (__m_ == nullptr) 434 __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex"); 435 if (__owns_) 436 __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked"); 437 __owns_ = __m_->try_lock_shared_until(__t); 438 return __owns_; 439} 440 441template <class _Mutex> 442void shared_lock<_Mutex>::unlock() { 443 if (!__owns_) 444 __throw_system_error(EPERM, "shared_lock::unlock: not locked"); 445 __m_->unlock_shared(); 446 __owns_ = false; 447} 448 449template <class _Mutex> 450inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT { 451 __x.swap(__y); 452} 453 454_LIBCPP_END_NAMESPACE_STD 455 456#endif // _LIBCPP_STD_VER >= 14 457 458_LIBCPP_POP_MACROS 459 460#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 461# include <system_error> 462#endif 463 464#endif // _LIBCPP_SHARED_MUTEX 465