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.

Top of this document<br />

<strong>IPC<strong>@CHIP</strong></strong> <strong>Documentation</strong> Index<br />

Critical Sections in Programs<br />

When multitasking is used, the programmer must beware of critical sections which may occur between threads.<br />

Critical sections can be protected with proper design. The important step at program design time is to identify<br />

these code sections, which are not always obvious. Most programmers are well aware of what critical sections<br />

are. However, due to their importance when multitasking, a simple example is provided here for emphasis.<br />

Example Critical Section<br />

Data sharing between tasks leads to critical sections when the shared data object can not be read or<br />

written atomically, within a single non-interruptible machine instruction.<br />

static unsigned long My_Ticker = 1 ;<br />

void huge Task_A(void)<br />

{<br />

if (My_Ticker < 0x7FFFFFFFL)<br />

{<br />

My_Ticker++ ;<br />

// Borland compiler's machine code:<br />

// ADD My_Ticker,1H ; Increment LS 16 bits<br />

// → Sensitive to task switch here!<br />

// ADC My_Ticker+2,0H ; Carry into MS 16 bits<br />

}<br />

}<br />

void huge Task_B(void)<br />

{<br />

if (My_Ticker == 0)<br />

{<br />

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

}<br />

}<br />

Figure 3 ) Critical Section Example<br />

After a brief review of the C code in the above example, the C programmer might suspect a hardware<br />

problem if the Surprised_to_be_Here() function was to ever execute. However, with a closer examination of<br />

the resulting machine assembly code and multitasking consideration, we will see that execution of the<br />

Surprised_to_be_Here() function is possible 2 .<br />

2 And "if something bad can happen, it will happen".<br />

All tasks in the @Chip-<strong>RTOS</strong> system have a unique task priority. So in the above example either Task_A<br />

Page 226 / 400

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

Saved successfully!

Ooh no, something went wrong!