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 #pragma once 15 16 #include <stdbool.h> 17 18 #include "pw_preprocessor/util.h" 19 #include "pw_sync/lock_annotations.h" 20 21 #ifdef __cplusplus 22 23 #include "pw_sync_backend/interrupt_spin_lock_native.h" 24 25 namespace pw::sync { 26 27 // The InterruptSpinLock is a synchronization primitive that can be used to 28 // protect shared data from being simultaneously accessed by multiple threads 29 // and/or interrupts as a targeted global lock, with the exception of 30 // Non-Maskable Interrupts (NMIs). 31 // It offers exclusive, non-recursive ownership semantics where IRQs up to a 32 // backend defined level of "NMIs" will be masked to solve priority-inversion. 33 // 34 // NOTE: This InterruptSpinLock relies on built-in local interrupt masking to 35 // make it interrupt safe without requiring the caller to separately mask and 36 // unmask interrupts when using this primitive. 37 // 38 // Unlike global interrupt locks, this also works safely and efficiently on SMP 39 // systems. On systems which are not SMP, spinning is not required and it's 40 // possible that only interrupt masking occurs but some state may still be used 41 // to detect recursion. 42 // 43 // This entire API is IRQ safe, but NOT NMI safe. 44 // 45 // Precondition: Code that holds a specific InterruptSpinLock must not try to 46 // re-acquire it. However, it is okay to nest distinct spinlocks. 47 class PW_LOCKABLE("pw::sync::InterruptSpinLock") InterruptSpinLock { 48 public: 49 using native_handle_type = backend::NativeInterruptSpinLockHandle; 50 51 constexpr InterruptSpinLock(); 52 ~InterruptSpinLock() = default; 53 InterruptSpinLock(const InterruptSpinLock&) = delete; 54 InterruptSpinLock(InterruptSpinLock&&) = delete; 55 InterruptSpinLock& operator=(const InterruptSpinLock&) = delete; 56 InterruptSpinLock& operator=(InterruptSpinLock&&) = delete; 57 58 // Locks the spinlock, blocking indefinitely. Failures are fatal. 59 // 60 // Precondition: Recursive locking is undefined behavior. 61 void lock() PW_EXCLUSIVE_LOCK_FUNCTION(); 62 63 // Attempts to lock the spinlock in a non-blocking manner. 64 // Returns true if the spinlock was successfully acquired. 65 // 66 // Precondition: Recursive locking is undefined behavior. 67 bool try_lock() PW_EXCLUSIVE_TRYLOCK_FUNCTION(true); 68 69 // Unlocks the spinlock. Failures are fatal. 70 // 71 // PRECONDITION: 72 // The spinlock is held by the caller. 73 void unlock() PW_UNLOCK_FUNCTION(); 74 75 native_handle_type native_handle(); 76 77 private: 78 // This may be a wrapper around a native type with additional members. 79 backend::NativeInterruptSpinLock native_type_; 80 }; 81 82 } // namespace pw::sync 83 84 #include "pw_sync_backend/interrupt_spin_lock_inline.h" 85 86 using pw_sync_InterruptSpinLock = pw::sync::InterruptSpinLock; 87 88 #else // !defined(__cplusplus) 89 90 typedef struct pw_sync_InterruptSpinLock pw_sync_InterruptSpinLock; 91 92 #endif // __cplusplus 93 94 PW_EXTERN_C_START 95 96 void pw_sync_InterruptSpinLock_Lock(pw_sync_InterruptSpinLock* spin_lock) 97 PW_NO_LOCK_SAFETY_ANALYSIS; 98 bool pw_sync_InterruptSpinLock_TryLock(pw_sync_InterruptSpinLock* spin_lock) 99 PW_NO_LOCK_SAFETY_ANALYSIS; 100 void pw_sync_InterruptSpinLock_Unlock(pw_sync_InterruptSpinLock* spin_lock) 101 PW_NO_LOCK_SAFETY_ANALYSIS; 102 103 PW_EXTERN_C_END 104