Sound Byte Libs 29c5ff3
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_PATTERNS_SYNCHRONIZATION_CRITICAL_SECTION_HPP_
2#define SBL_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
14
15namespace sbl {
16namespace patterns {
17namespace synchronization {
18
19/**
20 * @brief ARM Cortex-M RAII critical section using PRIMASK
21 *
22 * Disables all interrupts (except NMI/Hard Fault) using ARM PRIMASK register.
23 * Automatically restores interrupt state when object goes out of scope.
24 * Compiles to minimal ARM Thumb-2 instructions using CMSIS intrinsics.
25 *
26 * Usage:
27 * @code
28 * volatile uint32_t shared_counter = 0;
29 *
30 * void increment_from_main() {
31 * CriticalSection cs;
32 * shared_counter++; // Safe from interrupt interference
33 * } // Interrupts automatically restored here
34 * @endcode
35 *
36 * Design principles:
37 * - Optimized abstraction - compiles to 2 ARM instructions
38 * - ARM Cortex-M native - uses PRIMASK register directly
39 * - Exception-safe - interrupts restored even if exceptions thrown
40 * - Efficient - only saves/restores actual PRIMASK state
41 *
42 * @note ISR-safe — compiles to 2 ARM instructions (MRS/MSR PRIMASK).
43 */
45public:
46 /**
47 * @brief Construct critical section and disable ARM interrupts
48 *
49 * Saves current PRIMASK state and disables all interrupts except
50 * NMI and Hard Fault. Uses HAL interrupt primitives.
51 */
52 CriticalSection() : saved_primask_(sbl::hal::interrupts::disable_and_save()) {
53 }
54
55 /**
56 * @brief Destructor - restore ARM PRIMASK state
57 *
58 * Restores PRIMASK to state before critical section was entered.
59 * Guarantees proper interrupt restoration even in error conditions.
60 */
62 sbl::hal::interrupts::restore(saved_primask_);
63 }
64
65 // Non-copyable to prevent accidental duplication
68
69 // Non-movable to maintain RAII semantics
72
73private:
74 const uint32_t saved_primask_;
75};
76
77/**
78 * @brief Convenience macro for ARM critical sections
79 *
80 * Creates an ARM critical section with automatic variable naming.
81 * Simplified for ARM-only usage without controller parameter.
82 *
83 * Usage:
84 * @code
85 * CRITICAL_SECTION {
86 * // Protected code here - ARM interrupts disabled
87 * shared_data++;
88 * } // Automatically restored
89 * @endcode
90 */
91#define CRITICAL_SECTION \
92 if (auto cs = sbl::patterns::synchronization::CriticalSection(); true)
93
94} // namespace synchronization
95} // namespace patterns
96} // namespace sbl
97
98#endif // SBL_PATTERNS_SYNCHRONIZATION_CRITICAL_SECTION_HPP_
ARM Cortex-M RAII critical section using PRIMASK.
~CriticalSection()
Destructor - restore ARM PRIMASK state.
CriticalSection & operator=(const CriticalSection &)=delete
CriticalSection()
Construct critical section and disable ARM interrupts.
CriticalSection & operator=(CriticalSection &&)=delete
CriticalSection(const CriticalSection &)=delete
ARM Cortex-M interrupt control primitives.
void restore(uint32_t saved_state)
Restore ARM interrupt state.
Definition control.hpp:54
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24