real-time embedded multithreading, using threadx and arm ~ chapter10 ~ 學生 : 曾楓喬 學生 :...

34
Real-Time Embedded Real-Time Embedded Multithreading, Usin Multithreading, Usin g ThreadX and ARM g ThreadX and ARM ~ Chapter10 ~ ~ Chapter10 ~ 學學 學學 : : 學學 學學 學學學學 學學學學 : : 學學學 學學學

Post on 22-Dec-2015

293 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Real-Time EmbeddedReal-Time EmbeddedMultithreading, Using ThreaMultithreading, Using Threa

dX and ARMdX and ARM~ Chapter10 ~~ Chapter10 ~

學生 學生 : : 曾楓喬曾楓喬指導教授 指導教授 : : 張軒彬張軒彬

Page 2: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

OutlineOutline IntroductionIntroduction Counting Semaphore Control BlockCounting Semaphore Control Block Summary of Counting Semaphore ServicesSummary of Counting Semaphore Services Avoiding two thingsAvoiding two things Comparing a Counting Semaphore with a MutexComparing a Counting Semaphore with a Mutex Sample System Using a Binary Semaphore in Place of a Sample System Using a Binary Semaphore in Place of a

MutexMutex Sample System Using a Counting Semaphore in a ProduSample System Using a Counting Semaphore in a Produ

cer-Consumer Applicationcer-Consumer Application Counting Semaphore InternalsCounting Semaphore Internals

Page 3: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

IntroductionIntroduction

ThreadX provides 32-bit counting semaphores wiThreadX provides 32-bit counting semaphores with counts that range in value from 0 to 2th counts that range in value from 0 to 23232-1.-1.

An An instanceinstance of a counting semaphore is a single c of a counting semaphore is a single count.ount.

Counting semaphores that have values restricted tCounting semaphores that have values restricted to 0 or 1 are called o 0 or 1 are called binary semaphoresbinary semaphores..

An application may use an unlimited number of cAn application may use an unlimited number of counting semaphores. ounting semaphores.

Page 4: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Counting Semaphore Control BlockCounting Semaphore Control Block

The characteristics of each counting semaphores are The characteristics of each counting semaphores are found in its Control Block. found in its Control Block.

The Control Block can be located anywhere in memThe Control Block can be located anywhere in memory.ory.

Page 5: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Summary of Counting Semaphore SSummary of Counting Semaphore Serviceservices

Page 6: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Creating a Counting Semaphore Creating a Counting Semaphore

tx_semaphore_createtx_semaphore_create Fig10.3 Counting semaphore attributesFig10.3 Counting semaphore attributes

The value for the count must be in the range frThe value for the count must be in the range from 0x00000000 to 0xFFFFFFFF.om 0x00000000 to 0xFFFFFFFF.

Counting Semaphore Control BlockCounting Semaphore Control Block

Counting semaphore nameCounting semaphore name

Initial countInitial count

Page 7: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Creating a Counting Semaphore(cont.)Creating a Counting Semaphore(cont.)

Page 8: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Getting an Instance of a Counting Getting an Instance of a Counting Semaphore Semaphore

tx_semaphore_gettx_semaphore_get input parametersinput parameters

semaphore_ptrsemaphore_ptr

wait_option wait_option The service retrieves an instance from the couThe service retrieves an instance from the cou

nting semaphore by decrementing its count.nting semaphore by decrementing its count.

Page 9: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Getting an Instance of a Counting SeGetting an Instance of a Counting Semaphore(cont.)maphore(cont.)

Page 10: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Getting an Instance of a Counting SeGetting an Instance of a Counting Semaphore(cont.)maphore(cont.)

Page 11: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Placing an Instance in a Counting SePlacing an Instance in a Counting Semaphoremaphore

tx_semaphore_puttx_semaphore_put input parameterinput parameter

semaphore_ptrsemaphore_ptr The service places an instance in a counting seThe service places an instance in a counting se

