1 // Copyright 2022 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_sync/recursive_mutex.h"
18 #include "pw_unit_test/framework.h"
19
20 namespace pw::sync {
21 namespace {
22
23 extern "C" {
24
25 // Functions defined in recusive_mutex_facade_test_c.c that call the API from C.
26 void pw_sync_RecursiveMutex_CallLock(pw_sync_RecursiveMutex* mutex);
27 bool pw_sync_RecursiveMutex_CallTryLock(pw_sync_RecursiveMutex* mutex);
28 void pw_sync_RecursiveMutex_CallUnlock(pw_sync_RecursiveMutex* mutex);
29
30 } // extern "C"
31
32 // TODO: b/235284163 - Add real concurrency tests once we have pw::thread.
33
TEST(RecursiveMutex,LockUnlock)34 TEST(RecursiveMutex, LockUnlock) PW_NO_LOCK_SAFETY_ANALYSIS {
35 pw::sync::RecursiveMutex mutex;
36 for (int i = 0; i < 10; ++i) {
37 mutex.lock();
38 }
39
40 for (int i = 0; i < 10; ++i) {
41 mutex.unlock();
42 }
43 }
44
45 RecursiveMutex static_mutex;
TEST(RecursiveMutex,LockUnlockStatic)46 TEST(RecursiveMutex, LockUnlockStatic) PW_NO_LOCK_SAFETY_ANALYSIS {
47 static_mutex.lock();
48 for (int i = 0; i < 10; ++i) {
49 EXPECT_TRUE(static_mutex.try_lock());
50 }
51 for (int i = 0; i < 10; ++i) {
52 static_mutex.unlock(); // undo the try_lock() calls
53 }
54 static_mutex.unlock(); // undo the inital lock() call
55 }
56
TEST(RecursiveMutex,TryLockUnlock)57 TEST(RecursiveMutex, TryLockUnlock) PW_NO_LOCK_SAFETY_ANALYSIS {
58 pw::sync::RecursiveMutex mutex;
59 ASSERT_TRUE(mutex.try_lock());
60
61 const bool locked_again = mutex.try_lock();
62 EXPECT_TRUE(locked_again);
63 if (locked_again) {
64 mutex.unlock();
65 }
66
67 mutex.unlock();
68 }
69
TEST(RecursiveMutex,LockUnlockInC)70 TEST(RecursiveMutex, LockUnlockInC) {
71 pw::sync::RecursiveMutex mutex;
72
73 pw_sync_RecursiveMutex_CallLock(&mutex);
74 pw_sync_RecursiveMutex_CallLock(&mutex);
75 pw_sync_RecursiveMutex_CallLock(&mutex);
76
77 pw_sync_RecursiveMutex_CallUnlock(&mutex);
78 pw_sync_RecursiveMutex_CallUnlock(&mutex);
79 pw_sync_RecursiveMutex_CallUnlock(&mutex);
80 }
81
TEST(RecursiveMutex,TryLockUnlockInC)82 TEST(RecursiveMutex, TryLockUnlockInC) {
83 pw::sync::RecursiveMutex mutex;
84 ASSERT_TRUE(pw_sync_RecursiveMutex_CallTryLock(&mutex));
85
86 const bool locked_again = pw_sync_RecursiveMutex_CallTryLock(&mutex);
87 EXPECT_TRUE(locked_again);
88 if (locked_again) {
89 pw_sync_RecursiveMutex_CallUnlock(&mutex);
90 }
91
92 pw_sync_RecursiveMutex_CallUnlock(&mutex);
93 }
94
95 } // namespace
96 } // namespace pw::sync
97