Using Synch Mechanisms To Protect Shared Resources
Using Synch Mechanisms To Protect Shared Resources
Synchronization
Mechanisms
to Protect
Shared Resources
in
MCU-Constrained
Systems
Table of Contents
Table of Contents
1. Introduction
2. Understanding the Problem of Shared
Resources
3. Using volatile for Shared Resources
4. Disabling and Restoring Interrupts (cli() and
sei())
5. Using
ATOMIC_BLOCK(ATOMIC_RESTORESTA
TE) for Critical Sections
6. Mutex Simulation in MCU-Based Systems
7. Protecting Shared Resources with Double
Buffering
8. Using Semaphores for Synchronization
9. Best Practices for Efficient Synchronization
Resources
Table of Contents
3. Using volatile for Shared Resources
4. Disabling and Restoring Interrupts (cli() and
sei())
5. Using
ATOMIC_BLOCK(ATOMIC_RESTORESTA
TE) for Critical Sections
6. Mutex Simulation in MCU-Based Systems
7. Protecting Shared Resources with Double
Buffering
8. Using Semaphores for Synchronization
9. Best Practices for Efficient Synchronization
in Embedded Systems
10.Conclusion
1. Introduction
1. Introduction
Example:
Using volatile to Access an ISR-Updated
Variable
• Memory-mapped registers for hardware
3. Using volatile for Shared
peripherals. Resources
Example:
Using volatile to Access an ISR-Updated
Variable
Example:
Protecting a Shared Variable with cli() and
sei()
Trade-offs
• Simple and effective for small critical
interrupts before accessing shared resources
4. Disabling and Restoring
and re-enable them(cli()
Interrupts afterward.
and sei())
Example:
Protecting a Shared Variable with cli() and
sei()
Trade-offs
• Simple and effective for small critical
sections.
• Disables all interrupts, which can impact
real-time performance if the critical section
• is too long.
5. Using
ATOMIC_BLOCK(
ATOMIC_RESTOR
ESTATE) for
Critical Sections
5. Using
ATOMIC_BLOCK(ATOMIC_RESTO
RESTATE) for Critical Sections
Example:
Reading a Multi-Byte Sensor Value Atomically
), which disables 5. Using only temporarily
interrupts
ATOMIC_BLOCK(ATOMIC_RESTO
andRESTATE)
restores their
fororiginal stateSections
Critical afterward.
Example:
Reading a Multi-Byte Sensor Value Atomically
Advantages:
• Safer than cli()/sei() because it restores the
previous interrupt state.
• Ensures atomic access to shared
5. Using
ATOMIC_BLOCK(ATOMIC_RESTO
RESTATE) for Critical Sections
Advantages:
• Safer than cli()/sei() because it restores the
previous interrupt state.
• Ensures atomic access to shared
resources. Not suitable for longer critical
sections (still blocks interrupts briefly).
6. Mutex
Simulation in
MCU-Based
Systems
6. Mutex Simulation in MCU-
Based Systems
Example:
Implementing a Mutex with Interrupt
Protection
Implementing a Mutex with Interrupt
6. Mutex Simulation in MCU-
Protection Based Systems
Advantages:
• Prevents simultaneous access to shared
resources.
6. Mutex Simulation in MCU-
Based Systems
Advantages:
• Prevents simultaneous access to shared
resources.
• Requires careful management to prevent
deadlocks.
7. Protecting
Shared Resources
with Double
Buffering
7. Protecting Shared Resources
with Double Buffering
How It Works
One buffer (active buffer) is used for reading.
Another buffer (working buffer) is updated in
the background.
Buffer swapping ensures consistency.
Example:
Double Buffering for Sensor Readings
the background.
7. Protecting Shared Resources
Buffer swapping ensuresBuffering
with Double consistency.
Example:
Double Buffering for Sensor Readings
Advantages:
• Prevents data corruption during updates.
• No need for disabling interrupts during
reads.
8. Using
Semaphores for
Synchronization
8. Using Semaphores for
Synchronization
Example:
Using a Semaphore to Synchronize ISR and
Main Loop
Using a Semaphore to Synchronize ISR and
8. Using Semaphores for
Main Loop Synchronization
Advantages:
• Efficient for ISR and main loop
coordination.
• Needs careful handling to avoid race
conditions.
9. Best Practices
for Efficient
Synchronization
in Embedded
Systems
9. Best Practices for Efficient
Synchronization in Embedded
Systems