blob: dcb93949e30d389177366f580fc6f40326894f79 [file] [log] [blame]
Howard Hinnantba898e42013-09-21 01:49:28 +00001// -*- C++ -*-
2//===------------------------ shared_mutex --------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_SHARED_MUTEX
12#define _LIBCPP_SHARED_MUTEX
13
14/*
15 shared_mutex synopsis
16
17// C++1y
18
19namespace std
20{
21
Marshall Clowabadb452015-06-30 14:04:14 +000022class shared_mutex // C++17
23{
24public:
25 shared_mutex();
26 ~shared_mutex();
27
28 shared_mutex(const shared_mutex&) = delete;
29 shared_mutex& operator=(const shared_mutex&) = delete;
30
31 // Exclusive ownership
32 void lock(); // blocking
33 bool try_lock();
34 void unlock();
35
36 // Shared ownership
37 void lock_shared(); // blocking
38 bool try_lock_shared();
39 void unlock_shared();
40
41 typedef implementation-defined native_handle_type; // See 30.2.3
42 native_handle_type native_handle(); // See 30.2.3
43};
44
David Majnemerf9f95be2014-03-17 20:19:44 +000045class shared_timed_mutex
Howard Hinnantba898e42013-09-21 01:49:28 +000046{
47public:
David Majnemerf9f95be2014-03-17 20:19:44 +000048 shared_timed_mutex();
49 ~shared_timed_mutex();
Howard Hinnantba898e42013-09-21 01:49:28 +000050
David Majnemerf9f95be2014-03-17 20:19:44 +000051 shared_timed_mutex(const shared_timed_mutex&) = delete;
52 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
Howard Hinnantba898e42013-09-21 01:49:28 +000053
54 // Exclusive ownership
55 void lock(); // blocking
56 bool try_lock();
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);
61 void unlock();
62
63 // Shared ownership
64 void lock_shared(); // blocking
65 bool try_lock_shared();
66 template <class Rep, class Period>
67 bool
68 try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
69 template <class Clock, class Duration>
70 bool
71 try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
72 void unlock_shared();
73};
74
75template <class Mutex>
76class shared_lock
77{
78public:
79 typedef Mutex mutex_type;
80
81 // Shared locking
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);
93 ~shared_lock();
94
95 shared_lock(shared_lock const&) = delete;
96 shared_lock& operator=(shared_lock const&) = delete;
97
98 shared_lock(shared_lock&& u) noexcept;
99 shared_lock& operator=(shared_lock&& u) noexcept;
100
101 void lock(); // blocking
102 bool try_lock();
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);
107 void unlock();
108
109 // Setters
110 void swap(shared_lock& u) noexcept;
111 mutex_type* release() noexcept;
112
113 // Getters
114 bool owns_lock() const noexcept;
115 explicit operator bool () const noexcept;
116 mutex_type* mutex() const noexcept;
117};
118
119template <class Mutex>
120 void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
121
122} // std
123
124*/
125
126#include <__config>
127
128#if _LIBCPP_STD_VER > 11 || defined(_LIBCPP_BUILDING_SHARED_MUTEX)
129
130#include <__mutex_base>
131
132#include <__undef_min_max>
133
134#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
135#pragma GCC system_header
136#endif
137
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000138#ifdef _LIBCPP_HAS_NO_THREADS
139#error <shared_mutex> is not supported on this single threaded system
140#else // !_LIBCPP_HAS_NO_THREADS
141
Howard Hinnantba898e42013-09-21 01:49:28 +0000142_LIBCPP_BEGIN_NAMESPACE_STD
143
Marshall Clowabadb452015-06-30 14:04:14 +0000144struct _LIBCPP_TYPE_VIS __shared_mutex_base
Howard Hinnantba898e42013-09-21 01:49:28 +0000145{
146 mutex __mut_;
147 condition_variable __gate1_;
148 condition_variable __gate2_;
149 unsigned __state_;
150
151 static const unsigned __write_entered_ = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1);
152 static const unsigned __n_readers_ = ~__write_entered_;
Marshall Clowabadb452015-06-30 14:04:14 +0000153
154 __shared_mutex_base();
155 _LIBCPP_INLINE_VISIBILITY ~__shared_mutex_base() = default;
156
157 __shared_mutex_base(const __shared_mutex_base&) = delete;
158 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
159
160 // Exclusive ownership
161 void lock(); // blocking
162 bool try_lock();
163 void unlock();
164
165 // Shared ownership
166 void lock_shared(); // blocking
167 bool try_lock_shared();
168 void unlock_shared();
169
170// typedef implementation-defined native_handle_type; // See 30.2.3
171// native_handle_type native_handle(); // See 30.2.3
172};
173
174
175#if _LIBCPP_STD_VER > 14
176class _LIBCPP_TYPE_VIS shared_mutex
177{
178 __shared_mutex_base __base;
179public:
180 shared_mutex() : __base() {}
181 _LIBCPP_INLINE_VISIBILITY ~shared_mutex() = default;
182
183 shared_mutex(const shared_mutex&) = delete;
184 shared_mutex& operator=(const shared_mutex&) = delete;
185
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(); }
190
191 // Shared ownership
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(); }
195
196// typedef __shared_mutex_base::native_handle_type native_handle_type;
197// _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() { return __base::unlock_shared(); }
198};
199#endif
200
201
202class _LIBCPP_TYPE_VIS shared_timed_mutex
203{
204 __shared_mutex_base __base;
Howard Hinnantba898e42013-09-21 01:49:28 +0000205public:
David Majnemerf9f95be2014-03-17 20:19:44 +0000206 shared_timed_mutex();
207 _LIBCPP_INLINE_VISIBILITY ~shared_timed_mutex() = default;
Howard Hinnantba898e42013-09-21 01:49:28 +0000208
David Majnemerf9f95be2014-03-17 20:19:44 +0000209 shared_timed_mutex(const shared_timed_mutex&) = delete;
210 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
Howard Hinnantba898e42013-09-21 01:49:28 +0000211
212 // Exclusive ownership
213 void lock();
214 bool try_lock();
215 template <class _Rep, class _Period>
216 _LIBCPP_INLINE_VISIBILITY
217 bool
218 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
219 {
220 return try_lock_until(chrono::steady_clock::now() + __rel_time);
221 }
222 template <class _Clock, class _Duration>
223 bool
224 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
225 void unlock();
226
227 // Shared ownership
228 void lock_shared();
229 bool try_lock_shared();
230 template <class _Rep, class _Period>
231 _LIBCPP_INLINE_VISIBILITY
232 bool
233 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time)
234 {
235 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
236 }
237 template <class _Clock, class _Duration>
238 bool
239 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
240 void unlock_shared();
241};
242
243template <class _Clock, class _Duration>
244bool
David Majnemerf9f95be2014-03-17 20:19:44 +0000245shared_timed_mutex::try_lock_until(
Howard Hinnantba898e42013-09-21 01:49:28 +0000246 const chrono::time_point<_Clock, _Duration>& __abs_time)
247{
Marshall Clowabadb452015-06-30 14:04:14 +0000248 unique_lock<mutex> __lk(__base.__mut_);
249 if (__base.__state_ & __base.__write_entered_)
Howard Hinnantba898e42013-09-21 01:49:28 +0000250 {
251 while (true)
252 {
Marshall Clowabadb452015-06-30 14:04:14 +0000253 cv_status __status = __base.__gate1_.wait_until(__lk, __abs_time);
254 if ((__base.__state_ & __base.__write_entered_) == 0)
Howard Hinnantba898e42013-09-21 01:49:28 +0000255 break;
256 if (__status == cv_status::timeout)
257 return false;
258 }
259 }
Marshall Clowabadb452015-06-30 14:04:14 +0000260 __base.__state_ |= __base.__write_entered_;
261 if (__base.__state_ & __base.__n_readers_)
Howard Hinnantba898e42013-09-21 01:49:28 +0000262 {
263 while (true)
264 {
Marshall Clowabadb452015-06-30 14:04:14 +0000265 cv_status __status = __base.__gate2_.wait_until(__lk, __abs_time);
266 if ((__base.__state_ & __base.__n_readers_) == 0)
Howard Hinnantba898e42013-09-21 01:49:28 +0000267 break;
268 if (__status == cv_status::timeout)
269 {
Marshall Clowabadb452015-06-30 14:04:14 +0000270 __base.__state_ &= ~__base.__write_entered_;
271 __base.__gate1_.notify_all();
Howard Hinnantba898e42013-09-21 01:49:28 +0000272 return false;
273 }
274 }
275 }
276 return true;
277}
278
279template <class _Clock, class _Duration>
280bool
David Majnemerf9f95be2014-03-17 20:19:44 +0000281shared_timed_mutex::try_lock_shared_until(
Howard Hinnantba898e42013-09-21 01:49:28 +0000282 const chrono::time_point<_Clock, _Duration>& __abs_time)
283{
Marshall Clowabadb452015-06-30 14:04:14 +0000284 unique_lock<mutex> __lk(__base.__mut_);
285 if ((__base.__state_ & __base.__write_entered_) || (__base.__state_ & __base.__n_readers_) == __base.__n_readers_)
Howard Hinnantba898e42013-09-21 01:49:28 +0000286 {
287 while (true)
288 {
Marshall Clowabadb452015-06-30 14:04:14 +0000289 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_)
Howard Hinnantba898e42013-09-21 01:49:28 +0000292 break;
293 if (status == cv_status::timeout)
294 return false;
295 }
296 }
Marshall Clowabadb452015-06-30 14:04:14 +0000297 unsigned __num_readers = (__base.__state_ & __base.__n_readers_) + 1;
298 __base.__state_ &= ~__base.__n_readers_;
299 __base.__state_ |= __num_readers;
Howard Hinnantba898e42013-09-21 01:49:28 +0000300 return true;
301}
302
303template <class _Mutex>
304class shared_lock
305{
306public:
307 typedef _Mutex mutex_type;
308
309private:
310 mutex_type* __m_;
311 bool __owns_;
312
313public:
314 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000315 shared_lock() _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000316 : __m_(nullptr),
317 __owns_(false)
318 {}
319
320 _LIBCPP_INLINE_VISIBILITY
321 explicit shared_lock(mutex_type& __m)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700322 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000323 __owns_(true)
324 {__m_->lock_shared();}
325
326 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000327 shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700328 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000329 __owns_(false)
330 {}
331
332 _LIBCPP_INLINE_VISIBILITY
333 shared_lock(mutex_type& __m, try_to_lock_t)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700334 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000335 __owns_(__m.try_lock_shared())
336 {}
337
338 _LIBCPP_INLINE_VISIBILITY
339 shared_lock(mutex_type& __m, adopt_lock_t)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700340 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000341 __owns_(true)
342 {}
343
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)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700348 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000349 __owns_(__m.try_lock_shared_until(__abs_time))
350 {}
351
352 template <class _Rep, class _Period>
353 _LIBCPP_INLINE_VISIBILITY
354 shared_lock(mutex_type& __m,
355 const chrono::duration<_Rep, _Period>& __rel_time)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700356 : __m_(&__m),
Howard Hinnantba898e42013-09-21 01:49:28 +0000357 __owns_(__m.try_lock_shared_for(__rel_time))
358 {}
359
360 _LIBCPP_INLINE_VISIBILITY
361 ~shared_lock()
362 {
363 if (__owns_)
364 __m_->unlock_shared();
365 }
366
367 shared_lock(shared_lock const&) = delete;
368 shared_lock& operator=(shared_lock const&) = delete;
369
370 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000371 shared_lock(shared_lock&& __u) _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000372 : __m_(__u.__m_),
373 __owns_(__u.__owns_)
374 {
375 __u.__m_ = nullptr;
376 __u.__owns_ = false;
377 }
378
379 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000380 shared_lock& operator=(shared_lock&& __u) _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000381 {
382 if (__owns_)
383 __m_->unlock_shared();
384 __m_ = nullptr;
385 __owns_ = false;
386 __m_ = __u.__m_;
387 __owns_ = __u.__owns_;
388 __u.__m_ = nullptr;
389 __u.__owns_ = false;
390 return *this;
391 }
392
393 void lock();
394 bool try_lock();
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);
399 void unlock();
400
401 // Setters
402 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000403 void swap(shared_lock& __u) _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000404 {
405 _VSTD::swap(__m_, __u.__m_);
406 _VSTD::swap(__owns_, __u.__owns_);
407 }
408
409 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000410 mutex_type* release() _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000411 {
412 mutex_type* __m = __m_;
413 __m_ = nullptr;
414 __owns_ = false;
415 return __m;
416 }
417
418 // Getters
419 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000420 bool owns_lock() const _NOEXCEPT {return __owns_;}
Howard Hinnantba898e42013-09-21 01:49:28 +0000421
422 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000423 explicit operator bool () const _NOEXCEPT {return __owns_;}
Howard Hinnantba898e42013-09-21 01:49:28 +0000424
425 _LIBCPP_INLINE_VISIBILITY
Marshall Clow861f1e92014-08-25 14:53:16 +0000426 mutex_type* mutex() const _NOEXCEPT {return __m_;}
Howard Hinnantba898e42013-09-21 01:49:28 +0000427};
428
429template <class _Mutex>
430void
431shared_lock<_Mutex>::lock()
432{
433 if (__m_ == nullptr)
434 __throw_system_error(EPERM, "shared_lock::lock: references null mutex");
435 if (__owns_)
436 __throw_system_error(EDEADLK, "shared_lock::lock: already locked");
437 __m_->lock_shared();
438 __owns_ = true;
439}
440
441template <class _Mutex>
442bool
443shared_lock<_Mutex>::try_lock()
444{
445 if (__m_ == nullptr)
446 __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex");
447 if (__owns_)
448 __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked");
449 __owns_ = __m_->try_lock_shared();
450 return __owns_;
451}
452
453template <class _Mutex>
454template <class _Rep, class _Period>
455bool
456shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d)
457{
458 if (__m_ == nullptr)
459 __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex");
460 if (__owns_)
461 __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked");
462 __owns_ = __m_->try_lock_shared_for(__d);
463 return __owns_;
464}
465
466template <class _Mutex>
467template <class _Clock, class _Duration>
468bool
469shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
470{
471 if (__m_ == nullptr)
472 __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex");
473 if (__owns_)
474 __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked");
475 __owns_ = __m_->try_lock_shared_until(__t);
476 return __owns_;
477}
478
479template <class _Mutex>
480void
481shared_lock<_Mutex>::unlock()
482{
483 if (!__owns_)
484 __throw_system_error(EPERM, "shared_lock::unlock: not locked");
485 __m_->unlock_shared();
486 __owns_ = false;
487}
488
489template <class _Mutex>
490inline _LIBCPP_INLINE_VISIBILITY
491void
Marshall Clow861f1e92014-08-25 14:53:16 +0000492swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT
Howard Hinnantba898e42013-09-21 01:49:28 +0000493 {__x.swap(__y);}
494
495_LIBCPP_END_NAMESPACE_STD
496
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000497#endif // !_LIBCPP_HAS_NO_THREADS
498
Howard Hinnantba898e42013-09-21 01:49:28 +0000499#endif // _LIBCPP_STD_VER > 11
500
501#endif // _LIBCPP_SHARED_MUTEX