libstdc++
shared_mutex
Go to the documentation of this file.
1// <shared_mutex> -*- C++ -*-
2
3// Copyright (C) 2013-2025 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/** @file include/shared_mutex
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_SHARED_MUTEX
30#define _GLIBCXX_SHARED_MUTEX 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#include <bits/requires_hosted.h> // concurrency
37
38#if __cplusplus >= 201402L
39
40#include <bits/chrono.h>
41#include <bits/error_constants.h>
42#include <bits/functexcept.h>
43#include <bits/move.h> // move, __exchange
44#include <bits/std_mutex.h> // defer_lock_t
45
46#define __glibcxx_want_shared_mutex
47#define __glibcxx_want_shared_timed_mutex
48#include <bits/version.h>
49
50#if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
51# include <condition_variable>
52#endif
53
54namespace std _GLIBCXX_VISIBILITY(default)
55{
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
57
58 /**
59 * @addtogroup mutexes
60 * @{
61 */
62
63#ifdef _GLIBCXX_HAS_GTHREADS
64
65#ifdef __cpp_lib_shared_mutex // C++ >= 17 && hosted && gthread
66 class shared_mutex;
67#endif
68
69 class shared_timed_mutex;
70
71 /// @cond undocumented
72
73#if _GLIBCXX_USE_PTHREAD_RWLOCK_T
74#ifdef __gthrw
75#define _GLIBCXX_GTHRW(name) \
76 __gthrw(pthread_ ## name); \
77 inline int \
78 __glibcxx_ ## name (pthread_rwlock_t *__rwlock) \
79 { \
80 if (__gthread_active_p ()) \
81 return __gthrw_(pthread_ ## name) (__rwlock); \
82 else \
83 return 0; \
84 }
85 _GLIBCXX_GTHRW(rwlock_rdlock)
86 _GLIBCXX_GTHRW(rwlock_tryrdlock)
87 _GLIBCXX_GTHRW(rwlock_wrlock)
88 _GLIBCXX_GTHRW(rwlock_trywrlock)
89 _GLIBCXX_GTHRW(rwlock_unlock)
90# ifndef PTHREAD_RWLOCK_INITIALIZER
91 _GLIBCXX_GTHRW(rwlock_destroy)
92 __gthrw(pthread_rwlock_init);
93 inline int
94 __glibcxx_rwlock_init (pthread_rwlock_t *__rwlock)
95 {
96 if (__gthread_active_p ())
97 return __gthrw_(pthread_rwlock_init) (__rwlock, NULL);
98 else
99 return 0;
100 }
101# endif
102# if _GTHREAD_USE_MUTEX_TIMEDLOCK
103 __gthrw(pthread_rwlock_timedrdlock);
104 inline int
105 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
106 const timespec *__ts)
107 {
108 if (__gthread_active_p ())
109 return __gthrw_(pthread_rwlock_timedrdlock) (__rwlock, __ts);
110 else
111 return 0;
112 }
113 __gthrw(pthread_rwlock_timedwrlock);
114 inline int
115 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
116 const timespec *__ts)
117 {
118 if (__gthread_active_p ())
119 return __gthrw_(pthread_rwlock_timedwrlock) (__rwlock, __ts);
120 else
121 return 0;
122 }
123# endif
124#else
125 inline int
126 __glibcxx_rwlock_rdlock (pthread_rwlock_t *__rwlock)
127 { return pthread_rwlock_rdlock (__rwlock); }
128 inline int
129 __glibcxx_rwlock_tryrdlock (pthread_rwlock_t *__rwlock)
130 { return pthread_rwlock_tryrdlock (__rwlock); }
131 inline int
132 __glibcxx_rwlock_wrlock (pthread_rwlock_t *__rwlock)
133 { return pthread_rwlock_wrlock (__rwlock); }
134 inline int
135 __glibcxx_rwlock_trywrlock (pthread_rwlock_t *__rwlock)
136 { return pthread_rwlock_trywrlock (__rwlock); }
137 inline int
138 __glibcxx_rwlock_unlock (pthread_rwlock_t *__rwlock)
139 { return pthread_rwlock_unlock (__rwlock); }
140 inline int
141 __glibcxx_rwlock_destroy(pthread_rwlock_t *__rwlock)
142 { return pthread_rwlock_destroy (__rwlock); }
143 inline int
144 __glibcxx_rwlock_init(pthread_rwlock_t *__rwlock)
145 { return pthread_rwlock_init (__rwlock, NULL); }
146# if _GTHREAD_USE_MUTEX_TIMEDLOCK
147 inline int
148 __glibcxx_rwlock_timedrdlock (pthread_rwlock_t *__rwlock,
149 const timespec *__ts)
150 { return pthread_rwlock_timedrdlock (__rwlock, __ts); }
151 inline int
152 __glibcxx_rwlock_timedwrlock (pthread_rwlock_t *__rwlock,
153 const timespec *__ts)
154 { return pthread_rwlock_timedwrlock (__rwlock, __ts); }
155# endif
156#endif
157
158 /// A shared mutex type implemented using pthread_rwlock_t.
159 class __shared_mutex_pthread
160 {
161 friend class shared_timed_mutex;
162
163#ifdef PTHREAD_RWLOCK_INITIALIZER
164 pthread_rwlock_t _M_rwlock = PTHREAD_RWLOCK_INITIALIZER;
165
166 public:
167 __shared_mutex_pthread() = default;
168 ~__shared_mutex_pthread() = default;
169#else
170 pthread_rwlock_t _M_rwlock;
171
172 public:
173 __shared_mutex_pthread()
174 {
175 int __ret = __glibcxx_rwlock_init(&_M_rwlock);
176 if (__ret == ENOMEM)
177 __throw_bad_alloc();
178 else if (__ret == EAGAIN)
179 __throw_system_error(int(errc::resource_unavailable_try_again));
180 else if (__ret == EPERM)
181 __throw_system_error(int(errc::operation_not_permitted));
182 // Errors not handled: EBUSY, EINVAL
183 __glibcxx_assert(__ret == 0);
184 }
185
186 ~__shared_mutex_pthread()
187 {
188 int __ret __attribute((__unused__)) = __glibcxx_rwlock_destroy(&_M_rwlock);
189 // Errors not handled: EBUSY, EINVAL
190 __glibcxx_assert(__ret == 0);
191 }
192#endif
193
194 __shared_mutex_pthread(const __shared_mutex_pthread&) = delete;
195 __shared_mutex_pthread& operator=(const __shared_mutex_pthread&) = delete;
196
197 void
198 lock()
199 {
200 int __ret = __glibcxx_rwlock_wrlock(&_M_rwlock);
201 if (__ret == EDEADLK)
202 __throw_system_error(int(errc::resource_deadlock_would_occur));
203 // Errors not handled: EINVAL
204 __glibcxx_assert(__ret == 0);
205 }
206
207 bool
208 try_lock()
209 {
210 int __ret = __glibcxx_rwlock_trywrlock(&_M_rwlock);
211 if (__ret == EBUSY) return false;
212 // Errors not handled: EINVAL
213 __glibcxx_assert(__ret == 0);
214 return true;
215 }
216
217 void
218 unlock()
219 {
220 int __ret __attribute((__unused__)) = __glibcxx_rwlock_unlock(&_M_rwlock);
221 // Errors not handled: EPERM, EBUSY, EINVAL
222 __glibcxx_assert(__ret == 0);
223 }
224
225 // Shared ownership
226
227 void
228 lock_shared()
229 {
230 int __ret;
231 // We retry if we exceeded the maximum number of read locks supported by
232 // the POSIX implementation; this can result in busy-waiting, but this
233 // is okay based on the current specification of forward progress
234 // guarantees by the standard.
235 do
236 __ret = __glibcxx_rwlock_rdlock(&_M_rwlock);
237 while (__ret == EAGAIN);
238 if (__ret == EDEADLK)
239 __throw_system_error(int(errc::resource_deadlock_would_occur));
240 // Errors not handled: EINVAL
241 __glibcxx_assert(__ret == 0);
242 }
243
244 bool
245 try_lock_shared()
246 {
247 int __ret = __glibcxx_rwlock_tryrdlock(&_M_rwlock);
248 // If the maximum number of read locks has been exceeded, we just fail
249 // to acquire the lock. Unlike for lock(), we are not allowed to throw
250 // an exception.
251 if (__ret == EBUSY || __ret == EAGAIN) return false;
252 // Errors not handled: EINVAL
253 __glibcxx_assert(__ret == 0);
254 return true;
255 }
256
257 void
258 unlock_shared()
259 {
260 unlock();
261 }
262
263 void* native_handle() { return &_M_rwlock; }
264 };
265#endif
266
267#if ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
268 /// A shared mutex type implemented using std::condition_variable.
269 class __shared_mutex_cv
270 {
271 friend class shared_timed_mutex;
272
273 // Based on Howard Hinnant's reference implementation from N2406.
274
275 // The high bit of _M_state is the write-entered flag which is set to
276 // indicate a writer has taken the lock or is queuing to take the lock.
277 // The remaining bits are the count of reader locks.
278 //
279 // To take a reader lock, block on gate1 while the write-entered flag is
280 // set or the maximum number of reader locks is held, then increment the
281 // reader lock count.
282 // To release, decrement the count, then if the write-entered flag is set
283 // and the count is zero then signal gate2 to wake a queued writer,
284 // otherwise if the maximum number of reader locks was held signal gate1
285 // to wake a reader.
286 //
287 // To take a writer lock, block on gate1 while the write-entered flag is
288 // set, then set the write-entered flag to start queueing, then block on
289 // gate2 while the number of reader locks is non-zero.
290 // To release, unset the write-entered flag and signal gate1 to wake all
291 // blocked readers and writers.
292 //
293 // This means that when no reader locks are held readers and writers get
294 // equal priority. When one or more reader locks is held a writer gets
295 // priority and no more reader locks can be taken while the writer is
296 // queued.
297
298 // Only locked when accessing _M_state or waiting on condition variables.
299 mutex _M_mut;
300 // Used to block while write-entered is set or reader count at maximum.
301 condition_variable _M_gate1;
302 // Used to block queued writers while reader count is non-zero.
303 condition_variable _M_gate2;
304 // The write-entered flag and reader count.
305 unsigned _M_state;
306
307 static constexpr unsigned _S_write_entered
308 = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
309 static constexpr unsigned _S_max_readers = ~_S_write_entered;
310
311 // Test whether the write-entered flag is set. _M_mut must be locked.
312 bool _M_write_entered() const { return _M_state & _S_write_entered; }
313
314 // The number of reader locks currently held. _M_mut must be locked.
315 unsigned _M_readers() const { return _M_state & _S_max_readers; }
316
317 public:
318 __shared_mutex_cv() : _M_state(0) {}
319
320 ~__shared_mutex_cv()
321 {
322 __glibcxx_assert( _M_state == 0 );
323 }
324
325 __shared_mutex_cv(const __shared_mutex_cv&) = delete;
326 __shared_mutex_cv& operator=(const __shared_mutex_cv&) = delete;
327
328 // Exclusive ownership
329
330 void
331 lock()
332 {
333 unique_lock<mutex> __lk(_M_mut);
334 // Wait until we can set the write-entered flag.
335 _M_gate1.wait(__lk, [this]{ return !_M_write_entered(); });
336 _M_state |= _S_write_entered;
337 // Then wait until there are no more readers.
338 _M_gate2.wait(__lk, [this]{ return _M_readers() == 0; });
339 }
340
341 bool
342 try_lock()
343 {
344 unique_lock<mutex> __lk(_M_mut, try_to_lock);
345 if (__lk.owns_lock() && _M_state == 0)
346 {
347 _M_state = _S_write_entered;
348 return true;
349 }
350 return false;
351 }
352
353 void
354 unlock()
355 {
356 lock_guard<mutex> __lk(_M_mut);
357 __glibcxx_assert( _M_write_entered() );
358 _M_state = 0;
359 // call notify_all() while mutex is held so that another thread can't
360 // lock and unlock the mutex then destroy *this before we make the call.
361 _M_gate1.notify_all();
362 }
363
364 // Shared ownership
365
366 void
367 lock_shared()
368 {
369 unique_lock<mutex> __lk(_M_mut);
370 _M_gate1.wait(__lk, [this]{ return _M_state < _S_max_readers; });
371 ++_M_state;
372 }
373
374 bool
375 try_lock_shared()
376 {
377 unique_lock<mutex> __lk(_M_mut, try_to_lock);
378 if (!__lk.owns_lock())
379 return false;
380 if (_M_state < _S_max_readers)
381 {
382 ++_M_state;
383 return true;
384 }
385 return false;
386 }
387
388 void
389 unlock_shared()
390 {
391 lock_guard<mutex> __lk(_M_mut);
392 __glibcxx_assert( _M_readers() > 0 );
393 auto __prev = _M_state--;
394 if (_M_write_entered())
395 {
396 // Wake the queued writer if there are no more readers.
397 if (_M_readers() == 0)
398 _M_gate2.notify_one();
399 // No need to notify gate1 because we give priority to the queued
400 // writer, and that writer will eventually notify gate1 after it
401 // clears the write-entered flag.
402 }
403 else
404 {
405 // Wake any thread that was blocked on reader overflow.
406 if (__prev == _S_max_readers)
407 _M_gate1.notify_one();
408 }
409 }
410 };
411#endif
412 /// @endcond
413
414#ifdef __cpp_lib_shared_mutex
415 /// The standard shared mutex type.
416 class shared_mutex
417 {
418 public:
419 shared_mutex() = default;
420 ~shared_mutex() = default;
421
422 shared_mutex(const shared_mutex&) = delete;
423 shared_mutex& operator=(const shared_mutex&) = delete;
424
425 // Exclusive ownership
426
427 void lock() { _M_impl.lock(); }
428 [[nodiscard]] bool try_lock() { return _M_impl.try_lock(); }
429 void unlock() { _M_impl.unlock(); }
430
431 // Shared ownership
432
433 void lock_shared() { _M_impl.lock_shared(); }
434 [[nodiscard]] bool try_lock_shared() { return _M_impl.try_lock_shared(); }
435 void unlock_shared() { _M_impl.unlock_shared(); }
436
437#if _GLIBCXX_USE_PTHREAD_RWLOCK_T
438 typedef void* native_handle_type;
439 native_handle_type native_handle() { return _M_impl.native_handle(); }
440
441 private:
442 __shared_mutex_pthread _M_impl;
443#else
444 private:
445 __shared_mutex_cv _M_impl;
446#endif
447 };
448#endif // __cpp_lib_shared_mutex
449
450 /// @cond undocumented
451#if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
452 using __shared_timed_mutex_base = __shared_mutex_pthread;
453#else
454 using __shared_timed_mutex_base = __shared_mutex_cv;
455#endif
456 /// @endcond
457
458 /// The standard shared timed mutex type.
459 class shared_timed_mutex
460 : private __shared_timed_mutex_base
461 {
462 using _Base = __shared_timed_mutex_base;
463
464 // Must use the same clock as condition_variable for __shared_mutex_cv.
465#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
466 using __clock_t = chrono::steady_clock;
467#else
468 using __clock_t = chrono::system_clock;
469#endif
470
471 public:
472 shared_timed_mutex() = default;
473 ~shared_timed_mutex() = default;
474
475 shared_timed_mutex(const shared_timed_mutex&) = delete;
476 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
477
478 // Exclusive ownership
479
480 void lock() { _Base::lock(); }
481 _GLIBCXX_NODISCARD bool try_lock() { return _Base::try_lock(); }
482 void unlock() { _Base::unlock(); }
483
484 template<typename _Rep, typename _Period>
485 _GLIBCXX_NODISCARD
486 bool
487 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
488 {
489 auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
490 if (ratio_greater<__clock_t::period, _Period>())
491 ++__rt;
492 return try_lock_until(__clock_t::now() + __rt);
493 }
494
495 // Shared ownership
496
497 void lock_shared() { _Base::lock_shared(); }
498 _GLIBCXX_NODISCARD
499 bool try_lock_shared() { return _Base::try_lock_shared(); }
500 void unlock_shared() { _Base::unlock_shared(); }
501
502 template<typename _Rep, typename _Period>
503 _GLIBCXX_NODISCARD
504 bool
505 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rtime)
506 {
507 auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime);
508 if (ratio_greater<__clock_t::period, _Period>())
509 ++__rt;
510 return try_lock_shared_until(__clock_t::now() + __rt);
511 }
512
513#if _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
514
515 // Exclusive ownership
516
517 template<typename _Duration>
518 _GLIBCXX_NODISCARD
519 bool
520 try_lock_until(const chrono::time_point<chrono::system_clock,
521 _Duration>& __atime)
522 {
523 struct timespec __ts = chrono::__to_timeout_timespec(__atime);
524 int __ret = __glibcxx_rwlock_timedwrlock(&_M_rwlock, &__ts);
525 // On self-deadlock, we just fail to acquire the lock. Technically,
526 // the program violated the precondition.
527 if (__ret == ETIMEDOUT || __ret == EDEADLK)
528 return false;
529 // Errors not handled: EINVAL
530 __glibcxx_assert(__ret == 0);
531 return true;
532 }
533
534#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
535 template<typename _Duration>
536 _GLIBCXX_NODISCARD
537 bool
538 try_lock_until(const chrono::time_point<chrono::steady_clock,
539 _Duration>& __atime)
540 {
541 struct timespec __ts = chrono::__to_timeout_timespec(__atime);
542 int __ret = pthread_rwlock_clockwrlock(&_M_rwlock, CLOCK_MONOTONIC,
543 &__ts);
544 // On self-deadlock, we just fail to acquire the lock. Technically,
545 // the program violated the precondition.
546 if (__ret == ETIMEDOUT || __ret == EDEADLK)
547 return false;
548 // Errors not handled: EINVAL
549 __glibcxx_assert(__ret == 0);
550 return true;
551 }
552#endif
553
554 template<typename _Clock, typename _Duration>
555 _GLIBCXX_NODISCARD
556 bool
557 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
558 {
559#if __cplusplus > 201703L
560 static_assert(chrono::is_clock_v<_Clock>);
561#endif
562 // The user-supplied clock may not tick at the same rate as
563 // steady_clock, so we must loop in order to guarantee that
564 // the timeout has expired before returning false.
565 typename _Clock::time_point __now = _Clock::now();
566 do {
567 auto __rtime = __atime - __now;
568 if (try_lock_for(__rtime))
569 return true;
570 __now = _Clock::now();
571 } while (__atime > __now);
572 return false;
573 }
574
575 // Shared ownership
576
577 template<typename _Duration>
578 _GLIBCXX_NODISCARD
579 bool
580 try_lock_shared_until(const chrono::time_point<chrono::system_clock,
581 _Duration>& __atime)
582 {
583 struct timespec __ts = chrono::__to_timeout_timespec(__atime);
584
585 int __ret;
586 // Unlike for lock(), we are not allowed to throw an exception so if
587 // the maximum number of read locks has been exceeded, or we would
588 // deadlock, we just try to acquire the lock again (and will time out
589 // eventually).
590 // In cases where we would exceed the maximum number of read locks
591 // throughout the whole time until the timeout, we will fail to
592 // acquire the lock even if it would be logically free; however, this
593 // is allowed by the standard, and we made a "strong effort"
594 // (see C++14 30.4.1.4p26).
595 // For cases where the implementation detects a deadlock we
596 // intentionally block and timeout so that an early return isn't
597 // mistaken for a spurious failure, which might help users realise
598 // there is a deadlock.
599 do
600 __ret = __glibcxx_rwlock_timedrdlock(&_M_rwlock, &__ts);
601 while (__ret == EAGAIN || __ret == EDEADLK);
602 if (__ret == ETIMEDOUT)
603 return false;
604 // Errors not handled: EINVAL
605 __glibcxx_assert(__ret == 0);
606 return true;
607 }
608
609#ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
610 template<typename _Duration>
611 _GLIBCXX_NODISCARD
612 bool
613 try_lock_shared_until(const chrono::time_point<chrono::steady_clock,
614 _Duration>& __atime)
615 {
616 struct timespec __ts = chrono::__to_timeout_timespec(__atime);
617 int __ret = pthread_rwlock_clockrdlock(&_M_rwlock, CLOCK_MONOTONIC,
618 &__ts);
619 // On self-deadlock, we just fail to acquire the lock. Technically,
620 // the program violated the precondition.
621 if (__ret == ETIMEDOUT || __ret == EDEADLK)
622 return false;
623 // Errors not handled: EINVAL
624 __glibcxx_assert(__ret == 0);
625 return true;
626 }
627#endif
628
629 template<typename _Clock, typename _Duration>
630 _GLIBCXX_NODISCARD
631 bool
632 try_lock_shared_until(const chrono::time_point<_Clock,
633 _Duration>& __atime)
634 {
635#if __cplusplus > 201703L
636 static_assert(chrono::is_clock_v<_Clock>);
637#endif
638 // The user-supplied clock may not tick at the same rate as
639 // steady_clock, so we must loop in order to guarantee that
640 // the timeout has expired before returning false.
641 typename _Clock::time_point __now = _Clock::now();
642 do {
643 auto __rtime = __atime - __now;
644 if (try_lock_shared_for(__rtime))
645 return true;
646 __now = _Clock::now();
647 } while (__atime > __now);
648 return false;
649 }
650
651#else // ! (_GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK)
652
653 // Exclusive ownership
654
655 template<typename _Clock, typename _Duration>
656 _GLIBCXX_NODISCARD
657 bool
658 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
659 {
660 unique_lock<mutex> __lk(_M_mut);
661 if (!_M_gate1.wait_until(__lk, __abs_time,
662 [this]{ return !_M_write_entered(); }))
663 {
664 return false;
665 }
666 _M_state |= _S_write_entered;
667 if (!_M_gate2.wait_until(__lk, __abs_time,
668 [this]{ return _M_readers() == 0; }))
669 {
670 _M_state ^= _S_write_entered;
671 // Wake all threads blocked while the write-entered flag was set.
672 _M_gate1.notify_all();
673 return false;
674 }
675 return true;
676 }
677
678 // Shared ownership
679
680 template <typename _Clock, typename _Duration>
681 _GLIBCXX_NODISCARD
682 bool
683 try_lock_shared_until(const chrono::time_point<_Clock,
684 _Duration>& __abs_time)
685 {
686 unique_lock<mutex> __lk(_M_mut);
687 if (!_M_gate1.wait_until(__lk, __abs_time,
688 [this]{ return _M_state < _S_max_readers; }))
689 {
690 return false;
691 }
692 ++_M_state;
693 return true;
694 }
695
696#endif // _GLIBCXX_USE_PTHREAD_RWLOCK_T && _GTHREAD_USE_MUTEX_TIMEDLOCK
697 };
698#endif // _GLIBCXX_HAS_GTHREADS
699
700 /// shared_lock
701 template<typename _Mutex>
702 class shared_lock
703 {
704 public:
705 typedef _Mutex mutex_type;
706
707 // Shared locking
708
709 shared_lock() noexcept : _M_pm(nullptr), _M_owns(false) { }
710
711 explicit
712 shared_lock(mutex_type& __m)
713 : _M_pm(std::__addressof(__m)), _M_owns(true)
714 { __m.lock_shared(); }
715
716 shared_lock(mutex_type& __m, defer_lock_t) noexcept
717 : _M_pm(std::__addressof(__m)), _M_owns(false) { }
718
719 shared_lock(mutex_type& __m, try_to_lock_t)
720 : _M_pm(std::__addressof(__m)), _M_owns(__m.try_lock_shared()) { }
721
722 shared_lock(mutex_type& __m, adopt_lock_t)
723 : _M_pm(std::__addressof(__m)), _M_owns(true) { }
724
725 template<typename _Clock, typename _Duration>
726 shared_lock(mutex_type& __m,
727 const chrono::time_point<_Clock, _Duration>& __abs_time)
728 : _M_pm(std::__addressof(__m)),
729 _M_owns(__m.try_lock_shared_until(__abs_time)) { }
730
731 template<typename _Rep, typename _Period>
732 shared_lock(mutex_type& __m,
733 const chrono::duration<_Rep, _Period>& __rel_time)
734 : _M_pm(std::__addressof(__m)),
735 _M_owns(__m.try_lock_shared_for(__rel_time)) { }
736
737 ~shared_lock()
738 {
739 if (_M_owns)
740 _M_pm->unlock_shared();
741 }
742
743 shared_lock(shared_lock const&) = delete;
744 shared_lock& operator=(shared_lock const&) = delete;
745
746 shared_lock(shared_lock&& __sl) noexcept : shared_lock()
747 { swap(__sl); }
748
749 shared_lock&
750 operator=(shared_lock&& __sl) noexcept
751 {
752 // _GLIBCXX_RESOLVE_LIB_DEFECTS
753 // 4172. unique_lock self-move-assignment is broken
754 shared_lock(std::move(__sl)).swap(*this);
755 return *this;
756 }
757
758 void
759 lock()
760 {
761 _M_lockable();
762 _M_pm->lock_shared();
763 _M_owns = true;
764 }
765
766 _GLIBCXX_NODISCARD
767 bool
768 try_lock()
769 {
770 _M_lockable();
771 return _M_owns = _M_pm->try_lock_shared();
772 }
773
774 template<typename _Rep, typename _Period>
775 _GLIBCXX_NODISCARD
776 bool
777 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
778 {
779 _M_lockable();
780 return _M_owns = _M_pm->try_lock_shared_for(__rel_time);
781 }
782
783 template<typename _Clock, typename _Duration>
784 _GLIBCXX_NODISCARD
785 bool
786 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
787 {
788 _M_lockable();
789 return _M_owns = _M_pm->try_lock_shared_until(__abs_time);
790 }
791
792 void
793 unlock()
794 {
795 if (!_M_owns)
796 __throw_system_error(int(errc::operation_not_permitted));
797 _M_pm->unlock_shared();
798 _M_owns = false;
799 }
800
801 // Setters
802
803 void
804 swap(shared_lock& __u) noexcept
805 {
806 std::swap(_M_pm, __u._M_pm);
807 std::swap(_M_owns, __u._M_owns);
808 }
809
810 mutex_type*
811 release() noexcept
812 {
813 _M_owns = false;
814 return std::__exchange(_M_pm, nullptr);
815 }
816
817 // Getters
818
819 _GLIBCXX_NODISCARD
820 bool owns_lock() const noexcept { return _M_owns; }
821
822 explicit operator bool() const noexcept { return _M_owns; }
823
824 _GLIBCXX_NODISCARD
825 mutex_type* mutex() const noexcept { return _M_pm; }
826
827 private:
828 void
829 _M_lockable() const
830 {
831 if (_M_pm == nullptr)
832 __throw_system_error(int(errc::operation_not_permitted));
833 if (_M_owns)
834 __throw_system_error(int(errc::resource_deadlock_would_occur));
835 }
836
837 mutex_type* _M_pm;
838 bool _M_owns;
839 };
840
841 /// Swap specialization for shared_lock
842 /// @relates shared_mutex
843 template<typename _Mutex>
844 void
845 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) noexcept
846 { __x.swap(__y); }
847
848 /// @} group mutexes
849_GLIBCXX_END_NAMESPACE_VERSION
850} // namespace
851
852#endif // C++14
853
854#endif // _GLIBCXX_SHARED_MUTEX