Files
RTOS/Kernel/SingleCore/os_priority.h
T

95 lines
4.5 KiB
C
Raw Normal View History

2026-05-19 11:49:22 +08:00
#ifndef INCLUDED_OS_PRIORITY_H
#define INCLUDED_OS_PRIORITY_H
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
#ifndef INCLUDED_OS_COMPILER_H
#include <os_compiler.h>
#endif /*INCLUDED_OS_COMPILER_H*/
#ifndef INCLUDED_CPU_CLZ_H
#include <cpu_clz.h>
#endif /*INCLUDED_CPU_CLZ_H*/
/* ======================================================================================================================== */
/* 常量 */
#define OS_PRIORITY_TABLE_SIZE ((OS_CFG_PRIORITY_MAX - 1u)/(OS_CFG_CPU_INT_NBITS) + 1u) // 优先级位图表的大小,单位为os_uint_t的数量
#define OS_PRIORITY_CMP_HIGH (1)
#define OS_PRIORITY_CMP_EQUAL (0)
#define OS_PRIORITY_CMP_LOW (-1)
#define OS_PRIORITY_IDLE OS_PRIORITY_LOWEST
#define OS_PRIORITY_LOWEST (OS_CFG_PRIORITY_MAX-1) /*31*/
#define OS_PRIORITY_HIGHEST (1) /*1*/
#define OS_PRIORITY_NORMAL ((OS_PRIORITY_LOWEST-OS_PRIORITY_HIGHEST)>>1) /*15*/
#define OS_PRIORITY_BELOW_NORMAL (((OS_PRIORITY_LOWEST-OS_PRIORITY_NORMAL)>>1)+OS_PRIORITY_NORMAL) /*23*/
#define OS_PRIORITY_ABOVE_NORMAL ((OS_PRIORITY_NORMAL-OS_PRIORITY_HIGHEST)>>1) /*7*/
/* ======================================================================================================================== */
/* 类型 */
typedef os_uint_t os_priority_t;
extern volatile os_priority_t g_os_priority_table[OS_PRIORITY_TABLE_SIZE];
/* ======================================================================================================================== */
/* 接口 */
OS_STATIC_FORCE_INLINE
void os_priority_init(void){
for(os_uint_t i = 0; i < OS_PRIORITY_TABLE_SIZE; i++){
g_os_priority_table[i] = 0u; // 初始化优先级位图表,将所有优先级位都设置为0,表示所有优先级都没有线程占用
}
}
OS_STATIC_FORCE_INLINE
void os_priority_set(os_priority_t priority){
os_size_t index = priority / OS_CFG_CPU_INT_NBITS; // 计算优先级在优先级位图表中的索引位置
os_size_t bit_idx = priority & (OS_CFG_CPU_INT_NBITS - 1u); // 计算优先级在索引位置的位偏移 0 - 31
os_uint_t bit = 1u << (OS_CFG_CPU_INT_NBITS - 1u - bit_idx); // 计算对应位的掩码,优先级0对应最高位,优先级31对应最低位
g_os_priority_table[index] |= bit; // 将对应位设置为1,表示该优先级被占用
}
OS_STATIC_FORCE_INLINE
void os_priority_clear(os_priority_t priority){
os_size_t index = priority / OS_CFG_CPU_INT_NBITS; // 计算优先级在优先级位图表中的索引位置
os_size_t bit_idx = priority & (OS_CFG_CPU_INT_NBITS - 1u); // 计算优先级在索引位置的位偏移 0 - 31
os_uint_t bit = 1u << (OS_CFG_CPU_INT_NBITS - 1u - bit_idx); // 计算对应位的掩码,优先级0对应最高位,优先级31对应最低位
g_os_priority_table[index] &= ~bit; // 将对应位清零,表示该优先级被释放
}
OS_STATIC_FORCE_INLINE
os_priority_t os_priority_get_highest(void){
os_priority_t* p_table = (os_priority_t*)&g_os_priority_table[0]; // 从优先级位图表的第一个元素开始查找
os_priority_t priority = 0;
while(*p_table == 0u){ // 如果当前元素为0,表示该范围内的优先级都没有线程占用
p_table++; // 移动到下一个元素
priority += OS_CFG_CPU_INT_NBITS; // 增加优先级偏移量,跳过当前范围
}
priority += cpu_clz(*p_table); // 使用内置函数计算当前元素中最高位1的索引位置,得到最高优先级的偏移量
return priority; // 返回最高优先级,优先级0对应最高位,没有线程占用时返回OS_CFG_PRIORITY_MAX
}
OS_STATIC_FORCE_INLINE
os_bool_t os_priority_is_high(os_priority_t prio_a, os_priority_t prio_b){
return (prio_a < prio_b)?OS_TRUE:OS_FALSE; // 优先级数值越小表示优先级越高,因此比较两个优先级的数值大小来判断哪个优先级更高
}
OS_STATIC_FORCE_INLINE
int os_priority_cmp(os_priority_t prio_a, os_priority_t prio_b){
return (int)((prio_a == prio_b)?OS_PRIORITY_CMP_EQUAL:((prio_a < prio_b)?OS_PRIORITY_CMP_HIGH:OS_PRIORITY_CMP_LOW));
}
#endif /* INCLUDED_OS_PRIORITY_H */