Professional Documents
Culture Documents
and
Why Use One?
May, 2013
2
2013, Express Logic, Inc.
3
2013, Express Logic, Inc.
4
2013, Express Logic, Inc.
Determinism
Time required to complete any function must be
finite and predictable
Maximum response time must be calculable
and guaranteed
Number of cycles required to execute a given
operation must always be the same
Execution can be interrupted, but interrupt
latency and processing time must be bounded
Not
N t allll systems
t
require
i it
Not all RTOSes deliver it
5
2013, Express Logic, Inc.
What is an RTOS?
Kernel
File System
Networking
USB
Graphics
RTOS
File
System
Kernel Components
Scheduler
Thread Management
Message Queues
Semaphores
p
Mutexes
Timers
Memoryy Pools
USB, etc.
Kernel
Graphics
Networking
RTOS p
provides centralized mechanisms for arbitrating
g requests
q
for resources
Memory allocation/de-allocation at run-time
Semaphores
p
and Mutexes to control single-use
g
hardware or critical
sections of software
Preemption-Threshold to help manage access to Critical Sections
File System
TCP/IP Network Stack
USB Stack
Graphics
3rd Party Products
Example: Non-RTOS
Simple application with two functions
One sends a message to a buffer and checks for reply
Second one checks for message and if found, replies
Non-RTOS Example
Main
Function1
Call
Function1
Check for
Message?
No
Call
Function2
Function2
Yes
Any
A
message
in queue?
No
Yes
Send
Message?
Yes
No
Any
A
message
in queue?
Yes
Send Message
Get message
from queue
Send Message
Get message
from queue
Change state
to check on
next run
Change state
to send on
next run
Change state
to check on
next run
Change state
to send on
next run
Exit
Exit
No
Example: RTOS
Routines become RTOS threads
M
Messages gett sentt tto, and
d retrieved
ti
d from
f
a queue
RTOS Example
Main
Start
Thread_0
Start
Thread_1
Thread_1
Thread_0
Send Message
Suspend
if queue
full
Resume
when
room
Get message
from queue
Suspend
if queue
empty
Resume
when
msg
R
T
O
S
Suspend
if queue
empty
Resume
when
msg
Suspend
if queue
full
Resume
when
room
Note no
return
Get message
from queue
Send Message
Thread
ead
Semi-independent program segment; multiple threads
can share the same memory space
Multithreading:
M ltith di
running
i severall th
threads
d concurrently
tl
Some RTOSes use task to mean thread
19
2013, Express Logic, Inc.
Threads
What is a thread?
Thread
Thread Priorities
Thread
Thread
Thread States
Modularize a program
Mi i i stalls
Minimize
ll
Thread Services
Semi-independent program
segment
Share same memory space
Run concurrently
Process
Priority
0
Highest
1
2
Lowest
Multithreading
I/O
Start
Thread A
Waits
I/O
Finish
Thread A
Thread B
Thread A has
to wait for I/O
While Thread A
is waiting for
I/O, Thread B
can use CPU
When I/O is
done, Thread A
can continue
Context Switch
Thread Context
Context Switch
What s involved in a
Whats
context switch?
See
Step
PC
Registers
Operation
SP
Cycles
2 - 20
S it h tto the
Switch
th system
t
stack
t k pointer.
i t
2 - 20
2 - 20
2 - 50
2 - 50
20 - 100
2 - 40
Other processing
0 - 100
TOTAL
50 - 500
20 - 100
Types of Schedulers
Thread-1
?
Thread-2
Round-Robin Scheduling
Thread-3
Thread-1
Based on priority
Performs context switches
Manages thread states
Big Loop
Thread-4
Scheduler
Preemptive Scheduling
Thread-2
Ready/Running
Suspended (Blocked/Sleeping/Relinquished)
Terminated
RoundThread-4
Robin
Scheduling
Thread-3
Preemptive Scheduling
Preemption
Interrupt
Thread
Preemptive Scheduling
P
Priority
Context
Switch
Thread-2
Runs
Thread-1
Begins
Thread-1
Resumes
Time
Context
Switch
Preemptive Problems
Thread Starvation
Excessive Overhead
If a higher-priority thread is
always ready
ready, the lower
priority threads never
execute
Priorrity
Thread-2 Preempts
Thread-1 and Runs
Thread-1
Begins
Time
Priority Inversion
Priority Inversion
Occurs when higher priority thread is
suspended because a lower priority thread has
a needed resource
May be necessary for 2 threads of different
priorities to share a common resource
Priority inversion time may become undeterministic and lead to application failure
26
2013, Express Logic, Inc.
Priority
y
Thread-3 Preempts
Thread-2, But
Suspends For Mutex-M
Thread-2
Preempts Thread-1
Thread-3
Blocked!
Thread-2 Resumes
Thread-1
Th
d 1 Obt
Obtains
i
Mutex-M
Time
Even though Thread-3 has the highest priority, it must wait for Thread-2.
Priority Inheritance
Priority
y
Thread-3 Preempts
Thread-2, But
Suspends For Mutex-M
Thread-2
Preempts
Thread-1
Thread-1
Thread-3
Resumes
Thread-1 Assumes
Thread-3 Priority and
releases the mutex
Thread-1 Obtains
Mutex-M
Thread-1
Time
Thread-1 assumes the priority of Thread-3
until it is finished with Mutex-M
Preemption-Threshold
Preemption-Threshold
Priority
Priority
Comment
0
:
14
15
:
19
20
Thread is assigned Priority = 20
:
31
Message Queues
messages inserted
at rear of queue
messages removed
from front of queue
Semaphore
Efficient means of inter-thread communication
Binary Semaphores
(0 or 1)
Only one occurrence (single-use resource)
Counting
g Semaphores
(0 - 0xFFFFFFFF)
Many occurrences (multiple-use resource)
Semaphore Get
UINT tx_semaphore_get (TX_SEMAPHORE *semaphore_ptr,
ULONG wait_option);
Semaphore Put
UINT tx_semaphore_put (TX_SEMAPHORE *semaphore_ptr);
33
2013, Express Logic, Inc.
Mutex
Used to control thread access
To critical sections
Or exclusive-use resources
Prevents interference with exclusive use
Si
Similar
il to
t binary
bi
semaphore,
h
but
b t used
d solely
l l for
f
mutual exclusion, and not for event notification
G
Get
t operation
ti obtains
bt i mutex
t nott owned
d by
b
another thread
Suspension if already owned by another thread
Mutex Get
UINT tx_mutex_get (TX_MUTEX *mutex_ptr, ULONG wait_option);
Mutex Put
UINT tx_mutex_put (TX_MUTEX mutex_put);
35
2013, Express Logic, Inc.
What Is An RTOS