os_sem.h 源码
2026/7/20大约 2 分钟源码参考内核信号量头文件
os_sem.h — 信号量接口头文件
源文件路径: kernel/include/os_sem.h
引用章节: 4.3.1 信号量
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group 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.
*
* @file os_sem.h
*
* @brief Header file for semaphore interface.
*
* @revision
* Date Author Notes
* 2020-12-28 OneOS Team First Version
***********************************************************************************************************************
*/
#ifndef __OS_SEM_H__
#define __OS_SEM_H__
#include <oneos_config.h>
#include <os_types.h>
#include <os_list.h>
#include <os_spinlock.h>
#include <os_dummy.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef OS_USING_SEMAPHORE
#define OS_SEM_WAKE_TYPE_PRIO 0x55 /* 按优先级唤醒 */
#define OS_SEM_WAKE_TYPE_FIFO 0xAA /* 按FIFO顺序唤醒 */
#define OS_SEM_MAX_VALUE OS_UINT32_MAX /* 信号量最大计数值 */
typedef struct dummy_semaphore os_semaphore_dummy_t;
typedef os_semaphore_dummy_t *os_semaphore_id;
/* 静态定义信号量控制块 */
#define OS_SEMAPHORE_DEFINE(name) os_semaphore_dummy_t name
/* 创建信号量 */
extern os_semaphore_id
os_semaphore_create(os_semaphore_dummy_t *semaphore_cb, const char *name,
uint32_t value, uint32_t max_value);
extern os_err_t os_semaphore_destroy(os_semaphore_id semaphore_id);
/* 等待/释放信号量 */
extern os_err_t os_semaphore_wait(os_semaphore_id semaphore_id, os_tick_t timeout);
extern os_err_t os_semaphore_post(os_semaphore_id semaphore_id);
/* 属性设置/查询 */
extern os_err_t os_semaphore_set_wake_type(os_semaphore_id semaphore_id, uint8_t wake_type);
extern os_err_t os_semaphore_get_value(os_semaphore_id semaphore_id, uint32_t *count);
extern os_err_t os_semaphore_get_max_value(os_semaphore_id semaphore_id, uint32_t *max_count);
extern os_bool_t os_semaphore_is_exist(os_semaphore_id semaphore_id);
extern const char *os_semaphore_get_name(os_semaphore_id semaphore_id);
#endif /* OS_USING_SEMAPHORE */
#ifdef __cplusplus
}
#endif
#endif /* __OS_SEM_H__ */关键说明
| API | 功能 | 说明 |
|---|---|---|
os_semaphore_create | 创建信号量 | value 初始值,max_value 最大值 |
os_semaphore_wait | 等待信号量 | timeout=OS_WAIT_FOREVER 永久等待,OS_NO_WAIT 非阻塞 |
os_semaphore_post | 释放信号量 | 计数+1,唤醒等待任务 |
OS_SEMAPHORE_DEFINE | 静态定义 | 零开销静态分配控制块 |