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_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
157
158struct _LIBCPP_EXPORTED_FROM_ABI __shared_mutex_base {
159 mutex __mut_;
160 condition_variable __gate1_;
161 condition_variable __gate2_;
162 unsigned __state_;
163
164 static const unsigned __write_entered_ = 1U << (sizeof(unsigned) * __CHAR_BIT__ - 1);
165 static const unsigned __n_readers_ = ~__write_entered_;
166
167 __shared_mutex_base();
168 _LIBCPP_HIDE_FROM_ABI ~__shared_mutex_base() = default;
169
170 __shared_mutex_base(const __shared_mutex_base&) = delete;
171 __shared_mutex_base& operator=(const __shared_mutex_base&) = delete;
172
173 // Exclusive ownership
174 void lock(); // blocking
175 bool try_lock();
176 void unlock();
177
178 // Shared ownership
179 void lock_shared(); // blocking
180 bool try_lock_shared();
181 void unlock_shared();
182
183 // typedef implementation-defined native_handle_type; // See 30.2.3
184 // native_handle_type native_handle(); // See 30.2.3
185};
186
187# if _LIBCPP_STD_VER >= 17
188class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("shared_mutex") shared_mutex {
189 __shared_mutex_base __base_;
190
191public:
192 _LIBCPP_HIDE_FROM_ABI shared_mutex() : __base_() {}
193 _LIBCPP_HIDE_FROM_ABI ~shared_mutex() = default;
194
195 shared_mutex(const shared_mutex&) = delete;
196 shared_mutex& operator=(const shared_mutex&) = delete;
197
198 // Exclusive ownership
199 _LIBCPP_ACQUIRE_CAPABILITY() _LIBCPP_HIDE_FROM_ABI void lock() { return __base_.lock(); }
200 [[nodiscard]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool try_lock() {
201 return __base_.try_lock();
202 }
203 _LIBCPP_RELEASE_CAPABILITY _LIBCPP_HIDE_FROM_ABI void unlock() { return __base_.unlock(); }
204
205 // Shared ownership
206 _LIBCPP_ACQUIRE_SHARED_CAPABILITY _LIBCPP_HIDE_FROM_ABI void lock_shared() { return __base_.lock_shared(); }
207 [[nodiscard]] _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool try_lock_shared() {
208 return __base_.try_lock_shared();
209 }
210 _LIBCPP_RELEASE_SHARED_CAPABILITY _LIBCPP_HIDE_FROM_ABI void unlock_shared() { return __base_.unlock_shared(); }
211
212 // typedef __shared_mutex_base::native_handle_type native_handle_type;
213 // _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __base::unlock_shared(); }
214};
215# endif
216
217class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("shared_timed_mutex") shared_timed_mutex {
218 __shared_mutex_base __base_;
219
220public:
221 shared_timed_mutex();
222 _LIBCPP_HIDE_FROM_ABI ~shared_timed_mutex() = default;
223
224 shared_timed_mutex(const shared_timed_mutex&) = delete;
225 shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
226
227 // Exclusive ownership
228 void lock() _LIBCPP_ACQUIRE_CAPABILITY();
229 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) bool try_lock();
230 template <class _Rep, class _Period>
231 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
232 try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time) {
233 return try_lock_until(chrono::steady_clock::now() + __rel_time);
234 }
235
236 template <class _Clock, class _Duration>
237 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
238 try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
239 unique_lock<mutex> __lk(__base_.__mut_);
240 if (__base_.__state_ & __base_.__write_entered_) {
241 while (true) {
242 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
243 if ((__base_.__state_ & __base_.__write_entered_) == 0)
244 break;
245 if (__status == cv_status::timeout)
246 return false;
247 }
248 }
249 __base_.__state_ |= __base_.__write_entered_;
250 if (__base_.__state_ & __base_.__n_readers_) {
251 while (true) {
252 cv_status __status = __base_.__gate2_.wait_until(__lk, __abs_time);
253 if ((__base_.__state_ & __base_.__n_readers_) == 0)
254 break;
255 if (__status == cv_status::timeout) {
256 __base_.__state_ &= ~__base_.__write_entered_;
257 __base_.__gate1_.notify_all();
258 return false;
259 }
260 }
261 }
262 return true;
263 }
264
265 _LIBCPP_RELEASE_CAPABILITY void unlock();
266
267 // Shared ownership
268 _LIBCPP_ACQUIRE_SHARED_CAPABILITY void lock_shared();
269 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) bool try_lock_shared();
270 template <class _Rep, class _Period>
271 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
272 try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time) {
273 return try_lock_shared_until(chrono::steady_clock::now() + __rel_time);
274 }
275
276 template <class _Clock, class _Duration>
277 [[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_SHARED_CAPABILITY(true) _LIBCPP_HIDE_FROM_ABI bool
278 try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time) {
279 unique_lock<mutex> __lk(__base_.__mut_);
280 if ((__base_.__state_ & __base_.__write_entered_) ||
281 (__base_.__state_ & __base_.__n_readers_) == __base_.__n_readers_) {
282 while (true) {
283 cv_status __status = __base_.__gate1_.wait_until(__lk, __abs_time);
284 if ((__base_.__state_ & __base_.__write_entered_) == 0 &&
285 (__base_.__state_ & __base_.__n_readers_) < __base_.__n_readers_)
286 break;
287 if (__status == cv_status::timeout)
288 return false;
289 }
290 }
291 unsigned __num_readers = (__base_.__state_ & __base_.__n_readers_) + 1;
292 __base_.__state_ &= ~__base_.__n_readers_;
293 __base_.__state_ |= __num_readers;
294 return true;
295 }
296
297 _LIBCPP_RELEASE_SHARED_CAPABILITY void unlock_shared();
298};
299
300template <class _Mutex>
301class shared_lock {
302public:
303 typedef _Mutex mutex_type;
304
305private:
306 mutex_type* __m_;
307 bool __owns_;
308
309public:
310 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
311
312 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI explicit shared_lock(mutex_type& __m)
313 : __m_(std::addressof(__m)), __owns_(true) {
314 __m_->lock_shared();
315 }
316
317 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
318 : __m_(std::addressof(__m)),
319 __owns_(false) {}
320
321 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, try_to_lock_t)
322 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared()) {}
323
324 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_lock(mutex_type& __m, adopt_lock_t)
325 : __m_(std::addressof(__m)), __owns_(true) {}
326
327 template <class _Clock, class _Duration>
328 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
329 shared_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __abs_time)
330 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_until(__abs_time)) {}
331
332 template <class _Rep, class _Period>
333 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
334 shared_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __rel_time)
335 : __m_(std::addressof(__m)), __owns_(__m.try_lock_shared_for(__rel_time)) {}
336
337 _LIBCPP_HIDE_FROM_ABI ~shared_lock() {
338 if (__owns_)
339 __m_->unlock_shared();
340 }
341
342 shared_lock(shared_lock const&) = delete;
343 shared_lock& operator=(shared_lock const&) = delete;
344
345 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_lock(shared_lock&& __u) _NOEXCEPT
346 : __m_(__u.__m_),
347 __owns_(__u.__owns_) {
348 __u.__m_ = nullptr;
349 __u.__owns_ = false;
350 }
351
352 _LIBCPP_HIDE_FROM_ABI shared_lock& operator=(shared_lock&& __u) _NOEXCEPT {
353 if (this != std::addressof(__u))
354 shared_lock(std::move(__u)).swap(u&: *this);
355 return *this;
356 }
357
358 _LIBCPP_HIDE_FROM_ABI void lock();
359 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock();
360 template <class _Rep, class _Period>
361 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time);
362 template <class _Clock, class _Duration>
363 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
364 _LIBCPP_HIDE_FROM_ABI void unlock();
365
366 // Setters
367 _LIBCPP_HIDE_FROM_ABI void swap(shared_lock& __u) _NOEXCEPT {
368 std::swap(__m_, __u.__m_);
369 std::swap(x&: __owns_, y&: __u.__owns_);
370 }
371
372 _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
373 mutex_type* __m = __m_;
374 __m_ = nullptr;
375 __owns_ = false;
376 return __m;
377 }
378
379 // Getters
380 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
381
382 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
383
384 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
385};
386_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(shared_lock);
387
388template <class _Mutex>
389void shared_lock<_Mutex>::lock() {
390 if (__m_ == nullptr)
391 std::__throw_system_error(EPERM, what_arg: "shared_lock::lock: references null mutex");
392 if (__owns_)
393 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::lock: already locked");
394 __m_->lock_shared();
395 __owns_ = true;
396}
397
398template <class _Mutex>
399bool shared_lock<_Mutex>::try_lock() {
400 if (__m_ == nullptr)
401 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock: references null mutex");
402 if (__owns_)
403 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock: already locked");
404 __owns_ = __m_->try_lock_shared();
405 return __owns_;
406}
407
408template <class _Mutex>
409template <class _Rep, class _Period>
410bool shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
411 if (__m_ == nullptr)
412 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock_for: references null mutex");
413 if (__owns_)
414 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock_for: already locked");
415 __owns_ = __m_->try_lock_shared_for(__d);
416 return __owns_;
417}
418
419template <class _Mutex>
420template <class _Clock, class _Duration>
421bool shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
422 if (__m_ == nullptr)
423 std::__throw_system_error(EPERM, what_arg: "shared_lock::try_lock_until: references null mutex");
424 if (__owns_)
425 std::__throw_system_error(EDEADLK, what_arg: "shared_lock::try_lock_until: already locked");
426 __owns_ = __m_->try_lock_shared_until(__t);
427 return __owns_;
428}
429
430template <class _Mutex>
431void shared_lock<_Mutex>::unlock() {
432 if (!__owns_)
433 std::__throw_system_error(EPERM, what_arg: "shared_lock::unlock: not locked");
434 __m_->unlock_shared();
435 __owns_ = false;
436}
437
438template <class _Mutex>
439inline _LIBCPP_HIDE_FROM_ABI void swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT {
440 __x.swap(__y);
441}
442
443_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
444_LIBCPP_END_NAMESPACE_STD
445
446# endif // _LIBCPP_STD_VER >= 14
447
448_LIBCPP_POP_MACROS
449
450# endif // _LIBCPP_HAS_THREADS
451
452# if defined(_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23) && _LIBCPP_STD_VER <= 20
453# include <optional>
454# include <system_error>
455# endif
456#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
457
458#endif // _LIBCPP_SHARED_MUTEX
459