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