Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
barrier.hpp
Go to the documentation of this file.
1/**
2 * @file barrier.hpp
3 * @brief ARM Cortex-M memory barrier primitives
4 * @ingroup hal
5 *
6 * Direct memory barrier operations for ARM Cortex-M processors.
7 * No templates - just hardware primitives that compile to single instructions.
8 */
9
10#ifndef SBL_HAL_MEMORY_BARRIER_HPP_
11#define SBL_HAL_MEMORY_BARRIER_HPP_
12
13namespace sbl {
14namespace hal {
15namespace memory {
16
17/**
18 * @brief Compiler memory barrier
19 *
20 * Prevents compiler from reordering memory operations.
21 * Sufficient for single-core ARM Cortex-M systems.
22 * Compiles to no instructions - affects compiler optimization only.
23 *
24 * @note ISR-safe — compiler-only barrier, zero instructions
25 */
26inline void compiler_barrier() {
27 asm volatile("" ::: "memory");
28}
29
30/**
31 * @brief Data Memory Barrier (ARM DMB instruction)
32 *
33 * Ensures all memory operations before the barrier complete
34 * before any memory operations after the barrier begin.
35 * Compiles to single ARM DMB instruction.
36 *
37 * @note ISR-safe — single DMB instruction
38 */
39inline void data_barrier() {
40#ifdef __ARM_ARCH
41 asm volatile("dmb" ::: "memory");
42#else
43 compiler_barrier(); // Fallback for non-ARM builds
44#endif
45}
46
47/**
48 * @brief Data Synchronization Barrier (ARM DSB instruction)
49 *
50 * Ensures all memory operations and instructions complete
51 * before proceeding. Stronger than DMB.
52 * Compiles to single ARM DSB instruction.
53 *
54 * @note ISR-safe — single DSB instruction
55 */
56inline void sync_barrier() {
57#ifdef __ARM_ARCH
58 asm volatile("dsb" ::: "memory");
59#else
60 compiler_barrier(); // Fallback for non-ARM builds
61#endif
62}
63
64/**
65 * @brief Instruction Synchronization Barrier (ARM ISB instruction)
66 *
67 * Flushes pipeline and ensures all subsequent instructions
68 * are fetched from cache/memory after barrier effects complete.
69 * Compiles to single ARM ISB instruction.
70 *
71 * @note ISR-safe — single ISB instruction
72 */
73inline void instruction_barrier() {
74#ifdef __ARM_ARCH
75 asm volatile("isb" ::: "memory");
76#else
77 compiler_barrier(); // Fallback for non-ARM builds
78#endif
79}
80
81/**
82 * @brief Memory barrier policy for RingBuffer template
83 *
84 * On single-core Cortex-M, a compiler barrier is sufficient for SPSC
85 * ring buffers. It prevents the compiler from reordering stores (data
86 * write before index update) without emitting any instructions.
87 */
89 /** @note ISR-safe — compiler-only barrier, zero instructions */
90 static void full_barrier() {
92 }
93};
94
95} // namespace memory
96} // namespace hal
97} // namespace sbl
98
99#endif // SBL_HAL_MEMORY_BARRIER_HPP_
void sync_barrier()
Data Synchronization Barrier (ARM DSB instruction)
Definition barrier.hpp:56
void data_barrier()
Data Memory Barrier (ARM DMB instruction)
Definition barrier.hpp:39
void instruction_barrier()
Instruction Synchronization Barrier (ARM ISB instruction)
Definition barrier.hpp:73
void compiler_barrier()
Compiler memory barrier.
Definition barrier.hpp:26
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
Memory barrier policy for RingBuffer template.
Definition barrier.hpp:88