• 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 
15 #include "pw_sync/interrupt_spin_lock.h"
16 
17 #include "pw_assert/assert.h"
18 #include "tx_api.h"
19 
20 namespace pw::sync {
21 
lock()22 void InterruptSpinLock::lock() {
23   // In order to be pw::sync::InterruptSpinLock compliant, mask the interrupts
24   // before attempting to grab the internal spin lock.
25   native_type_.saved_interrupt_mask = tx_interrupt_control(TX_INT_DISABLE);
26 
27   // This implementation is not set up to support SMP, meaning we cannot
28   // deadlock here due to the global interrupt lock, so we crash on recursion
29   // on a specific spinlock instead.
30   PW_CHECK(!native_type_.locked.load(std::memory_order_relaxed),
31            "Recursive InterruptSpinLock::lock() detected");
32 
33   native_type_.locked.store(true, std::memory_order_relaxed);
34 }
35 
try_lock()36 bool InterruptSpinLock::try_lock() {
37   // In order to be pw::sync::InterruptSpinLock compliant, mask the interrupts
38   // before attempting to grab the internal spin lock.
39   UINT saved_interrupt_mask = tx_interrupt_control(TX_INT_DISABLE);
40 
41   if (native_type_.locked.load(std::memory_order_relaxed)) {
42     // Already locked, restore interrupts and bail out.
43     tx_interrupt_control(saved_interrupt_mask);
44     return false;
45   }
46 
47   native_type_.saved_interrupt_mask = saved_interrupt_mask;
48   native_type_.locked.store(true, std::memory_order_relaxed);
49   return true;
50 }
51 
unlock()52 void InterruptSpinLock::unlock() {
53   native_type_.locked.store(false, std::memory_order_relaxed);
54   tx_interrupt_control(native_type_.saved_interrupt_mask);
55 }
56 
57 }  // namespace pw::sync
58