1 // <shared_mutex> -*- C++ -*-
3 // Copyright (C) 2013-2018 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_USE_C99_STDINT_TR1
50 #ifdef _GLIBCXX_HAS_GTHREADS
52 #if __cplusplus >= 201703L
53 #define __cpp_lib_shared_mutex 201505
57 #define __cpp_lib_shared_timed_mutex 201402
58 class shared_timed_mutex;
60 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
61 /// A shared mutex type implemented using pthread_rwlock_t.
62 class __shared_mutex_pthread
64 friend class shared_timed_mutex;
66 #ifdef PTHREAD_RWLOCK_INITIALIZER
67 pthread_rwlock_t _M_rwlock = PTHREAD_RWLOCK_INITIALIZER;
70 __shared_mutex_pthread() = default;
71 ~__shared_mutex_pthread() = default;
73 pthread_rwlock_t _M_rwlock;
76 __shared_mutex_pthread()
78 int __ret = pthread_rwlock_init(&_M_rwlock, NULL);
81 else if (__ret == EAGAIN)
82 __throw_system_error(int(errc::resource_unavailable_try_again));
83 else if (__ret == EPERM)
84 __throw_system_error(int(errc::operation_not_permitted));
85 // Errors not handled: EBUSY, EINVAL
86 __glibcxx_assert(__ret == 0);
89 ~__shared_mutex_pthread()
91 int __ret __attribute((__unused__)) = pthread_rwlock_destroy(&_M_rwlock);
92 // Errors not handled: EBUSY, EINVAL
93 __glibcxx_assert(__ret == 0);
97 __shared_mutex_pthread(const __shared_mutex_pthread&) = delete;
98 __shared_mutex_pthread& operator=(const __shared_mutex_pthread&) = delete;
103 int __ret = pthread_rwlock_wrlock(&_M_rwlock);
104 if (__ret == EDEADLK)
105 __throw_system_error(int(errc::resource_deadlock_would_occur));
106 // Errors not handled: EINVAL
107 __glibcxx_assert(__ret == 0);
113 int __ret = pthread_rwlock_trywrlock(&_M_rwlock);
114 if (__ret == EBUSY) return false;
115 // Errors not handled: EINVAL
116 __glibcxx_assert(__ret == 0);
123 int __ret __attribute((__unused__)) = pthread_rwlock_unlock(&_M_rwlock);
124 // Errors not handled: EPERM, EBUSY, EINVAL
125 __glibcxx_assert(__ret == 0);
134 // We retry if we exceeded the maximum number of read locks supported by
135 // the POSIX implementation; this can result in busy-waiting, but this
136 // is okay based on the current specification of forward progress
137 // guarantees by the standard.
139 __ret = pthread_rwlock_rdlock(&_M_rwlock);
140 while (__ret == EAGAIN);
141 if (__ret == EDEADLK)
142 __throw_system_error(int(errc::resource_deadlock_would_occur));
143 // Errors not handled: EINVAL
144 __glibcxx_assert(__ret == 0);
150 int __ret = pthread_rwlock_tryrdlock(&_M_rwlock);
151 // If the maximum number of read locks has been exceeded, we just fail
152 // to acquire the lock. Unlike for lock(), we are not allowed to throw
154 if (__ret == EBUSY || __ret == EAGAIN) return false;
155 // Errors not handled: EINVAL
156 __glibcxx_assert(__ret == 0);
166 void* native_handle() { return &_M_rwlock; }
170 #if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
171 /// A shared mutex type implemented using std::condition_variable.
172 class __shared_mutex_cv
174 friend class shared_timed_mutex;
176 // Based on Howard Hinnant's reference implementation from N2406.
178 // The high bit of _M_state is the write-entered flag which is set to
179 // indicate a writer has taken the lock or is queuing to take the lock.
180 // The remaining bits are the count of reader locks.
182 // To take a reader lock, block on gate1 while the write-entered flag is
183 // set or the maximum number of reader locks is held, then increment the
184 // reader lock count.
185 // To release, decrement the count, then if the write-entered flag is set
186 // and the count is zero then signal gate2 to wake a queued writer,
187 // otherwise if the maximum number of reader locks was held signal gate1
190 // To take a writer lock, block on gate1 while the write-entered flag is
191 // set, then set the write-entered flag to start queueing, then block on
192 // gate2 while the number of reader locks is non-zero.
193 // To release, unset the write-entered flag and signal gate1 to wake all
194 // blocked readers and writers.
196 // This means that when no reader locks are held readers and writers get
197 // equal priority. When one or more reader locks is held a writer gets
198 // priority and no more reader locks can be taken while the writer is
201 // Only locked when accessing _M_state or waiting on condition variables.
203 // Used to block while write-entered is set or reader count at maximum.
204 condition_variable _M_gate1;
205 // Used to block queued writers while reader count is non-zero.
206 condition_variable _M_gate2;
207 // The write-entered flag and reader count.
210 static constexpr unsigned _S_write_entered
211 = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
212 static constexpr unsigned _S_max_readers = ~_S_write_entered;
214 // Test whether the write-entered flag is set. _M_mut must be locked.
215 bool _M_write_entered() const { return _M_state & _S_write_entered; }
217 // The number of reader locks currently held. _M_mut must be locked.
218 unsigned _M_readers() const { return _M_state & _S_max_readers; }
221 __shared_mutex_cv() : _M_state(0) {}
225 __glibcxx_assert( _M_state == 0 );
228 __shared_mutex_cv(const __shared_mutex_cv&) = delete;
229 __shared_mutex_cv& operator=(const __shared_mutex_cv&) = delete;
231 // Exclusive ownership
236 unique_lock<mutex> __lk(_M_mut);
237 // Wait until we can set the write-entered flag.
238 _M_gate1.wait(__lk, [=]{ return !_M_write_entered(); });
239 _M_state |= _S_write_entered;
240 // Then wait until there are no more readers.
241 _M_gate2.wait(__lk, [=]{ return _M_readers() == 0; });
247 unique_lock<mutex> __lk(_M_mut, try_to_lock);
248 if (__lk.owns_lock() && _M_state == 0)
250 _M_state = _S_write_entered;
259 lock_guard<mutex> __lk(_M_mut);
260 __glibcxx_assert( _M_write_entered() );
262 // call notify_all() while mutex is held so that another thread can't
263 // lock and unlock the mutex then destroy *this before we make the call.
264 _M_gate1.notify_all();
272 unique_lock<mutex> __lk(_M_mut);
273 _M_gate1.wait(__lk, [=]{ return _M_state < _S_max_readers; });
280 unique_lock<mutex> __lk(_M_mut, try_to_lock);
281 if (!__lk.owns_lock())
283 if (_M_state < _S_max_readers)
294 lock_guard<mutex> __lk(_M_mut);
295 __glibcxx_assert( _M_readers() > 0 );
296 auto __prev = _M_state--;
297 if (_M_write_entered())
299 // Wake the queued writer if there are no more readers.
300 if (_M_readers() == 0)
301 _M_gate2.notify_one();
302 // No need to notify gate1 because we give priority to the queued
303 // writer, and that writer will eventually notify gate1 after it
304 // clears the write-entered flag.
308 // Wake any thread that was blocked on reader overflow.
309 if (__prev == _S_max_readers)
310 _M_gate1.notify_one();
316 #if __cplusplus > 201402L
317 /// The standard shared mutex type.
321 shared_mutex() = default;
322 ~shared_mutex() = default;
324 shared_mutex(const shared_mutex&) = delete;
325 shared_mutex& operator=(const shared_mutex&) = delete;
327 // Exclusive ownership
329 void lock() { _M_impl.lock(); }
330 bool try_lock() { return _M_impl.try_lock(); }
331 void unlock() { _M_impl.unlock(); }
335 void lock_shared() { _M_impl.lock_shared(); }
336 bool try_lock_shared() { return _M_impl.try_lock_shared(); }
337 void unlock_shared() { _M_impl.unlock_shared(); }
339 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
340 typedef void* native_handle_type;
341 native_handle_type native_handle() { return _M_impl.native_handle(); }
344 __shared_mutex_pthread _M_impl;
347 __shared_mutex_cv _M_impl;
352 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
353 using __shared_timed_mutex_base = __shared_mutex_pthread;
355 using __shared_timed_mutex_base = __shared_mutex_cv;
358 /// The standard shared timed mutex type.
359 class shared_timed_mutex
360 : private __shared_timed_mutex_base
362 using _Base = __shared_timed_mutex_base;
364 // Must use the same clock as condition_variable for __shared_mutex_cv.
365 typedef chrono::system_clock __clock_t;
368 shared_timed_mutex() = default;
369 ~shared_timed_mutex() = default;
371 shared_timed_mutex(const shared_timed_mutex&) = delete;
372 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
374 // Exclusive ownership
376 void lock() { _Base::lock(); }
377 bool try_lock() { return _Base::try_lock(); }
378 void unlock() { _Base::unlock(); }
380 template<typename _Rep, typename _Period>
382 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
384 return try_lock_until(__clock_t::now() + __rel_time);
389 void lock_shared() { _Base::lock_shared(); }
390 bool try_lock_shared() { return _Base::try_lock_shared(); }
391 void unlock_shared() { _Base::unlock_shared(); }
393 template<typename _Rep, typename _Period>
395 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
397 return try_lock_shared_until(__clock_t::now() + __rel_time);
400 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
402 // Exclusive ownership
404 template<typename _Duration>
406 try_lock_until(const chrono::time_point<__clock_t, _Duration>& __atime)
408 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
409 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
411 __gthread_time_t __ts =
413 static_cast<std::time_t>(__s.time_since_epoch().count()),
414 static_cast<long>(__ns.count())
417 int __ret = pthread_rwlock_timedwrlock(&_M_rwlock, &__ts);
418 // On self-deadlock, we just fail to acquire the lock. Technically,
419 // the program violated the precondition.
420 if (__ret == ETIMEDOUT || __ret == EDEADLK)
422 // Errors not handled: EINVAL
423 __glibcxx_assert(__ret == 0);
427 template<typename _Clock, typename _Duration>
429 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
431 // DR 887 - Sync unknown clock to known clock.
432 const typename _Clock::time_point __c_entry = _Clock::now();
433 const __clock_t::time_point __s_entry = __clock_t::now();
434 const auto __delta = __abs_time - __c_entry;
435 const auto __s_atime = __s_entry + __delta;
436 return try_lock_until(__s_atime);
441 template<typename _Duration>
443 try_lock_shared_until(const chrono::time_point<__clock_t,
446 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
447 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
449 __gthread_time_t __ts =
451 static_cast<std::time_t>(__s.time_since_epoch().count()),
452 static_cast<long>(__ns.count())
456 // Unlike for lock(), we are not allowed to throw an exception so if
457 // the maximum number of read locks has been exceeded, or we would
458 // deadlock, we just try to acquire the lock again (and will time out
460 // In cases where we would exceed the maximum number of read locks
461 // throughout the whole time until the timeout, we will fail to
462 // acquire the lock even if it would be logically free; however, this
463 // is allowed by the standard, and we made a "strong effort"
464 // (see C++14 30.4.1.4p26).
465 // For cases where the implementation detects a deadlock we
466 // intentionally block and timeout so that an early return isn't
467 // mistaken for a spurious failure, which might help users realise
468 // there is a deadlock.
470 __ret = pthread_rwlock_timedrdlock(&_M_rwlock, &__ts);
471 while (__ret == EAGAIN || __ret == EDEADLK);
472 if (__ret == ETIMEDOUT)
474 // Errors not handled: EINVAL
475 __glibcxx_assert(__ret == 0);
479 template<typename _Clock, typename _Duration>
481 try_lock_shared_until(const chrono::time_point<_Clock,
482 _Duration>& __abs_time)
484 // DR 887 - Sync unknown clock to known clock.
485 const typename _Clock::time_point __c_entry = _Clock::now();
486 const __clock_t::time_point __s_entry = __clock_t::now();
487 const auto __delta = __abs_time - __c_entry;
488 const auto __s_atime = __s_entry + __delta;
489 return try_lock_shared_until(__s_atime);
492 #else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
494 // Exclusive ownership
496 template<typename _Clock, typename _Duration>
498 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
500 unique_lock<mutex> __lk(_M_mut);
501 if (!_M_gate1.wait_until(__lk, __abs_time,
502 [=]{ return !_M_write_entered(); }))
506 _M_state |= _S_write_entered;
507 if (!_M_gate2.wait_until(__lk, __abs_time,
508 [=]{ return _M_readers() == 0; }))
510 _M_state ^= _S_write_entered;
511 // Wake all threads blocked while the write-entered flag was set.
512 _M_gate1.notify_all();
520 template <typename _Clock, typename _Duration>
522 try_lock_shared_until(const chrono::time_point<_Clock,
523 _Duration>& __abs_time)
525 unique_lock<mutex> __lk(_M_mut);
526 if (!_M_gate1.wait_until(__lk, __abs_time,
527 [=]{ return _M_state < _S_max_readers; }))
535 #endif // _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
537 #endif // _GLIBCXX_HAS_GTHREADS
540 template<typename _Mutex>
544 typedef _Mutex mutex_type;
548 shared_lock() noexcept : _M_pm(nullptr), _M_owns(false) { }
551 shared_lock(mutex_type& __m)
552 : _M_pm(std::__addressof(__m)), _M_owns(true)
553 { __m.lock_shared(); }
555 shared_lock(mutex_type& __m, defer_lock_t) noexcept
556 : _M_pm(std::__addressof(__m)), _M_owns(false) { }
558 shared_lock(mutex_type& __m, try_to_lock_t)
559 : _M_pm(std::__addressof(__m)), _M_owns(__m.try_lock_shared()) { }
561 shared_lock(mutex_type& __m, adopt_lock_t)
562 : _M_pm(std::__addressof(__m)), _M_owns(true) { }
564 template<typename _Clock, typename _Duration>
565 shared_lock(mutex_type& __m,
566 const chrono::time_point<_Clock, _Duration>& __abs_time)
567 : _M_pm(std::__addressof(__m)),
568 _M_owns(__m.try_lock_shared_until(__abs_time)) { }
570 template<typename _Rep, typename _Period>
571 shared_lock(mutex_type& __m,
572 const chrono::duration<_Rep, _Period>& __rel_time)
573 : _M_pm(std::__addressof(__m)),
574 _M_owns(__m.try_lock_shared_for(__rel_time)) { }
579 _M_pm->unlock_shared();
582 shared_lock(shared_lock const&) = delete;
583 shared_lock& operator=(shared_lock const&) = delete;
585 shared_lock(shared_lock&& __sl) noexcept : shared_lock()
589 operator=(shared_lock&& __sl) noexcept
591 shared_lock(std::move(__sl)).swap(*this);
599 _M_pm->lock_shared();
607 return _M_owns = _M_pm->try_lock_shared();
610 template<typename _Rep, typename _Period>
612 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
615 return _M_owns = _M_pm->try_lock_shared_for(__rel_time);
618 template<typename _Clock, typename _Duration>
620 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
623 return _M_owns = _M_pm->try_lock_shared_until(__abs_time);
630 __throw_system_error(int(errc::resource_deadlock_would_occur));
631 _M_pm->unlock_shared();
638 swap(shared_lock& __u) noexcept
640 std::swap(_M_pm, __u._M_pm);
641 std::swap(_M_owns, __u._M_owns);
648 return std::exchange(_M_pm, nullptr);
653 bool owns_lock() const noexcept { return _M_owns; }
655 explicit operator bool() const noexcept { return _M_owns; }
657 mutex_type* mutex() const noexcept { return _M_pm; }
663 if (_M_pm == nullptr)
664 __throw_system_error(int(errc::operation_not_permitted));
666 __throw_system_error(int(errc::resource_deadlock_would_occur));
673 /// Swap specialization for shared_lock
674 template<typename _Mutex>
676 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) noexcept
679 #endif // _GLIBCXX_USE_C99_STDINT_TR1
682 _GLIBCXX_END_NAMESPACE_VERSION
687 #endif // _GLIBCXX_SHARED_MUTEX