Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 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 "gtest/gtest.h" |
| 16 | #include "pw_sync/interrupt_spin_lock.h" |
| 17 | |
| 18 | namespace pw::sync { |
| 19 | namespace { |
| 20 | |
| 21 | extern "C" { |
| 22 | |
| 23 | // Functions defined in interrupt_spin_lock_facade_test_c.c which call the API |
| 24 | // from C. |
| 25 | void pw_sync_InterruptSpinLock_CallLock( |
| 26 | pw_sync_InterruptSpinLock* interrupt_spin_lock); |
| 27 | bool pw_sync_InterruptSpinLock_CallTryLock( |
| 28 | pw_sync_InterruptSpinLock* interrupt_spin_lock); |
| 29 | void pw_sync_InterruptSpinLock_CallUnlock( |
| 30 | pw_sync_InterruptSpinLock* interrupt_spin_lock); |
| 31 | |
| 32 | } // extern "C" |
| 33 | |
| 34 | TEST(InterruptSpinLock, LockUnlock) { |
| 35 | pw::sync::InterruptSpinLock interrupt_spin_lock; |
| 36 | interrupt_spin_lock.lock(); |
| 37 | interrupt_spin_lock.unlock(); |
| 38 | } |
| 39 | |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 40 | // TODO(pwbug/291): Add real concurrency tests once we have pw::thread on SMP |
| 41 | // systems given that uniprocessor systems cannot fail to acquire an ISL. |
Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 42 | |
| 43 | InterruptSpinLock static_interrupt_spin_lock; |
| 44 | TEST(InterruptSpinLock, LockUnlockStatic) { |
| 45 | static_interrupt_spin_lock.lock(); |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 46 | // TODO(pwbug/291): Ensure other cores fail to lock when its locked. |
| 47 | // EXPECT_FALSE(static_interrupt_spin_lock.try_lock()); |
Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 48 | static_interrupt_spin_lock.unlock(); |
| 49 | } |
| 50 | |
| 51 | TEST(InterruptSpinLock, TryLockUnlock) { |
| 52 | pw::sync::InterruptSpinLock interrupt_spin_lock; |
Ewout van Bekkum | cc9ef83 | 2021-04-08 08:51:16 -0700 | [diff] [blame] | 53 | const bool locked = interrupt_spin_lock.try_lock(); |
| 54 | EXPECT_TRUE(locked); |
| 55 | if (locked) { |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 56 | // TODO(pwbug/291): Ensure other cores fail to lock when its locked. |
| 57 | // EXPECT_FALSE(interrupt_spin_lock.try_lock()); |
Ewout van Bekkum | cc9ef83 | 2021-04-08 08:51:16 -0700 | [diff] [blame] | 58 | interrupt_spin_lock.unlock(); |
| 59 | } |
Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Ewout van Bekkum | baf2fdc | 2021-09-09 14:11:51 -0700 | [diff] [blame] | 62 | TEST(VirtualInterruptSpinLock, LockUnlock) { |
| 63 | pw::sync::VirtualInterruptSpinLock interrupt_spin_lock; |
| 64 | interrupt_spin_lock.lock(); |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 65 | // TODO(pwbug/291): Ensure other cores fail to lock when its locked. |
Ewout van Bekkum | baf2fdc | 2021-09-09 14:11:51 -0700 | [diff] [blame] | 66 | // EXPECT_FALSE(interrupt_spin_lock.try_lock()); |
| 67 | interrupt_spin_lock.unlock(); |
| 68 | } |
| 69 | |
| 70 | VirtualInterruptSpinLock static_virtual_interrupt_spin_lock; |
| 71 | TEST(VirtualInterruptSpinLock, LockUnlockStatic) { |
| 72 | static_virtual_interrupt_spin_lock.lock(); |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 73 | // TODO(pwbug/291): Ensure other cores fail to lock when its locked. |
Ewout van Bekkum | baf2fdc | 2021-09-09 14:11:51 -0700 | [diff] [blame] | 74 | // EXPECT_FALSE(static_virtual_interrupt_spin_lock.try_lock()); |
| 75 | static_virtual_interrupt_spin_lock.unlock(); |
| 76 | } |
| 77 | |
Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 78 | TEST(InterruptSpinLock, LockUnlockInC) { |
| 79 | pw::sync::InterruptSpinLock interrupt_spin_lock; |
| 80 | pw_sync_InterruptSpinLock_CallLock(&interrupt_spin_lock); |
| 81 | pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock); |
| 82 | } |
| 83 | |
| 84 | TEST(InterruptSpinLock, TryLockUnlockInC) { |
| 85 | pw::sync::InterruptSpinLock interrupt_spin_lock; |
| 86 | ASSERT_TRUE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock)); |
Ewout van Bekkum | 2fed999 | 2021-11-23 11:49:02 -0800 | [diff] [blame] | 87 | // TODO(pwbug/291): Ensure other cores fail to lock when its locked. |
| 88 | // EXPECT_FALSE(pw_sync_InterruptSpinLock_CallTryLock(&interrupt_spin_lock)); |
Ewout van Bekkum | 126e011 | 2021-03-12 10:59:49 -0800 | [diff] [blame] | 89 | pw_sync_InterruptSpinLock_CallUnlock(&interrupt_spin_lock); |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | } // namespace pw::sync |