36
Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

  • View
    231

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Chapter 7 Real-Time Embedded Multithreading

Mutual Exclusion Challenges and Considerations

Page 2: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

OUTLINE

Introduction Protecting a critical Section Providing Exclusive Access to Shared Resources Mutex Control Block Summary of Mutex Services Avoiding the Deadly Embrace Sample System Using a Mutex to Protect Critical Sec

tions Mutex Internals

Page 3: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Introduction

To guarantee that a thread has exclusive a access to a shared resource or to a critical section

Ensure that exclusive access can be provided

A mutex is a public resource that can be owned by, at most, one thread at any point in time.

Page 4: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Protecting a Critical Section (CONT.)

A critical section is a code segment in which instructions must be executed in sequence without interruption.

To enter this critical section, a thread must first obtain ownership of a certain mutex that protects the critical section.

Page 5: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Protecting a Critical Section

Page 6: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Providing Exclusive Access to Shared resources (CONT.)

A mutex can provide exclusive access to one shared resource in the same manner that it can protect a critical section.

If a thread must have exclusive access to two (or more) shared resources at the same time , then it must protect each shared resource with a separate mutex.

Page 7: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Providing Exclusive Access to Shared resources

Page 8: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Mutex Control Block (CONT.)

The mutex control block (MCB) is a structure used to maintain the state of a mutex during run-time.

Page 9: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Mutex Control Block

Page 10: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Creating a Mutex

A mutex is declared with the TX_MUTEX data type and is defined with the tx_mutex_create service.

Priority inheritance allows a lower-priority thread to temporarily assume the priority of a higher-priority thread that is waiting for a mutex owned by the lower-priority thread

Page 11: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 12: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Deleting a Mutex

Delete a mutex

When a mutex is deleted, all thread that have been suspended because they are waiting for that mutex are resumed.

Page 13: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Speedy 5, 5 Slow 5,5

Page 14: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 15: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 16: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Obtaining Ownership of a Mutex

Attempt to obtain ownership of a mutex

If you use priority inheritance, make certain that you do not allow external thread to modify the priority of the thread that has inherited a higher priority during mutex ownership.

Page 17: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 18: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Slow 10,10

Speedy 5,5

TX_INHERIT

Page 19: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 20: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 21: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Speedy 5,5 Slow 10,10 Third 10,10

Page 22: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 23: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 24: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 25: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Retrieving mutex Information

Retrieve information about a mutex

Page 26: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations
Page 27: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Page 28: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services (CONT.)

Prioritizing the Mutex Suspension List

Put highest-priority suspended thread at front of suspension list

Page 29: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Summary of Mutex Services

Releasing Ownership of a Mutex

Release ownership of mutex If the ownership count become zero, the mute

x becomes available. If the mutex becomes available and if priority i

nheritance is enabled for this mutex, then the priority of the releasing thread reverts to the priority it had when it originally obtained ownership of the mutex.

Page 30: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Avoiding the Deadly Embrace (CONT.)

Permit a thread to own at most one mutex at any time.

If thread must own multiple mutexes, you can generally avoid deadly embraces if you make the thread gather the mutexes in the same order.

Page 31: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Avoiding the Deadly Embrace (CONT.)

One way to recover from a deadly embrace is to use the suspension time-out feature associated with the tx_mutex_get service.

Another way to recover from a deadly embrace is for another to invoke the tx_thread_wait_abort service to abort the suspension of a thread trapped in a deadly embrace

Page 32: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations
Page 33: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Sample System using a Mutex to Protect Critical Sections (CONT.)

Page 34: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Sample System using a Mutex to Protect Critical Sections (CONT.)

Page 35: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Sample System using a Mutex to Protect Critical Sections

Page 36: Chapter 7 Real-Time Embedded Multithreading Mutual Exclusion Challenges and Considerations

Mutex Internals