1 // Copyright 2021 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 "RTOS.h" 18 #include "pw_assert/check.h" 19 20 namespace pw::sync { 21 lock()22void InterruptSpinLock::lock() { 23 // Mask interrupts. 24 OS_IncDI(); 25 26 // Disable task switching to ensure kernel APIs cannot switch to other tasks 27 // which could then end up deadlocking recursively on this same lock. 28 OS_SuspendAllTasks(); 29 30 // We can't deadlock here so crash instead. 31 PW_DCHECK(!native_type_.locked, 32 "Recursive InterruptSpinLock::lock() detected"); 33 native_type_.locked = true; 34 } 35 unlock()36void InterruptSpinLock::unlock() { 37 native_type_.locked = false; 38 OS_ResumeAllSuspendedTasks(); // Restore task switching. 39 OS_DecRI(); // Restore interrupts. 40 } 41 42 } // namespace pw::sync 43