1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SHARED_MUTEX
11#define _LIBCPP_SHARED_MUTEX
12
13/*
14 shared_mutex synopsis
15
16// C++1y
17
18namespace std
19{
20
21class shared_mutex // C++17
22{
23public:
24 shared_mutex();
25 ~shared_mutex();
26
27 shared_mutex(const shared_mutex&) = delete;
28 shared_mutex& operator=(const shared_mutex&) = delete;
29
30 // Exclusive ownership
31 void lock(); // blocking
32 bool try_lock();
33 void unlock();
34
35 // Shared ownership
36 void lock_shared(); // blocking
37 bool try_lock_shared();
38 void unlock_shared();
39
40 typedef implementation-defined native_handle_type; // See 30.2.3
41 native_handle_type native_handle(); // See 30.2.3
42};
43
44class shared_timed_mutex
45{
46public:
47 shared_timed_mutex();
48 ~shared_timed_mutex();
49
50 shared_timed_mutex(const shared_timed_mutex&) = delete;
51 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
52
53 // Exclusive ownership
54 void lock(); // blocking
55 bool try_lock();
56 template <class Rep, class Period>
57 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
58 template <class Clock, class Duration>
59 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
60 void unlock();
61
62 // Shared ownership
63 void lock_shared(); // blocking
64 bool try_lock_shared();
65 template <class Rep, class Period>
66 bool
67 try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
68 template <class Clock, class Duration>
69 bool
70 try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
71 void unlock_shared();
72};
73
74template <class Mutex>
75class shared_lock
76{
77public:
78 typedef Mutex mutex_type;
79
80 // Shared locking
81 shared_lock() noexcept;
82 explicit shared_lock(mutex_type& m); // blocking
83 shared_lock(mutex_type& m, defer_lock_t) noexcept;
84 shared_lock(mutex_type& m, try_to_lock_t);
85 shared_lock(mutex_type& m, adopt_lock_t);
86 template <class Clock, class Duration>
87 shared_lock(mutex_type& m,
88 const chrono::time_point<Clock, Duration>& abs_time);
89 template <class Rep, class Period>
90 shared_lock(mutex_type& m,
91 const chrono::duration<Rep, Period>& rel_time);
92 ~shared_lock();
93
94 shared_lock(shared_lock const&) = delete;
95 shared_lock& operator=(shared_lock const&) = delete;
96
97 shared_lock(shared_lock&& u) noexcept;
98 shared_lock& operator=(shared_lock&& u) noexcept;
99
100 void lock(); // blocking
101 bool try_lock();
102 template <class Rep, class Period>
103 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
104 template <class Clock, class Duration>
105 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
106 void unlock();
107
108 // Setters
109 void swap(shared_lock& u) noexcept;
110 mutex_type* release() noexcept;
111
112 // Getters
113 bool owns_lock() const noexcept;
114 explicit operator bool () const noexcept;
115 mutex_type* mutex() const noexcept;
116};
117
118template <class Mutex>
119 void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
120
121} // std
122
123*/
124
125#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
126# include <__cxx03/__config>
127#else
128# include <__config>
129
130# if _LIBCPP_HAS_THREADS
131
132# include <__chrono/duration.h>
133# include <__chrono/steady_clock.h>
134# include <__chrono/time_point.h>
135# include <__condition_variable/condition_variable.h>
136# include <__memory/addressof.h>
137# include <__mutex/mutex.h>
138# include <__mutex/tag_types.h>
139# include <__mutex/unique_lock.h>
140# include <__system_error/throw_system_error.h>
141# include <__utility/move.h>
142# include <__utility/swap.h>
143# include <cerrno>
144# include <version>
145
146_LIBCPP_PUSH_MACROS
147# include <__undef_macros>
148
149# if _LIBCPP_STD_VER >= 14
150
151# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
152# pragma GCC system_header
153# endif
154
155_LIBCPP_BEGIN_NAMESPACE_STD
156
157struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
158 mutex __mut_;
159 condition_variable __gate1_;
160 condition_variable __gate2_;
161 unsigned __state_;
162
163 static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
164 static const unsigned __n_readers_ = ~__write_entered_;
165
166 __shared_mutex_base();
167 _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
168
169 __shared_mutex_base(const __shared_mutex_base&) = delete;
170 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
171
172 // Exclusive ownership
173 void lock(); // blocking
174 bool try_lock();
175 void unlock();
176
177 // Shared ownership
178 void lock_shared(); // blocking
179 bool try_lock_shared();
180 void unlock_shared();
181
182 // typedef implementation-defined native_handle_type; // See 30.2.3
183 // native_handle_type native_handle(); // See 30.2.3
184};
185
186# if _LIBCPP_STD_VER >= 17
187class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("shared_mutex") shared_mutex {
188 __shared_mutex_base __base_;
189
190public:
191 _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
192 _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
193
194 shared_mutex(const shared_mutex&) = delete;
195 shared_mutex& operator=(const shared_mutex&) = delete;
196
197 // Exclusive ownership
198 _LIBCPP_ACQUIRE_CAPABILITY() _LIBCPP_HIDE_FROM_ABI void lock() { return __base_.lock(); }
199 _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool try_lock() { return __base_.try_lock(); }
200 _LIBCPP_RELEASE_CAPABILITY _LIBCPP_HIDE_FROM_ABI void unlock() { return __base_.unlock(); }
201
202 // Shared ownership
203 _LIBCPP_ACQUIRE_SHARED_CAPABILITY _LIBCPP_HIDE_FROM_ABI void lock_shared() { return __base_.lock_shared(); }
204 _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool try_lock_shared() {
205 return __base_.try_lock_shared();
206 }
207 _LIBCPP_RELEASE_SHARED_CAPABILITY _LIBCPP_HIDE_FROM_ABI void unlock_shared() { return __base_.unlock_shared(); }
208
209 // typedef __shared_mutex_base::native_handle_type native_handle_type;
210 // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
211};
212# endif
213
214class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("shared_timed_mutex") shared_timed_mutex {
215 __shared_mutex_base __base_;
216
217public:
218 shared_timed_mutex();
219 _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
220
221 shared_timed_mutex(const shared_timed_mutex&) = delete;
222 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
223
224 // Exclusive ownership
225 void lock() _LIBCPP_ACQUIRE_CAPABILITY();
226 _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) bool try_lock();
227 template <class _Rep, class _Period>
228 _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
229 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time) {
230 return try_lock_until(chrono::steady_clock::now() + __rel_time);
231 }
232
233 template <class _Clock, class _Duration>
234 _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
235 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
236 unique_lock<mutex> __lk(__base_.__mut_);
237 if (__base_.__state_ & __base_.__write_entered_) {
238 while (true) {
239 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
240 if ((__base_.__state_ & __base_.__write_entered_) == 0)
241 break;
242 if (__status == cv_status::timeout)
243 return false;
244 }
245 }
246 __base_.__state_ |= __base_.__write_entered_;
247 if (__base_.__state_ & __base_.__n_readers_) {
248 while (true) {
249 cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
250 if ((__base_.__state_ & __base_.__n_readers_) == 0)
251 break;
252 if (__status == cv_status::timeout) {
253 __base_.__state_ &= ~__base_.__write_entered_;
254 __base_.__gate1_.notify_all();
255 return false;
256 }
257 }
258 }
259 return true;
260 }
261
262 _LIBCPP_RELEASE_CAPABILITY void unlock();
263
264 // Shared ownership
265 _LIBCPP_ACQUIRE_SHARED_CAPABILITY void lock_shared();
266 _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) bool try_lock_shared();
267 template <class _Rep, class _Period>
268 _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
269 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time) {
270 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
271 }
272
273 template <class _Clock, class _Duration>
274 _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
275 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
276 unique_lock<mutex> __lk(__base_.__mut_);
277 if ((__base_.__state_ & __base_.__write_entered_) ||
278 (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
279 while (true) {
280 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
281 if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
282 (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
283 break;
284 if (__status == cv_status::timeout)
285 return false;
286 }
287 }
288 unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
289 __base_.__state_ &= ~__base_.__n_readers_;
290 __base_.__state_ |= __num_readers;
291 return true;
292 }
293
294 _LIBCPP_RELEASE_SHARED_CAPABILITY void unlock_shared();
295};
296
297template <class _Mutex>
298class shared_lock {
299public:
300 typedef _Mutex mutex_type;
301
302private:
303 mutex_type* __m_;
304 bool __owns_;
305
306public:
307 _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
308
309 _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m) : __m_(std::addressof(__m)), __owns_(true) {
310 __m_->lock_shared();
311 }
312
313 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
314 : __m_(std::addressof(__m)),
315 __owns_(false) {}
316
317 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
318 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {}
319
320 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t) : __m_(std::addressof(__m)), __owns_(true) {}
321
322 template <class _Clock, class _Duration>
323 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
324 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
325
326 template <class _Rep, class _Period>
327 _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
328 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
329
330 _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
331 if (__owns_)
332 __m_->unlock_shared();
333 }
334
335 shared_lock(shared_lock const&) = delete;
336 shared_lock& operator=(shared_lock const&) = delete;
337
338 _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT : __m_(__u.__m_), __owns_(__u.__owns_) {
339 __u.__m_ = nullptr;
340 __u.__owns_ = false;
341 }
342
343 _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
344 if (this != std::addressof(__u))
345 shared_lock(std::move(__u)).swap(*this);
346 return *this;
347 }
348
349 _LIBCPP_HIDE_FROM_ABI void lock();
350 _LIBCPP_HIDE_FROM_ABI bool try_lock();
351 template <class _Rep, class _Period>
352 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
353 template <class _Clock, class _Duration>
354 _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
355 _LIBCPP_HIDE_FROM_ABI void unlock();
356
357 // Setters
358 _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
359 std::swap(__m_, __u.__m_);
360 std::swap(__owns_, __u.__owns_);
361 }
362
363 _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
364 mutex_type* __m = __m_;
365 __m_ = nullptr;
366 __owns_ = false;
367 return __m;
368 }
369
370 // Getters
371 _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
372
373 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
374
375 _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
376};
377_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
378
379template <class _Mutex>
380void shared_lock<_Mutex>::lock() {
381 if (__m_ == nullptr)
382 std::__throw_system_error(EPERM, what_arg: "shared_lock::lock: references null mutex");
383 if (__owns_)
384 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::lock: already locked");
385 __m_->lock_shared();
386 __owns_ = true;
387}
388
389template <class _Mutex>
390bool shared_lock<_Mutex>::try_lock() {
391 if (__m_ == nullptr)
392 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock: references null mutex");
393 if (__owns_)
394 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock: already locked");
395 __owns_ = __m_->try_lock_shared();
396 return __owns_;
397}
398
399template <class _Mutex>
400template <class _Rep, class _Period>
401bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
402 if (__m_ == nullptr)
403 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock_for: references null mutex");
404 if (__owns_)
405 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock_for: already locked");
406 __owns_ = __m_->try_lock_shared_for(__d);
407 return __owns_;
408}
409
410template <class _Mutex>
411template <class _Clock, class _Duration>
412bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
413 if (__m_ == nullptr)
414 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock_until: references null mutex");
415 if (__owns_)
416 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock_until: already locked");
417 __owns_ = __m_->try_lock_shared_until(__t);
418 return __owns_;
419}
420
421template <class _Mutex>
422void shared_lock<_Mutex>::unlock() {
423 if (!__owns_)
424 std::__throw_system_error(EPERM, what_arg: "shared_lock::unlock: not locked");
425 __m_->unlock_shared();
426 __owns_ = false;
427}
428
429template <class _Mutex>
430inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
431 __x.swap(__y);
432}
433
434_LIBCPP_END_NAMESPACE_STD
435
436# endif // _LIBCPP_STD_VER >= 14
437
438_LIBCPP_POP_MACROS
439
440# endif // _LIBCPP_HAS_THREADS
441
442# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
443# include <optional>
444# include <system_error>
445# endif
446#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
447
448#endif // _LIBCPP_SHARED_MUTEX
449