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/interrupt_spin_lock.h"
16 #include "pw_sync_private/borrow_lockable_tests.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 PW_SYNC_ADD_BORROWABLE_LOCK_NAMED_TESTS(BorrowableInterruptSpinLock,
64 InterruptSpinLock);
65
TEST(VirtualInterruptSpinLock,LockUnlock)66 TEST(VirtualInterruptSpinLock, LockUnlock) {
67 pw::sync::VirtualInterruptSpinLock interrupt_spin_lock;
68 interrupt_spin_lock.lock();
69 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
70 // EXPECT_FALSE(interrupt_spin_lock.try_lock());
71 interrupt_spin_lock.unlock();
72 }
73
74 VirtualInterruptSpinLock static_virtual_interrupt_spin_lock;
TEST(VirtualInterruptSpinLock,LockUnlockStatic)75 TEST(VirtualInterruptSpinLock, LockUnlockStatic) {
76 static_virtual_interrupt_spin_lock.lock();
77 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
78 // EXPECT_FALSE(static_virtual_interrupt_spin_lock.try_lock());
79 static_virtual_interrupt_spin_lock.unlock();
80 }
81
82 PW_SYNC_ADD_BORROWABLE_LOCK_NAMED_TESTS(BorrowableVirtualInterruptSpinLock,
83 VirtualInterruptSpinLock);
84
TEST(InterruptSpinLock,LockUnlockInC)85 TEST(InterruptSpinLock, LockUnlockInC) {
86 pw::sync::InterruptSpinLock interrupt_spin_lock;
87 pw_sync_InterruptSpinLock_CallLock(&interrupt_spin_lock);
88 pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock);
89 }
90
TEST(InterruptSpinLock,TryLockUnlockInC)91 TEST(InterruptSpinLock, TryLockUnlockInC) {
92 pw::sync::InterruptSpinLock interrupt_spin_lock;
93 ASSERT_TRUE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock));
94 // TODO: b/235284163 - Ensure other cores fail to lock when its locked.
95 // EXPECT_FALSE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock));
96 pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock);
97 }
98
99 } // namespace
100 } // namespace pw::sync
101