• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   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   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 : public VirtualBasicLockable {
85  public:
86   VirtualInterruptSpinLock() = default;
87 
88   VirtualInterruptSpinLock(const VirtualInterruptSpinLock&) = delete;
89   VirtualInterruptSpinLock(VirtualInterruptSpinLock&&) = delete;
90   VirtualInterruptSpinLock& operator=(const VirtualInterruptSpinLock&) = delete;
91   VirtualInterruptSpinLock& operator=(VirtualInterruptSpinLock&&) = delete;
92 
interrupt_spin_lock()93   InterruptSpinLock& interrupt_spin_lock() { return interrupt_spin_lock_; }
94 
95  private:
DoLockOperation(Operation operation)96   void DoLockOperation(Operation operation) override
97       PW_NO_LOCK_SAFETY_ANALYSIS {
98     switch (operation) {
99       case Operation::kLock:
100         return interrupt_spin_lock_.lock();
101 
102       case Operation::kUnlock:
103       default:
104         return interrupt_spin_lock_.unlock();
105     }
106   }
107 
108   InterruptSpinLock interrupt_spin_lock_;
109 };
110 
111 }  // namespace pw::sync
112 
113 #include "pw_sync_backend/interrupt_spin_lock_inline.h"
114 
115 using pw_sync_InterruptSpinLock = pw::sync::InterruptSpinLock;
116 
117 #else  // !defined(__cplusplus)
118 
119 typedef struct pw_sync_InterruptSpinLock pw_sync_InterruptSpinLock;
120 
121 #endif  // __cplusplus
122 
123 PW_EXTERN_C_START
124 
125 /// Invokes the `InterruptSpinLock::lock` member function on the given
126 /// `interrupt_spin_lock`.
127 void pw_sync_InterruptSpinLock_Lock(pw_sync_InterruptSpinLock* spin_lock)
128     PW_NO_LOCK_SAFETY_ANALYSIS;
129 
130 /// Invokes the `InterruptSpinLock::try_lock` member function on the given
131 /// `interrupt_spin_lock`.
132 bool pw_sync_InterruptSpinLock_TryLock(pw_sync_InterruptSpinLock* spin_lock)
133     PW_NO_LOCK_SAFETY_ANALYSIS;
134 
135 /// Invokes the `InterruptSpinLock::unlock` member function on the given
136 /// `interrupt_spin_lock`.
137 void pw_sync_InterruptSpinLock_Unlock(pw_sync_InterruptSpinLock* spin_lock)
138     PW_NO_LOCK_SAFETY_ANALYSIS;
139 
140 PW_EXTERN_C_END
141