Files
MuditaOS/module-os/LockGuard.cpp
Mateusz Piesta 2f42cd5e73 [BH-1424] FreeRTOS update
Updated FreeRTOS source code to 10.4.6.
Moved FreeRTOS sources to the third-party directory.
Added necessary changes to the CMake configuration.
Split FreeRTOSConfig.h. From now, each board has its own.
Added missing log headers.
Minor refactor of the module-os cmake.
Fixed stack overflows in bell application main and time service.
2022-04-07 13:31:28 +02:00

42 lines
924 B
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "critical.hpp"
#include "LockGuard.hpp"
#include <macros.h>
#include <stdexcept>
namespace
{
[[nodiscard]] bool isOSRunning()
{
return xTaskGetSchedulerState() == taskSCHEDULER_RUNNING;
}
} // namespace
LockGuard::LockGuard(cpp_freertos::MutexStandard &mutex) : mutex(mutex)
{
if (not isOSRunning()) {
return;
}
if (isIRQ()) {
savedInterruptStatus = cpp_freertos::CriticalSection::EnterFromISR();
}
else if (!mutex.Lock()) {
throw std::runtime_error("LockGuard failed to lock mutex");
}
}
LockGuard::~LockGuard()
{
if (not isOSRunning()) {
return;
}
if (isIRQ()) {
cpp_freertos::CriticalSection::ExitFromISR(savedInterruptStatus);
}
else {
mutex.Unlock();
}
}