1 // <shared_mutex> -*- C++ -*-
3 // Copyright (C) 2013-2019 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 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
61 #define _GLIBCXX_GTHRW(name) \
62 __gthrw(pthread_ ## name); \
64 __glibcxx_ ## name (pthread_rwlock_t *__rwlock) \
66 if (__gthread_active_p ()) \
67 return __gthrw_(pthread_ ## name) (__rwlock); \
71 _GLIBCXX_GTHRW(rwlock_rdlock)
72 _GLIBCXX_GTHRW(rwlock_tryrdlock)
73 _GLIBCXX_GTHRW(rwlock_wrlock)
74 _GLIBCXX_GTHRW(rwlock_trywrlock)
75 _GLIBCXX_GTHRW(rwlock_unlock)
76 # ifndef PTHREAD_RWLOCK_INITIALIZER
77 _GLIBCXX_GTHRW(rwlock_destroy)
78 __gthrw(pthread_rwlock_init);
80 __glibcxx_rwlock_init (pthread_rwlock_t *__rwlock)
82 if (__gthread_active_p ())
83 return __gthrw_(pthread_rwlock_init) (__rwlock, NULL);
88 # if _GTHREAD_USE_MUTEX_TIMEDLOCK
89 __gthrw(pthread_rwlock_timedrdlock);
91 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
94 if (__gthread_active_p ())
95 return __gthrw_(pthread_rwlock_timedrdlock) (__rwlock, __ts);
99 __gthrw(pthread_rwlock_timedwrlock);
101 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
102 const timespec *__ts)
104 if (__gthread_active_p ())
105 return __gthrw_(pthread_rwlock_timedwrlock) (__rwlock, __ts);
112 __glibcxx_rwlock_rdlock (pthread_rwlock_t *__rwlock)
113 { return pthread_rwlock_rdlock (__rwlock); }
115 __glibcxx_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
116 { return pthread_rwlock_tryrdlock (__rwlock); }
118 __glibcxx_rwlock_wrlock (pthread_rwlock_t *__rwlock)
119 { return pthread_rwlock_wrlock (__rwlock); }
121 __glibcxx_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
122 { return pthread_rwlock_trywrlock (__rwlock); }
124 __glibcxx_rwlock_unlock (pthread_rwlock_t *__rwlock)
125 { return pthread_rwlock_unlock (__rwlock); }
127 __glibcxx_rwlock_destroy(pthread_rwlock_t *__rwlock)
128 { return pthread_rwlock_destroy (__rwlock); }
130 __glibcxx_rwlock_init(pthread_rwlock_t *__rwlock)
131 { return pthread_rwlock_init (__rwlock, NULL); }
132 # if _GTHREAD_USE_MUTEX_TIMEDLOCK
134 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
135 const timespec *__ts)
136 { return pthread_rwlock_timedrdlock (__rwlock, __ts); }
138 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
139 const timespec *__ts)
140 { return pthread_rwlock_timedwrlock (__rwlock, __ts); }
144 /// A shared mutex type implemented using pthread_rwlock_t.
145 class __shared_mutex_pthread
147 friend class shared_timed_mutex;
149 #ifdef PTHREAD_RWLOCK_INITIALIZER
150 pthread_rwlock_t _M_rwlock = PTHREAD_RWLOCK_INITIALIZER;
153 __shared_mutex_pthread() = default;
154 ~__shared_mutex_pthread() = default;
156 pthread_rwlock_t _M_rwlock;
159 __shared_mutex_pthread()
161 int __ret = __glibcxx_rwlock_init(&_M_rwlock);
164 else if (__ret == EAGAIN)
165 __throw_system_error(int(errc::resource_unavailable_try_again));
166 else if (__ret == EPERM)
167 __throw_system_error(int(errc::operation_not_permitted));
168 // Errors not handled: EBUSY, EINVAL
169 __glibcxx_assert(__ret == 0);
172 ~__shared_mutex_pthread()
174 int __ret __attribute((__unused__)) = __glibcxx_rwlock_destroy(&_M_rwlock);
175 // Errors not handled: EBUSY, EINVAL
176 __glibcxx_assert(__ret == 0);
180 __shared_mutex_pthread(const __shared_mutex_pthread&) = delete;
181 __shared_mutex_pthread& operator=(const __shared_mutex_pthread&) = delete;
186 int __ret = __glibcxx_rwlock_wrlock(&_M_rwlock);
187 if (__ret == EDEADLK)
188 __throw_system_error(int(errc::resource_deadlock_would_occur));
189 // Errors not handled: EINVAL
190 __glibcxx_assert(__ret == 0);
196 int __ret = __glibcxx_rwlock_trywrlock(&_M_rwlock);
197 if (__ret == EBUSY) return false;
198 // Errors not handled: EINVAL
199 __glibcxx_assert(__ret == 0);
206 int __ret __attribute((__unused__)) = __glibcxx_rwlock_unlock(&_M_rwlock);
207 // Errors not handled: EPERM, EBUSY, EINVAL
208 __glibcxx_assert(__ret == 0);
217 // We retry if we exceeded the maximum number of read locks supported by
218 // the POSIX implementation; this can result in busy-waiting, but this
219 // is okay based on the current specification of forward progress
220 // guarantees by the standard.
222 __ret = __glibcxx_rwlock_rdlock(&_M_rwlock);
223 while (__ret == EAGAIN);
224 if (__ret == EDEADLK)
225 __throw_system_error(int(errc::resource_deadlock_would_occur));
226 // Errors not handled: EINVAL
227 __glibcxx_assert(__ret == 0);
233 int __ret = __glibcxx_rwlock_tryrdlock(&_M_rwlock);
234 // If the maximum number of read locks has been exceeded, we just fail
235 // to acquire the lock. Unlike for lock(), we are not allowed to throw
237 if (__ret == EBUSY || __ret == EAGAIN) return false;
238 // Errors not handled: EINVAL
239 __glibcxx_assert(__ret == 0);
249 void* native_handle() { return &_M_rwlock; }
253 #if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
254 /// A shared mutex type implemented using std::condition_variable.
255 class __shared_mutex_cv
257 friend class shared_timed_mutex;
259 // Based on Howard Hinnant's reference implementation from N2406.
261 // The high bit of _M_state is the write-entered flag which is set to
262 // indicate a writer has taken the lock or is queuing to take the lock.
263 // The remaining bits are the count of reader locks.
265 // To take a reader lock, block on gate1 while the write-entered flag is
266 // set or the maximum number of reader locks is held, then increment the
267 // reader lock count.
268 // To release, decrement the count, then if the write-entered flag is set
269 // and the count is zero then signal gate2 to wake a queued writer,
270 // otherwise if the maximum number of reader locks was held signal gate1
273 // To take a writer lock, block on gate1 while the write-entered flag is
274 // set, then set the write-entered flag to start queueing, then block on
275 // gate2 while the number of reader locks is non-zero.
276 // To release, unset the write-entered flag and signal gate1 to wake all
277 // blocked readers and writers.
279 // This means that when no reader locks are held readers and writers get
280 // equal priority. When one or more reader locks is held a writer gets
281 // priority and no more reader locks can be taken while the writer is
284 // Only locked when accessing _M_state or waiting on condition variables.
286 // Used to block while write-entered is set or reader count at maximum.
287 condition_variable _M_gate1;
288 // Used to block queued writers while reader count is non-zero.
289 condition_variable _M_gate2;
290 // The write-entered flag and reader count.
293 static constexpr unsigned _S_write_entered
294 = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
295 static constexpr unsigned _S_max_readers = ~_S_write_entered;
297 // Test whether the write-entered flag is set. _M_mut must be locked.
298 bool _M_write_entered() const { return _M_state & _S_write_entered; }
300 // The number of reader locks currently held. _M_mut must be locked.
301 unsigned _M_readers() const { return _M_state & _S_max_readers; }
304 __shared_mutex_cv() : _M_state(0) {}
308 __glibcxx_assert( _M_state == 0 );
311 __shared_mutex_cv(const __shared_mutex_cv&) = delete;
312 __shared_mutex_cv& operator=(const __shared_mutex_cv&) = delete;
314 // Exclusive ownership
319 unique_lock<mutex> __lk(_M_mut);
320 // Wait until we can set the write-entered flag.
321 _M_gate1.wait(__lk, [=]{ return !_M_write_entered(); });
322 _M_state |= _S_write_entered;
323 // Then wait until there are no more readers.
324 _M_gate2.wait(__lk, [=]{ return _M_readers() == 0; });
330 unique_lock<mutex> __lk(_M_mut, try_to_lock);
331 if (__lk.owns_lock() && _M_state == 0)
333 _M_state = _S_write_entered;
342 lock_guard<mutex> __lk(_M_mut);
343 __glibcxx_assert( _M_write_entered() );
345 // call notify_all() while mutex is held so that another thread can't
346 // lock and unlock the mutex then destroy *this before we make the call.
347 _M_gate1.notify_all();
355 unique_lock<mutex> __lk(_M_mut);
356 _M_gate1.wait(__lk, [=]{ return _M_state < _S_max_readers; });
363 unique_lock<mutex> __lk(_M_mut, try_to_lock);
364 if (!__lk.owns_lock())
366 if (_M_state < _S_max_readers)
377 lock_guard<mutex> __lk(_M_mut);
378 __glibcxx_assert( _M_readers() > 0 );
379 auto __prev = _M_state--;
380 if (_M_write_entered())
382 // Wake the queued writer if there are no more readers.
383 if (_M_readers() == 0)
384 _M_gate2.notify_one();
385 // No need to notify gate1 because we give priority to the queued
386 // writer, and that writer will eventually notify gate1 after it
387 // clears the write-entered flag.
391 // Wake any thread that was blocked on reader overflow.
392 if (__prev == _S_max_readers)
393 _M_gate1.notify_one();
399 #if __cplusplus > 201402L
400 /// The standard shared mutex type.
404 shared_mutex() = default;
405 ~shared_mutex() = default;
407 shared_mutex(const shared_mutex&) = delete;
408 shared_mutex& operator=(const shared_mutex&) = delete;
410 // Exclusive ownership
412 void lock() { _M_impl.lock(); }
413 bool try_lock() { return _M_impl.try_lock(); }
414 void unlock() { _M_impl.unlock(); }
418 void lock_shared() { _M_impl.lock_shared(); }
419 bool try_lock_shared() { return _M_impl.try_lock_shared(); }
420 void unlock_shared() { _M_impl.unlock_shared(); }
422 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
423 typedef void* native_handle_type;
424 native_handle_type native_handle() { return _M_impl.native_handle(); }
427 __shared_mutex_pthread _M_impl;
430 __shared_mutex_cv _M_impl;
435 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
436 using __shared_timed_mutex_base = __shared_mutex_pthread;
438 using __shared_timed_mutex_base = __shared_mutex_cv;
441 /// The standard shared timed mutex type.
442 class shared_timed_mutex
443 : private __shared_timed_mutex_base
445 using _Base = __shared_timed_mutex_base;
447 // Must use the same clock as condition_variable for __shared_mutex_cv.
448 typedef chrono::system_clock __clock_t;
451 shared_timed_mutex() = default;
452 ~shared_timed_mutex() = default;
454 shared_timed_mutex(const shared_timed_mutex&) = delete;
455 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
457 // Exclusive ownership
459 void lock() { _Base::lock(); }
460 bool try_lock() { return _Base::try_lock(); }
461 void unlock() { _Base::unlock(); }
463 template<typename _Rep, typename _Period>
465 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
467 return try_lock_until(__clock_t::now() + __rel_time);
472 void lock_shared() { _Base::lock_shared(); }
473 bool try_lock_shared() { return _Base::try_lock_shared(); }
474 void unlock_shared() { _Base::unlock_shared(); }
476 template<typename _Rep, typename _Period>
478 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
480 return try_lock_shared_until(__clock_t::now() + __rel_time);
483 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
485 // Exclusive ownership
487 template<typename _Duration>
489 try_lock_until(const chrono::time_point<__clock_t, _Duration>& __atime)
491 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
492 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
494 __gthread_time_t __ts =
496 static_cast<std::time_t>(__s.time_since_epoch().count()),
497 static_cast<long>(__ns.count())
500 int __ret = __glibcxx_rwlock_timedwrlock(&_M_rwlock, &__ts);
501 // On self-deadlock, we just fail to acquire the lock. Technically,
502 // the program violated the precondition.
503 if (__ret == ETIMEDOUT || __ret == EDEADLK)
505 // Errors not handled: EINVAL
506 __glibcxx_assert(__ret == 0);
510 template<typename _Clock, typename _Duration>
512 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
514 // DR 887 - Sync unknown clock to known clock.
515 const typename _Clock::time_point __c_entry = _Clock::now();
516 const __clock_t::time_point __s_entry = __clock_t::now();
517 const auto __delta = __abs_time - __c_entry;
518 const auto __s_atime = __s_entry + __delta;
519 return try_lock_until(__s_atime);
524 template<typename _Duration>
526 try_lock_shared_until(const chrono::time_point<__clock_t,
529 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
530 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
532 __gthread_time_t __ts =
534 static_cast<std::time_t>(__s.time_since_epoch().count()),
535 static_cast<long>(__ns.count())
539 // Unlike for lock(), we are not allowed to throw an exception so if
540 // the maximum number of read locks has been exceeded, or we would
541 // deadlock, we just try to acquire the lock again (and will time out
543 // In cases where we would exceed the maximum number of read locks
544 // throughout the whole time until the timeout, we will fail to
545 // acquire the lock even if it would be logically free; however, this
546 // is allowed by the standard, and we made a "strong effort"
547 // (see C++14 30.4.1.4p26).
548 // For cases where the implementation detects a deadlock we
549 // intentionally block and timeout so that an early return isn't
550 // mistaken for a spurious failure, which might help users realise
551 // there is a deadlock.
553 __ret = __glibcxx_rwlock_timedrdlock(&_M_rwlock, &__ts);
554 while (__ret == EAGAIN || __ret == EDEADLK);
555 if (__ret == ETIMEDOUT)
557 // Errors not handled: EINVAL
558 __glibcxx_assert(__ret == 0);
562 template<typename _Clock, typename _Duration>
564 try_lock_shared_until(const chrono::time_point<_Clock,
565 _Duration>& __abs_time)
567 // DR 887 - Sync unknown clock to known clock.
568 const typename _Clock::time_point __c_entry = _Clock::now();
569 const __clock_t::time_point __s_entry = __clock_t::now();
570 const auto __delta = __abs_time - __c_entry;
571 const auto __s_atime = __s_entry + __delta;
572 return try_lock_shared_until(__s_atime);
575 #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
577 // Exclusive ownership
579 template<typename _Clock, typename _Duration>
581 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
583 unique_lock<mutex> __lk(_M_mut);
584 if (!_M_gate1.wait_until(__lk, __abs_time,
585 [=]{ return !_M_write_entered(); }))
589 _M_state |= _S_write_entered;
590 if (!_M_gate2.wait_until(__lk, __abs_time,
591 [=]{ return _M_readers() == 0; }))
593 _M_state ^= _S_write_entered;
594 // Wake all threads blocked while the write-entered flag was set.
595 _M_gate1.notify_all();
603 template <typename _Clock, typename _Duration>
605 try_lock_shared_until(const chrono::time_point<_Clock,
606 _Duration>& __abs_time)
608 unique_lock<mutex> __lk(_M_mut);
609 if (!_M_gate1.wait_until(__lk, __abs_time,
610 [=]{ return _M_state < _S_max_readers; }))
618 #endif // _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
620 #endif // _GLIBCXX_HAS_GTHREADS
623 template<typename _Mutex>
627 typedef _Mutex mutex_type;
631 shared_lock() noexcept : _M_pm(nullptr), _M_owns(false) { }
634 shared_lock(mutex_type& __m)
635 : _M_pm(std::__addressof(__m)), _M_owns(true)
636 { __m.lock_shared(); }
638 shared_lock(mutex_type& __m, defer_lock_t) noexcept
639 : _M_pm(std::__addressof(__m)), _M_owns(false) { }
641 shared_lock(mutex_type& __m, try_to_lock_t)
642 : _M_pm(std::__addressof(__m)), _M_owns(__m.try_lock_shared()) { }
644 shared_lock(mutex_type& __m, adopt_lock_t)
645 : _M_pm(std::__addressof(__m)), _M_owns(true) { }
647 template<typename _Clock, typename _Duration>
648 shared_lock(mutex_type& __m,
649 const chrono::time_point<_Clock, _Duration>& __abs_time)
650 : _M_pm(std::__addressof(__m)),
651 _M_owns(__m.try_lock_shared_until(__abs_time)) { }
653 template<typename _Rep, typename _Period>
654 shared_lock(mutex_type& __m,
655 const chrono::duration<_Rep, _Period>& __rel_time)
656 : _M_pm(std::__addressof(__m)),
657 _M_owns(__m.try_lock_shared_for(__rel_time)) { }
662 _M_pm->unlock_shared();
665 shared_lock(shared_lock const&) = delete;
666 shared_lock& operator=(shared_lock const&) = delete;
668 shared_lock(shared_lock&& __sl) noexcept : shared_lock()
672 operator=(shared_lock&& __sl) noexcept
674 shared_lock(std::move(__sl)).swap(*this);
682 _M_pm->lock_shared();
690 return _M_owns = _M_pm->try_lock_shared();
693 template<typename _Rep, typename _Period>
695 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
698 return _M_owns = _M_pm->try_lock_shared_for(__rel_time);
701 template<typename _Clock, typename _Duration>
703 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
706 return _M_owns = _M_pm->try_lock_shared_until(__abs_time);
713 __throw_system_error(int(errc::resource_deadlock_would_occur));
714 _M_pm->unlock_shared();
721 swap(shared_lock& __u) noexcept
723 std::swap(_M_pm, __u._M_pm);
724 std::swap(_M_owns, __u._M_owns);
731 return std::exchange(_M_pm, nullptr);
736 bool owns_lock() const noexcept { return _M_owns; }
738 explicit operator bool() const noexcept { return _M_owns; }
740 mutex_type* mutex() const noexcept { return _M_pm; }
746 if (_M_pm == nullptr)
747 __throw_system_error(int(errc::operation_not_permitted));
749 __throw_system_error(int(errc::resource_deadlock_would_occur));
756 /// Swap specialization for shared_lock
757 template<typename _Mutex>
759 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) noexcept
763 _GLIBCXX_END_NAMESPACE_VERSION
768 #endif // _GLIBCXX_SHARED_MUTEX