HI3861学习笔记(7)——LiteOS(CMSIS-RTOS2)信号量

一、简介

1.1 信号量

信号量(Semaphore) 是一种实现任务间通信的机制,实现任务之间同步或临界资源的互斥访问。在多任务操作系统中,不同的任务之间需要同步运行,信号量功能可以为用户提供这方面的支持。

1.2 信号量的使用方式

信号量可以被任务获取或者申请,不同的信号量通过信号量索引号来唯一确定,每个信号量都有一个计数值和任务队列。

通常一个信号量的计数值用于对应有效的资源数,表示剩下的可被占用的互斥资源数,其值的含义分两种情况:

  1. 0: 表示没有积累下来的 Post 操作,且有可能有在此信号量上阻塞的任务;
  2. 正值: 表示有一个或多个 Post 下来的释放操作;

当任务申请(Pend)信号量时,如果申请成功,则信号量的计数值递减,如若申请失败,则挂起在该信号量的等待任务队列上,一旦有任务释放该信号量,则等待任务队列中的任务被唤醒开始执行。

1.3 信号量的使用场景

信号量是一种非常灵活的同步方式,可以运用在多种场合中,实现锁、同步、资源计数等功能,也能方便的用于任务与任务,中断与任务的同步中。

  • 互斥锁
    用作互斥时,信号量创建后记数是满的,在需要使用临界资源时,先申请信号量,使其变空,这样其他任务需要使用临界资源时就会因为无法申请到信号量而阻塞,从而保证了临界资源的安全。

  • 任务间同步
    用作同步时,信号量在创建后被置为空,任务1申请信号量而阻塞,任务2在某种条件发生后,释放信号量,于是任务1得以进入 READY 或 RUNNING 态,从而达到了两个任务间的同步。

  • 资源计数
    用作资源计数时,信号量的作用是一个特殊的计数器,可以递增或者递减,但是值永远不能为负值,典型的应用场景是生产者与消费者的场景。

  • 中断与任务的同步
    用作中断与任务的同步时,可以在中断未触发时将信号量的值置为0,从而堵塞断服务处理任务,一旦中断被触发,则唤醒堵塞的中断服务处理任务进行中断处理。

二、API说明

以下任务管理接口位于 kernel/liteos_m/components/cmsis/2.0/cmsis_os2.h

业务BUILD.gn中包含路径

include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
    ]

2.1 osSemaphoreNew

功能创建信号量,不能在中断服务调用该函数
函数定义osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
参数max_count:信号量计数值的最大值
initial_count:信号量计数值的初始值
attr:信号量属性
返回信号量ID

2.2 osSemaphoreAcquire

功能获取信号量,一直等待,直到由参数semaphore_id指定的信号量对象的标记可用为止。如果参数timeout设置为0,可以从中断服务例程调用
函数定义osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout)
参数semaphore_id:信号量ID
timeout:超时值
返回0 - 成功,非0 - 失败

2.3 osSemaphoreRelease

功能释放信号量,该函数可以在中断服务例程调用
函数定义osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id)
参数semaphore_id:信号量ID
返回0 - 成功,非0 - 失败

2.4 osSemaphoreDelete

功能删除信号量
函数定义osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id)
参数semaphore_id:信号量ID
返回0 - 成功,非0 - 失败

三、通过信号量同时从不同的线程访问共享资源

编译时在业务BUILD.gn中包含路径

include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
    ]

在Semaphore_example函数中,通过osSemaphoreNew()函数创建了sem1信号量,Thread_Semaphore1()函数中通过osSemaphoreAcquire()函数获取两个信号量,Thread_Semaphore2()和Thread_Semaphore3()函数中,先开始阻塞等待sem1信号量。只有当Thread_Semaphore1()函数中增加两次信号量,Thread_Semaphore2()和Thread_Semaphore3()才能继续同步运行。若Thread_Semaphore1()函数中只增加一次信号量,那Thread_Semaphore2()和Thread_Semaphore3()只能轮流执行。

/*
 * @Author: your name
 * @Date: 2021-06-27 14:35:55
 * @LastEditTime: 2021-06-27 14:44:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \applications\BearPi\BearPi-HM_Nano\sample\my_app\hello_world.c
 */
/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

osSemaphoreId_t sem1;

void Thread_Semaphore1(void)
{
    while (1)
    {
        //申请两次sem1信号量,使得Thread_Semaphore2和Thread_Semaphore3能同步执行
        osSemaphoreRelease(sem1);

        //此处若只申请一次信号量,则Thread_Semaphore2和Thread_Semaphore3会交替运行。
        osSemaphoreRelease(sem1);

        printf("Thread_Semaphore1 Release  Semap \n");
        osDelay(100);
    }
}
void Thread_Semaphore2(void)
{
    while (1)
    {
        //等待sem1信号量
        osSemaphoreAcquire(sem1, osWaitForever);

        printf("Thread_Semaphore2 get Semap \n");
        osDelay(1);
    }
}

void Thread_Semaphore3(void)
{
    while (1)
    {
        //等待sem1信号量
        osSemaphoreAcquire(sem1, osWaitForever);

        printf("Thread_Semaphore3 get Semap \n");
        osDelay(1);
    }
}

void Semaphore_example(void)
{
    osThreadAttr_t attr;

    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 4;
    attr.priority = 24;

    attr.name = "Thread_Semaphore1";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore1, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore1!\n");
    }
    attr.name = "Thread_Semaphore2";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore2, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore2!\n");
    }
    attr.name = "Thread_Semaphore3";
    if (osThreadNew((osThreadFunc_t)Thread_Semaphore3, NULL, &attr) == NULL)
    {
        printf("Falied to create Thread_Semaphore3!\n");
    }
    sem1 = osSemaphoreNew(4, 0, NULL);
    if (sem1 == NULL)
    {
        printf("Falied to create Semaphore1!\n");
    }
}
APP_FEATURE_INIT(Semaphore_example);

查看打印:


• 由 Leung 写于 2021 年 6 月 27 日

• 参考:【鸿蒙2.0设备开发教程】小熊派HarmonyOS 鸿蒙·季 开发教程
    小熊派华为物联网操作系统 LiteOS内核教程04-信号量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leung_ManWah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值