• 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 "pw_interrupt/context.h"
19 #include "task.h"
20 
21 namespace pw::sync {
22 
lock()23 void InterruptSpinLock::lock() {
24   if (interrupt::InInterruptContext()) {
25     native_type_.saved_interrupt_mask = taskENTER_CRITICAL_FROM_ISR();
26   } else {  // Task context
27     taskENTER_CRITICAL();
28   }
29   // We can't deadlock here so crash instead.
30   PW_CHECK(!native_type_.locked.load(std::memory_order_relaxed),
31            "Recursive InterruptSpinLock::lock() detected");
32   native_type_.locked.store(true, std::memory_order_relaxed);
33 }
34 
try_lock()35 bool InterruptSpinLock::try_lock() {
36   if (interrupt::InInterruptContext()) {
37     UBaseType_t saved_interrupt_mask = taskENTER_CRITICAL_FROM_ISR();
38     if (native_type_.locked.load(std::memory_order_relaxed)) {
39       // Already locked, restore interrupts and bail out.
40       taskEXIT_CRITICAL_FROM_ISR(saved_interrupt_mask);
41       return false;
42     }
43     native_type_.saved_interrupt_mask = saved_interrupt_mask;
44   } else {  // Task context
45     taskENTER_CRITICAL();
46     if (native_type_.locked.load(std::memory_order_relaxed)) {
47       // ALready locked, restore interrupts and bail out.
48       taskEXIT_CRITICAL();
49       return false;
50     }
51   }
52   native_type_.locked.store(true, std::memory_order_relaxed);
53   return true;
54 }
55 
unlock()56 void InterruptSpinLock::unlock() {
57   native_type_.locked.store(false, std::memory_order_relaxed);
58   if (interrupt::InInterruptContext()) {
59     taskEXIT_CRITICAL_FROM_ISR(native_type_.saved_interrupt_mask);
60   } else {  // Task context
61     taskEXIT_CRITICAL();
62   }
63 }
64 
65 }  // namespace pw::sync
66