2 //===------------------------ shared_mutex --------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_SHARED_MUTEX
12 #define _LIBCPP_SHARED_MUTEX
22 class shared_mutex // C++17
28 shared_mutex(const shared_mutex&) = delete;
29 shared_mutex& operator=(const shared_mutex&) = delete;
31 // Exclusive ownership
32 void lock(); // blocking
37 void lock_shared(); // blocking
38 bool try_lock_shared();
41 typedef implementation-defined native_handle_type; // See 30.2.3
42 native_handle_type native_handle(); // See 30.2.3
45 class shared_timed_mutex
49 ~shared_timed_mutex();
51 shared_timed_mutex(const shared_timed_mutex&) = delete;
52 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
54 // Exclusive ownership
55 void lock(); // blocking
57 template <class Rep, class Period>
58 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
59 template <class Clock, class Duration>
60 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
64 void lock_shared(); // blocking
65 bool try_lock_shared();
66 template <class Rep, class Period>
68 try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
69 template <class Clock, class Duration>
71 try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
75 template <class Mutex>
79 typedef Mutex mutex_type;
82 shared_lock() noexcept;
83 explicit shared_lock(mutex_type& m); // blocking
84 shared_lock(mutex_type& m, defer_lock_t) noexcept;
85 shared_lock(mutex_type& m, try_to_lock_t);
86 shared_lock(mutex_type& m, adopt_lock_t);
87 template <class Clock, class Duration>
88 shared_lock(mutex_type& m,
89 const chrono::time_point<Clock, Duration>& abs_time);
90 template <class Rep, class Period>
91 shared_lock(mutex_type& m,
92 const chrono::duration<Rep, Period>& rel_time);
95 shared_lock(shared_lock const&) = delete;
96 shared_lock& operator=(shared_lock const&) = delete;
98 shared_lock(shared_lock&& u) noexcept;
99 shared_lock& operator=(shared_lock&& u) noexcept;
101 void lock(); // blocking
103 template <class Rep, class Period>
104 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
105 template <class Clock, class Duration>
106 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
110 void swap(shared_lock& u) noexcept;
111 mutex_type* release() noexcept;
114 bool owns_lock() const noexcept;
115 explicit operator bool () const noexcept;
116 mutex_type* mutex() const noexcept;
119 template <class Mutex>
120 void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
128 #if _LIBCPP_STD_VER > 11 || defined(_LIBCPP_BUILDING_SHARED_MUTEX)
130 #include <__mutex_base>
132 #include <__undef_min_max>
134 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
135 #pragma GCC system_header
138 #ifdef _LIBCPP_HAS_NO_THREADS
139 #error <shared_mutex> is not supported on this single threaded system
140 #else // !_LIBCPP_HAS_NO_THREADS
142 _LIBCPP_BEGIN_NAMESPACE_STD
144 struct _LIBCPP_TYPE_VIS __shared_mutex_base
147 condition_variable __gate1_;
148 condition_variable __gate2_;
151 static const unsigned __write_entered_ = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
152 static const unsigned __n_readers_ = ~__write_entered_;
154 __shared_mutex_base();
155 _LIBCPP_INLINE_VISIBILITY ~__shared_mutex_base() = default;
157 __shared_mutex_base(const __shared_mutex_base&) = delete;
158 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
160 // Exclusive ownership
161 void lock(); // blocking
166 void lock_shared(); // blocking
167 bool try_lock_shared();
168 void unlock_shared();
170 // typedef implementation-defined native_handle_type; // See 30.2.3
171 // native_handle_type native_handle(); // See 30.2.3
175 #if _LIBCPP_STD_VER > 14
176 class _LIBCPP_TYPE_VIS shared_mutex
178 __shared_mutex_base __base;
180 shared_mutex() : __base() {}
181 _LIBCPP_INLINE_VISIBILITY ~shared_mutex() = default;
183 shared_mutex(const shared_mutex&) = delete;
184 shared_mutex& operator=(const shared_mutex&) = delete;
186 // Exclusive ownership
187 _LIBCPP_INLINE_VISIBILITY void lock() { return __base.lock(); }
188 _LIBCPP_INLINE_VISIBILITY bool try_lock() { return __base.try_lock(); }
189 _LIBCPP_INLINE_VISIBILITY void unlock() { return __base.unlock(); }
192 _LIBCPP_INLINE_VISIBILITY void lock_shared() { return __base.lock_shared(); }
193 _LIBCPP_INLINE_VISIBILITY bool try_lock_shared() { return __base.try_lock_shared(); }
194 _LIBCPP_INLINE_VISIBILITY void unlock_shared() { return __base.unlock_shared(); }
196 // typedef __shared_mutex_base::native_handle_type native_handle_type;
197 // _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() { return __base::unlock_shared(); }
202 class _LIBCPP_TYPE_VIS shared_timed_mutex
204 __shared_mutex_base __base;
206 shared_timed_mutex();
207 _LIBCPP_INLINE_VISIBILITY ~shared_timed_mutex() = default;
209 shared_timed_mutex(const shared_timed_mutex&) = delete;
210 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
212 // Exclusive ownership
215 template <class _Rep, class _Period>
216 _LIBCPP_INLINE_VISIBILITY
218 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
220 return try_lock_until(chrono::steady_clock::now() + __rel_time);
222 template <class _Clock, class _Duration>
224 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
229 bool try_lock_shared();
230 template <class _Rep, class _Period>
231 _LIBCPP_INLINE_VISIBILITY
233 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
235 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
237 template <class _Clock, class _Duration>
239 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
240 void unlock_shared();
243 template <class _Clock, class _Duration>
245 shared_timed_mutex::try_lock_until(
246 const chrono::time_point<_Clock, _Duration>& __abs_time)
248 unique_lock<mutex> __lk(__base.__mut_);
249 if (__base.__state_ & __base.__write_entered_)
253 cv_status __status = __base.__gate1_.wait_until(__lk, __abs_time);
254 if ((__base.__state_ & __base.__write_entered_) == 0)
256 if (__status == cv_status::timeout)
260 __base.__state_ |= __base.__write_entered_;
261 if (__base.__state_ & __base.__n_readers_)
265 cv_status __status = __base.__gate2_.wait_until(__lk, __abs_time);
266 if ((__base.__state_ & __base.__n_readers_) == 0)
268 if (__status == cv_status::timeout)
270 __base.__state_ &= ~__base.__write_entered_;
271 __base.__gate1_.notify_all();
279 template <class _Clock, class _Duration>
281 shared_timed_mutex::try_lock_shared_until(
282 const chrono::time_point<_Clock, _Duration>& __abs_time)
284 unique_lock<mutex> __lk(__base.__mut_);
285 if ((__base.__state_ & __base.__write_entered_) || (__base.__state_ & __base.__n_readers_) == __base.__n_readers_)
289 cv_status status = __base.__gate1_.wait_until(__lk, __abs_time);
290 if ((__base.__state_ & __base.__write_entered_) == 0 &&
291 (__base.__state_ & __base.__n_readers_) < __base.__n_readers_)
293 if (status == cv_status::timeout)
297 unsigned __num_readers = (__base.__state_ & __base.__n_readers_) + 1;
298 __base.__state_ &= ~__base.__n_readers_;
299 __base.__state_ |= __num_readers;
303 template <class _Mutex>
307 typedef _Mutex mutex_type;
314 _LIBCPP_INLINE_VISIBILITY
315 shared_lock() _NOEXCEPT
320 _LIBCPP_INLINE_VISIBILITY
321 explicit shared_lock(mutex_type& __m)
324 {__m_->lock_shared();}
326 _LIBCPP_INLINE_VISIBILITY
327 shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
332 _LIBCPP_INLINE_VISIBILITY
333 shared_lock(mutex_type& __m, try_to_lock_t)
335 __owns_(__m.try_lock_shared())
338 _LIBCPP_INLINE_VISIBILITY
339 shared_lock(mutex_type& __m, adopt_lock_t)
344 template <class _Clock, class _Duration>
345 _LIBCPP_INLINE_VISIBILITY
346 shared_lock(mutex_type& __m,
347 const chrono::time_point<_Clock, _Duration>& __abs_time)
349 __owns_(__m.try_lock_shared_until(__abs_time))
352 template <class _Rep, class _Period>
353 _LIBCPP_INLINE_VISIBILITY
354 shared_lock(mutex_type& __m,
355 const chrono::duration<_Rep, _Period>& __rel_time)
357 __owns_(__m.try_lock_shared_for(__rel_time))
360 _LIBCPP_INLINE_VISIBILITY
364 __m_->unlock_shared();
367 shared_lock(shared_lock const&) = delete;
368 shared_lock& operator=(shared_lock const&) = delete;
370 _LIBCPP_INLINE_VISIBILITY
371 shared_lock(shared_lock&& __u) _NOEXCEPT
379 _LIBCPP_INLINE_VISIBILITY
380 shared_lock& operator=(shared_lock&& __u) _NOEXCEPT
383 __m_->unlock_shared();
387 __owns_ = __u.__owns_;
395 template <class Rep, class Period>
396 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
397 template <class Clock, class Duration>
398 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
402 _LIBCPP_INLINE_VISIBILITY
403 void swap(shared_lock& __u) _NOEXCEPT
405 _VSTD::swap(__m_, __u.__m_);
406 _VSTD::swap(__owns_, __u.__owns_);
409 _LIBCPP_INLINE_VISIBILITY
410 mutex_type* release() _NOEXCEPT
412 mutex_type* __m = __m_;
419 _LIBCPP_INLINE_VISIBILITY
420 bool owns_lock() const _NOEXCEPT {return __owns_;}
422 _LIBCPP_INLINE_VISIBILITY
423 explicit operator bool () const _NOEXCEPT {return __owns_;}
425 _LIBCPP_INLINE_VISIBILITY
426 mutex_type* mutex() const _NOEXCEPT {return __m_;}
429 template <class _Mutex>
431 shared_lock<_Mutex>::lock()
434 __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
436 __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
441 template <class _Mutex>
443 shared_lock<_Mutex>::try_lock()
446 __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
448 __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
449 __owns_ = __m_->try_lock_shared();
453 template <class _Mutex>
454 template <class _Rep, class _Period>
456 shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
459 __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
461 __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
462 __owns_ = __m_->try_lock_shared_for(__d);
466 template <class _Mutex>
467 template <class _Clock, class _Duration>
469 shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
472 __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
474 __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
475 __owns_ = __m_->try_lock_shared_until(__t);
479 template <class _Mutex>
481 shared_lock<_Mutex>::unlock()
484 __throw_system_error(EPERM, "shared_lock::unlock: not locked");
485 __m_->unlock_shared();
489 template <class _Mutex>
490 inline _LIBCPP_INLINE_VISIBILITY
492 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT
495 _LIBCPP_END_NAMESPACE_STD
497 #endif // !_LIBCPP_HAS_NO_THREADS
499 #endif // _LIBCPP_STD_VER > 11
501 #endif // _LIBCPP_SHARED_MUTEX