Title: Chapter 6 RealTime Embedded Multithreading
1Chapter 6 Real-Time Embedded Multithreading
- The Thread The Essential Component
2OUTLINE
- Thread Control Block
- Summary of Thread Services
- Execution Overview
- Thread States
- Thread Design
- Thread Internals
3Thread Control Block (cont.)
- The Thread Control Block (TCB) is a structure
used to maintain the state of a thread during
run-time. - A TCB can be located anywhere in memory, but it
is most common to make the Control Block a global
structure by defining it outside the scope of any
function.
4Thread Control Block (cont.)
- In most case, the developer need not know the
contents of the TCB.
5Thread Control Block (cont.)
- Two useful members of the Thread control Block
- tx_run_count
- This member contains a count of how many times
the thread has been scheduled. - tx_state
- This member contain the state of the
associated thread.
6Thread Control Block (cont.)
7Thread Control Block
- The develop may inspect the members of the TCB,
but is strictly prohibited from modifying them. - The value of tx_state for an executing thread is
TX_READY.(0x00)
8Summary of Thread Services (cont.)
9Summary of Thread Services (cont.)
- A thread is declared with the TX_THREAD
data type and is defined with the
tx_thread_create service. - Each thread must have its own stack
- The stack size is crucial, it must be large
enougth to accommodate worst-case function call
nesting, local variable allocation, and saving
the threads last execution context
10Summary of Thread Services (cont.)
- The predefined minimum stack size constant,
TX_MINIMUM_STACK, is probably too small for most
application. - It is important to design applications that
create a reasonable number of thread and that
avoid excessive stack usage within threads. - Developers should generally avoid recursive
algorithms and large local data structure.
11Summary of Thread Services (cont.)
12Summary of Thread Services (cont.)
- What happens when a stack area is too small?
- An important feature of multithreading is that
the same C function can be called from multiple
threads. - A reentrant function is one that can be safely
called while it is already being executed.
13Summary of Thread Services (cont.)
14Summary of Thread Services (cont.)
15Summary of Thread Services (cont.)
- If a thread is placed in completed state, it
cannot be executed again.
16Summary of Thread Services (cont.)
- A thread can be deleted only if it is in a
terminated or completed state. - This service cannot be called from a thread
attempting to delete itself.
17Summary of Thread Services (cont.)
18Summary of Thread Services (cont.)
- It is the responsibility of the application to
manage the memory area used by the deleted
threads stack, which is available after the
thread has beendeleted.
19Summary of Thread Services (cont.)
- The tx_thread_identify service returns a pointer
to the currently executing thread. - If this service is called from an ISR, then the
return value represents the thread that was
running prior to the executing interrupt handler. - My_thread_ptr tx_thread_identify()
20Summary of Thread Services (cont.)
- The tx_thread_info_get service obtain such
information about a thread.
21Summary of Thread Services (cont.)
22Summary of Thread Services (cont.)
- Preemption-Threshold Change
- The service tx_thread_preemption_change changes
the preemption-thres_hold of an existing thread.
23Summary of Thread Services (cont.)
24Summary of Thread Services (cont.)
25Summary of Thread Services (cont.)
- When this service is called, the
preemption-threshold of the specified thread is
automatically set to the new priority.
26Summary of Thread Services (cont.)
27Summary of Thread Services (cont.)
28Summary of Thread Services (cont.)
- A thread may voluntarily relinquish control to
another thread by using the tx_thread_relinquish
service.
29Summary of Thread Services (cont.)
- Speedy_thread 5,5 slow_thread 10,10
30Summary of Thread Services (cont.)
31Summary of Thread Services (cont.)
32Summary of Thread Services (cont.)
33Summary of Thread Services (cont.)
34Summary of Thread Services (cont.)
35Summary of Thread Services (cont.)
- When a thread is created with the TX_DONT_START
option, it is placed in a suspended state. - When a thread is suspended because of a call to
tx_thread_suspend, it is also placed in a
suspended state. - The only way such threads can be resumed is when
another thread calls the tx_thread_resume service.
36Summary of Thread Services (cont.)
37Summary of Thread Services (cont.)
38Summary of Thread Services (cont.)
- On some occasions, a thread needs to be suspended
for a specific amount of time . - This is achieved with the tx_thread_sleep
service, which causes the calling thread to
suspend for the specified number of timer-ticks - Status tx_thread_sleep(100)
39Summary of Thread Services (cont.)
- A specified thread can be suspended by calling
the tx_thread_suspend service. - A thread can suspend itself , it can suspend
another thread , or it can be suspended by
another thread. - This type of suspension is called unconditional
suspension. - Condition suspension in which a thread is
suspended because it is waiting for a resource
that is not available, or a thread is sleeping
for a specific period of time.
40Summary of Thread Services (cont.)
- If the specified thread is already suspended
conditionally, the unconditional suspension is
held internally until the prior suspension is
lifted. - When the prior suspension is lifted, the
unconditional suspension of the specified thread
is then performed.
41Summary of Thread Services (cont.)
42Summary of Thread Services (cont.)
43Summary of Thread Services (cont.)
- Terminate Application Thread
- This service terminates the specified application
thread, regardless of whether or not that thread
is currently suspended. - A terminated thread cannot be executed again.
- If you need to execute a terminated thread, then
you must delete it and then create it again.
44Summary of Thread Services (cont.)
45Summary of Thread Services (cont.)
46Summary of Thread Services (cont.)
- This service permits a thread to change its own
time-slice or that of another thread. - Note that if a preemption-threshold has been
specified, then time-slicing for that thread is
disabled.
47Summary of Thread Services (cont.)
48Summary of Thread Services (cont.)
49Summary of Thread Services (cont.)
- This service aborts sleep or any wait-related
suspension of the specified thead. - This service does not release explicit suspension
that is made by the tx_thread_suspend service.
50Summary of Thread Services (cont.)
51Summary of Thread Services
52Execution Overview (cont.)
- There are four types of program execution within
a threadX aplication initialization, thread
execution, interrupt service routines(ISRs), and
application timers.
53Execution Overview (cont.)
54Execution Overview (cont.)
- Initialization includes all program execution
between processor reset and the entry point of
the thread scheduling loop. - The scheduling loop looks for an application
thread that is ready for execution. - When a ready thread is found, ThreadX transfer
control to it. - Without interrupts it would be extremely
difficult to respond to changer in the external
world in a timely manner.
55Execution Overview
- What happen when an interrupt occurs?
- Interrupt may also occur inside an executing ISR
or an application timer. - Application timers are very similar to ISRs,
except actual hardware implementation is hidden
from the application. - Like ISRs, application timers most often
interrupt thread execution. - Unlike ISRs, application timers cannot interrupt
each other
56Thread States
57Thread Design (cont.)
- Minimize the number of thread in the application
system - Choose priorities carefully
- Minimize the number of priorities
- Consider preemption-threshold
- Consider priority inheritance when mutexes are
employed - Consider round-robin scheduling
- Consider time-slicing
58Thread Design (cont.)
- Minimize the number of thread in the application
system
- The number of threads in an application
significantly affects the amount of system
overhead.
59Thread Design (cont.)
- Choose priorities carefully
- Misuse of thread priorities can starve other
thread, create priority inversion, reduce
processing bandwidth, and make the applications
run-time behavior difficult to understand.
60Thread Design (cont.)
- Minimize the number of priorities
- An application that has many different thread
priorities inherently requires more system
overhead than one with a smaller number of
priorities. - If distinct priorities for these thread are
required, then the ThreadX preemption-threshold
mechanism can prevent these extra context
switches.
61Thread Design (cont.)
- Consider preemption-threshold
- There are three primary methods of preventing
priority inversion in ThreadX. - First, the developer can select application
priorities and design run-time behavior in a
manner that prevents the priority inversion
problem - Second, lower-priority threads can utilize
preemption-threshold to block preemption from
intermediate threads while they share resources
with higher-priority threads.
62Thread Design (cont.)
- Finally, threads using ThreadX mutex objects to
protect system resources may utilize the optional
mutex priority inheritance to eliminate
nondeterministic priority inversion.
63Thread Design (cont.)
- Consider Priority Inheritance
- Note that priority inheritance is available only
with a mutex but not with a counting semaphore.
64Thread Design (cont.)
- Consider Round-Robin Scheduling
- ThreadX supports round-robin scheduling of
multiple thread that have the same priority. - This is accomplished through cooperative calls to
the tx_thread_relinquish service.
65Thread Design
- Time-slicing provides another form of round-robin
scheduling for threads with the same priority .
66Thread Internals
67(No Transcript)