01.06.2013 Views

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

IPC@CHIP Documentation - SC12 @CHIP-RTOS V1.10

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

{<br />

}<br />

Surprised_to_be_Here() ; // How did this happen?<br />

Figure 4 ) Protected Critical Section with Semaphore<br />

Now the Surprised_to_be_Here() function will never be executed.<br />

A potential disadvantage to using semaphores is a possible task priority inversion, where a high<br />

priority task is blocked by a lower priority task as it awaits the semaphore. To illustrate this point,<br />

consider an example where task priorities are designed as follows:<br />

Task_A - Priority 60 (low priority)<br />

Task_B - Priority 4 (very high priority)<br />

If Task_A is suspended while it has possession of the semaphore, Task_B will have to wait if it then<br />

tries to access the same semaphore at that moment. This wait is effectively at the very low priority 60,<br />

which would mean that Task_B (priority 4) must sit waiting behind time consuming system activities<br />

such as FTP transfer (priority 41). In applications where this potential priority inversion is not<br />

acceptable, either the interrupt masking or task lock methods of protecting critical sections discussed<br />

below can be considered as an alternative to using semaphores.<br />

Interrupt Masking<br />

Interrupt masking can in some cases be a safe alternative to using semaphores to protect critical<br />

sections. This fast method places a minimum load on the system, so is most suitable where<br />

performance is a concern. The interrupt masking method is used in the example below.<br />

define MASK_INTERRUPTS asm{CLI}<br />

#define ENABLE_INTERRUPTS asm{STI}<br />

static unsigned long My_Ticker = 1 ;<br />

void huge Task_A(void)<br />

{<br />

MASK_INTERRUPTS ; // Needed if Task_A is lower priority<br />

if (My_Ticker < 0x7FFFFFFFL)<br />

{<br />

My_Ticker++ ;<br />

}<br />

ENABLE_INTERRUPTS ;<br />

}<br />

void huge Task_B(void)<br />

{<br />

unsigned long ticker ;<br />

MASK_INTERRUPTS ; // Needed if Task_B is lower priority<br />

ticker = My_Ticker ;<br />

ENABLE_INTERRUPTS ;<br />

if (ticker == 0)<br />

{<br />

Surprised_to_be_Here() ; // How did this happen?<br />

Page 228 / 400

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!