分享

FreeRTOS 的互斥信号量与二进制信号量(原创)

 毛豆1111 2015-02-11

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条评论

    发表

    请遵守用户 评论公约

    类似文章 更多