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