29 #ifndef _GLIBCXX_SHARED_MUTEX
30 #define _GLIBCXX_SHARED_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus <= 201103L
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
52 #ifdef _GLIBCXX_HAS_GTHREADS
54 #if __cplusplus > 201402L
55 #define __cpp_lib_shared_mutex 201505
59 #define __cpp_lib_shared_timed_mutex 201402
60 class shared_timed_mutex;
62 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
64 class __shared_mutex_pthread
66 friend class shared_timed_mutex;
68 #ifdef PTHREAD_RWLOCK_INITIALIZER
69 pthread_rwlock_t _M_rwlock = PTHREAD_RWLOCK_INITIALIZER;
72 __shared_mutex_pthread() =
default;
73 ~__shared_mutex_pthread() =
default;
75 pthread_rwlock_t _M_rwlock;
78 __shared_mutex_pthread()
80 int __ret = pthread_rwlock_init(&_M_rwlock, NULL);
83 else if (__ret == EAGAIN)
84 __throw_system_error(
int(errc::resource_unavailable_try_again));
85 else if (__ret == EPERM)
86 __throw_system_error(
int(errc::operation_not_permitted));
88 __glibcxx_assert(__ret == 0);
91 ~__shared_mutex_pthread()
93 int __ret __attribute((__unused__)) = pthread_rwlock_destroy(&_M_rwlock);
95 __glibcxx_assert(__ret == 0);
99 __shared_mutex_pthread(
const __shared_mutex_pthread&) =
delete;
100 __shared_mutex_pthread& operator=(
const __shared_mutex_pthread&) =
delete;
105 int __ret = pthread_rwlock_wrlock(&_M_rwlock);
106 if (__ret == EDEADLK)
107 __throw_system_error(
int(errc::resource_deadlock_would_occur));
109 __glibcxx_assert(__ret == 0);
115 int __ret = pthread_rwlock_trywrlock(&_M_rwlock);
116 if (__ret == EBUSY)
return false;
118 __glibcxx_assert(__ret == 0);
125 int __ret __attribute((__unused__)) = pthread_rwlock_unlock(&_M_rwlock);
127 __glibcxx_assert(__ret == 0);
141 __ret = pthread_rwlock_rdlock(&_M_rwlock);
142 while (__ret == EAGAIN);
143 if (__ret == EDEADLK)
144 __throw_system_error(
int(errc::resource_deadlock_would_occur));
146 __glibcxx_assert(__ret == 0);
152 int __ret = pthread_rwlock_tryrdlock(&_M_rwlock);
156 if (__ret == EBUSY || __ret == EAGAIN)
return false;
158 __glibcxx_assert(__ret == 0);
168 void* native_handle() {
return &_M_rwlock; }
172 #if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
212 static constexpr
unsigned _S_write_entered
213 = 1U << (
sizeof(unsigned)*__CHAR_BIT__ - 1);
214 static constexpr
unsigned _S_max_readers = ~_S_write_entered;
217 bool _M_write_entered()
const {
return _M_state & _S_write_entered; }
220 unsigned _M_readers()
const {
return _M_state & _S_max_readers; }
227 __glibcxx_assert( _M_state == 0 );
240 _M_gate1.wait(__lk, [=]{
return !_M_write_entered(); });
241 _M_state |= _S_write_entered;
243 _M_gate2.wait(__lk, [=]{
return _M_readers() == 0; });
250 if (__lk.owns_lock() && _M_state == 0)
252 _M_state = _S_write_entered;
262 __glibcxx_assert( _M_write_entered() );
266 _M_gate1.notify_all();
275 _M_gate1.wait(__lk, [=]{
return _M_state < _S_max_readers; });
283 if (!__lk.owns_lock())
285 if (_M_state < _S_max_readers)
297 __glibcxx_assert( _M_readers() > 0 );
298 auto __prev = _M_state--;
299 if (_M_write_entered())
302 if (_M_readers() == 0)
303 _M_gate2.notify_one();
311 if (__prev == _S_max_readers)
312 _M_gate1.notify_one();
318 #if __cplusplus > 201402L
323 shared_mutex() =
default;
324 ~shared_mutex() =
default;
326 shared_mutex(
const shared_mutex&) =
delete;
327 shared_mutex& operator=(
const shared_mutex&) =
delete;
331 void lock() { _M_impl.lock(); }
332 bool try_lock() {
return _M_impl.try_lock(); }
333 void unlock() { _M_impl.unlock(); }
337 void lock_shared() { _M_impl.lock_shared(); }
338 bool try_lock_shared() {
return _M_impl.try_lock_shared(); }
339 void unlock_shared() { _M_impl.unlock_shared(); }
341 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
342 typedef void* native_handle_type;
343 native_handle_type native_handle() {
return _M_impl.native_handle(); }
346 __shared_mutex_pthread _M_impl;
349 __shared_mutex_cv _M_impl;
354 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
367 typedef chrono::system_clock __clock_t;
378 void lock() { _Base::lock(); }
379 bool try_lock() {
return _Base::try_lock(); }
380 void unlock() { _Base::unlock(); }
382 template<
typename _Rep,
typename _Period>
386 return try_lock_until(__clock_t::now() + __rel_time);
391 void lock_shared() { _Base::lock_shared(); }
392 bool try_lock_shared() {
return _Base::try_lock_shared(); }
393 void unlock_shared() { _Base::unlock_shared(); }
395 template<
typename _Rep,
typename _Period>
399 return try_lock_shared_until(__clock_t::now() + __rel_time);
402 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
406 template<
typename _Duration>
413 __gthread_time_t __ts =
415 static_cast<std::time_t
>(__s.time_since_epoch().count()),
416 static_cast<long>(__ns.count())
419 int __ret = pthread_rwlock_timedwrlock(&_M_rwlock, &__ts);
422 if (__ret == ETIMEDOUT || __ret == EDEADLK)
425 __glibcxx_assert(__ret == 0);
429 template<
typename _Clock,
typename _Duration>
434 const typename _Clock::time_point __c_entry = _Clock::now();
435 const __clock_t::time_point __s_entry = __clock_t::now();
436 const auto __delta = __abs_time - __c_entry;
437 const auto __s_atime = __s_entry + __delta;
438 return try_lock_until(__s_atime);
443 template<
typename _Duration>
451 __gthread_time_t __ts =
453 static_cast<std::time_t
>(__s.time_since_epoch().count()),
454 static_cast<long>(__ns.count())
472 __ret = pthread_rwlock_timedrdlock(&_M_rwlock, &__ts);
473 while (__ret == EAGAIN || __ret == EDEADLK);
474 if (__ret == ETIMEDOUT)
477 __glibcxx_assert(__ret == 0);
481 template<
typename _Clock,
typename _Duration>
484 _Duration>& __abs_time)
487 const typename _Clock::time_point __c_entry = _Clock::now();
488 const __clock_t::time_point __s_entry = __clock_t::now();
489 const auto __delta = __abs_time - __c_entry;
490 const auto __s_atime = __s_entry + __delta;
491 return try_lock_shared_until(__s_atime);
494 #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
498 template<
typename _Clock,
typename _Duration>
503 if (!_M_gate1.wait_until(__lk, __abs_time,
504 [=]{
return !_M_write_entered(); }))
508 _M_state |= _S_write_entered;
509 if (!_M_gate2.wait_until(__lk, __abs_time,
510 [=]{
return _M_readers() == 0; }))
512 _M_state ^= _S_write_entered;
514 _M_gate1.notify_all();
522 template <
typename _Clock,
typename _Duration>
525 _Duration>& __abs_time)
528 if (!_M_gate1.wait_until(__lk, __abs_time,
529 [=]{
return _M_state < _S_max_readers; }))
537 #endif // _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
539 #endif // _GLIBCXX_HAS_GTHREADS
542 template<
typename _Mutex>
546 typedef _Mutex mutex_type;
550 shared_lock() noexcept : _M_pm(
nullptr), _M_owns(
false) { }
553 shared_lock(mutex_type& __m) : _M_pm(&__m), _M_owns(
true)
554 { __m.lock_shared(); }
557 : _M_pm(&__m), _M_owns(
false) { }
560 : _M_pm(&__m), _M_owns(__m.try_lock_shared()) { }
563 : _M_pm(&__m), _M_owns(
true) { }
565 template<
typename _Clock,
typename _Duration>
568 : _M_pm(&__m), _M_owns(__m.try_lock_shared_until(__abs_time)) { }
570 template<
typename _Rep,
typename _Period>
573 : _M_pm(&__m), _M_owns(__m.try_lock_shared_for(__rel_time)) { }
578 _M_pm->unlock_shared();
598 _M_pm->lock_shared();
606 return _M_owns = _M_pm->try_lock_shared();
609 template<
typename _Rep,
typename _Period>
614 return _M_owns = _M_pm->try_lock_shared_for(__rel_time);
617 template<
typename _Clock,
typename _Duration>
622 return _M_owns = _M_pm->try_lock_shared_until(__abs_time);
629 __throw_system_error(
int(errc::resource_deadlock_would_occur));
630 _M_pm->unlock_shared();
639 std::swap(_M_pm, __u._M_pm);
640 std::swap(_M_owns, __u._M_owns);
652 bool owns_lock()
const noexcept {
return _M_owns; }
654 explicit operator bool()
const noexcept {
return _M_owns; }
656 mutex_type*
mutex()
const noexcept {
return _M_pm; }
662 if (_M_pm ==
nullptr)
663 __throw_system_error(
int(errc::operation_not_permitted));
665 __throw_system_error(
int(errc::resource_deadlock_would_occur));
673 template<
typename _Mutex>
678 #endif // _GLIBCXX_USE_C99_STDINT_TR1
681 _GLIBCXX_END_NAMESPACE_VERSION
686 #endif // _GLIBCXX_SHARED_MUTEX
A movable scoped lock type.
Assume the calling thread has already obtained mutex ownership and manage it.
constexpr enable_if< __is_duration< _ToDur >::value, _ToDur >::type duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
_Tp exchange(_Tp &__obj, _Up &&__new_val)
Assign __new_val to __obj and return its previous value.
Do not acquire ownership of the mutex.
__shared_mutex_cv __shared_timed_mutex_base
Swap specialization for shared_lock.
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
A shared mutex type implemented using std::condition_variable.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
A simple scoped lock type.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
Try to acquire ownership of the mutex without blocking.
The standard shared timed mutex type.
void swap(shared_lock< _Mutex > &__x, shared_lock< _Mutex > &__y) noexcept
Swap specialization for shared_lock.