mutual exculsion semaphores

Mutual exculsion - Chapter 8 Mutual Exclusion Semaphores Mutual exclusion semaphores(mutexes are used by tasks to gain exclusive access to

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8 Mutual Exclusion Semaphores • Mutual exclusion semaphores (mutexes) are used by tasks to gain exclusive access to resources – It is a binary semaphores – Use to reduce the priority inversion problem • In order to implement mutexes, a real-time kernel needs to provide the ability to support multiple tasks at the same priority – uC/OS-II doesn’t allow multiple tasks at the same priority – Solution • A priority just above the highest priority task that needs to access the mutex was reserved by the mutex to allow a lower priority task to be raised in priority.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Figure 2.7 Priority Inversion problem Task 1 (H) Task 2 (M) Task 3 (L) Priority Inversion Task 3 Get Semaphore Task 1 Preempts Task 3 Task 1 Tries to get Semaphore Task 2 Preempts Task 3 Task 3 Resumes Task 3 Releases the Semaphore (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) Figure 2.8 Kernel that supports priority inheritance Task 1 (H) Task 2 (M) Task 3 (L) Priority Inversion Task 3 Get Semaphore Task 1 Preempts Task 3 Task 1 Tries to get Semaphore (Priority of Task 3 is raised to Task 1's) Task 3 Releases the Semaphore (Task 1 Resumes) Task 1 Completes (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
Background image of page 2
Listing 8.1 Mutex use example Task #3 accesses the common resource first and thus acquires the mutex Task #3 runs for a while and then gets preempted by task #1 Task #1 needs the resource and thus attempts to acquire the mutex (call OSMutexPend()) OSMutexPend() notices that a higher priority task needs the resource and thus raises the priority of task #3 to 9 OS_EVENT *ResourceMutex; OS_STK TaskPrio10Stk[1000]; OS_STK TaskPrio15Stk[1000]; OS_STK TaskPrio20Stk[1000]; Void main (void) { OSInit(); ------------------------Application Initialization --------------------- OSMutexCreate(9,&err); OSTaskCreate(TaskPrio10, (void *)0, &TaskPrio10Stk[999],10); OSTaskCreate(TaskPrio10, (void *)0, &TaskPrio10Stk[999],15); OSTaskCreate(TaskPrio10, (void *)0, &TaskPrio10Stk[999],20); ------------------------Application Initialization --------------------- OSStart (); } uC/OS-II’s mutexes consist of three elements: – a flag indicating whether the mutex is available – A priority to assign the task that owns the mutex in case a higher priority task attempts to gain access to the mutex, – A list of tasks waiting for the mutex • Six services to access mutexes: – OSMutexCreate(), OSMutexDel(), OSMutexPend(),
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8.00 Creating a Mutex, OsMutexCreate() OS_EVENT *OSMutexCreate (INT8U prio, INT8U *err) { #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr; #endif OS_EVENT *pevent ; if (OSIntNesting > 0) { /* See if called from ISR . .. */ *err = OS_ERR_CREATE_ISR; /* . .. can't CREATE mutex from an ISR */ return ((OS_EVENT *)0); } #if OS_ARG_CHK_EN > 0 if (prio >= OS_LOWEST_PRIO) { /* Validate PIP */ *err = OS_PRIO_INVALID; return ((OS_EVENT *)0); } #endif OS_ENTER_CRITICAL(); if (OSTCBPrioTbl[prio] != (OS_TCB *)0) {
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course SIT 202 taught by Professor Robindoss during the Two '09 term at Deakin.

Page1 / 9

Mutual exculsion - Chapter 8 Mutual Exclusion Semaphores Mutual exclusion semaphores(mutexes are used by tasks to gain exclusive access to

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online