【STM32 FreeRTOS】信号量与互斥锁

二值信号量

二值信号量的本质是一个队列长度为1的队列,该队列就只有空和满两种情况,这就是二值。

二值信号量通常用于互斥访问或任务同步,与互斥信号量比较类似,但是二值信号量有可能会导致优先级翻转的问题,所以二值信号量更适合用于同步。

c 复制代码
SemaphoreHandle_t xSemaphoreCreateBinary( void );

xSemaphoreTake( SemaphoreHandle_t xSemaphore,
                 TickType_t xTicksToWait );

xSemaphoreTakeFromISR
 (
 SemaphoreHandle_t xSemaphore,
 signed BaseType_t *pxHigherPriorityTaskWoken
 )

xSemaphoreGive( SemaphoreHandle_t xSemaphore );

xSemaphoreGiveFromISR
 (
 SemaphoreHandle_t xSemaphore,
 signed BaseType_t *pxHigherPriorityTaskWoken
 )

 void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );

重点在许多使用场景中,使用直达任务通知要比使用二值信号量的速度更快,内存效率更高。所以,没有实例代码。

计数型信号量

计数型信号量相当于队列长度大于1的队列,因此计数型信号量能够容纳多个资源,这在计数型信号量被创建的时候确定的。

c 复制代码
SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount,
                                            UBaseType_t uxInitialCount);

UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );

其他接口与二值信号量的接口一致。

重点在许多情况下,任务通知可以提供计数信号量的轻量级替代方案

互斥信号量(互斥锁)

互斥信号量其实就是一个拥有优先级继承的二值信号量,在同步的应用中二值信号量最合适。互斥信号量适合那些需要互斥访问的应用中。

注意:互斥信号量不能用于中断服务函数中,原因如下:

  • 互斥信号量有任务优先级继承的机制,但是中断不是任务,没有任务优先级,所以互斥信号量只能用于任务中。
  • 中断服务函数中不能因为要等待互斥信号量而设置阻塞时间进入阻塞态。
c 复制代码
SemaphoreHandle_t xSemaphoreCreateMutex( void )
    
xSemaphoreGive( SemaphoreHandle_t xSemaphore );

xSemaphoreTake( SemaphoreHandle_t xSemaphore,
                 TickType_t xTicksToWait );
c 复制代码
SemaphoreHandle_t 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, ( TickType_t ) 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. */
        }
    }
}

递归互斥信号量(递归互斥锁)

xSemaphoreCreateMutex()用于创建非递归互斥锁。非递归互斥锁只能被一个任务 获取一次,如果同一个任务想再次获取则会失败, 因为当任务第一次释放互斥锁时,互斥锁就一直 处于释放状态。

与非递归互斥锁相反,递归互斥锁可以被同一个任务获取很多次, 获取多少次就需要释放多少次, 此时才会返回递归互斥锁。

c 复制代码
SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )

xSemaphoreTakeRecursive( SemaphoreHandle_t xMutex,
                         TickType_t xTicksToWait );

xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )
相关推荐
草莓熊Lotso1 小时前
【Linux网络】UDP Socket 编程全解析:从回显服务到通用字典服务,从零实现工业级代码
linux·运维·服务器·数据库·c++·单片机·udp
fengfuyao98513 小时前
利用 STM32 和 ADS1256 进行高精度数据采集
stm32·单片机·嵌入式硬件
黑白园13 小时前
ADC读取XY二轴操纵杆数据通过I2C_GPIO模拟 控制0.96寸OLED显示
stm32·单片机·嵌入式硬件
一个平凡而乐于分享的小比特14 小时前
还在手动挡写单片机?MicroPython 一脚油门踩进 Python 硬件世界
单片机·嵌入式硬件·micropython
FreakStudio15 小时前
WIZnet-EVB-Pico2开始,用MicroPython玩转以太网开发
python·单片机·嵌入式·大学生·面向对象·技术栈·并行计算·电子diy·电子计算机
LCG元15 小时前
STM32实战:基于STM32F103的工业仪表数据采集(多路ADC)
stm32·单片机·嵌入式硬件
BT-BOX16 小时前
Stm32CubeMX+Proteus仿真--STM32外部中断
stm32·单片机·proteus
森利威尔电子-17 小时前
森利威尔SL8700 降压型大功率 LED 恒流驱动器:5A/95%效率,支持 PWM/模拟调光
单片机·嵌入式硬件·集成电路·芯片·电源芯片
三佛科技-1873661339717 小时前
GP8892SEH贴片SOP7省外围5V2A隔离型原边反馈芯片直接替代MT3723
单片机·嵌入式硬件
Quinn2717 小时前
正点原子 STM32MP257 修复异核 FreeRTOS 例程 osDelay() 函数比 HAL_Delay() 延时快的问题
stm32·单片机·嵌入式硬件