您的位置:首页 > 其它

FreeRTOS 的互斥信号量与二进制信号量

2014-06-30 16:02 162 查看
URL: http://bbs.ednchina.com/BLOG_ARTICLE_145889.HTM
FreeRTOS Ver4.5以上支持两种信号量: 互斥型性号量mutex和二进制信号量binary.

二进制信号量相关宏或函数:

vSemaphoreCreateBinary // Macro that creates a Binary semaphore

xSemaphoreTake // Macro to obtain a semaphore

xSemaphoreGive // Macro to release a semaphore

xSemaphoreGiveFromISR // Macro to release a semaphore (used from ISR)

互斥型信号量相关宏或函数:

xSemaphoreCreateRecursiveMutex // Macro that creates a mutex semaphore

xQueueTakeMutexRecursive // obtain a semaphore

xQueueGiveMutexRecursive // release a semaphore

注:二进制信号量与互斥型信号量比较

英文版:

Binary semaphores and mutexes are very similar but have some subtle differences: Mutexes include a priority inheritance mechanism, binary semaphores do not. This makes binary semaphores the better choice
for implementing synchronisation (between tasks or between tasks and an interrupt), and mutexes the better choice for implementing simple mutual exclusion.

A binary semaphore need not be given back once obtained, so task synchronisation can be implemented by one task/interrupt continuously 'giving' the semaphore while another continuously 'takes' the semaphore.

The priority of a task that 'takes' a mutex can potentially be raised if another task of higher priority attempts to obtain the same mutex. The task that owns the
mutex 'inherits' the priority of the task attempting to 'take' the same mutex. This means the mutex must always be 'given' back - otherwise the higher priority task will never be able to obtain the mutex, and the lower priority task will never 'disinherit'
the priority.

======================================

中文版:

互斥型信号量必须是同一个任务申请,同一个任务释放,其他任务释放无效。

二进制信号量,一个任务申请成功后,可以由另一个任务释放。

二进制信号量实现任务互斥: 打印机资源只有一个,abc三个任务共享,当a取得使用权后,为了防止其他任务错误地释放了信号量(),必须将打印机房的门关起来(进入临界段),用完后,释放信号量,再把门打开(出临界段),其他任务再进去打印。(而互斥型信号量由于必须由取得信号量的那个任务释放,故不会出现其他任务错误地释放了信号量的情况出现,故不需要有临界段。互斥型信号量是二进制信号量的子集。)

二进制信号量实现任务同步: a任务一直等待信号量,b任务定时释放信号量,完成同步功能

========================================

Example usage:

xSemaphoreHandle xSemaphore = NULL;

// A task that creates a semaphore.

void vATask( void * pvParameters )

{

/* Create the semaphore to guard a shared resource.

As we are using the semaphore for mutual exclusion

we create a mutex semaphore rather than a binary

semaphore.*/

xSemaphore = xSemaphoreCreateMutex();

}

// A task that uses the semaphore.

void vAnotherTask( void * pvParameters )

{

// ... Do other things.

if( xSemaphore != NULL )

{

/* See if we can obtain the semaphore. If the semaphore

is not available wait 10 ticks to see if it becomes free.*/

if(xSemaphoreTake(xSemaphore,(portTickType)10)

==pdTRUE)

{

/* We were able to obtain the semaphore and can now

access the shared resource....

We have finished accessing the shared resource.

Release the semaphore.*/

xSemaphoreGive( xSemaphore );

}

else

{

/* We could not obtain the semaphore and can therefore

not access the shared resource safely. */

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: