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 "pw_sync/borrow_testing.h"
16 #include "pw_sync/interrupt_spin_lock.h"
17 #include "pw_unit_test/framework.h"
18
19 namespace pw::sync {
20 namespace {
21
22 extern "C" {
23
24 // Functions defined in interrupt_spin_lock_facade_test_c.c which call the API
25 // from C.
26 void pw_sync_InterruptSpinLock_CallLock(
27 pw_sync_InterruptSpinLock* interrupt_spin_lock);
28 bool pw_sync_InterruptSpinLock_CallTryLock(
29 pw_sync_InterruptSpinLock* interrupt_spin_lock);
30 void pw_sync_InterruptSpinLock_CallUnlock(
31 pw_sync_InterruptSpinLock* interrupt_spin_lock);
32
33 } // extern "C"
34
TEST(InterruptSpinLock,LockUnlock)35 TEST(InterruptSpinLock, LockUnlock) {
36 pw::sync::InterruptSpinLock interrupt_spin_lock;
37 interrupt_spin_lock.lock();
38 interrupt_spin_lock.unlock();
39 }
40
41 // TODO: b/235284163 - Add real concurrency tests once we have pw::thread on SMP
42 // systems given that uniprocessor systems cannot fail to acquire an ISL.
43
44 InterruptSpinLock static_interrupt_spin_lock;
TEST(InterruptSpinLock,LockUnlockStatic)45 TEST(InterruptSpinLock, LockUnlockStatic) {
46 static_interrupt_spin_lock.lock();
47 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
48 // EXPECT_FALSE(static_interrupt_spin_lock.try_lock());
49 static_interrupt_spin_lock.unlock();
50 }
51
TEST(InterruptSpinLock,TryLockUnlock)52 TEST(InterruptSpinLock, TryLockUnlock) {
53 pw::sync::InterruptSpinLock interrupt_spin_lock;
54 const bool locked = interrupt_spin_lock.try_lock();
55 EXPECT_TRUE(locked);
56 if (locked) {
57 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
58 // EXPECT_FALSE(interrupt_spin_lock.try_lock());
59 interrupt_spin_lock.unlock();
60 }
61 }
62
63 // Unit tests for a `Borrowable`that uses a `InterruptSpinLock` as its lock.
64 using InterruptSpinLockBorrowTest = test::BorrowTest<InterruptSpinLock>;
65
TEST_F(InterruptSpinLockBorrowTest,Acquire)66 TEST_F(InterruptSpinLockBorrowTest, Acquire) { TestAcquire(); }
67
TEST_F(InterruptSpinLockBorrowTest,ConstAcquire)68 TEST_F(InterruptSpinLockBorrowTest, ConstAcquire) { TestConstAcquire(); }
69
TEST_F(InterruptSpinLockBorrowTest,RepeatedAcquire)70 TEST_F(InterruptSpinLockBorrowTest, RepeatedAcquire) { TestRepeatedAcquire(); }
71
TEST_F(InterruptSpinLockBorrowTest,Moveable)72 TEST_F(InterruptSpinLockBorrowTest, Moveable) { TestMoveable(); }
73
TEST_F(InterruptSpinLockBorrowTest,Copyable)74 TEST_F(InterruptSpinLockBorrowTest, Copyable) { TestCopyable(); }
75
TEST_F(InterruptSpinLockBorrowTest,CopyableCovariant)76 TEST_F(InterruptSpinLockBorrowTest, CopyableCovariant) {
77 TestCopyableCovariant();
78 }
79
TEST_F(InterruptSpinLockBorrowTest,TryAcquireSuccess)80 TEST_F(InterruptSpinLockBorrowTest, TryAcquireSuccess) {
81 TestTryAcquireSuccess();
82 }
83
TEST_F(InterruptSpinLockBorrowTest,TryAcquireFailure)84 TEST_F(InterruptSpinLockBorrowTest, TryAcquireFailure) {
85 TestTryAcquireFailure();
86 }
87
TEST(VirtualInterruptSpinLock,LockUnlock)88 TEST(VirtualInterruptSpinLock, LockUnlock) {
89 pw::sync::VirtualInterruptSpinLock interrupt_spin_lock;
90 interrupt_spin_lock.lock();
91 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
92 // EXPECT_FALSE(interrupt_spin_lock.try_lock());
93 interrupt_spin_lock.unlock();
94 }
95
96 VirtualInterruptSpinLock static_virtual_interrupt_spin_lock;
TEST(VirtualInterruptSpinLock,LockUnlockStatic)97 TEST(VirtualInterruptSpinLock, LockUnlockStatic) {
98 static_virtual_interrupt_spin_lock.lock();
99 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
100 // EXPECT_FALSE(static_virtual_interrupt_spin_lock.try_lock());
101 static_virtual_interrupt_spin_lock.unlock();
102 }
103
104 // Unit tests for a `Borrowable`that uses a `VirtualInterruptSpinLock` as its
105 // lock.
106 using VirtualInterruptSpinLockBorrowTest =
107 test::BorrowTest<VirtualInterruptSpinLock>;
108
TEST_F(VirtualInterruptSpinLockBorrowTest,Acquire)109 TEST_F(VirtualInterruptSpinLockBorrowTest, Acquire) { TestAcquire(); }
110
TEST_F(VirtualInterruptSpinLockBorrowTest,ConstAcquire)111 TEST_F(VirtualInterruptSpinLockBorrowTest, ConstAcquire) { TestConstAcquire(); }
112
TEST_F(VirtualInterruptSpinLockBorrowTest,RepeatedAcquire)113 TEST_F(VirtualInterruptSpinLockBorrowTest, RepeatedAcquire) {
114 TestRepeatedAcquire();
115 }
116
TEST_F(VirtualInterruptSpinLockBorrowTest,Moveable)117 TEST_F(VirtualInterruptSpinLockBorrowTest, Moveable) { TestMoveable(); }
118
TEST_F(VirtualInterruptSpinLockBorrowTest,Copyable)119 TEST_F(VirtualInterruptSpinLockBorrowTest, Copyable) { TestCopyable(); }
120
TEST_F(VirtualInterruptSpinLockBorrowTest,CopyableCovariant)121 TEST_F(VirtualInterruptSpinLockBorrowTest, CopyableCovariant) {
122 TestCopyableCovariant();
123 }
124
TEST_F(VirtualInterruptSpinLockBorrowTest,TryAcquireSuccess)125 TEST_F(VirtualInterruptSpinLockBorrowTest, TryAcquireSuccess) {
126 TestTryAcquireSuccess();
127 }
128
TEST_F(VirtualInterruptSpinLockBorrowTest,TryAcquireFailure)129 TEST_F(VirtualInterruptSpinLockBorrowTest, TryAcquireFailure) {
130 TestTryAcquireFailure();
131 }
132
TEST(InterruptSpinLock,LockUnlockInC)133 TEST(InterruptSpinLock, LockUnlockInC) {
134 pw::sync::InterruptSpinLock interrupt_spin_lock;
135 pw_sync_InterruptSpinLock_CallLock(&interrupt_spin_lock);
136 pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock);
137 }
138
TEST(InterruptSpinLock,TryLockUnlockInC)139 TEST(InterruptSpinLock, TryLockUnlockInC) {
140 pw::sync::InterruptSpinLock interrupt_spin_lock;
141 ASSERT_TRUE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock));
142 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
143 // EXPECT_FALSE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock));
144 pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock);
145 }
146
147 } // namespace
148 } // namespace pw::sync
149