Atmel CAVR-4 사용자 설명서

다운로드
페이지 323
CAVR-4
30
Special function types
AVR® IAR C/C++ Compiler
Reference Guide
MONITOR FUNCTIONS
A monitor function causes interrupts to be disabled during execution of the function. At 
function entry, the status register is saved and interrupts are disabled. At function exit, 
the original status register is restored, and thereby the interrupt status that existed before 
the function call is also restored.
To define a monitor function, you can use the 
_ _monitor
 keyword. For reference 
Example of implementing a semaphore in C
In the following example, a semaphore is implemented using one static variable and two 
monitor functions. A semaphore can be locked by one process, and is used for 
preventing processes from simultaneously using resources that can only be used by one 
process at a time, for example a printer.
/* When the_lock is non-zero, someone owns the lock. */
static volatile unsigned int the_lock = 0;
/* get_lock -- Try to lock the lock.
 * Return 1 on success and 0 on failure. */
_ _monitor int get_lock(void)
{
  if (the_lock == 0)
  {
    /* Success, we managed to lock the lock. */
    the_lock = 1;
    return 1;
  }
  else
  {
    /* Failure, someone else has locked the lock. */
    return 0;
  }
}
/* release_lock -- Unlock the lock. */
_ _monitor void release_lock(void)
{
  the_lock = 0;
}