Пример из IAR
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено Bill 23 октября 2004 г. 11:58
В ответ на: у меня таких нет. Вообщем если несложно, нет ли академической реализации крит секции ? отправлено DASM 23 октября 2004 г. 11:47

MONITOR FUNCTIONS
The __monitor keyword causes interrupts to be disabled during
execution of the function. This allows atomic operations to be performed,
such as operations on semaphores that control access to resources by
multiple processes.
Avoid using the __monitor keyword on large functions since the
interrupt will otherwise be turned off for too long.
A function declared with monitor is equivalent to any other function in
all other respects.
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 to simultaneously use
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 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;
}
The following is an example of a program fragment that uses the
sempahore.
void my_program(void)
{
if (get_lock())
{
/* ... Do something ... */
/* When done, release the lock. */
release_lock();
}
}

Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru