当前位置: 首页 > news >正文

RT_Thread内核机制学习(二)

对于RTT来说,每个线程创建时都自带一个定时器
在这里插入图片描述

rt_err_t rt_thread_sleep(rt_tick_t tick)
{register rt_base_t temp;struct rt_thread *thread;/* set to current thread */thread = rt_thread_self();RT_ASSERT(thread != RT_NULL);RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);/* disable interrupt */temp = rt_hw_interrupt_disable();/* suspend thread */rt_thread_suspend(thread); //从就绪链表移除/* reset the timeout of thread timer and start it */rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick); //设置定时器定时时间rt_timer_start(&(thread->thread_timer)); //开启定时器/* enable interrupt */rt_hw_interrupt_enable(temp);rt_schedule();/* clear error number of this thread to RT_EOK */if (thread->error == -RT_ETIMEOUT)thread->error = RT_EOK;return RT_EOK;
}

延时过程:

  1. 将线程从就绪链表移除。
  2. 设置/启动定时器
  3. 每个Tick判断,定时器是否超时,调用超时函数。
/*** This function is the timeout function for thread, normally which is invoked* when thread is timeout to wait some resource.** @param parameter the parameter of thread timeout function*/
void rt_thread_timeout(void *parameter)
{struct rt_thread *thread;thread = (struct rt_thread *)parameter;/* thread check */RT_ASSERT(thread != RT_NULL);RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);/* set error number */thread->error = -RT_ETIMEOUT; //表示被唤醒原因是:超时/* remove from suspend list */rt_list_remove(&(thread->tlist)); //可能因为等待队列等原因而挂起,先移除/* insert to schedule ready list */rt_schedule_insert_thread(thread);/* do schedule */rt_schedule();
}
RTM_EXPORT(rt_thread_timeout);

超时函数:

  1. 将线程放入就绪链表尾部
  2. 发起调度
void rt_schedule_remove_thread(struct rt_thread *thread)
{register rt_base_t level;RT_ASSERT(thread != RT_NULL);/* disable interrupt */level = rt_hw_interrupt_disable();RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%.*s], the priority: %d\n",RT_NAME_MAX, thread->name,thread->current_priority));/* remove thread from ready list */rt_list_remove(&(thread->tlist));if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority]))){
#if RT_THREAD_PRIORITY_MAX > 32rt_thread_ready_table[thread->number] &= ~thread->high_mask;if (rt_thread_ready_table[thread->number] == 0){rt_thread_ready_priority_group &= ~thread->number_mask;}
#elsert_thread_ready_priority_group &= ~thread->number_mask;
#endif}/* enable interrupt */rt_hw_interrupt_enable(level);
}

将线程从就绪链表中移除,首先从链表中删除,再判断优先级对应的链表是否空了?

在这里插入图片描述
可以快速查找到哪一位是1。

每次Tick中断还需要检查定时器

/*** This function will check timer list, if a timeout event happens, the* corresponding timeout function will be invoked.** @note this function shall be invoked in operating system timer interrupt.*/
void rt_timer_check(void)
{struct rt_timer *t;rt_tick_t current_tick;register rt_base_t level;RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));current_tick = rt_tick_get();/* disable interrupt */level = rt_hw_interrupt_disable();while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])){t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]); //从链表中取出定时器/** It supposes that the new tick shall less than the half duration of* tick max.*/if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2) //判断时间是否到了{RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));/* remove timer from timer list firstly */_rt_timer_remove(t);/* call timeout function */t->timeout_func(t->parameter);/* re-get tick */current_tick = rt_tick_get();RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&(t->parent.flag & RT_TIMER_FLAG_ACTIVATED)){/* start it */t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;rt_timer_start(t);}else{/* stop timer */t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;}}elsebreak;}/* enable interrupt */rt_hw_interrupt_enable(level);RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
}

依次判断定时器时间是否到了,到了的话就调用定时器超时函数。
在这里插入图片描述
定时器启动后放到链表中。

Tick中断检查定时器时,也是从链表中取出判断。

http://www.lryc.cn/news/144180.html

相关文章:

  • 线性代数的学习和整理12: 矩阵与行列式,计算上的差别对比
  • 2023年MySQL核心技术面试第一篇
  • linux启动jar 缺失lib
  • 【Bash】常用命令总结
  • 小研究 - Java虚拟机性能及关键技术分析
  • Repo manifests默认default.xml清单文件中的各个标签详解
  • javacv基础02-调用本机摄像头并预览摄像头图像画面视频
  • 【Nginx21】Nginx学习:FastCGI模块(三)缓冲区与响应头
  • 正则表达式(常用字符简单版)
  • 从零开始学习Python爬虫:详细指南
  • 分布式计算框架:Spark、Dask、Ray
  • 什么是伪类链(Pseudo-class Chaining)?
  • 每日一题:leetcode 57 插入区间
  • 第五节:实现自己的第一个environment
  • 无套路,财务数据分析-多组织损益表分析分享
  • Java并发编程第6讲——线程池(万字详解)
  • AI + Milvus:将时尚应用搭建进行到底
  • 归并排序(Java 实例代码)
  • 【VUE】数字动态变化到目标值-vue-count-to
  • Mysql /etc/my.cnf参数详解(二)
  • AUTOSAR规范与ECU软件开发(实践篇)6.10AUTOSAR操作系统概念与配置方法介绍(下)
  • 蓝牙 - 经典蓝牙物理信道介绍
  • 性能测试中未做集群时,在登入中已经保存了登入的session,但可能会出现在不同的服务器上显示登入失败
  • Python环境下载安装使用
  • 图像扭曲之波浪扭曲
  • 《自动驾驶与机器人中的SLAM技术》之GNSS相关基础知识总结
  • 【前端|CSS系列第4篇】面试官:你了解居中布局吗?
  • 安全物理环境技术测评要求项
  • SAP MTS案例教程PP生产前台操作
  • Celery task 执行报错 TypeError: Object of type set is not JSON serializable 问题分析处理