Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
InterruptLock.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdint>
10
11#if defined(ESP_PLATFORM) || defined(ARDUINO_ARCH_ESP32)
12#include <freertos/FreeRTOS.h>
13#include <freertos/portmacro.h>
14#define CORIUM_HAS_ESP32_CRITICAL 1
15#elif defined(__arm__) || defined(__thumb__)
16#if __has_include(<cmsis_compiler.h>)
17#include <cmsis_compiler.h>
18#define CORIUM_HAS_ARM_CMSIS 1
19#endif
20#endif
21
22namespace corium::embedded {
23
27public:
28 InterruptLock() noexcept
29 {
30 lock();
31 }
32
33 ~InterruptLock() noexcept
34 {
35 unlock();
36 }
37
38 InterruptLock(const InterruptLock&) = delete;
40
43
44 void lock() noexcept
45 {
46 if (!_locked) {
47#if defined(CORIUM_HAS_ESP32_CRITICAL)
48 portENTER_CRITICAL(&_mux);
49#elif defined(CORIUM_HAS_ARM_CMSIS)
50 _primask = __get_PRIMASK();
51 __disable_irq();
52#endif
53 _locked = true;
54 }
55 }
56
57 void unlock() noexcept
58 {
59 if (_locked) {
60#if defined(CORIUM_HAS_ESP32_CRITICAL)
61 portEXIT_CRITICAL(&_mux);
62#elif defined(CORIUM_HAS_ARM_CMSIS)
63 __set_PRIMASK(_primask);
64#endif
65 _locked = false;
66 }
67 }
68
69 [[nodiscard]] bool isLocked() const noexcept
70 {
71 return _locked;
72 }
73
74private:
75 bool _locked = false;
76#if defined(CORIUM_HAS_ESP32_CRITICAL)
77 static inline portMUX_TYPE _mux = portMUX_INITIALIZER_UNLOCKED;
78#elif defined(CORIUM_HAS_ARM_CMSIS)
79 uint32_t _primask = 0;
80#endif
81};
82
83} // namespace corium::embedded
RAII interrupt locking helper disabling interrupts upon construction and restoring them upon destruct...
Definition InterruptLock.hpp:26
InterruptLock & operator=(const InterruptLock &)=delete
InterruptLock(InterruptLock &&)=delete
void lock() noexcept
Definition InterruptLock.hpp:44
InterruptLock(const InterruptLock &)=delete
~InterruptLock() noexcept
Definition InterruptLock.hpp:33
bool isLocked() const noexcept
Definition InterruptLock.hpp:69
InterruptLock & operator=(InterruptLock &&)=delete
InterruptLock() noexcept
Definition InterruptLock.hpp:28
void unlock() noexcept
Definition InterruptLock.hpp:57
Definition CanAdapter.hpp:16