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 | |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 13 | #include <shared_mutex> |
Michał Górny | a9b5fff | 2019-12-02 10:49:20 | [diff] [blame] | 14 | #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB) |
Arthur O'Dwyer | bbb0f2c | 2022-02-11 18:00:39 | [diff] [blame] | 15 | # pragma comment(lib, "pthread") |
Petr Hosek | 996e62e | 2019-05-30 01:34:41 | [diff] [blame] | 16 | #endif |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 17 | |
| 18 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 19 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 20 | // Shared Mutex Base |
| 21 | __shared_mutex_base::__shared_mutex_base() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 22 | : __state_(0) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | // Exclusive ownership |
| 27 | |
| 28 | void |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 29 | __shared_mutex_base::lock() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 30 | { |
| 31 | unique_lock<mutex> lk(__mut_); |
| 32 | while (__state_ & __write_entered_) |
| 33 | __gate1_.wait(lk); |
| 34 | __state_ |= __write_entered_; |
| 35 | while (__state_ & __n_readers_) |
| 36 | __gate2_.wait(lk); |
| 37 | } |
| 38 | |
| 39 | bool |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 40 | __shared_mutex_base::try_lock() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 41 | { |
| 42 | unique_lock<mutex> lk(__mut_); |
| 43 | if (__state_ == 0) |
| 44 | { |
| 45 | __state_ = __write_entered_; |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | void |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 52 | __shared_mutex_base::unlock() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 53 | { |
| 54 | lock_guard<mutex> _(__mut_); |
| 55 | __state_ = 0; |
| 56 | __gate1_.notify_all(); |
| 57 | } |
| 58 | |
| 59 | // Shared ownership |
| 60 | |
| 61 | void |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 62 | __shared_mutex_base::lock_shared() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 63 | { |
| 64 | unique_lock<mutex> lk(__mut_); |
| 65 | while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) |
| 66 | __gate1_.wait(lk); |
| 67 | unsigned num_readers = (__state_ & __n_readers_) + 1; |
| 68 | __state_ &= ~__n_readers_; |
| 69 | __state_ |= num_readers; |
| 70 | } |
| 71 | |
| 72 | bool |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 73 | __shared_mutex_base::try_lock_shared() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 74 | { |
| 75 | unique_lock<mutex> lk(__mut_); |
| 76 | unsigned num_readers = __state_ & __n_readers_; |
| 77 | if (!(__state_ & __write_entered_) && num_readers != __n_readers_) |
| 78 | { |
| 79 | ++num_readers; |
| 80 | __state_ &= ~__n_readers_; |
| 81 | __state_ |= num_readers; |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | void |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 88 | __shared_mutex_base::unlock_shared() |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 89 | { |
| 90 | lock_guard<mutex> _(__mut_); |
| 91 | unsigned num_readers = (__state_ & __n_readers_) - 1; |
| 92 | __state_ &= ~__n_readers_; |
| 93 | __state_ |= num_readers; |
| 94 | if (__state_ & __write_entered_) |
| 95 | { |
| 96 | if (num_readers == 0) |
| 97 | __gate2_.notify_one(); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | if (num_readers == __n_readers_ - 1) |
| 102 | __gate1_.notify_one(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 107 | // Shared Timed Mutex |
| 108 | // These routines are here for ABI stability |
Nikolas Klauser | 84fc2c3 | 2022-09-02 14:19:07 | [diff] [blame^] | 109 | shared_timed_mutex::shared_timed_mutex() : __base_() {} |
| 110 | void shared_timed_mutex::lock() { return __base_.lock(); } |
| 111 | bool shared_timed_mutex::try_lock() { return __base_.try_lock(); } |
| 112 | void shared_timed_mutex::unlock() { return __base_.unlock(); } |
| 113 | void shared_timed_mutex::lock_shared() { return __base_.lock_shared(); } |
| 114 | bool shared_timed_mutex::try_lock_shared() { return __base_.try_lock_shared(); } |
| 115 | void shared_timed_mutex::unlock_shared() { return __base_.unlock_shared(); } |
Marshall Clow | f69ae47 | 2015-06-30 14:04:14 | [diff] [blame] | 116 | |
Howard Hinnant | ead6f16 | 2013-09-21 01:49:28 | [diff] [blame] | 117 | _LIBCPP_END_NAMESPACE_STD |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 | [diff] [blame] | 118 | |
| 119 | #endif // !_LIBCPP_HAS_NO_THREADS |