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() { |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 41 | { |
| 42 | lock_guard<mutex> _(__mut_); |
| 43 | __state_ = 0; |
| 44 | } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 45 | __gate1_.notify_all(); |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Shared ownership |
| 49 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 50 | void __shared_mutex_base::lock_shared() { |
| 51 | unique_lock<mutex> lk(__mut_); |
| 52 | while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) |
| 53 | __gate1_.wait(lk); |
| 54 | unsigned num_readers = (__state_ & __n_readers_) + 1; |
| 55 | __state_ &= ~__n_readers_; |
| 56 | __state_ |= num_readers; |
| 57 | } |
| 58 | |
| 59 | bool __shared_mutex_base::try_lock_shared() { |
| 60 | unique_lock<mutex> lk(__mut_); |
| 61 | unsigned num_readers = __state_ & __n_readers_; |
| 62 | if (!(__state_ & __write_entered_) && num_readers != __n_readers_) { |
| 63 | ++num_readers; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 64 | __state_ &= ~__n_readers_; |
| 65 | __state_ |= num_readers; |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | return false; |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 69 | } |
| 70 | |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 71 | void __shared_mutex_base::unlock_shared() { |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 72 | unique_lock<mutex> lk(__mut_); |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 73 | unsigned num_readers = (__state_ & __n_readers_) - 1; |
| 74 | __state_ &= ~__n_readers_; |
| 75 | __state_ |= num_readers; |
| 76 | if (__state_ & __write_entered_) { |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 77 | if (num_readers == 0) { |
| 78 | lk.unlock(); |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 79 | __gate2_.notify_one(); |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 80 | } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 81 | } else { |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 82 | if (num_readers == __n_readers_ - 1) { |
| 83 | lk.unlock(); |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 84 | __gate1_.notify_one(); |
Brotcrunsher | ab6c89c | 2025-01-13 15:16:23 | [diff] [blame^] | 85 | } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 86 | } |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 87 | } |
| 88 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 89 | // Shared Timed Mutex |
| 90 | // These routines are here for ABI stability |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 91 | shared_timed_mutex::shared_timed_mutex() : __base_() {} |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 92 | void shared_timed_mutex::lock() { return __base_.lock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 93 | bool shared_timed_mutex::try_lock() { return __base_.try_lock(); } |
Louis Dionne | 2d7eb9c | 2023-07-04 15:19:10 | [diff] [blame] | 94 | void shared_timed_mutex::unlock() { return __base_.unlock(); } |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame] | 95 | void shared_timed_mutex::lock_shared() { return __base_.lock_shared(); } |
| 96 | bool shared_timed_mutex::try_lock_shared() { return __base_.try_lock_shared(); } |
| 97 | void shared_timed_mutex::unlock_shared() { return __base_.unlock_shared(); } |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 98 | |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 99 | _LIBCPP_END_NAMESPACE_STD |