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.

can potentially interrupt execution of Task_B, or visa-versa, depending on the assigned priorities. Consider<br />

the case where priority of Task_A is lower (higher numerically) than priority of Task_B, such that Task_B<br />

can preempt Task_A. This case can lead to execution of the Surprised_to_be_Here() function under the<br />

following circumstances.<br />

Let us say that Task_A has already executed 0xFFFE times and on its 0xFFFF'th execution it is preempted<br />

by Task_B at the indicated "Sensitive" point immediately after executing the ADD opcode which increments<br />

the lower half of the 32 bit up counter. At this exact point, the My_Ticker value will read zero due to the<br />

carry from the lower 16 bits not yet being applied to the upper half word. And thus Task_B lands in the<br />

Surprised_to_be_Here() function when it encounters the half updated My_Ticker reading.<br />

Protecting Critical Sections<br />

Three methods for protecting critical sections are presented here.<br />

1) Semaphore<br />

2) Interrupt Masking<br />

3) <strong>RTOS</strong> Task Switch Lock<br />

Each method has its advantages and limitations, which are summarized at the end of this discussion. The<br />

choice of which method to use will depend on the design situation.<br />

Semaphore Protection<br />

A common way to protect critical sections is with the use of semaphores. The @Chip-<strong>RTOS</strong> provides<br />

resource semaphores which provide a mutually exclusive access to a resource or data object.<br />

The example defective code from Figure 3 above can be corrected with the use of a resource<br />

semaphore as shown below.<br />

static unsigned long My_Ticker = 1 ;<br />

void huge Task_A(void)<br />

{<br />

RTX_Reserve_Sem(semID, 0) ;<br />

if (My_Ticker < 0x7FFFFFFFL)<br />

{<br />

My_Ticker++ ;<br />

}<br />

RTX_Release_Sem(semID) ;<br />

}<br />

void huge Task_B(void)<br />

{<br />

unsigned long ticker ;<br />

RTX_Reserve_Sem(semID, 0) ;<br />

ticker = My_Ticker ;<br />

RTX_Release_Sem(semID) ;<br />

if (ticker == 0)<br />

Page 227 / 400

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

Saved successfully!

Ooh no, something went wrong!