1 // <shared_mutex> -*- C++ -*-
3 // Copyright (C) 2013-2020 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/shared_mutex
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_SHARED_MUTEX
30 #define _GLIBCXX_SHARED_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
36 #include <bits/c++config.h>
37 #include <condition_variable>
38 #include <bits/functexcept.h>
40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 #ifdef _GLIBCXX_HAS_GTHREADS
51 #if __cplusplus >= 201703L
52 #define __cpp_lib_shared_mutex 201505
56 #define __cpp_lib_shared_timed_mutex 201402
57 class shared_timed_mutex;
59 /// @cond undocumented
61 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
63 #define _GLIBCXX_GTHRW(name) \
64 __gthrw(pthread_ ## name); \
66 __glibcxx_ ## name (pthread_rwlock_t *__rwlock) \
68 if (__gthread_active_p ()) \
69 return __gthrw_(pthread_ ## name) (__rwlock); \
73 _GLIBCXX_GTHRW(rwlock_rdlock)
74 _GLIBCXX_GTHRW(rwlock_tryrdlock)
75 _GLIBCXX_GTHRW(rwlock_wrlock)
76 _GLIBCXX_GTHRW(rwlock_trywrlock)
77 _GLIBCXX_GTHRW(rwlock_unlock)
78 # ifndef PTHREAD_RWLOCK_INITIALIZER
79 _GLIBCXX_GTHRW(rwlock_destroy)
80 __gthrw(pthread_rwlock_init);
82 __glibcxx_rwlock_init (pthread_rwlock_t *__rwlock)
84 if (__gthread_active_p ())
85 return __gthrw_(pthread_rwlock_init) (__rwlock, NULL);
90 # if _GTHREAD_USE_MUTEX_TIMEDLOCK
91 __gthrw(pthread_rwlock_timedrdlock);
93 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
96 if (__gthread_active_p ())
97 return __gthrw_(pthread_rwlock_timedrdlock) (__rwlock, __ts);
101 __gthrw(pthread_rwlock_timedwrlock);
103 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
104 const timespec *__ts)
106 if (__gthread_active_p ())
107 return __gthrw_(pthread_rwlock_timedwrlock) (__rwlock, __ts);
114 __glibcxx_rwlock_rdlock (pthread_rwlock_t *__rwlock)
115 { return pthread_rwlock_rdlock (__rwlock); }
117 __glibcxx_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
118 { return pthread_rwlock_tryrdlock (__rwlock); }
120 __glibcxx_rwlock_wrlock (pthread_rwlock_t *__rwlock)
121 { return pthread_rwlock_wrlock (__rwlock); }
123 __glibcxx_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
124 { return pthread_rwlock_trywrlock (__rwlock); }
126 __glibcxx_rwlock_unlock (pthread_rwlock_t *__rwlock)
127 { return pthread_rwlock_unlock (__rwlock); }
129 __glibcxx_rwlock_destroy(pthread_rwlock_t *__rwlock)
130 { return pthread_rwlock_destroy (__rwlock); }
132 __glibcxx_rwlock_init(pthread_rwlock_t *__rwlock)
133 { return pthread_rwlock_init (__rwlock, NULL); }
134 # if _GTHREAD_USE_MUTEX_TIMEDLOCK
136 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
137 const timespec *__ts)
138 { return pthread_rwlock_timedrdlock (__rwlock, __ts); }
140 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
141 const timespec *__ts)
142 { return pthread_rwlock_timedwrlock (__rwlock, __ts); }
146 /// A shared mutex type implemented using pthread_rwlock_t.
147 class __shared_mutex_pthread
149 friend class shared_timed_mutex;
151 #ifdef PTHREAD_RWLOCK_INITIALIZER
152 pthread_rwlock_t _M_rwlock = PTHREAD_RWLOCK_INITIALIZER;
155 __shared_mutex_pthread() = default;
156 ~__shared_mutex_pthread() = default;
158 pthread_rwlock_t _M_rwlock;
161 __shared_mutex_pthread()
163 int __ret = __glibcxx_rwlock_init(&_M_rwlock);
166 else if (__ret == EAGAIN)
167 __throw_system_error(int(errc::resource_unavailable_try_again));
168 else if (__ret == EPERM)
169 __throw_system_error(int(errc::operation_not_permitted));
170 // Errors not handled: EBUSY, EINVAL
171 __glibcxx_assert(__ret == 0);
174 ~__shared_mutex_pthread()
176 int __ret __attribute((__unused__)) = __glibcxx_rwlock_destroy(&_M_rwlock);
177 // Errors not handled: EBUSY, EINVAL
178 __glibcxx_assert(__ret == 0);
182 __shared_mutex_pthread(const __shared_mutex_pthread&) = delete;
183 __shared_mutex_pthread& operator=(const __shared_mutex_pthread&) = delete;
188 int __ret = __glibcxx_rwlock_wrlock(&_M_rwlock);
189 if (__ret == EDEADLK)
190 __throw_system_error(int(errc::resource_deadlock_would_occur));
191 // Errors not handled: EINVAL
192 __glibcxx_assert(__ret == 0);
198 int __ret = __glibcxx_rwlock_trywrlock(&_M_rwlock);
199 if (__ret == EBUSY) return false;
200 // Errors not handled: EINVAL
201 __glibcxx_assert(__ret == 0);
208 int __ret __attribute((__unused__)) = __glibcxx_rwlock_unlock(&_M_rwlock);
209 // Errors not handled: EPERM, EBUSY, EINVAL
210 __glibcxx_assert(__ret == 0);
219 // We retry if we exceeded the maximum number of read locks supported by
220 // the POSIX implementation; this can result in busy-waiting, but this
221 // is okay based on the current specification of forward progress
222 // guarantees by the standard.
224 __ret = __glibcxx_rwlock_rdlock(&_M_rwlock);
225 while (__ret == EAGAIN);
226 if (__ret == EDEADLK)
227 __throw_system_error(int(errc::resource_deadlock_would_occur));
228 // Errors not handled: EINVAL
229 __glibcxx_assert(__ret == 0);
235 int __ret = __glibcxx_rwlock_tryrdlock(&_M_rwlock);
236 // If the maximum number of read locks has been exceeded, we just fail
237 // to acquire the lock. Unlike for lock(), we are not allowed to throw
239 if (__ret == EBUSY || __ret == EAGAIN) return false;
240 // Errors not handled: EINVAL
241 __glibcxx_assert(__ret == 0);
251 void* native_handle() { return &_M_rwlock; }
255 #if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
256 /// A shared mutex type implemented using std::condition_variable.
257 class __shared_mutex_cv
259 friend class shared_timed_mutex;
261 // Based on Howard Hinnant's reference implementation from N2406.
263 // The high bit of _M_state is the write-entered flag which is set to
264 // indicate a writer has taken the lock or is queuing to take the lock.
265 // The remaining bits are the count of reader locks.
267 // To take a reader lock, block on gate1 while the write-entered flag is
268 // set or the maximum number of reader locks is held, then increment the
269 // reader lock count.
270 // To release, decrement the count, then if the write-entered flag is set
271 // and the count is zero then signal gate2 to wake a queued writer,
272 // otherwise if the maximum number of reader locks was held signal gate1
275 // To take a writer lock, block on gate1 while the write-entered flag is
276 // set, then set the write-entered flag to start queueing, then block on
277 // gate2 while the number of reader locks is non-zero.
278 // To release, unset the write-entered flag and signal gate1 to wake all
279 // blocked readers and writers.
281 // This means that when no reader locks are held readers and writers get
282 // equal priority. When one or more reader locks is held a writer gets
283 // priority and no more reader locks can be taken while the writer is
286 // Only locked when accessing _M_state or waiting on condition variables.
288 // Used to block while write-entered is set or reader count at maximum.
289 condition_variable _M_gate1;
290 // Used to block queued writers while reader count is non-zero.
291 condition_variable _M_gate2;
292 // The write-entered flag and reader count.
295 static constexpr unsigned _S_write_entered
296 = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
297 static constexpr unsigned _S_max_readers = ~_S_write_entered;
299 // Test whether the write-entered flag is set. _M_mut must be locked.
300 bool _M_write_entered() const { return _M_state & _S_write_entered; }
302 // The number of reader locks currently held. _M_mut must be locked.
303 unsigned _M_readers() const { return _M_state & _S_max_readers; }
306 __shared_mutex_cv() : _M_state(0) {}
310 __glibcxx_assert( _M_state == 0 );
313 __shared_mutex_cv(const __shared_mutex_cv&) = delete;
314 __shared_mutex_cv& operator=(const __shared_mutex_cv&) = delete;
316 // Exclusive ownership
321 unique_lock<mutex> __lk(_M_mut);
322 // Wait until we can set the write-entered flag.
323 _M_gate1.wait(__lk, [=]{ return !_M_write_entered(); });
324 _M_state |= _S_write_entered;
325 // Then wait until there are no more readers.
326 _M_gate2.wait(__lk, [=]{ return _M_readers() == 0; });
332 unique_lock<mutex> __lk(_M_mut, try_to_lock);
333 if (__lk.owns_lock() && _M_state == 0)
335 _M_state = _S_write_entered;
344 lock_guard<mutex> __lk(_M_mut);
345 __glibcxx_assert( _M_write_entered() );
347 // call notify_all() while mutex is held so that another thread can't
348 // lock and unlock the mutex then destroy *this before we make the call.
349 _M_gate1.notify_all();
357 unique_lock<mutex> __lk(_M_mut);
358 _M_gate1.wait(__lk, [=]{ return _M_state < _S_max_readers; });
365 unique_lock<mutex> __lk(_M_mut, try_to_lock);
366 if (!__lk.owns_lock())
368 if (_M_state < _S_max_readers)
379 lock_guard<mutex> __lk(_M_mut);
380 __glibcxx_assert( _M_readers() > 0 );
381 auto __prev = _M_state--;
382 if (_M_write_entered())
384 // Wake the queued writer if there are no more readers.
385 if (_M_readers() == 0)
386 _M_gate2.notify_one();
387 // No need to notify gate1 because we give priority to the queued
388 // writer, and that writer will eventually notify gate1 after it
389 // clears the write-entered flag.
393 // Wake any thread that was blocked on reader overflow.
394 if (__prev == _S_max_readers)
395 _M_gate1.notify_one();
402 #if __cplusplus > 201402L
403 /// The standard shared mutex type.
407 shared_mutex() = default;
408 ~shared_mutex() = default;
410 shared_mutex(const shared_mutex&) = delete;
411 shared_mutex& operator=(const shared_mutex&) = delete;
413 // Exclusive ownership
415 void lock() { _M_impl.lock(); }
416 bool try_lock() { return _M_impl.try_lock(); }
417 void unlock() { _M_impl.unlock(); }
421 void lock_shared() { _M_impl.lock_shared(); }
422 bool try_lock_shared() { return _M_impl.try_lock_shared(); }
423 void unlock_shared() { _M_impl.unlock_shared(); }
425 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
426 typedef void* native_handle_type;
427 native_handle_type native_handle() { return _M_impl.native_handle(); }
430 __shared_mutex_pthread _M_impl;
433 __shared_mutex_cv _M_impl;
438 /// @cond undocumented
439 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
440 using __shared_timed_mutex_base = __shared_mutex_pthread;
442 using __shared_timed_mutex_base = __shared_mutex_cv;
446 /// The standard shared timed mutex type.
447 class shared_timed_mutex
448 : private __shared_timed_mutex_base
450 using _Base = __shared_timed_mutex_base;
452 // Must use the same clock as condition_variable for __shared_mutex_cv.
453 #ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
454 using __clock_t = chrono::steady_clock;
456 using __clock_t = chrono::system_clock;
460 shared_timed_mutex() = default;
461 ~shared_timed_mutex() = default;
463 shared_timed_mutex(const shared_timed_mutex&) = delete;
464 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
466 // Exclusive ownership
468 void lock() { _Base::lock(); }
469 bool try_lock() { return _Base::try_lock(); }
470 void unlock() { _Base::unlock(); }
472 template<typename _Rep, typename _Period>
474 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
476 auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
477 if (ratio_greater<__clock_t::period, _Period>())
479 return try_lock_until(__clock_t::now() + __rt);
484 void lock_shared() { _Base::lock_shared(); }
485 bool try_lock_shared() { return _Base::try_lock_shared(); }
486 void unlock_shared() { _Base::unlock_shared(); }
488 template<typename _Rep, typename _Period>
490 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rtime)
492 auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
493 if (ratio_greater<__clock_t::period, _Period>())
495 return try_lock_shared_until(__clock_t::now() + __rt);
498 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
500 // Exclusive ownership
502 template<typename _Duration>
504 try_lock_until(const chrono::time_point<chrono::system_clock,
507 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
508 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
510 __gthread_time_t __ts =
512 static_cast<std::time_t>(__s.time_since_epoch().count()),
513 static_cast<long>(__ns.count())
516 int __ret = __glibcxx_rwlock_timedwrlock(&_M_rwlock, &__ts);
517 // On self-deadlock, we just fail to acquire the lock. Technically,
518 // the program violated the precondition.
519 if (__ret == ETIMEDOUT || __ret == EDEADLK)
521 // Errors not handled: EINVAL
522 __glibcxx_assert(__ret == 0);
526 #ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
527 template<typename _Duration>
529 try_lock_until(const chrono::time_point<chrono::steady_clock,
532 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
533 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
535 __gthread_time_t __ts =
537 static_cast<std::time_t>(__s.time_since_epoch().count()),
538 static_cast<long>(__ns.count())
541 int __ret = pthread_rwlock_clockwrlock(&_M_rwlock, CLOCK_MONOTONIC,
543 // On self-deadlock, we just fail to acquire the lock. Technically,
544 // the program violated the precondition.
545 if (__ret == ETIMEDOUT || __ret == EDEADLK)
547 // Errors not handled: EINVAL
548 __glibcxx_assert(__ret == 0);
553 template<typename _Clock, typename _Duration>
555 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
557 #if __cplusplus > 201703L
558 static_assert(chrono::is_clock_v<_Clock>);
560 // The user-supplied clock may not tick at the same rate as
561 // steady_clock, so we must loop in order to guarantee that
562 // the timeout has expired before returning false.
563 typename _Clock::time_point __now = _Clock::now();
565 auto __rtime = __atime - __now;
566 if (try_lock_for(__rtime))
568 __now = _Clock::now();
569 } while (__atime > __now);
575 template<typename _Duration>
577 try_lock_shared_until(const chrono::time_point<chrono::system_clock,
580 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
581 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
583 __gthread_time_t __ts =
585 static_cast<std::time_t>(__s.time_since_epoch().count()),
586 static_cast<long>(__ns.count())
590 // Unlike for lock(), we are not allowed to throw an exception so if
591 // the maximum number of read locks has been exceeded, or we would
592 // deadlock, we just try to acquire the lock again (and will time out
594 // In cases where we would exceed the maximum number of read locks
595 // throughout the whole time until the timeout, we will fail to
596 // acquire the lock even if it would be logically free; however, this
597 // is allowed by the standard, and we made a "strong effort"
598 // (see C++14 30.4.1.4p26).
599 // For cases where the implementation detects a deadlock we
600 // intentionally block and timeout so that an early return isn't
601 // mistaken for a spurious failure, which might help users realise
602 // there is a deadlock.
604 __ret = __glibcxx_rwlock_timedrdlock(&_M_rwlock, &__ts);
605 while (__ret == EAGAIN || __ret == EDEADLK);
606 if (__ret == ETIMEDOUT)
608 // Errors not handled: EINVAL
609 __glibcxx_assert(__ret == 0);
613 #ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
614 template<typename _Duration>
616 try_lock_shared_until(const chrono::time_point<chrono::steady_clock,
619 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
620 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
622 __gthread_time_t __ts =
624 static_cast<std::time_t>(__s.time_since_epoch().count()),
625 static_cast<long>(__ns.count())
628 int __ret = pthread_rwlock_clockrdlock(&_M_rwlock, CLOCK_MONOTONIC,
630 // On self-deadlock, we just fail to acquire the lock. Technically,
631 // the program violated the precondition.
632 if (__ret == ETIMEDOUT || __ret == EDEADLK)
634 // Errors not handled: EINVAL
635 __glibcxx_assert(__ret == 0);
640 template<typename _Clock, typename _Duration>
642 try_lock_shared_until(const chrono::time_point<_Clock,
645 #if __cplusplus > 201703L
646 static_assert(chrono::is_clock_v<_Clock>);
648 // The user-supplied clock may not tick at the same rate as
649 // steady_clock, so we must loop in order to guarantee that
650 // the timeout has expired before returning false.
651 typename _Clock::time_point __now = _Clock::now();
653 auto __rtime = __atime - __now;
654 if (try_lock_shared_for(__rtime))
656 __now = _Clock::now();
657 } while (__atime > __now);
661 #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
663 // Exclusive ownership
665 template<typename _Clock, typename _Duration>
667 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
669 unique_lock<mutex> __lk(_M_mut);
670 if (!_M_gate1.wait_until(__lk, __abs_time,
671 [=]{ return !_M_write_entered(); }))
675 _M_state |= _S_write_entered;
676 if (!_M_gate2.wait_until(__lk, __abs_time,
677 [=]{ return _M_readers() == 0; }))
679 _M_state ^= _S_write_entered;
680 // Wake all threads blocked while the write-entered flag was set.
681 _M_gate1.notify_all();
689 template <typename _Clock, typename _Duration>
691 try_lock_shared_until(const chrono::time_point<_Clock,
692 _Duration>& __abs_time)
694 unique_lock<mutex> __lk(_M_mut);
695 if (!_M_gate1.wait_until(__lk, __abs_time,
696 [=]{ return _M_state < _S_max_readers; }))
704 #endif // _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
706 #endif // _GLIBCXX_HAS_GTHREADS
709 template<typename _Mutex>
713 typedef _Mutex mutex_type;
717 shared_lock() noexcept : _M_pm(nullptr), _M_owns(false) { }
720 shared_lock(mutex_type& __m)
721 : _M_pm(std::__addressof(__m)), _M_owns(true)
722 { __m.lock_shared(); }
724 shared_lock(mutex_type& __m, defer_lock_t) noexcept
725 : _M_pm(std::__addressof(__m)), _M_owns(false) { }
727 shared_lock(mutex_type& __m, try_to_lock_t)
728 : _M_pm(std::__addressof(__m)), _M_owns(__m.try_lock_shared()) { }
730 shared_lock(mutex_type& __m, adopt_lock_t)
731 : _M_pm(std::__addressof(__m)), _M_owns(true) { }
733 template<typename _Clock, typename _Duration>
734 shared_lock(mutex_type& __m,
735 const chrono::time_point<_Clock, _Duration>& __abs_time)
736 : _M_pm(std::__addressof(__m)),
737 _M_owns(__m.try_lock_shared_until(__abs_time)) { }
739 template<typename _Rep, typename _Period>
740 shared_lock(mutex_type& __m,
741 const chrono::duration<_Rep, _Period>& __rel_time)
742 : _M_pm(std::__addressof(__m)),
743 _M_owns(__m.try_lock_shared_for(__rel_time)) { }
748 _M_pm->unlock_shared();
751 shared_lock(shared_lock const&) = delete;
752 shared_lock& operator=(shared_lock const&) = delete;
754 shared_lock(shared_lock&& __sl) noexcept : shared_lock()
758 operator=(shared_lock&& __sl) noexcept
760 shared_lock(std::move(__sl)).swap(*this);
768 _M_pm->lock_shared();
776 return _M_owns = _M_pm->try_lock_shared();
779 template<typename _Rep, typename _Period>
781 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
784 return _M_owns = _M_pm->try_lock_shared_for(__rel_time);
787 template<typename _Clock, typename _Duration>
789 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
792 return _M_owns = _M_pm->try_lock_shared_until(__abs_time);
799 __throw_system_error(int(errc::resource_deadlock_would_occur));
800 _M_pm->unlock_shared();
807 swap(shared_lock& __u) noexcept
809 std::swap(_M_pm, __u._M_pm);
810 std::swap(_M_owns, __u._M_owns);
817 return std::exchange(_M_pm, nullptr);
822 bool owns_lock() const noexcept { return _M_owns; }
824 explicit operator bool() const noexcept { return _M_owns; }
826 mutex_type* mutex() const noexcept { return _M_pm; }
832 if (_M_pm == nullptr)
833 __throw_system_error(int(errc::operation_not_permitted));
835 __throw_system_error(int(errc::resource_deadlock_would_occur));
842 /// Swap specialization for shared_lock
843 /// @relates shared_mutex
844 template<typename _Mutex>
846 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) noexcept
850 _GLIBCXX_END_NAMESPACE_VERSION
855 #endif // _GLIBCXX_SHARED_MUTEX