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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

}<br />

Figure 5 ) Protected Critical Section with Interrupt Masking<br />

This method of protection is safe to use when the section being protected executes in very few<br />

machine cycles, as is the case in this example. The concern is the hardware interrupt latency created<br />

by this interrupt mask period. Masking interrupts for as long as 50 microseconds should be tolerable<br />

on most systems. Caution must be used to assure that interrupts are always quickly re-enabled when<br />

ever they are disabled!<br />

Note that when the nature of the two tasks competing for access to the resource (Task_A and<br />

Task_B in this example) dictates that one is higher priority than the other, only the lower priority task<br />

requires the interrupt masking. It is not possible that the lower priority task could preempt the higher<br />

priority task (unless the program design was to change task priorities dynamically somewhere).<br />

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

A further alternative to using semaphores to protect critical sections is to prevent task switching within<br />

the critical section. This method is shown in the example below.<br />

static unsigned long My_Ticker = 1 ;<br />

void huge Task_A(void)<br />

{<br />

// Disable needed if Task_A is lower priority<br />

RTX_Disable_Task_Scheduling() ; // Task switch lock<br />

if (My_Ticker < 0x7FFFFFFFL)<br />

{<br />

My_Ticker++ ;<br />

}<br />

RTX_Enable_Task_Scheduling() ; // Resume task switching<br />

}<br />

void huge Task_B(void)<br />

{<br />

unsigned long ticker ;<br />

// Disable needed if Task_B is lower priority<br />

RTX_Disable_Task_Scheduling() ; // Task switch lock<br />

ticker = My_Ticker ;<br />

RTX_Enable_Task_Scheduling() ; // Resume task switching<br />

if (ticker == 0)<br />

{<br />

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

}<br />

}<br />

Figure 6 ) Protected Critical Section with Task Lock<br />

Hardware interrupts continue to be serviced during the task lock, so you can include more work now<br />

within the critical section than was possible with the interrupt masking method. However, the task lock<br />

Page 229 / 400

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

Saved successfully!

Ooh no, something went wrong!