Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 9 | #include <__config> |
| 10 | |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 12 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 13 | # include <mutex> |
| 14 | # include <shared_mutex> |
| 15 | # if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
| 16 | # pragma comment(lib, "pthread") |
| 17 | # endif |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 18 | |
| 19 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 20 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 21 | // Shared Mutex Base |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 22 | __shared_mutex_base::__shared_mutex_base() : __state_(0) {} |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 23 | |
| 24 | // Exclusive ownership |
| 25 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 26 | void __shared_mutex_base::lock() { |
| 27 | unique_lock<mutex> lk(__mut_); |
| 28 | while (__state_ & __write_entered_) |
| 29 | __gate1_.wait(lk); |
| 30 | __state_ |= __write_entered_; |
| 31 | while (__state_ & __n_readers_) |
| 32 | __gate2_.wait(lk); |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 33 | } |
| 34 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 35 | bool __shared_mutex_base::try_lock() { |
| 36 | unique_lock<mutex> lk(__mut_); |
| 37 | if (__state_ == 0) { |
| 38 | __state_ = __write_entered_; |
| 39 | return true; |
| 40 | } |
| 41 | return false; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 42 | } |
| 43 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 44 | void __shared_mutex_base::unlock() { |
| 45 | lock_guard<mutex> _(__mut_); |
| 46 | __state_ = 0; |
| 47 | __gate1_.notify_all(); |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Shared ownership |
| 51 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 52 | void __shared_mutex_base::lock_shared() { |
| 53 | unique_lock<mutex> lk(__mut_); |
| 54 | while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) |
| 55 | __gate1_.wait(lk); |
| 56 | unsigned num_readers = (__state_ & __n_readers_) + 1; |
| 57 | __state_ &= ~__n_readers_; |
| 58 | __state_ |= num_readers; |
| 59 | } |
| 60 | |
| 61 | bool __shared_mutex_base::try_lock_shared() { |
| 62 | unique_lock<mutex> lk(__mut_); |
| 63 | unsigned num_readers = __state_ & __n_readers_; |
| 64 | if (!(__state_ & __write_entered_) && num_readers != __n_readers_) { |
| 65 | ++num_readers; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 66 | __state_ &= ~__n_readers_; |
| 67 | __state_ |= num_readers; |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 68 | return true; |
| 69 | } |
| 70 | return false; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 71 | } |
| 72 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 73 | void __shared_mutex_base::unlock_shared() { |
| 74 | lock_guard<mutex> _(__mut_); |
| 75 | unsigned num_readers = (__state_ & __n_readers_) - 1; |
| 76 | __state_ &= ~__n_readers_; |
| 77 | __state_ |= num_readers; |
| 78 | if (__state_ & __write_entered_) { |
| 79 | if (num_readers == 0) |
| 80 | __gate2_.notify_one(); |
| 81 | } else { |
| 82 | if (num_readers == __n_readers_ - 1) |
| 83 | __gate1_.notify_one(); |
| 84 | } |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 85 | } |
| 86 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 87 | // Shared Timed Mutex |
| 88 | // These routines are here for ABI stability |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 89 | shared_timed_mutex::shared_timed_mutex() : __base_() {} |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 90 | void shared_timed_mutex::lock() { return __base_.lock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 91 | bool shared_timed_mutex::try_lock() { return __base_.try_lock(); } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame^] | 92 | void shared_timed_mutex::unlock() { return __base_.unlock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 93 | void shared_timed_mutex::lock_shared() { return __base_.lock_shared(); } |
| 94 | bool shared_timed_mutex::try_lock_shared() { return __base_.try_lock_shared(); } |
| 95 | void shared_timed_mutex::unlock_shared() { return __base_.unlock_shared(); } |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 96 | |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 97 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 98 | |
| 99 | #endif // !_LIBCPP_HAS_NO_THREADS |