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

15 篇文章 1 订阅
10 篇文章 0 订阅

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. */
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值