maphore by incrementing its count.maphore by incrementing its count.

Page 12: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Placing an Instance in a Counting SePlacing an Instance in a Counting Semaphore(cont.)maphore(cont.)

Page 13: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Deleting a Counting SemaphoreDeleting a Counting Semaphore

tx_semaphore_deletetx_semaphore_delete input parameterinput parameter

semaphore_ptrsemaphore_ptr The service to delete a counting semaphore.The service to delete a counting semaphore.

Page 14: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Deleting a Counting Semaphore(conDeleting a Counting Semaphore(cont.)t.)

Page 15: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Prioriting a Counting Semaphore SuPrioriting a Counting Semaphore Suspension Listspension List

tx_semaphore_prioritizetx_semaphore_prioritize input parameterinput parameter

semaphore_ptrsemaphore_ptr The service places the highest-priority thread sThe service places the highest-priority thread s

uspended on a specific counting semaphore at uspended on a specific counting semaphore at the front of the suspension list.the front of the suspension list.

All other threads remain in the same FIFO ordAll other threads remain in the same FIFO order in which they were suspended. er in which they were suspended.

Page 16: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Prioriting a Counting Semaphore SuPrioriting a Counting Semaphore Suspension List(cont.)spension List(cont.)

Page 17: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Prioriting a Counting Semaphore SuPrioriting a Counting Semaphore Suspension List(cont.)spension List(cont.)

Priority order : speedy > slow > minPriority order : speedy > slow > min Enter order : min -> slow -> speedyEnter order : min -> slow -> speedy After prioritizing : speedy-> min-> slowAfter prioritizing : speedy-> min-> slow

Page 18: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Retrieving Information About a CouRetrieving Information About a Counting Semaphore nting Semaphore

tx_semaphore_info_gettx_semaphore_info_get input parameterinput parameter

semaphore_ptrsemaphore_ptr output parametersoutput parameters

namenamecurrent_valuecurrent_valuefirst_suspendedfirst_suspendedsuspended_countsuspended_countnext_semaphorenext_semaphore

The service retrieves several useful pieces of informatThe service retrieves several useful pieces of information about a counting semaphore .ion about a counting semaphore .

Page 19: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Retrieving Information About a CouRetrieving Information About a Counting Semaphore(cont.)nting Semaphore(cont.)

Page 20: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Avoiding two thingsAvoiding two things

There are two dangerous pitfalls associated in There are two dangerous pitfalls associated in using semaphores for mutual exclusion.using semaphores for mutual exclusion.

Avoiding deadly embraceAvoiding deadly embrace

Avoiding priority inversionAvoiding priority inversion

Page 21: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Comparing a Counting Semaphore Comparing a Counting Semaphore with a Mutexwith a Mutex

Page 22: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutexphore in Place of a Mutex

Page 23: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutex(cont.)phore in Place of a Mutex(cont.)

Page 24: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutex(cont.)phore in Place of a Mutex(cont.)

Page 25: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutex(cont.)phore in Place of a Mutex(cont.)

Page 26: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutex(cont.)phore in Place of a Mutex(cont.)

Page 27: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Binary SemaSample System Using a Binary Semaphore in Place of a Mutex(cont.)phore in Place of a Mutex(cont.)

Page 28: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

nn

Page 29: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

n (cont.)n (cont.)

Page 30: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

n (cont.)n (cont.)

Page 31: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

n (cont.)n (cont.)

Page 32: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

n (cont.)n (cont.)

Page 33: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Sample System Using a Counting SemapSample System Using a Counting Semaphore in a Producer-Consumer Applicatiohore in a Producer-Consumer Applicatio

n (cont.)n (cont.)

Page 34: Real-Time Embedded Multithreading, Using ThreadX and ARM ~ Chapter10 ~ 學生 : 曾楓喬 學生 : 曾楓喬 指導教授 : 張軒彬

Counting Semaphore InternalsCounting Semaphore Internals