Chapter 6 RealTime Embedded Multithreading - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

Chapter 6 RealTime Embedded Multithreading

Description:

The Thread Control Block (TCB) is a structure used ... Time-Slice Change. This service permits a thread to change its own time-slice or that of another thread. ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 68
Provided by: Supe1
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 RealTime Embedded Multithreading


1
Chapter 6 Real-Time Embedded Multithreading
  • The Thread The Essential Component

2
OUTLINE
  • Thread Control Block
  • Summary of Thread Services
  • Execution Overview
  • Thread States
  • Thread Design
  • Thread Internals

3
Thread 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.

4
Thread Control Block (cont.)
  • In most case, the developer need not know the
    contents of the TCB.

5
Thread 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.

6
Thread Control Block (cont.)
7
Thread 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)

8
Summary of Thread Services (cont.)
9
Summary of Thread Services (cont.)
  • Thread Creation
  • 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

10
Summary 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.

11
Summary of Thread Services (cont.)
12
Summary 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.

13
Summary of Thread Services (cont.)
14
Summary of Thread Services (cont.)
15
Summary of Thread Services (cont.)
  • If a thread is placed in completed state, it
    cannot be executed again.

16
Summary of Thread Services (cont.)
  • Thread Deletion
  • 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.

17
Summary of Thread Services (cont.)
18
Summary 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.

19
Summary of Thread Services (cont.)
  • Identify Thread
  • 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()

20
Summary of Thread Services (cont.)
  • Get Thread Information
  • The tx_thread_info_get service obtain such
    information about a thread.

21
Summary of Thread Services (cont.)
22
Summary of Thread Services (cont.)
  • Preemption-Threshold Change
  • The service tx_thread_preemption_change changes
    the preemption-thres_hold of an existing thread.

23
Summary of Thread Services (cont.)
24
Summary of Thread Services (cont.)
25
Summary of Thread Services (cont.)
  • Priority Change
  • When this service is called, the
    preemption-threshold of the specified thread is
    automatically set to the new priority.

26
Summary of Thread Services (cont.)
27
Summary of Thread Services (cont.)
28
Summary of Thread Services (cont.)
  • Relinquish
  • A thread may voluntarily relinquish control to
    another thread by using the tx_thread_relinquish
    service.

29
Summary of Thread Services (cont.)
  • Speedy_thread 5,5 slow_thread 10,10

30
Summary of Thread Services (cont.)
31
Summary of Thread Services (cont.)
  • Speedy 5,5
  • Slow 10,10

32
Summary of Thread Services (cont.)
33
Summary of Thread Services (cont.)
34
Summary of Thread Services (cont.)
35
Summary of Thread Services (cont.)
  • Resume Thread Execution
  • 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.

36
Summary of Thread Services (cont.)
37
Summary of Thread Services (cont.)
38
Summary of Thread Services (cont.)
  • Thread Sleep
  • 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)

39
Summary of Thread Services (cont.)
  • Suspend thread Execution
  • 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.

40
Summary 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.

41
Summary of Thread Services (cont.)
42
Summary of Thread Services (cont.)
43
Summary 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.

44
Summary of Thread Services (cont.)
45
Summary of Thread Services (cont.)
46
Summary of Thread Services (cont.)
  • Time-Slice Change
  • 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.

47
Summary of Thread Services (cont.)
48
Summary of Thread Services (cont.)
49
Summary of Thread Services (cont.)
  • Abort Thread suspension
  • 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.

50
Summary of Thread Services (cont.)
51
Summary of Thread Services
52
Execution Overview (cont.)
  • There are four types of program execution within
    a threadX aplication initialization, thread
    execution, interrupt service routines(ISRs), and
    application timers.

53
Execution Overview (cont.)
54
Execution 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.

55
Execution 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

56
Thread States
57
Thread 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

58
Thread 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.

59
Thread 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.

60
Thread 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.

61
Thread 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.

62
Thread Design (cont.)
  • Finally, threads using ThreadX mutex objects to
    protect system resources may utilize the optional
    mutex priority inheritance to eliminate
    nondeterministic priority inversion.

63
Thread Design (cont.)
  • Consider Priority Inheritance
  • Note that priority inheritance is available only
    with a mutex but not with a counting semaphore.

64
Thread 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.

65
Thread Design
  • Consider Time-Slicing
  • Time-slicing provides another form of round-robin
    scheduling for threads with the same priority .

66
Thread Internals
67
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com