/Users/deen/code/yugabyte-db/src/yb/util/rw_mutex.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/rw_mutex.h" |
34 | | |
35 | | #include <mutex> |
36 | | |
37 | | #include <glog/logging.h> |
38 | | |
39 | | #include "yb/gutil/map-util.h" |
40 | | #include "yb/util/env.h" |
41 | | |
42 | | using std::lock_guard; |
43 | | |
44 | | namespace { |
45 | | |
46 | 107M | void unlock_rwlock(pthread_rwlock_t* rwlock) { |
47 | 107M | int rv = pthread_rwlock_unlock(rwlock); |
48 | 107M | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
49 | 107M | } |
50 | | |
51 | | } // anonymous namespace |
52 | | |
53 | | namespace yb { |
54 | | |
55 | | RWMutex::RWMutex() |
56 | | #ifndef NDEBUG |
57 | | : writer_tid_(0) |
58 | | #endif |
59 | 19.0k | { |
60 | 19.0k | Init(Priority::PREFER_READING); |
61 | 19.0k | } |
62 | | |
63 | | RWMutex::RWMutex(Priority prio) |
64 | | #ifndef NDEBUG |
65 | | : writer_tid_(0) |
66 | | #endif |
67 | 8.08k | { |
68 | 8.08k | Init(prio); |
69 | 8.08k | } |
70 | | |
71 | 27.1k | void RWMutex::Init(Priority prio) { |
72 | | #ifdef __linux__ |
73 | | // Adapt from priority to the pthread type. |
74 | | int kind = PTHREAD_RWLOCK_PREFER_READER_NP; |
75 | | switch (prio) { |
76 | | case Priority::PREFER_READING: |
77 | | kind = PTHREAD_RWLOCK_PREFER_READER_NP; |
78 | | break; |
79 | | case Priority::PREFER_WRITING: |
80 | | kind = PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP; |
81 | | break; |
82 | | } |
83 | | |
84 | | // Initialize the new rwlock with the user's preference. |
85 | | pthread_rwlockattr_t attr; |
86 | | int rv = pthread_rwlockattr_init(&attr); |
87 | | DCHECK_EQ(0, rv) << strerror(rv); |
88 | | rv = pthread_rwlockattr_setkind_np(&attr, kind); |
89 | | DCHECK_EQ(0, rv) << strerror(rv); |
90 | | rv = pthread_rwlock_init(&native_handle_, &attr); |
91 | | DCHECK_EQ(0, rv) << strerror(rv); |
92 | | rv = pthread_rwlockattr_destroy(&attr); |
93 | | DCHECK_EQ(0, rv) << strerror(rv); |
94 | | #else |
95 | 27.1k | int rv = pthread_rwlock_init(&native_handle_, NULL); |
96 | 27.1k | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
97 | 27.1k | #endif |
98 | 27.1k | } |
99 | | |
100 | 6.97k | RWMutex::~RWMutex() { |
101 | 6.97k | int rv = pthread_rwlock_destroy(&native_handle_); |
102 | 6.97k | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
103 | 6.97k | } |
104 | | |
105 | 51.1M | void RWMutex::ReadLock() { |
106 | 51.1M | CheckLockState(LockState::NEITHER); |
107 | 51.1M | int rv = pthread_rwlock_rdlock(&native_handle_); |
108 | 51.1M | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
109 | 51.1M | MarkForReading(); |
110 | 51.1M | } |
111 | | |
112 | 91.2M | void RWMutex::ReadUnlock() { |
113 | 91.2M | CheckLockState(LockState::READER); |
114 | 91.2M | UnmarkForReading(); |
115 | 91.2M | unlock_rwlock(&native_handle_); |
116 | 91.2M | } |
117 | | |
118 | 48.0M | bool RWMutex::TryReadLock() { |
119 | 48.0M | CheckLockState(LockState::NEITHER); |
120 | 48.0M | int rv = pthread_rwlock_tryrdlock(&native_handle_); |
121 | 48.0M | if (rv == EBUSY) { |
122 | 7.97M | return false; |
123 | 7.97M | } |
124 | 40.0M | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
125 | 40.0M | MarkForReading(); |
126 | 40.0M | return true; |
127 | 48.0M | } |
128 | | |
129 | 15.9M | void RWMutex::WriteLock() { |
130 | 15.9M | CheckLockState(LockState::NEITHER); |
131 | 15.9M | int rv = pthread_rwlock_wrlock(&native_handle_); |
132 | 15.9M | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
133 | 15.9M | MarkForWriting(); |
134 | 15.9M | } |
135 | | |
136 | 15.9M | void RWMutex::WriteUnlock() { |
137 | 15.9M | CheckLockState(LockState::WRITER); |
138 | 15.9M | UnmarkForWriting(); |
139 | 15.9M | unlock_rwlock(&native_handle_); |
140 | 15.9M | } |
141 | | |
142 | 7.45M | bool RWMutex::TryWriteLock() { |
143 | 7.45M | CheckLockState(LockState::NEITHER); |
144 | 7.45M | int rv = pthread_rwlock_trywrlock(&native_handle_); |
145 | 7.45M | if (rv == EBUSY7.45M ) { |
146 | 7.45M | return false; |
147 | 7.45M | } |
148 | 18.4E | DCHECK_EQ(0, rv) << strerror(rv)0 ; |
149 | 18.4E | MarkForWriting(); |
150 | 18.4E | return true; |
151 | 7.45M | } |
152 | | |
153 | | #ifndef NDEBUG |
154 | | |
155 | 8 | void RWMutex::AssertAcquiredForReading() const { |
156 | 8 | lock_guard<simple_spinlock> l(tid_lock_); |
157 | 8 | CHECK(ContainsKey(reader_tids_, Env::Default()->gettid())); |
158 | 8 | } |
159 | | |
160 | 0 | void RWMutex::AssertAcquiredForWriting() const { |
161 | 0 | lock_guard<simple_spinlock> l(tid_lock_); |
162 | 0 | CHECK_EQ(Env::Default()->gettid(), writer_tid_); |
163 | 0 | } |
164 | | |
165 | 229M | void RWMutex::CheckLockState(LockState state) const { |
166 | 229M | auto my_tid = Env::Default()->gettid(); |
167 | 229M | bool is_reader; |
168 | 229M | bool is_writer; |
169 | 229M | { |
170 | 229M | lock_guard<simple_spinlock> l(tid_lock_); |
171 | 229M | is_reader = ContainsKey(reader_tids_, my_tid); |
172 | 229M | is_writer = writer_tid_ == my_tid; |
173 | 229M | } |
174 | | |
175 | 229M | switch (state) { |
176 | 122M | case LockState::NEITHER: |
177 | 122M | CHECK(!is_reader) << "Invalid state, already holding lock for reading"285 ; |
178 | 122M | CHECK(!is_writer) << "Invalid state, already holding lock for writing"323 ; |
179 | 122M | break; |
180 | 91.3M | case LockState::READER: |
181 | 91.3M | CHECK(!is_writer) << "Invalid state, already holding lock for writing"4 ; |
182 | 91.3M | CHECK(is_reader) << "Invalid state, wasn't holding lock for reading"48 ; |
183 | 91.3M | break; |
184 | 15.9M | case LockState::WRITER: |
185 | 15.9M | CHECK(!is_reader) << "Invalid state, already holding lock for reading"4 ; |
186 | 15.9M | CHECK(is_writer) << "Invalid state, wasn't holding lock for writing"6 ; |
187 | 15.9M | break; |
188 | 229M | } |
189 | 229M | } |
190 | | |
191 | 91.2M | void RWMutex::MarkForReading() { |
192 | 91.2M | lock_guard<simple_spinlock> l(tid_lock_); |
193 | 91.2M | reader_tids_.insert(Env::Default()->gettid()); |
194 | 91.2M | } |
195 | | |
196 | 15.9M | void RWMutex::MarkForWriting() { |
197 | 15.9M | lock_guard<simple_spinlock> l(tid_lock_); |
198 | 15.9M | writer_tid_ = Env::Default()->gettid(); |
199 | 15.9M | } |
200 | | |
201 | 91.3M | void RWMutex::UnmarkForReading() { |
202 | 91.3M | lock_guard<simple_spinlock> l(tid_lock_); |
203 | 91.3M | reader_tids_.erase(Env::Default()->gettid()); |
204 | 91.3M | } |
205 | | |
206 | 15.9M | void RWMutex::UnmarkForWriting() { |
207 | 15.9M | lock_guard<simple_spinlock> l(tid_lock_); |
208 | 15.9M | writer_tid_ = 0; |
209 | 15.9M | } |
210 | | |
211 | | #endif |
212 | | |
213 | | } // namespace yb |