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

Preview:

Citation preview

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

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.

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.

Protecting a Critical Section

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.

Providing Exclusive Access to Shared resources

Mutex Control Block (CONT.)

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

Mutex Control Block

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

Summary of Mutex Services (CONT.)

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.

Summary of Mutex Services (CONT.)

Speedy 5, 5 Slow 5,5

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

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.

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Slow 10,10

Speedy 5,5

TX_INHERIT

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

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

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Retrieving mutex Information

Retrieve information about a mutex

Summary of Mutex Services (CONT.)

Summary of Mutex Services (CONT.)

Prioritizing the Mutex Suspension List

Put highest-priority suspended thread at front of suspension list

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.

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.

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

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

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

Sample System using a Mutex to Protect Critical Sections

Mutex Internals

Recommended