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 | |
Louis Dionne | cf7d4f5 | 2023-11-06 00:08:24 | [diff] [blame^] | 9 | #include <mutex> |
| 10 | #include <shared_mutex> |
| 11 | #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
| 12 | # pragma comment(lib, "pthread") |
| 13 | #endif |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 14 | |
| 15 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 16 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 17 | // Shared Mutex Base |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 18 | __shared_mutex_base::__shared_mutex_base() : __state_(0) {} |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 19 | |
| 20 | // Exclusive ownership |
| 21 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 22 | void __shared_mutex_base::lock() { |
| 23 | unique_lock<mutex> lk(__mut_); |
| 24 | while (__state_ & __write_entered_) |
| 25 | __gate1_.wait(lk); |
| 26 | __state_ |= __write_entered_; |
| 27 | while (__state_ & __n_readers_) |
| 28 | __gate2_.wait(lk); |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 29 | } |
| 30 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 31 | bool __shared_mutex_base::try_lock() { |
| 32 | unique_lock<mutex> lk(__mut_); |
| 33 | if (__state_ == 0) { |
| 34 | __state_ = __write_entered_; |
| 35 | return true; |
| 36 | } |
| 37 | return false; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 38 | } |
| 39 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 40 | void __shared_mutex_base::unlock() { |
| 41 | lock_guard<mutex> _(__mut_); |
| 42 | __state_ = 0; |
| 43 | __gate1_.notify_all(); |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Shared ownership |
| 47 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 48 | void __shared_mutex_base::lock_shared() { |
| 49 | unique_lock<mutex> lk(__mut_); |
| 50 | while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) |
| 51 | __gate1_.wait(lk); |
| 52 | unsigned num_readers = (__state_ & __n_readers_) + 1; |
| 53 | __state_ &= ~__n_readers_; |
| 54 | __state_ |= num_readers; |
| 55 | } |
| 56 | |
| 57 | bool __shared_mutex_base::try_lock_shared() { |
| 58 | unique_lock<mutex> lk(__mut_); |
| 59 | unsigned num_readers = __state_ & __n_readers_; |
| 60 | if (!(__state_ & __write_entered_) && num_readers != __n_readers_) { |
| 61 | ++num_readers; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 62 | __state_ &= ~__n_readers_; |
| 63 | __state_ |= num_readers; |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | return false; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 67 | } |
| 68 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 69 | void __shared_mutex_base::unlock_shared() { |
| 70 | lock_guard<mutex> _(__mut_); |
| 71 | unsigned num_readers = (__state_ & __n_readers_) - 1; |
| 72 | __state_ &= ~__n_readers_; |
| 73 | __state_ |= num_readers; |
| 74 | if (__state_ & __write_entered_) { |
| 75 | if (num_readers == 0) |
| 76 | __gate2_.notify_one(); |
| 77 | } else { |
| 78 | if (num_readers == __n_readers_ - 1) |
| 79 | __gate1_.notify_one(); |
| 80 | } |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 81 | } |
| 82 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 83 | // Shared Timed Mutex |
| 84 | // These routines are here for ABI stability |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 85 | shared_timed_mutex::shared_timed_mutex() : __base_() {} |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 86 | void shared_timed_mutex::lock() { return __base_.lock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 87 | bool shared_timed_mutex::try_lock() { return __base_.try_lock(); } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 88 | void shared_timed_mutex::unlock() { return __base_.unlock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 89 | void shared_timed_mutex::lock_shared() { return __base_.lock_shared(); } |
| 90 | bool shared_timed_mutex::try_lock_shared() { return __base_.try_lock_shared(); } |
| 91 | void shared_timed_mutex::unlock_shared() { return __base_.unlock_shared(); } |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 92 | |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 93 | _LIBCPP_END_NAMESPACE_STD |