Critical Sections


Detailed Description

Critical Sections can be used to prevent preemption.

Within a critical section, a task will not be interrupted. The following code structure is used to implement critical sections:

Eg.

 unsigned char port_status;  // This is global, may be accessed
                             // through an interrupt and its
                             // access must be protected with
                             // critical sections

 void tst(void) {
  int a;
  char x;
  char txt[20];
  OS_CRITICAL_ENABLE();  // This should be declared immediately after
                        // other variable declarations.

  tst[0] = 3;

  OS_CRITICAL_BEGIN();      // Protect port_status using critical
  if (port_status) {        // sections 
      x = port_status;
      port_status = 0;
  }
  OS_CRITICAL_END();
 }


Defines

#define OS_CRITICAL_ENABLE()   OS_PORT_CRITICAL_ENABLE()
#define OS_CRITICAL_BEGIN()   OS_PORT_CRITICAL_BEGIN()
#define OS_CRITICAL_END()   OS_PORT_CRITICAL_END()
#define OS_INT_ENABLE()   OS_PORT_INT_ENABLE()
#define OS_INT_DISABLE()   OS_PORT_INT_DISABLE()


Define Documentation

 
#define OS_CRITICAL_ENABLE (  )     OS_PORT_CRITICAL_ENABLE()

Enable a critical section within a function.

This must be the first call in the functions after the variable declarations. This has to be called before OS_BEGIN_CRITICAL() or OS_CRITICAL_END() can be called. See example for critical sections.

Definition at line 229 of file tiros.h.

 
#define OS_CRITICAL_BEGIN (  )     OS_PORT_CRITICAL_BEGIN()

Begin a critical section.

This should always be matched with an OS_CRITICAL_END() in the same function. Critical sections should be enabled in the function using OS_CRITICAL_ENABLE()

Definition at line 236 of file tiros.h.

 
#define OS_CRITICAL_END (  )     OS_PORT_CRITICAL_END()

End a critical section.

This should be matched to a corresponding OS_BEGIN_CRITICAL() call.

Definition at line 240 of file tiros.h.

 
#define OS_INT_ENABLE (  )     OS_PORT_INT_ENABLE()

Explicitly enable interrupts.

Ideally, there should be no need to use this call. The OS_CRITICAL_BEGIN() and OS_CRITICAL_END() calls are much safer, since they will store the interrupt state at the beginning of a critical section and restore the state at the end of a critical section. There might be rare occassions where this call may be needed (for example to explicitly re-enable interrupts within an ISR)

Definition at line 250 of file tiros.h.

 
#define OS_INT_DISABLE (  )     OS_PORT_INT_DISABLE()

Explicitly disable interrupts.

Ideally, there should be no need to use this call. The OS_CRITICAL_BEGIN() and OS_CRITICAL_END() calls are much safer, since they will store the interrupt state at the beginning of a critical section and restore the state at the end of a critical section. There might be rare occassions where this call may be needed to explicitly disable interrupts.

Definition at line 260 of file tiros.h.


TiROS User Manual: Last Updated on Fri Jul 20 10:52:24 2007