Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
rt_timer
内核学习
rt_timer_start中的疑问
发布于 2024-04-17 22:30:24 浏览:381
订阅该版
下面的代码来自:RT-Thread-nano v5.0.2 ```c rt_err_t rt_timer_start(rt_timer_t timer) { ... /* Interestingly, this super simple timer insert counter works very very * well on distributing the list height uniformly. By means of "very very * well", I mean it beats the randomness of timer->timeout_tick very easily * (actually, the timeout_tick is not random and easy to be attacked). */ random_nr++; tst_nr = random_nr; rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1], &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1])); for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) { if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK)) rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl], &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl])); else break; /* Shift over the bits we have tested. Works well with 1 bit and 2 * bits. */ tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1; } ... } ``` 对于上面代码中的for循环,是否应该在预编译阶段就进行判断,如下 ```c rt_err_t rt_timer_start(rt_timer_t timer) { ... #if (RT_TIMER_SKIP_LIST_LEVEL > 1) for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) { if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK)) rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl], &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl])); else break; /* Shift over the bits we have tested. Works well with 1 bit and 2 * bits. */ tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1; } #endif ... } ```
查看更多
1
个回答
默认排序
按发布时间排序
用户名由3_15位
2024-04-29
代码总在「分分钟搞定」的狂喜与「必有暗坑」的隐忧间往复
- 修改代码段如下 ```c row_head[0] = &timer_list[0]; for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) { for (; row_head[row_lvl] != timer_list[row_lvl].prev; row_head[row_lvl] = row_head[row_lvl]->next) { struct rt_timer *t; rt_list_t *p = row_head[row_lvl]->next; /* fix up the entry pointer */ t = rt_list_entry(p, struct rt_timer, row[row_lvl]); /* If we have two timers that timeout at the same time, it's * preferred that the timer inserted early get called early. * So insert the new timer to the end the the some-timeout timer * list. */ if ((t->timeout_tick - timer->timeout_tick) == 0) { continue; } else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2) { break; } } #if (RT_TIMER_SKIP_LIST_LEVEL > 1) if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1) row_head[row_lvl + 1] = row_head[row_lvl] + 1; #endif //RT_TIMER_SKIP_LIST_LEVEL > 1 } /* Interestingly, this super simple timer insert counter works very very * well on distributing the list height uniformly. By means of "very very * well", I mean it beats the randomness of timer->timeout_tick very easily * (actually, the timeout_tick is not random and easy to be attacked). */ random_nr++; tst_nr = random_nr; rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1], &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1])); #if (RT_TIMER_SKIP_LIST_LEVEL > 1) for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) { if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK)) rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl], &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl])); else break; /* Shift over the bits we have tested. Works well with 1 bit and 2 * bits. */ tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1; } #endif //RT_TIMER_SKIP_LIST_LEVEL > 1 ``` - KEIL AC6 -O0编译 未执行宏条件判断 ```c Total RO Size (Code + RO Data) 400896 ( 391.50kB) Total RW Size (RW Data + ZI Data) 25080 ( 24.49kB) Total ROM Size (Code + RO Data + RW Data) 401632 ( 392.22kB) ``` - KEIL AC6 -O0编译 执行宏条件判断 ```c Total RO Size (Code + RO Data) 400808 ( 391.41kB) Total RW Size (RW Data + ZI Data) 25080 ( 24.49kB) Total ROM Size (Code + RO Data + RW Data) 401544 ( 392.13kB) ``` - KEIL AC6 -Oz编译 不执行宏条件判断 ```c Total RO Size (Code + RO Data) 233208 ( 227.74kB) Total RW Size (RW Data + ZI Data) 24864 ( 24.28kB) Total ROM Size (Code + RO Data + RW Data) 233816 ( 228.34kB) ``` - KEIL AC6 -Oz编译 执行宏条件判断 ```c Total RO Size (Code + RO Data) 233208 ( 227.74kB) Total RW Size (RW Data + ZI Data) 24864 ( 24.28kB) Total ROM Size (Code + RO Data + RW Data) 233816 ( 228.34kB) ``` - 综上可以分析出,在低优化等级下,使用这个宏去判断是可以减少占用的 https://github.com/RT-Thread/rt-thread/pull/8884
撰写答案
登录
注册新账号
关注者
0
被浏览
381
关于作者
pillar3
这家伙很懒,什么也没写!
提问
1
回答
0
被采纳
0
关注TA
发私信
相关问题
1
rt_timer在低功耗stop模式下的超时问题。
2
rt_timer 导致任务优先级反转问题
3
rt thread入门到内核学习记录
4
关于rt-thread内核`thread.c`: `rt_thread_control`调整线程优先级的疑问
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
6
五分钟玩转RT-Thread新社区
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
睿擎派RK3506开发板开箱记录&amp;初步调试
2
睿擎工业开发平台rk3506常用外设功能评测
3
RT-Thread BSP支持玄铁全系列RISC-V内核啦
4
ubuntu20搭建rtthead5.2.0开发环境
5
BitsButton嵌入式按键处理框架
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
Bootloader
AT
Hardfault
CAN总线
ART-Pi
FinSH
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
FAL
RTC
rt-smart
I2C_IIC
UART
cubemx
ESP8266
BSP
ota在线升级
WIZnet_W5500
PWM
packages_软件包
flash
freemodbus
GD32
潘多拉开发板_Pandora
ADC
keil_MDK
定时器
flashDB
编译报错
ulog
socket
rt_mq_消息队列_msg_queue
msh
中断
Debug
SFUD
C++_cpp
at_device
本月问答贡献
出出啊
1524
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
821
个答案
179
次被采纳
crystal266
555
个答案
162
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
2
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部