Lec 03
Lec 03
ECE445M/ECE380L.12
Embedded and Real-Time Systems/
Real-Time Operating Systems
Lecture 3:
Thread Communication &
Synchronization
Thread Communication/Sharing
Thread1 Thread2 Thread3
pt pt pt
Global
Treat I/O device
• Shared Globals registers like
globals
• Mailbox (Lab 2)
• FIFO queues (Lab 2)
• Message (Lab 6)
Lecture 3 J. Valvano, A. Gerstlauer 2
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 1
ECE445M/ECE380L.12, Lecture 3 1/28/2024
Thread Communication
Fifo MailBox
ADC ADC Producer Consumer Display RxFifo
USART1 serial Interpreter
• Types
– Data sharing (global variable)
– Flag, Mailbox (one to one, unbuffered)
– Pipes=FIFO (one to one, buffered, ordered)
– Messages (many to many)
• Performance measures
– Latency
– Bandwidth
– Error rate
Lecture 3 J. Valvano, A. Gerstlauer 3
ECE445M/ECE380L.12
Flag
Main Main
program ISR program
ISR
Flag = 1
Other calculations Flag = 1
0
Flag
0
Flag Other calculations 1
1 Flag = 0
Do important stuff
Flag = 0
Do important stuff
J. Valvano, A. Gerstlauer 2
ECE445M/ECE380L.12, Lecture 3 1/28/2024
Mailbox
Main ISR
program
Read data
Other calculations a from input
Empty c Mail = data b
Status Status = Full
Full
Process Mail
Status = Empty d
FIFO
Input ISR Output ISR
Input
Read data Empty
from input TxFifo
Empty Output Not empty
RxFifo Full
RxFifo TxFifo_Get
Not empty Not full Full Not full Disarm
TxFifo
RxFifo_Get RxFifo_Put Write data output
TxFifo_Put to output
return ERROR
Arm output
return
UARTInts_4C123.zip
J. Valvano, A. Gerstlauer 3
ECE445M/ECE380L.12, Lecture 3 1/28/2024
Channel 7
GPIOB
Race Condition
• Timing bug
– Result depends on the sequence of threads
• E.g. two threads writing to the same global
• Hard to debug
– Depends on specific order/interleaving
• Non-deterministic (external events)
• Hard to reproduce/stabilize (“Heisenbug”)
• Critical or non-critical
– Final program output invalid?
Lecture 3 J. Valvano, A. Gerstlauer 8
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 4
ECE445M/ECE380L.12, Lecture 3 1/28/2024
Critical Section
• Non-atomic access sequence
– Begins/ends with access to permanent resource
– Involves at least one write
– WR(+W), WW(+R/W), RR(+W)
• Load/store architecture
– Read access creates two copies
• Original copy in memory
• Temporary copy in register
– Write access changes official copy
– Read-modify-write sequence: RMW(+W)
Lecture 3 J. Valvano, A. Gerstlauer 9
ECE445M/ECE380L.12
Thread-Safe, Reentrant
• Thread-safe code
– No global resources
• Variables in registers, stack
– No critical section
• No write access sequence
– Mutual exclusion
• Make accesses atomic (no preemption)
• Prevent other threads from entering critical section
• Reentrant code
– Multiple threads can (re-)enter same section
• No non-atomic RMW, WW, WR sequence
Lecture 3 J. Valvano, A. Gerstlauer 10
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 5
ECE445M/ECE380L.12, Lecture 3 1/28/2024
Mutual Exclusion
• Disable all interrupts Measure time with interrupts disabled
- Maximum time
– Make atomic - Total time
LDREX
Lecture 3 J. Valvano, A. Gerstlauer 11
STREXECE445M/ECE380L.12
Cortex-M3/M4F Instruction Set, pg. 50
Thread Synchronization
• Sequential
• Rendezvous, Barrier
– Fork/spawn & join
• Trigger, event flags
– OR, AND
– I/O event (e.g., I/O edge, RX, TX)
• Time
– Periodic time triggered (e.g., TATOMIS)
– Sleep
Lecture 3 J. Valvano, A. Gerstlauer 12
ECE445M/ECE380L.12
J. Valvano, A. Gerstlauer 6