xref: /aosp_15_r20/external/pigweed/pw_sync/mutex_facade_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_sync/mutex.h"
18 #include "pw_sync_private/borrow_lockable_tests.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace pw::sync {
22 namespace {
23 
24 extern "C" {
25 
26 // Functions defined in mutex_facade_test_c.c which call the API from C.
27 void pw_sync_Mutex_CallLock(pw_sync_Mutex* mutex);
28 bool pw_sync_Mutex_CallTryLock(pw_sync_Mutex* mutex);
29 void pw_sync_Mutex_CallUnlock(pw_sync_Mutex* mutex);
30 
31 }  // extern "C"
32 
33 // TODO: b/235284163 - Add real concurrency tests once we have pw::thread.
34 
TEST(Mutex,LockUnlock)35 TEST(Mutex, LockUnlock) {
36   Mutex mutex;
37   mutex.lock();
38   // TODO: b/235284163 - Ensure it fails to lock when already held.
39   // EXPECT_FALSE(mutex.try_lock());
40   mutex.unlock();
41 }
42 
43 Mutex static_mutex;
TEST(Mutex,LockUnlockStatic)44 TEST(Mutex, LockUnlockStatic) {
45   static_mutex.lock();
46   // TODO: b/235284163 - Ensure it fails to lock when already held.
47   // EXPECT_FALSE(static_mutex.try_lock());
48   static_mutex.unlock();
49 }
50 
TEST(Mutex,TryLockUnlock)51 TEST(Mutex, TryLockUnlock) {
52   Mutex mutex;
53   const bool locked = mutex.try_lock();
54   EXPECT_TRUE(locked);
55   if (locked) {
56     // TODO: b/235284163 - Ensure it fails to lock when already held.
57     // EXPECT_FALSE(mutex.try_lock());
58     mutex.unlock();
59   }
60 }
61 
62 PW_SYNC_ADD_BORROWABLE_LOCK_NAMED_TESTS(BorrowableMutex, Mutex);
63 
TEST(VirtualMutex,LockUnlock)64 TEST(VirtualMutex, LockUnlock) {
65   VirtualMutex mutex;
66   mutex.lock();
67   // TODO: b/235284163 - Ensure it fails to lock when already held.
68   // EXPECT_FALSE(mutex.try_lock());
69   mutex.unlock();
70 }
71 
72 VirtualMutex static_virtual_mutex;
TEST(VirtualMutex,LockUnlockStatic)73 TEST(VirtualMutex, LockUnlockStatic) {
74   static_virtual_mutex.lock();
75   // TODO: b/235284163 - Ensure it fails to lock when already held.
76   // EXPECT_FALSE(static_virtual_mutex.try_lock());
77   static_virtual_mutex.unlock();
78 }
79 
TEST(VirtualMutex,LockUnlockExternal)80 TEST(VirtualMutex, LockUnlockExternal) {
81   VirtualMutex virtual_mutex;
82   auto& mutex = virtual_mutex.mutex();
83   mutex.lock();
84   // TODO: b/235284163 - Ensure it fails to lock when already held.
85   // EXPECT_FALSE(mutex.try_lock());
86   mutex.unlock();
87 }
88 
89 PW_SYNC_ADD_BORROWABLE_LOCK_NAMED_TESTS(BorrowableVirtualMutex, VirtualMutex);
90 
TEST(Mutex,LockUnlockInC)91 TEST(Mutex, LockUnlockInC) {
92   Mutex mutex;
93   pw_sync_Mutex_CallLock(&mutex);
94   pw_sync_Mutex_CallUnlock(&mutex);
95 }
96 
TEST(Mutex,TryLockUnlockInC)97 TEST(Mutex, TryLockUnlockInC) {
98   Mutex mutex;
99   ASSERT_TRUE(pw_sync_Mutex_CallTryLock(&mutex));
100   // TODO: b/235284163 - Ensure it fails to lock when already held.
101   // EXPECT_FALSE(pw_sync_Mutex_CallTryLock(&mutex));
102   pw_sync_Mutex_CallUnlock(&mutex);
103 }
104 
105 }  // namespace
106 }  // namespace pw::sync
107