可以将其初始化为一个特定的值(比如你想要计数的最大值),然后每次计数增加或减少时,就调用 Semaphore 的 release() 或 acquire() 方法。但请注意,这样做可能不是最直观的,因为 acquire() 方法的调用可能会阻塞,直到 Semaphore 的内部计数器大于零。
Semaphore API
|---------------------|--------------------------------|
| API名称 | 说明 |
| osSemaphoreNew | 创建并初始化一个信号量 |
| osSemaphoreGetName | 获取一个信号量的名字 |
| osSemaphoreAcquire | 获取一个信号量的令牌,若获取不到,则会超时返回 |
| osSemaphoreRelease | 释放一个信号量的令牌,但是令牌的数量不超过初始定义的的令牌数 |
| osSemaphoreGetCount | 获取当前的信号量令牌数 |
| osSemaphoreDelete | 删除一个信号量 |
代码编写
修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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.
import("//build/lite/config/component/lite_component.gni")
lite_component("demo") {
features = [
#"base_00_helloworld:base_helloworld_example",
#"base_01_led:base_led_example",
#"base_02_loopkey:base_loopkey_example",
#"base_03_irqkey:base_irqkey_example",
#"base_04_adc:base_adc_example",
#"base_05_pwm:base_pwm_example",
#"base_06_ssd1306:base_ssd1306_example",
#"kernel_01_task:kernel_task_example",
#"kernel_02_timer:kernel_timer_example",
#"kernel_03_event:kernel_event_example",
#"kernel_04_mutex:kernel_mutex_example",
#"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",
#"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",
"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example",
]
}
创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_07_semaphore_for_count文件夹
文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_07_semaphore_for_count\kernel_semaphore_as_mutex_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_07_semaphore_for_count\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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.
static_library("kernel_semaphore_for_count_example") {
sources = [
"kernel_semaphore_for_count_example.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal/cmsis",
]
}
/*
* Copyright (c) 2023 Beijing HuaQing YuanJian Education 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 <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
osThreadId_t Task1_ID; // 任务1 ID
osThreadId_t Task2_ID; // 任务2 ID
osSemaphoreId_t Semaphore_ID; // 信号量ID
#define SEM_MAX_COUNT 10
#define TASK_STACK_SIZE (1024)
#define TASK1_DELAY_TIME 1
#define TASK2_DELAY_TIME 2
/**
* @description: 任务1 模拟停车场的入口
* 每隔一秒钟,停车场就会进入一辆车
* @param {*}
* @return {*}
*/
void Task1(void)
{
while (1) {
if (osSemaphoreGetCount(Semaphore_ID) < SEM_MAX_COUNT) {
if (osSemaphoreRelease(Semaphore_ID) == osOK) {
// 释放信号量 +1
printf("[进入%d辆车, 停车场容量: %d] 信号量+1.\n", osSemaphoreGetCount(Semaphore_ID), SEM_MAX_COUNT);
}
} else {
printf("[进入停车场失败, 请等待...]\n");
}
sleep(TASK1_DELAY_TIME);
}
}
/**
* @description: 任务2
* @param {*}
* @return {*}
*/
void Task2(void)
{
while (1) {
if (osSemaphoreAcquire(Semaphore_ID, osWaitForever) == osOK) {
// 释放信号量 -1
printf("[出去1辆车, 剩余停车场容量: %d] 信号量-1.\n", osSemaphoreGetCount(Semaphore_ID));
} else {
printf("[出停车场失败]\n");
}
sleep(TASK2_DELAY_TIME);
}
}
/**
* @description: 初始化并创建任务
* @param {*}
* @return {*}
*/
static void kernel_sync_semaphore_example(void)
{
printf("Enter kernel_sync_semaphore_example()!\n");
// 创建信号量 一个停车场最多可以停10辆车,
Semaphore_ID = osSemaphoreNew(SEM_MAX_COUNT - 1, 0, NULL); // 参数: 最大计数值,初始计数值,参数配置
if (Semaphore_ID != NULL) {
printf("ID = %d, Create Semaphore_ID is OK!\n", Semaphore_ID);
}
osThreadAttr_t taskOptions;
taskOptions.name = "Task1"; // 任务的名字
taskOptions.attr_bits = 0; // 属性位
taskOptions.cb_mem = NULL; // 堆空间地址
taskOptions.cb_size = 0; // 堆空间大小
taskOptions.stack_mem = NULL; // 栈空间地址
taskOptions.stack_size = TASK_STACK_SIZE; // 栈空间大小 单位:字节
taskOptions.priority = osPriorityNormal; // 任务的优先级
Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &taskOptions); // 创建任务1
if (Task1_ID != NULL) {
printf("ID = %d, Create Task1_ID is OK!\n", Task1_ID);
}
taskOptions.name = "Task2"; // 任务的名字
taskOptions.priority = osPriorityNormal; // 任务的优先级
Task2_ID = osThreadNew((osThreadFunc_t)Task2, NULL, &taskOptions); // 创建任务2
if (Task2_ID != NULL) {
printf("ID = %d, Create Task2_ID is OK!\n", Task2_ID);
}
}
SYS_RUN(kernel_sync_semaphore_example);
使用build,编译成功后,使用upload进行烧录。