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_timed_mutex
26 ~shared_timed_mutex();
28 shared_timed_mutex(const shared_timed_mutex&) = delete;
29 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
31 // Exclusive ownership
32 void lock(); // blocking
34 template <class Rep, class Period>
35 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
36 template <class Clock, class Duration>
37 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
41 void lock_shared(); // blocking
42 bool try_lock_shared();
43 template <class Rep, class Period>
45 try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
46 template <class Clock, class Duration>
48 try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
52 template <class Mutex>
56 typedef Mutex mutex_type;
59 shared_lock() noexcept;
60 explicit shared_lock(mutex_type& m); // blocking
61 shared_lock(mutex_type& m, defer_lock_t) noexcept;
62 shared_lock(mutex_type& m, try_to_lock_t);
63 shared_lock(mutex_type& m, adopt_lock_t);
64 template <class Clock, class Duration>
65 shared_lock(mutex_type& m,
66 const chrono::time_point<Clock, Duration>& abs_time);
67 template <class Rep, class Period>
68 shared_lock(mutex_type& m,
69 const chrono::duration<Rep, Period>& rel_time);
72 shared_lock(shared_lock const&) = delete;
73 shared_lock& operator=(shared_lock const&) = delete;
75 shared_lock(shared_lock&& u) noexcept;
76 shared_lock& operator=(shared_lock&& u) noexcept;
78 void lock(); // blocking
80 template <class Rep, class Period>
81 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
82 template <class Clock, class Duration>
83 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
87 void swap(shared_lock& u) noexcept;
88 mutex_type* release() noexcept;
91 bool owns_lock() const noexcept;
92 explicit operator bool () const noexcept;
93 mutex_type* mutex() const noexcept;
96 template <class Mutex>
97 void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
105 #if _LIBCPP_STD_VER > 11 || defined(_LIBCPP_BUILDING_SHARED_MUTEX)
107 #include <__mutex_base>
109 #include <__undef_min_max>
111 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
112 #pragma GCC system_header
115 #ifdef _LIBCPP_HAS_NO_THREADS
116 #error <shared_mutex> is not supported on this single threaded system
117 #else // !_LIBCPP_HAS_NO_THREADS
119 _LIBCPP_BEGIN_NAMESPACE_STD
121 class _LIBCPP_TYPE_VIS shared_timed_mutex
124 condition_variable __gate1_;
125 condition_variable __gate2_;
128 static const unsigned __write_entered_ = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
129 static const unsigned __n_readers_ = ~__write_entered_;
131 shared_timed_mutex();
132 _LIBCPP_INLINE_VISIBILITY ~shared_timed_mutex() = default;
134 shared_timed_mutex(const shared_timed_mutex&) = delete;
135 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
137 // Exclusive ownership
140 template <class _Rep, class _Period>
141 _LIBCPP_INLINE_VISIBILITY
143 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
145 return try_lock_until(chrono::steady_clock::now() + __rel_time);
147 template <class _Clock, class _Duration>
149 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
154 bool try_lock_shared();
155 template <class _Rep, class _Period>
156 _LIBCPP_INLINE_VISIBILITY
158 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
160 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
162 template <class _Clock, class _Duration>
164 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
165 void unlock_shared();
168 template <class _Clock, class _Duration>
170 shared_timed_mutex::try_lock_until(
171 const chrono::time_point<_Clock, _Duration>& __abs_time)
173 unique_lock<mutex> __lk(__mut_);
174 if (__state_ & __write_entered_)
178 cv_status __status = __gate1_.wait_until(__lk, __abs_time);
179 if ((__state_ & __write_entered_) == 0)
181 if (__status == cv_status::timeout)
185 __state_ |= __write_entered_;
186 if (__state_ & __n_readers_)
190 cv_status __status = __gate2_.wait_until(__lk, __abs_time);
191 if ((__state_ & __n_readers_) == 0)
193 if (__status == cv_status::timeout)
195 __state_ &= ~__write_entered_;
203 template <class _Clock, class _Duration>
205 shared_timed_mutex::try_lock_shared_until(
206 const chrono::time_point<_Clock, _Duration>& __abs_time)
208 unique_lock<mutex> __lk(__mut_);
209 if ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_)
213 cv_status status = __gate1_.wait_until(__lk, __abs_time);
214 if ((__state_ & __write_entered_) == 0 &&
215 (__state_ & __n_readers_) < __n_readers_)
217 if (status == cv_status::timeout)
221 unsigned __num_readers = (__state_ & __n_readers_) + 1;
222 __state_ &= ~__n_readers_;
223 __state_ |= __num_readers;
227 template <class _Mutex>
231 typedef _Mutex mutex_type;
238 _LIBCPP_INLINE_VISIBILITY
239 shared_lock() _NOEXCEPT
244 _LIBCPP_INLINE_VISIBILITY
245 explicit shared_lock(mutex_type& __m)
248 {__m_->lock_shared();}
250 _LIBCPP_INLINE_VISIBILITY
251 shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
256 _LIBCPP_INLINE_VISIBILITY
257 shared_lock(mutex_type& __m, try_to_lock_t)
259 __owns_(__m.try_lock_shared())
262 _LIBCPP_INLINE_VISIBILITY
263 shared_lock(mutex_type& __m, adopt_lock_t)
268 template <class _Clock, class _Duration>
269 _LIBCPP_INLINE_VISIBILITY
270 shared_lock(mutex_type& __m,
271 const chrono::time_point<_Clock, _Duration>& __abs_time)
273 __owns_(__m.try_lock_shared_until(__abs_time))
276 template <class _Rep, class _Period>
277 _LIBCPP_INLINE_VISIBILITY
278 shared_lock(mutex_type& __m,
279 const chrono::duration<_Rep, _Period>& __rel_time)
281 __owns_(__m.try_lock_shared_for(__rel_time))
284 _LIBCPP_INLINE_VISIBILITY
288 __m_->unlock_shared();
291 shared_lock(shared_lock const&) = delete;
292 shared_lock& operator=(shared_lock const&) = delete;
294 _LIBCPP_INLINE_VISIBILITY
295 shared_lock(shared_lock&& __u) _NOEXCEPT
303 _LIBCPP_INLINE_VISIBILITY
304 shared_lock& operator=(shared_lock&& __u) _NOEXCEPT
307 __m_->unlock_shared();
311 __owns_ = __u.__owns_;
319 template <class Rep, class Period>
320 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
321 template <class Clock, class Duration>
322 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
326 _LIBCPP_INLINE_VISIBILITY
327 void swap(shared_lock& __u) _NOEXCEPT
329 _VSTD::swap(__m_, __u.__m_);
330 _VSTD::swap(__owns_, __u.__owns_);
333 _LIBCPP_INLINE_VISIBILITY
334 mutex_type* release() _NOEXCEPT
336 mutex_type* __m = __m_;
343 _LIBCPP_INLINE_VISIBILITY
344 bool owns_lock() const _NOEXCEPT {return __owns_;}
346 _LIBCPP_INLINE_VISIBILITY
347 explicit operator bool () const _NOEXCEPT {return __owns_;}
349 _LIBCPP_INLINE_VISIBILITY
350 mutex_type* mutex() const _NOEXCEPT {return __m_;}
353 template <class _Mutex>
355 shared_lock<_Mutex>::lock()
358 __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
360 __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
365 template <class _Mutex>
367 shared_lock<_Mutex>::try_lock()
370 __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
372 __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
373 __owns_ = __m_->try_lock_shared();
377 template <class _Mutex>
378 template <class _Rep, class _Period>
380 shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
383 __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
385 __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
386 __owns_ = __m_->try_lock_shared_for(__d);
390 template <class _Mutex>
391 template <class _Clock, class _Duration>
393 shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
396 __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
398 __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
399 __owns_ = __m_->try_lock_shared_until(__t);
403 template <class _Mutex>
405 shared_lock<_Mutex>::unlock()
408 __throw_system_error(EPERM, "shared_lock::unlock: not locked");
409 __m_->unlock_shared();
413 template <class _Mutex>
414 inline _LIBCPP_INLINE_VISIBILITY
416 swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT
419 _LIBCPP_END_NAMESPACE_STD
421 #endif // !_LIBCPP_HAS_NO_THREADS
423 #endif // _LIBCPP_STD_VER > 11
425 #endif // _LIBCPP_SHARED_MUTEX