Single thread vs multithread
Real-Time Operating Systems Busy wait
Task1 Thread scheduler
Ready Ready
Status1 Status1
Terminology Busy Input data1 Busy Input data1
Task2
uC/OS-III, The Real-Time Kernel, or a High Performance, Scalable, ROMable, Preemptive, Ready
Status2
Multitasking Kernel for Microprocessors, Microcontrollers & DSPs, there are a bunch of
versions, with and without a board, Hardcover, by Jean J Labrosse Busy Ready
Output data2 Status2
MicroC OS II: The Real Time Kernel, by Jean J. Labrosse , 2002, ISBN 1-5782-0103-9, $72.76
Busy Output data2
Other
The Definitive Guide to the ARM Cortex-M3 TI, Second Edition, Paperback, Joseph Yiu, $53.95
function1
Task3 Task4
Chapters 5 8 13, Embedded Microcomputer Systems: Real Time Operating Systems for Arm
Cortex M Microcontrollers, , Jonathan W. Valvano, Other
function2 Other Other
function3 function4
Reference McDermott EE382N-4
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
What is a thread? Thread or Task
void Producer(void){
unsigned short data; void Consumer(void){
data = ADC_In(1); unsigned short data,average;
if(OS_Fifo_Put(data) == 0){ unsigned long sum;
Thread1 Thread2 Thread3 Thread4 DataLost++; unsigned short n;
} for(;;){
R0 Stack R0 Stack R0 Stack R0 Stack
R1 R1 R1 R1 } sum = 0;
R2 R2 R2 R2 for(n = 0; n < LENGTH; n++){
... ... ... ...
SP SP SP SP data = OS_Fifo_Get();
PC PC PC PC void Display(void){ sum = sum + data;
unsigned long data,voltage; }
Program Program Program Program
for(;;){ average = sum/LENGTH;
data = OS_MailBox_Recv(); OS_MailBox_Send(average);
voltage = 31*data/64; }
LCD_Message(0,"v(mV) =",voltage); }
}
}
Show main, threads in Lab7
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
1
Real-time tasks Thread Classification
• Hard real-time • Periodic, execution at regular intervals
– Bounded latency – E.g., ADC, DAC, motor control
– E.g., Check CO levels
• Soft real-time
– Execute ASAP
• Aperiodic, execution can not be anticipated
– Execution is frequent
• Not real-time – E.g., New position detected as wheel turns
• Sporadic, execution can not be anticipated
– Execution is infrequent
– E.g., Faults, errors, catastrophes
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
Thread Scheduler Priority
• List possible thread states
• Execute highest priority first
• List possible scheduling algorithms
– Can you have two tasks at same priority?
– What? When to run scheduler??
– How? • Minimize latency on real-time tasks
– Why? Round robin • Assign a dollar cost for delays
Weighted round robin
• Performance measures Priority – Minimize cost
– Utilization
– Latency Static
Dynamic
– Bandwidth Deterministic
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
2
Rate Monotonic Scheduling
Priority Schedulers
Theorem
• Earliest deadline first, dynamic • All n tasks are periodic
• Earliest slack-time first , dynamic – Priority based on period of Ti
– Slack = (time to deadline)-(work left to do) – Maximum execution time Ei
• Rate monotonic scheduling, static • No synchronization between tasks
– Assign priority based on how often Ti is runs • Execute highest priority task first
– Lower Ti (more frequent) are higher priority
E 1/n 1 ln(2)
i n2
Ti
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
Time Management Communication
• Types
• System time – Data sharing
– Pipes=FIFO (one to one, buffered, ordered)
• Time stamps – Mailbox (one to one, unbuffered)
– When did it occur? – Messages (many to many)
– Performance measures • Deadlock
– prevention, avoidance, detection, recovery
• Thread sleeping • Performance measures
– Latency
• Measurements
– Bandwidth
– Input capture period -> wheel RPM – Error rate
– Input capture PW -> ultrasonic distance
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
3
Race condition Race, Critical Sections
• Two or more threads access the same global • Permanently allocated object
• At least one access is a write – Shared variables
– I/O ports
Module 3 Module 7 GPIOB Channel 3
• Write access changes official copy
• Read access creates two copies
Channel 7 – Original copy in memory
GPIOB
– Temporary copy in register
• Nonatomic access, load/store architecture
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
Reentrant Making the access atomic
• Variables in registers, stack • Disable all interrupts Measure time with I=1
- Maximum time
• No nonatomic write sequence • Lock the scheduler - Total time
– Permanently allocated object – No other foreground threads can run
– WR, RMW, WW sequence – Background ISR will occur Show code with NestCnt++
If NestCnt-- == 0 then run or
Look at programming manual • Mutex semaphore don’t run scheduler??
LDREX STREX
[Link] pg33,71 – Blocks other threads trying to access info
[Link] pg 39
– All nonrelated operations not delayed
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
4
Synchronization Semaphore
Fifo MailBox
ADC ADC Producer Consumer Display RxFifo
USART1 serial Interpreter
• Sequential Switch Tamper LCD TxFifo
Main
program ISR
Main
program
ISR
• Fork, spawn, join Flag = 1
Other calculations Flag = 1
0
• Rendezvous Flag
0
Other calculations
• Trigger, event flags Flag 1
1 Flag = 0
– or, and Do important stuff
Flag = 0
– I/O event (e.g., I/O edge, RX, TX) Do important stuff
– periodic time (e.g., TATOMIS)
• Sleep
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
Mailbox Portability
Main ISR
program • Small kernel
Read data • Common structure
Other calculations a from input
• Hardware
Empty c Mail = data abstraction layer
Status Status = Full b
[Link]
Full Show Micrium directory
Process Mail
Status = Empty d
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
5
Hooks Additional OS terms
• Run user supplied code at strategic places • Run-time configurable
• Allows you to – Priority, stack size, fifo size, time slice
– Extend the OS • Certification
– Implement debugging – Medical, transportation, nuclear, military
– Implement performance testing • Scalable
– Implement black box recording – 10 threads versus 200 threads
• Collect run-time performance data • ROMable
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6
Performance measures Summary
• Breakdown Utilization (BU) • Threads are executing software
– The percentage of resource utilization below which
the RTOS can guarantee that all deadlines will be • Synchronization is important
met. (shipping company analogy)
• Normalized Mean Response Time (NMRT) • RTOS has unique requirements
– The ratio of the “best case” time interval a task – Reliability
becomes ready to execute and then terminates, and
the actual CPU time consumed. – Real-Time
• Guaranteed ratio (GR) – Priority
– For dynamic scheduling, the number of tasks whose
deadlines can be guaranteed to be met versus the – Certification
total number of tasks requesting execution.
– Runs in ROM
January 21, 2014 Jonathan Valvano January 21, 2014 Jonathan Valvano
EE445M/EE380L.6 EE445M/EE380L.6