/Users/deen/code/yugabyte-db/src/yb/util/rw_mutex-test.cc
Line | Count | Source |
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 <mutex> |
34 | | #include <thread> |
35 | | #include <vector> |
36 | | |
37 | | #include <gtest/gtest.h> |
38 | | |
39 | | #include "yb/gutil/integral_types.h" |
40 | | #include "yb/util/atomic.h" |
41 | | #include "yb/util/locks.h" |
42 | | #include "yb/util/monotime.h" |
43 | | #include "yb/util/rw_mutex.h" |
44 | | #include "yb/util/test_util.h" |
45 | | |
46 | | using std::lock_guard; |
47 | | using std::thread; |
48 | | using std::try_to_lock; |
49 | | using std::unique_lock; |
50 | | using std::vector; |
51 | | |
52 | | namespace yb { |
53 | | |
54 | | class RWMutexTest : public YBTest, |
55 | | public ::testing::WithParamInterface<RWMutex::Priority> { |
56 | | public: |
57 | | RWMutexTest() |
58 | 4 | : lock_(GetParam()) { |
59 | 4 | } |
60 | | protected: |
61 | | RWMutex lock_; |
62 | | }; |
63 | | |
64 | | // Instantiate every test for each kind of RWMutex priority. |
65 | | INSTANTIATE_TEST_CASE_P(Priorities, RWMutexTest, |
66 | | ::testing::Values(RWMutex::Priority::PREFER_READING, |
67 | | RWMutex::Priority::PREFER_WRITING)); |
68 | | |
69 | | // Multi-threaded test that tries to find deadlocks in the RWMutex wrapper. |
70 | 2 | TEST_P(RWMutexTest, TestDeadlocks) NO_THREAD_SAFETY_ANALYSIS { |
71 | 2 | uint64_t number_of_writes = 0; |
72 | 2 | AtomicInt<uint64_t> number_of_reads(0); |
73 | | |
74 | 2 | AtomicBool done(false); |
75 | 2 | vector<thread> threads; |
76 | | |
77 | | // Start several blocking and non-blocking read-write workloads. |
78 | 6 | for (int i = 0; i < 2; i++) { |
79 | 4 | threads.emplace_back([&](){ |
80 | 97.7k | while (!done.Load()) { |
81 | 97.7k | lock_guard<RWMutex> l(lock_); |
82 | 97.7k | number_of_writes++; |
83 | 97.7k | } |
84 | 4 | }); |
85 | 4 | threads.emplace_back([&](){ |
86 | 7.65M | while (!done.Load()) { |
87 | 7.65M | unique_lock<RWMutex> l(lock_, try_to_lock); |
88 | 7.65M | if (l.owns_lock()) { |
89 | 4.31k | number_of_writes++; |
90 | 4.31k | } |
91 | 7.65M | } |
92 | 4 | }); |
93 | 4 | } |
94 | | |
95 | | // Start several blocking and non-blocking read-only workloads. |
96 | 6 | for (int i = 0; i < 2; i++) { |
97 | 4 | threads.emplace_back([&](){ |
98 | 77.9k | while (!done.Load()) { |
99 | 77.9k | shared_lock<RWMutex> l(lock_); |
100 | 77.9k | number_of_reads.Increment(); |
101 | 77.9k | } |
102 | 4 | }); |
103 | 4 | threads.emplace_back([&](){ |
104 | 8.33M | while (!done.Load()) { |
105 | 8.33M | shared_lock<RWMutex> l(lock_, try_to_lock); |
106 | 8.33M | if (l.owns_lock()) { |
107 | 38.1k | number_of_reads.Increment(); |
108 | 38.1k | } |
109 | 8.33M | } |
110 | 4 | }); |
111 | 4 | } |
112 | | |
113 | 2 | SleepFor(MonoDelta::FromSeconds(1)); |
114 | 2 | done.Store(true); |
115 | 16 | for (auto& t : threads) { |
116 | 16 | t.join(); |
117 | 16 | } |
118 | | |
119 | 2 | shared_lock<RWMutex> l(lock_); |
120 | 2 | LOG(INFO) << "Number of writes: " << number_of_writes; |
121 | 2 | LOG(INFO) << "Number of reads: " << number_of_reads.Load(); |
122 | 2 | } |
123 | | |
124 | | #ifndef NDEBUG |
125 | | // Tests that the RWMutex wrapper catches basic usage errors. This checking is |
126 | | // only enabled in debug builds. |
127 | 2 | TEST_P(RWMutexTest, TestLockChecking) NO_THREAD_SAFETY_ANALYSIS { |
128 | 2 | EXPECT_DEATH({ |
129 | 2 | lock_.ReadLock(); |
130 | 2 | lock_.ReadLock(); |
131 | 2 | }, "already holding lock for reading"); |
132 | | |
133 | 2 | EXPECT_DEATH(({ |
134 | 2 | CHECK(lock_.TryReadLock()); |
135 | 2 | CHECK(lock_.TryReadLock()); |
136 | 2 | }), "already holding lock for reading"); |
137 | | |
138 | 2 | EXPECT_DEATH({ |
139 | 2 | lock_.ReadLock(); |
140 | 2 | lock_.WriteLock(); |
141 | 2 | }, "already holding lock for reading"); |
142 | | |
143 | 2 | EXPECT_DEATH(({ |
144 | 2 | CHECK(lock_.TryReadLock()); |
145 | 2 | CHECK(lock_.TryWriteLock()); |
146 | 2 | }), "already holding lock for reading"); |
147 | | |
148 | 2 | EXPECT_DEATH({ |
149 | 2 | lock_.WriteLock(); |
150 | 2 | lock_.ReadLock(); |
151 | 2 | }, "already holding lock for writing"); |
152 | | |
153 | 2 | EXPECT_DEATH(({ |
154 | 2 | CHECK(lock_.TryWriteLock()); |
155 | 2 | CHECK(lock_.TryReadLock()); |
156 | 2 | }), "already holding lock for writing"); |
157 | | |
158 | 2 | EXPECT_DEATH(({ |
159 | 2 | lock_.WriteLock(); |
160 | 2 | lock_.WriteLock(); |
161 | 2 | }), "already holding lock for writing"); |
162 | | |
163 | 2 | EXPECT_DEATH(({ |
164 | 2 | CHECK(lock_.TryWriteLock()); |
165 | 2 | CHECK(lock_.TryWriteLock()); |
166 | 2 | }), "already holding lock for writing"); |
167 | | |
168 | 2 | EXPECT_DEATH({ |
169 | 2 | lock_.ReadUnlock(); |
170 | 2 | }, "wasn't holding lock for reading"); |
171 | | |
172 | 2 | EXPECT_DEATH({ |
173 | 2 | lock_.WriteUnlock(); |
174 | 2 | }, "wasn't holding lock for writing"); |
175 | | |
176 | 2 | EXPECT_DEATH({ |
177 | 2 | lock_.ReadLock(); |
178 | 2 | lock_.WriteUnlock(); |
179 | 2 | }, "already holding lock for reading"); |
180 | | |
181 | 2 | EXPECT_DEATH(({ |
182 | 2 | CHECK(lock_.TryReadLock()); |
183 | 2 | lock_.WriteUnlock(); |
184 | 2 | }), "already holding lock for reading"); |
185 | | |
186 | 2 | EXPECT_DEATH({ |
187 | 2 | lock_.WriteLock(); |
188 | 2 | lock_.ReadUnlock(); |
189 | 2 | }, "already holding lock for writing"); |
190 | | |
191 | 2 | EXPECT_DEATH(({ |
192 | 2 | CHECK(lock_.TryWriteLock()); |
193 | 2 | lock_.ReadUnlock(); |
194 | 2 | }), "already holding lock for writing"); |
195 | 2 | } |
196 | | #endif |
197 | | |
198 | | } // namespace yb |