Debian Codegraph @

Source/UnitTests/Common/MutexTest.cpp

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <gtest/gtest.h>
5
6 #include <algorithm>
7 #include <chrono>
8 #include <mutex>
9 #include <shared_mutex>
10 #include <thread>
11
12 #include "Common/Mutex.h"
13 #include "Common/TransferableSharedMutex.h"
14
15 template <typename MutexType>
16 static void DoAtomicMutexTests(const char mutex_name[])
17 {
18 MutexType work_mutex;
19
20 bool worker_done = false;
21
22 static constexpr auto SLEEP_TIME = std::chrono::microseconds{1};
23
24 // lock() on main thread, unlock() on worker thread.
25 std::thread thread{[&, lk = std::unique_lock{work_mutex}] {
26 std::this_thread::sleep_for(SLEEP_TIME);
27 worker_done = true;
28 }};
29
30 // lock() waits for the thread to unlock().
31 {
32 std::lock_guard lk{work_mutex};
33 EXPECT_TRUE(worker_done);
34 }
35
36 thread.join();
37
38 // Prevent below workers from incrementing `done_count`.
39 MutexType done_mutex;
40 std::unique_lock done_lk{done_mutex};
41
42 // try_lock() fails when holding a lock.
43 EXPECT_FALSE(done_mutex.try_lock());
44
45 static constexpr int THREAD_COUNT = 4;
46 static constexpr int REPEAT_COUNT = 100;
47 static constexpr int TOTAL_ITERATIONS = THREAD_COUNT * REPEAT_COUNT;
48
49 int done_count = 0;
50 int work_count = 0;
51 std::atomic<int> try_lock_fail_count{};
52 std::vector<std::thread> threads(THREAD_COUNT);
53 for (auto& t : threads)
54 {
55 t = std::thread{[&] {
56 // lock() blocks until main thread unlock()s.
57 {
58 std::lock_guard lk{done_mutex};
59 ++done_count;
60 }
61
62 // Contesting lock() and try_lock() doesn't explode.
63 for (int i = 0; i != REPEAT_COUNT; ++i)
64 {
65 {
66 std::lock_guard lk{work_mutex};
67 ++work_count;
68 }
69
70 // Try lock in a loop.
71 while (!work_mutex.try_lock())
72 {
73 try_lock_fail_count.fetch_add(1, std::memory_order_relaxed);
74 }
75 std::lock_guard lk{work_mutex, std::adopt_lock};
76 ++work_count;
77 }
78 }};
79 }
80
81 std::this_thread::sleep_for(SLEEP_TIME);
82
83 // The threads are still blocking on done_mutex.
84 EXPECT_EQ(done_count, 0);
85
86 done_lk.unlock();
87 std::ranges::for_each(threads, &std::thread::join);
88
89 // The threads finished.
90 EXPECT_EQ(done_count, THREAD_COUNT);
91 EXPECT_EQ(work_count, TOTAL_ITERATIONS * 2);
92
93 GTEST_LOG_(INFO) << mutex_name << "::try_lock() failure %: "
94 << (try_lock_fail_count * 100.0 / (TOTAL_ITERATIONS + try_lock_fail_count));
95
96 // Things are still sane after contesting in worker threads.
97 std::lock_guard lk{work_mutex};
98 }
99
100 TEST(Mutex, AtomicMutex)
101 {
102 DoAtomicMutexTests<Common::AtomicMutex>("AtomicMutex");
103 DoAtomicMutexTests<Common::SpinMutex>("SpinMutex");
104 }
105
106 TEST(Mutex, TransferableSharedMutex)
107 {
108 Common::TransferableSharedMutex work_mutex;
109
110 bool worker_done = false;
111
112 static constexpr auto SLEEP_TIME = std::chrono::microseconds{1};
113
114 // lock() on main thread, unlock() on worker thread.
115 std::thread thread{[&, lk = std::unique_lock{work_mutex}] {
116 std::this_thread::sleep_for(SLEEP_TIME);
117 worker_done = true;
118 }};
119
120 // lock() waits for the thread to unlock().
121 {
122 std::lock_guard lk{work_mutex};
123 EXPECT_TRUE(worker_done);
124 }
125
126 thread.join();
127
128 // Prevent below workers from incrementing `done_count`.
129 Common::TransferableSharedMutex done_mutex;
130 std::unique_lock done_lk{done_mutex};
131
132 // try_*() fails when holding an exclusive lock.
133 EXPECT_FALSE(done_mutex.try_lock());
134 EXPECT_FALSE(done_mutex.try_lock_shared());
135
136 static constexpr int THREAD_COUNT = 4;
137 static constexpr int REPEAT_COUNT = 100;
138 static constexpr int TOTAL_ITERATIONS = THREAD_COUNT * REPEAT_COUNT;
139
140 std::atomic<int> work_count = 0;
141 std::atomic<int> done_count = 0;
142 int additional_work_count = 0;
143
144 std::atomic<int> try_lock_fail_count = 0;
145 std::atomic<int> try_lock_shared_fail_count = 0;
146
147 std::vector<std::thread> threads(THREAD_COUNT);
148 for (auto& t : threads)
149 {
150 // lock_shared() multiple times on main thread.
151 t = std::thread{[&, work_lk = std::shared_lock{work_mutex}]() mutable {
152 std::this_thread::sleep_for(SLEEP_TIME);
153
154 // try_lock() fails after lock_shared().
155 EXPECT_FALSE(work_mutex.try_lock());
156
157 // Main thread already holds done_mutex.
158 EXPECT_FALSE(done_mutex.try_lock());
159 EXPECT_FALSE(done_mutex.try_lock_shared());
160
161 ++work_count;
162
163 // Signal work is done.
164 work_lk.unlock();
165
166 // lock_shared() blocks until main thread unlock()s.
167 {
168 std::shared_lock lk{done_mutex};
169 ++done_count;
170 }
171
172 // Contesting all of [try_]lock[_shared] doesn't explode.
173 for (int i = 0; i != REPEAT_COUNT; ++i)
174 {
175 while (!work_mutex.try_lock())
176 {
177 try_lock_fail_count.fetch_add(1, std::memory_order_relaxed);
178 }
179 work_mutex.unlock();
180
181 while (!work_mutex.try_lock_shared())
182 {
183 try_lock_shared_fail_count.fetch_add(1, std::memory_order_relaxed);
184 }
185 work_mutex.unlock_shared();
186 {
187 std::lock_guard lk{work_mutex};
188 ++additional_work_count;
189 }
190 std::shared_lock lk{work_mutex};
191 }
192 }};
193 }
194
195 // lock() waits for threads to unlock_shared().
196 {
197 std::lock_guard lk{work_mutex};
198 EXPECT_EQ(work_count.load(std::memory_order_relaxed), THREAD_COUNT);
199 }
200
201 std::this_thread::sleep_for(SLEEP_TIME);
202
203 // The threads are still blocking on done_mutex.
204 EXPECT_EQ(done_count, 0);
205
206 done_lk.unlock();
207 std::ranges::for_each(threads, &std::thread::join);
208
209 // The threads finished.
210 EXPECT_EQ(done_count, THREAD_COUNT);
211
212 EXPECT_EQ(additional_work_count, TOTAL_ITERATIONS);
213
214 GTEST_LOG_(INFO) << "try_lock() failure %: "
215 << (try_lock_fail_count * 100.0 / (TOTAL_ITERATIONS + try_lock_fail_count));
216
217 GTEST_LOG_(INFO) << "try_lock_shared() failure %: "
218 << (try_lock_shared_fail_count * 100.0 /
219 (TOTAL_ITERATIONS + try_lock_shared_fail_count));
220
221 // Things are still sane after contesting in worker threads.
222 done_lk.lock();
223 std::lock_guard lk{work_mutex};
224 }