1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include <chrono>
16
17 #include "pw_chrono/system_clock.h"
18 #include "pw_sync/timed_borrow_testing.h"
19 #include "pw_sync/timed_mutex.h"
20 #include "pw_unit_test/framework.h"
21
22 using pw::chrono::SystemClock;
23 using namespace std::chrono_literals;
24
25 namespace pw::sync {
26 namespace {
27
28 extern "C" {
29
30 // Functions defined in mutex_facade_test_c.c which call the API from C.
31 void pw_sync_TimedMutex_CallLock(pw_sync_TimedMutex* mutex);
32 bool pw_sync_TimedMutex_CallTryLock(pw_sync_TimedMutex* mutex);
33 bool pw_sync_TimedMutex_CallTryLockFor(pw_sync_TimedMutex* mutex,
34 pw_chrono_SystemClock_Duration timeout);
35 bool pw_sync_TimedMutex_CallTryLockUntil(
36 pw_sync_TimedMutex* mutex, pw_chrono_SystemClock_TimePoint deadline);
37 void pw_sync_TimedMutex_CallUnlock(pw_sync_TimedMutex* mutex);
38
39 } // extern "C"
40
41 // We can't control the SystemClock's period configuration, so just in case
42 // duration cannot be accurately expressed in integer ticks, round the
43 // duration up.
44 constexpr SystemClock::duration kRoundedArbitraryDuration =
45 SystemClock::for_at_least(42ms);
46 constexpr pw_chrono_SystemClock_Duration kRoundedArbitraryDurationInC =
47 PW_SYSTEM_CLOCK_MS(42);
48
49 // TODO: b/235284163 - Add real concurrency tests once we have pw::thread.
50
TEST(TimedMutex,LockUnlock)51 TEST(TimedMutex, LockUnlock) {
52 TimedMutex mutex;
53 mutex.lock();
54 mutex.unlock();
55 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
56 // else.
57 // EXPECT_FALSE(mutex.try_lock());
58 }
59
60 TimedMutex static_mutex;
TEST(TimedMutex,LockUnlockStatic)61 TEST(TimedMutex, LockUnlockStatic) {
62 static_mutex.lock();
63 static_mutex.unlock();
64 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
65 // else.
66 // EXPECT_FALSE(static_mutex.try_lock());
67 }
68
TEST(TimedMutex,TryLockUnlock)69 TEST(TimedMutex, TryLockUnlock) {
70 TimedMutex mutex;
71 const bool locked = mutex.try_lock();
72 EXPECT_TRUE(locked);
73 if (locked) {
74 // EXPECT_FALSE(mutex.try_lock());
75 mutex.unlock();
76 }
77 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
78 // else.
79 }
80
TEST(TimedMutex,TryLockUnlockFor)81 TEST(TimedMutex, TryLockUnlockFor) {
82 TimedMutex mutex;
83
84 SystemClock::time_point before = SystemClock::now();
85 const bool locked = mutex.try_lock_for(kRoundedArbitraryDuration);
86 EXPECT_TRUE(locked);
87 if (locked) {
88 SystemClock::duration time_elapsed = SystemClock::now() - before;
89 EXPECT_LT(time_elapsed, kRoundedArbitraryDuration);
90 mutex.unlock();
91 }
92 // TODO: b/235284163 - Ensure it blocks and fails to lock when already held by
93 // someone else.
94 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
95 // held by someone else and a zero length duration is used.
96 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
97 // held by someone else and a negative duration is used.
98 }
99
TEST(TimedMutex,TryLockUnlockUntil)100 TEST(TimedMutex, TryLockUnlockUntil) {
101 TimedMutex mutex;
102
103 const SystemClock::time_point deadline =
104 SystemClock::now() + kRoundedArbitraryDuration;
105 const bool locked = mutex.try_lock_until(deadline);
106 EXPECT_TRUE(locked);
107 if (locked) {
108 EXPECT_LT(SystemClock::now(), deadline);
109 mutex.unlock();
110 }
111 // TODO: b/235284163 - Ensure it blocks and fails to lock when already held by
112 // someone else.
113 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
114 // held by someone else and now is used.
115 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
116 // held by someone else and a timestamp in the past is used.
117 }
118
119 // Unit tests for a `Borrowable`that uses a `TimedMutex` as its lock.
120 using TimedMutexBorrowTest = test::TimedBorrowTest<TimedMutex>;
121
TEST_F(TimedMutexBorrowTest,Acquire)122 TEST_F(TimedMutexBorrowTest, Acquire) { TestAcquire(); }
123
TEST_F(TimedMutexBorrowTest,ConstAcquire)124 TEST_F(TimedMutexBorrowTest, ConstAcquire) { TestConstAcquire(); }
125
TEST_F(TimedMutexBorrowTest,RepeatedAcquire)126 TEST_F(TimedMutexBorrowTest, RepeatedAcquire) { TestRepeatedAcquire(); }
127
TEST_F(TimedMutexBorrowTest,Moveable)128 TEST_F(TimedMutexBorrowTest, Moveable) { TestMoveable(); }
129
TEST_F(TimedMutexBorrowTest,Copyable)130 TEST_F(TimedMutexBorrowTest, Copyable) { TestCopyable(); }
131
TEST_F(TimedMutexBorrowTest,CopyableCovariant)132 TEST_F(TimedMutexBorrowTest, CopyableCovariant) { TestCopyableCovariant(); }
133
TEST_F(TimedMutexBorrowTest,TryAcquireSuccess)134 TEST_F(TimedMutexBorrowTest, TryAcquireSuccess) { TestTryAcquireSuccess(); }
135
TEST_F(TimedMutexBorrowTest,TryAcquireFailure)136 TEST_F(TimedMutexBorrowTest, TryAcquireFailure) { TestTryAcquireFailure(); }
137
TEST_F(TimedMutexBorrowTest,TryAcquireForSuccess)138 TEST_F(TimedMutexBorrowTest, TryAcquireForSuccess) {
139 TestTryAcquireForSuccess(kRoundedArbitraryDuration);
140 }
141
TEST_F(TimedMutexBorrowTest,TryAcquireForFailure)142 TEST_F(TimedMutexBorrowTest, TryAcquireForFailure) {
143 TestTryAcquireForFailure(kRoundedArbitraryDuration);
144 }
145
TEST_F(TimedMutexBorrowTest,TryAcquireUntilSuccess)146 TEST_F(TimedMutexBorrowTest, TryAcquireUntilSuccess) {
147 TestTryAcquireUntilSuccess(kRoundedArbitraryDuration);
148 }
149
TEST_F(TimedMutexBorrowTest,TryAcquireUntilFailure)150 TEST_F(TimedMutexBorrowTest, TryAcquireUntilFailure) {
151 TestTryAcquireUntilFailure(kRoundedArbitraryDuration);
152 }
153
TEST(VirtualTimedMutex,LockUnlock)154 TEST(VirtualTimedMutex, LockUnlock) {
155 VirtualTimedMutex mutex;
156 mutex.lock();
157 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
158 // else.
159 // EXPECT_FALSE(mutex.try_lock());
160 mutex.unlock();
161 }
162
163 VirtualTimedMutex static_virtual_mutex;
TEST(VirtualTimedMutex,LockUnlockStatic)164 TEST(VirtualTimedMutex, LockUnlockStatic) {
165 static_virtual_mutex.lock();
166 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
167 // else.
168 // EXPECT_FALSE(static_virtual_mutex.try_lock());
169 static_virtual_mutex.unlock();
170 }
171
TEST(VirtualMutex,LockUnlockExternal)172 TEST(VirtualMutex, LockUnlockExternal) {
173 VirtualTimedMutex virtual_timed_mutex;
174 auto& mutex = virtual_timed_mutex.timed_mutex();
175 mutex.lock();
176 // TODO: b/235284163 - Ensure it fails to lock when already held.
177 // EXPECT_FALSE(mutex.try_lock());
178 mutex.unlock();
179 }
180
181 // Unit tests for a `Borrowable`that uses a `VirtualTimedMutex` as its lock.
182 using VirtualTimedMutexBorrowTest = test::TimedBorrowTest<VirtualTimedMutex>;
183
TEST_F(VirtualTimedMutexBorrowTest,Acquire)184 TEST_F(VirtualTimedMutexBorrowTest, Acquire) { TestAcquire(); }
185
TEST_F(VirtualTimedMutexBorrowTest,ConstAcquire)186 TEST_F(VirtualTimedMutexBorrowTest, ConstAcquire) { TestConstAcquire(); }
187
TEST_F(VirtualTimedMutexBorrowTest,RepeatedAcquire)188 TEST_F(VirtualTimedMutexBorrowTest, RepeatedAcquire) { TestRepeatedAcquire(); }
189
TEST_F(VirtualTimedMutexBorrowTest,Moveable)190 TEST_F(VirtualTimedMutexBorrowTest, Moveable) { TestMoveable(); }
191
TEST_F(VirtualTimedMutexBorrowTest,Copyable)192 TEST_F(VirtualTimedMutexBorrowTest, Copyable) { TestCopyable(); }
193
TEST_F(VirtualTimedMutexBorrowTest,CopyableCovariant)194 TEST_F(VirtualTimedMutexBorrowTest, CopyableCovariant) {
195 TestCopyableCovariant();
196 }
197
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireSuccess)198 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireSuccess) {
199 TestTryAcquireSuccess();
200 }
201
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireFailure)202 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireFailure) {
203 TestTryAcquireFailure();
204 }
205
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireForSuccess)206 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireForSuccess) {
207 TestTryAcquireForSuccess(kRoundedArbitraryDuration);
208 }
209
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireForFailure)210 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireForFailure) {
211 TestTryAcquireForFailure(kRoundedArbitraryDuration);
212 }
213
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireUntilSuccess)214 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireUntilSuccess) {
215 TestTryAcquireUntilSuccess(kRoundedArbitraryDuration);
216 }
217
TEST_F(VirtualTimedMutexBorrowTest,TryAcquireUntilFailure)218 TEST_F(VirtualTimedMutexBorrowTest, TryAcquireUntilFailure) {
219 TestTryAcquireUntilFailure(kRoundedArbitraryDuration);
220 }
221
TEST(TimedMutex,LockUnlockInC)222 TEST(TimedMutex, LockUnlockInC) {
223 TimedMutex mutex;
224 pw_sync_TimedMutex_CallLock(&mutex);
225 pw_sync_TimedMutex_CallUnlock(&mutex);
226 }
227
TEST(TimedMutex,TryLockUnlockInC)228 TEST(TimedMutex, TryLockUnlockInC) {
229 TimedMutex mutex;
230 ASSERT_TRUE(pw_sync_TimedMutex_CallTryLock(&mutex));
231 // TODO: b/235284163 - Ensure it fails to lock when already held by someone
232 // else.
233 // EXPECT_FALSE(pw_sync_TimedMutex_CallTryLock(&mutex));
234 pw_sync_TimedMutex_CallUnlock(&mutex);
235 }
236
TEST(TimedMutex,TryLockUnlockForInC)237 TEST(TimedMutex, TryLockUnlockForInC) {
238 TimedMutex mutex;
239
240 pw_chrono_SystemClock_TimePoint before = pw_chrono_SystemClock_Now();
241 ASSERT_TRUE(
242 pw_sync_TimedMutex_CallTryLockFor(&mutex, kRoundedArbitraryDurationInC));
243 pw_chrono_SystemClock_Duration time_elapsed =
244 pw_chrono_SystemClock_TimeElapsed(before, pw_chrono_SystemClock_Now());
245 EXPECT_LT(time_elapsed.ticks, kRoundedArbitraryDurationInC.ticks);
246 pw_sync_TimedMutex_CallUnlock(&mutex);
247 // TODO: b/235284163 - Ensure it blocks and fails to lock when already held by
248 // someone else.
249 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
250 // held by someone else and a zero length duration is used.
251 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
252 // held by someone else and a negative duration is used.
253 }
254
TEST(TimedMutex,TryLockUnlockUntilInC)255 TEST(TimedMutex, TryLockUnlockUntilInC) {
256 TimedMutex mutex;
257 pw_chrono_SystemClock_TimePoint deadline;
258 deadline.duration_since_epoch.ticks =
259 pw_chrono_SystemClock_Now().duration_since_epoch.ticks +
260 kRoundedArbitraryDurationInC.ticks;
261 ASSERT_TRUE(pw_sync_TimedMutex_CallTryLockUntil(&mutex, deadline));
262 EXPECT_LT(pw_chrono_SystemClock_Now().duration_since_epoch.ticks,
263 deadline.duration_since_epoch.ticks);
264 pw_sync_TimedMutex_CallUnlock(&mutex);
265 // TODO: b/235284163 - Ensure it blocks and fails to lock when already held by
266 // someone else.
267 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
268 // held by someone else and now is used.
269 // TODO: b/235284163 - Ensure it does not block and fails to lock when already
270 // held by someone else and a timestamp in the past is used.
271 }
272
273 } // namespace
274 } // namespace pw::sync
275