Xenomai 的模式切换浅析

本文详细介绍了Xenomai系统中实时程序如何在主模式与次模式之间切换,以及这两种模式如何实现硬实时调度延迟和普通Linux线程间的平衡。文章还深入探讨了影子线程的作用及其调度过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Xenomai的用户空间下,有两种模式:primary mode (主模式) 和 secondary mode(次模式).

在主模式下调用Linux系统调用后程序就会进入次模式,反之,在次模式下调用Xenomai的系统调用后程序会进入主模式。

主模式和次模式的引入主要是丰富了实时程序可调用的库,实时程序也能够调用Linux的库。可是实时性受Linux自身的影响。

參考:http://www.xenomai.org/index.php/Porting_POSIX_applications_to_Xenomai

To ease working with this dual-kernel system, a Xenomai application thread may run in two modes: either the primary mode, where it is scheduled by the Xenomai kernel, and benefits from hard real-time scheduling latencies, or the secondary mode, where it is an ordinary Linux thread, and as such may call any Linux services.

Such a thread may change mode dynamically, that is, when this thread calls a Xenomai real-time service while running in secondary mode, it switches to primary mode, when it calls any non real-time Xenomai service or any Linux service (including exceptions such as page faults) while running in primary mode, it switches to secondary mode.


内部的处理流程例如以下:

1. 初始化

在引用各个skin的创建任务的接口时。系统会作例如以下处理:映射一个和linux thread匹配的xenomai thread, 这个xenomai thread被称为影子线程 (shadow thread)。由于Linux下的调度器事实上是无法知道Xenomai下的任务的,所以这个影子线程就是给Xenomai进行调度使用的。


2. 系统调用处理时

这时会依据线程所在Domain和系统调用的Domain进行推断,假设有发生模式切换。主要是调用例如以下两个函数处理:

int  xnshadow_harden (void)
  Migrate a Linux task to the Xenomai domain. 

void  xnshadow_relax (int notify)
  Switch a shadow thread back to the Linux domain. 

前者是迁移到Xenomai域。后者是迁移到Linux域 。

曾经者为例:

參考xnshadow_harden的代码,这时会唤醒一个守护线程gatekeeper, 守护线程会将相应的影子线程放在xenomai的可运行队列时,并调用xenomai自身的调度器xnpod_schedule(),这里会恢复linux thread下的寄存器并运行影子线程。


而后者的情况下,有一点要注意:xenomai是通过virq的方式通知linux,

 lostage_apc =
           rthal_apc_alloc("lostage_handler", &lostage_handler, NULL);

这里注冊了虚拟中断的处理函数: lostage_hander,其它的跟xnshadow_harden类似。

參考:

http://www.cs.ru.nl/lab/xenomai/api/group__shadow.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值