Sound Byte Libs 1ee2ca6
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
critical_section.hpp
Go to the documentation of this file.
1#ifndef SBL_CORE_PATTERNS_SYNCHRONIZATION_CRITICAL_SECTION_HPP_
2#define SBL_CORE_PATTERNS_SYNCHRONIZATION_CRITICAL_SECTION_HPP_
3
4/**
5 * @file critical_section.hpp
6 * @brief ARM Cortex-M RAII critical section for interrupt disable/restore
7 * @ingroup patterns
8 *
9 * Provides automatic ARM interrupt disable/restore using HAL interrupt primitives.
10 * RAII wrapper that ensures interrupt state is always restored properly.
11 */
12
13#include <sbl/core/hal/interrupts/control.hpp>
14
15namespace sbl {
16namespace core {
17namespace patterns {
18namespace synchronization {
19
20/**
21 * @brief ARM Cortex-M RAII critical section using PRIMASK
22 *
23 * Disables all interrupts (except NMI/Hard Fault) using ARM PRIMASK register.
24 * Automatically restores interrupt state when object goes out of scope.
25 * Compiles to minimal ARM Thumb-2 instructions using CMSIS intrinsics.
26 *
27 * Usage:
28 * @code
29 * volatile uint32_t shared_counter = 0;
30 *
31 * void increment_from_main() {
32 * CriticalSection cs;
33 * shared_counter++; // Safe from interrupt interference
34 * } // Interrupts automatically restored here
35 * @endcode
36 *
37 * Design principles:
38 * - Optimized abstraction - compiles to 2 ARM instructions
39 * - ARM Cortex-M native - uses PRIMASK register directly
40 * - Exception-safe - interrupts restored even if exceptions thrown
41 * - Efficient - only saves/restores actual PRIMASK state
42 */
44public:
45 /**
46 * @brief Construct critical section and disable ARM interrupts
47 *
48 * Saves current PRIMASK state and disables all interrupts except
49 * NMI and Hard Fault. Uses HAL interrupt primitives.
50 */
51 CriticalSection() : saved_primask_(sbl::core::hal::interrupts::disable_and_save()) {
52 }
53
54 /**
55 * @brief Destructor - restore ARM PRIMASK state
56 *
57 * Restores PRIMASK to state before critical section was entered.
58 * Guarantees proper interrupt restoration even in error conditions.
59 */
63
64 // Non-copyable to prevent accidental duplication
67
68 // Non-movable to maintain RAII semantics
71
72private:
73 const uint32_t saved_primask_;
74};
75
76/**
77 * @brief Convenience macro for ARM critical sections
78 *
79 * Creates an ARM critical section with automatic variable naming.
80 * Simplified for ARM-only usage without controller parameter.
81 *
82 * Usage:
83 * @code
84 * CRITICAL_SECTION {
85 * // Protected code here - ARM interrupts disabled
86 * shared_data++;
87 * } // Automatically restored
88 * @endcode
89 */
90#define CRITICAL_SECTION \
91 if (auto cs = sbl::core::patterns::synchronization::CriticalSection(); true)
92
93} // namespace synchronization
94} // namespace patterns
95} // namespace core
96} // namespace sbl
97
98#endif // SBL_CORE_PATTERNS_SYNCHRONIZATION_CRITICAL_SECTION_HPP_
ARM Cortex-M RAII critical section using PRIMASK.
CriticalSection & operator=(const CriticalSection &)=delete
CriticalSection(const CriticalSection &)=delete
CriticalSection & operator=(CriticalSection &&)=delete
CriticalSection()
Construct critical section and disable ARM interrupts.
~CriticalSection()
Destructor - restore ARM PRIMASK state.
void restore(uint32_t saved_state)
Restore ARM interrupt state.
Definition control.hpp:55
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24