RTAI API---task function

本文详细介绍了实时任务管理的一系列API,包括任务创建、删除、周期性设置等关键操作。这些API允许开发者精确控制实时任务的执行状态和调度策略。
rt_task_init 
rt_task_init_cpuid 
rt_task_delete 
rt_task_make_periodic 
rt_task_make_periodic_relative_ns 
rt_task_wait_period 
rt_task_yield 
rt_task_suspend 
rt_task_resume 
rt_busy_sleep 
rt_sleep 
rt_sleep_until 
rt_get_task_state 
rt_whoami 
rt_task_signal_handler 
rt_set_runnable_on_cpus 
rt_set_runnable_on_cpuid 
rt_task_use_fpu 
rt_linux_use_fpu 

rt_preempt_always 

please refer to: https://www.rtai.org/userfiles/documentation/magma/html/api

******************************************************************************************************************************************************************

NAME

rt_task_init, rt_task_init_cpuid - create a new real time task

SYNOPSIS
#include "rtai_sched.h"
int rt_task_init (RT_TASK *task, void (*rt_thread)(int), int data, int stack_size, int priority, int uses_fpu, void(*signal)(void));
int rt_task_init_cpuid (RT_TASK *task, void (*rt_thread)(int), int data, int stack_size, int priority, int uses_fpu, void(*signal)(void), unsigned int cpuid);

DESCRIPTION
rt_task_init and rt_task_init_cpuid create a real time task. 
task is a pointer to an RT_TASK type structure whose space must be provided by the application. It must be kept during the whole lifetime of the real time task and cannot be an automatic variable. 
rt_thread is the entry point of the task function. The parent task can pass a single integer value data to the new task. 
stack_size is the size of the stack to be used by the new task, and priority is the priority to be given the task. The highest priority is 0, while the lowest is RT_LOWEST_PRIORITY. 
uses_fpu is a flag. Nonzero value indicates that the task will use the floating point unit. 
signal is a function that is called, within the task environment and with interrupts disabled, when the task becomes the current running task after a context switch.
The newly created real time task is initially in a suspend state. It is can be made active either with rt_task_make_periodic, rt_task_make_periodic_relative_ns or rt_task_resume.

On multiprocessor systems rt_task_init_cpuid assigns task to a specific CPU cpuid. rt_task_init automatically selects which CPU will the task run on. This assignment may be changed by calling rt_set_runnable_on_cpus or rt_set_runnable_on_cpuid. If cpuid is invalid rt_task_init_cpuid falls back to automatic CPU selection.

RETURN VALUE
On success 0 is returned. On failure a negative value is returned as described below.
ERRORS

-EINVAL
Task structure pointed by task is already in use.
-ENOMEM
stack_size bytes could not be allocated for the stack.
**************************************************************************************************************************************************************
NAME
rt_task_delete - delete a real time task


SYNOPSIS

#include "rtai_sched.h"

int rt_task_delete (RT_TASK *task);

DESCRIPTION
rt_task_delete deletes a real time task previously created by rt_task_init or rt_task_init_cpuid. 
task is the pointer to the task structure. 
If task task was waiting for a semaphore it is removed from the semaphore waiting queue else any other task blocked on message exchange with task is unvlocked.


RETURN VALUE
On success 0 is returned. On failure a negative value is returned as described below.


ERRORS
-EINVAL
task does not refer to a valid task.
*********************************************************************************************************************************************************
NAME
rt_task_make_periodic, rt_task_make_periodic_relative_ns - make a task run periodically

SYNOPSIS
#include "rtai_sched.h"
int rt_task_make_periodic (RT_TASK *task, RTIME start_time, RTIME period);
int rt_task_make_periodic_relative_ns (RT_TASK *task, RTIME start_delay, RTIME period);

DESCRIPTION
rt_task_make_periodic and rt_task_make_periodic_relative_ns mark the task task, previously created with rt_task_init, as suitable for a periodic execution, with period period, when rt_task_wait_period is called. 
The time of first execution is given by start_time or start_delay. start_time is an absolute value measured in clock ticks. start_delay is relative to the current time and measured in nanosecs.


RETURN VALUE
On success 0 is returned. On failure a negative value is returned as described below.
ERRORS
-EINVAL
task does not refer to a valid task.

***************************************************************************************************************************************************************
NAME
rt_task_wait_period - wait till next period
SYNOPSIS
#include "rtai_sched.h"
void rt_task_wait_period (void);


DESCRIPTION
rt_task_wait_period suspends the execution of the currently running real time task until the next period is reached. The task must have been previously marked for execution with rt_task_make_periodic or rt_task_make_periodic_relative_ns. 
Note that the task is suspended only temporarily, i.e. it simply gives up control until the next time period.
**************************************************************************************************************************************************************
NAME
rt_task_yield - yield the current task
SYNOPSIS
#include "rtai_sched.h"
void rt_task_yield (void);

DESCRIPTION
rt_task_yield stops the current task and takes it at the end of the list of ready tasks, with the same priority. The scheduler makes the next ready task of the same priority active.
*************************************************************************************************************************************************************
NAME
rt_task_suspend - suspend a task


SYNOPSIS

#include "rtai_sched.h"

int rt_task_suspend (RT_TASK *task);


DESCRIPTION
rt_task_suspend suspends execution of the task task. It will not be executed until a call to rt_task_resume or rt_task_make_periodic is made.
RETURN VALUE

On success 0 is returned. On failure a negative value is returned as described below.
ERRORS

-EINVAL
task does not refer to a valid task.

*******************************************************************************************************************************************************
NAME
rt_task_resume - resume a task


SYNOPSIS
#include "rtai_sched.h"
int rt_task_resume (RT_TASK *task);

DESCRIPTION
rt_task_resume resumes execution of the task task previously suspended by rt_task_suspend or makes a newly created task ready to run.
RETURN VALUE
On success 0 is returned. On failure a negative value is returned as described below.

ERRORS
-EINVAL
task does not refer to a valid task.
***************************************************************************************************************************************************************
NAME

rt_busy_sleep, rt_sleep, rt_sleep_until - delay/suspend execution for a while
SYNOPSIS
#include "rtai_sched.h"
void rt_busy_sleep (int nanosecs);
void rt_sleep (RTIME delay);
void rt_sleep_until (RTIME time);


DESCRIPTION
rt_busy_sleep delays the execution of the caller task without giving back the control to the scheduler. This function burns away CPU cycles in a busy wait loop It may be used for very short synchronization delays only. 
nanosecs is the number of nanoseconds to wait.
rt_sleep suspends execution of the caller task for a time of delay internal count units. During this time the CPU is used by other tasks.

rt_sleep_until is similar to rt_sleep but the parameter time is the absolute time till the task have to be suspended. If the given time is already passed this call has no effect.

Note: a higher priority task or interrupt handler can run during wait so the actual time spent in these functions may be longer than the specified.

NOTE

A higher priority task or interrupt handler can run during wait so the actual time spent in these functions may be longer than the specified.

************************************************************************************************************************************************************
NAME
rt_get_task_state - query task state
SYNOPSIS

#include "rtai_sched.h"
int rt_get_task_state (RT_TASK *task);

DESCRIPTION
rt_get_task_state returns the state of a real time task. 
task is a pointer to the task structure.
RETURN VALUE

Task state is formed by the bitwise OR of one or more of the following flags:
READY
Task task is ready to run (i.e. unblocked).
SUSPENDED
Task task is suspended.
DELAYED
Task task waits for its next running period or expiration of a timeout.
SEMAPHORE
Task task is blocked on a semaphore.
SEND
Task task sent a message and waits for the receiver task.
RECEIVE
Task task waits for an incoming message.
RPC
Task task sent a Remote Procedure Call and the receiver was not get it yet.
RETURN
Task task waits for reply to a Remote Procedure Call.
Note: the returned task state is just an approximative information. Timer and other hardware interrupts may cause a change in the state of the queried task before the caller could evaluate the returned value. Caller should disable interrupts if it wants reliable info about an other task.

BUGS
rt_get_task_state does not perform any check on pointer task.
***********************************************************************************************************************************************************
NAME
rt_whoami - get the task pointer of the current task

SYNOPSIS
#include "rtai_sched.h"
RT_TASK *rt_whoami (void);

DESCRIPTION
Calling rt_whoami a task can get a pointer to its own task structure.

RETURN VALUE
The pointer to the current task is returned.

************************************************************************************************************************************************************
NAME

rt_task_signal_handler - set the signal handler of a task
SYNOPSIS

#include "rtai_sched.h"
void rt_task_signal_handler (RT_TASK *task, void (*handler)(void));

DESCRIPTION
rt_task_signal_handler installs or changes the signal function of a real time task. 
task is a pointer to the real time task 
handler is the entry point of the signal function. 
Signal handler function can be set also when the task is newly created with rt_task_init. Signal handler is a function called within the task environment and with interrupts disabled, when the task becomes the current running task after a context switch.


RETURN VALUE
On success 0 is returned. On failure a negative value is returned as described below.
ERRORS

-EINVAL
task does not refer to a valid task.
*************************************************************************************************************************************************************
NAME

rt_set_runnable_on_cpus, rt_set_runnable_on_cpuid - assign CPUs to a task
SYNOPSIS

#include "rtai_sched.h"
void rt_set_runnable_on_cpus (RT_TASK *task, unsigned int cpu_mask);
void rt_set_runnable_on_cpuid (RT_TASK *task, unsigned int cpuid);

DESCRIPTION

rt_set_runnable_on_cpus, rt_set_runnable_on_cpuid select one or more CPUs which are allowed to run task task. rt_set_runnable_on_cpuid assigns task to a specific CPU however rt_set_runnable_on_cpus magically selects one CPU from the given set which task task will run on. 
Bit<n> of cpu_mask enables CPU<n>.
If no CPU selected by cpu_mask or cpuid is available, both functions choose a possible CPU automagically.

Note: This call has no effect on uniprocessor systems.

***************************************************************************************************************************************************************

NAME

rt_task_use_fpu, rt_linux_use_fpu - set indication of FPU usage
SYNOPSIS

#include "rtai_sched.h"
int rt_task_use_fpu (RT_TASK* task, int use_fpu_flag);
void rt_linux_use_fpu (int use_fpu_flag);

DESCRIPTION

rt_task_use_fpu informs the scheduler that floating point arithmetic operations will be used by the real time task task.
rt_linux_use_fpu informs the scheduler that floating point arithmetic operations will be used the background task (i.e. the Linux kernel itself and all of its processes!).

If use_fpu_flag has nonzero value, FPU context is also switched when task or the kernel became active. This makes task switching slower.

Initial value of this flag is set by rt_task_init when the real time task is created. By default Linux "task" has this flag cleared. It can be set with LinuxFpu command line parameter of the rtai_sched module.

RETURN VALUE

On success 0 is returned. On failure a negative value is returned as described below.
ERRORS

-EINVAL
task does not refer to a valid task.
************************************************************************************************************************************************************
NAME
rt_preempt_always, rt_preempt_always_cpuid - enable hard preemption

SYNOPSIS
#include "rtai_sched.h"
void rt_preempt_always (int yes_no);
void rt_preempt_always_cpuid (int yes_no, unsigned intcpu_id);

DESCRIPTION
In the oneshot mode a timed task is made active/current at the expiration of the timer shot. The next timer expiration is programmed by choosing among the timed tasks the one with a priority higher than the current after the current has released the CPU, always assuring the Linux timing. While this policy minimizes the programming of the oneshot mode, enhancing efficiency, it can be unsuitable when a task has to be guarded against looping by watch dog task with high priority value, as in such a case the latter as no chance of running.
Calling these functions with nonzero value assures that a timed high priority preempting task is always programmed to be fired while another task is current. The default is no immediate preemption in oneshot mode, firing of the next shot programmed only after the current task releases the CPU.

Initial value of this flag can be set with PreemptAlways command line parameter of the rtai_sched module.

Note: currently the booth functions are equal, parameter cpu_id is ignored.

<think>我们正在解决“无法打开源文件rtapi.h”的问题。根据提供的Makefile内容,我们需要检查以下几个方面: 1. 确保rtapi.h文件存在于正确的目录中。 2. 检查编译命令中是否包含正确的头文件路径(使用-I选项)。 3. 确保在源代码中正确包含头文件。 从Makefile中我们可以看到: - 编译命令为:gcc $(CCFLAGS) -I./ -c $< -o $@ - 其中 -I./ 指定了头文件搜索路径为当前目录(./) 因此,rtapi.h应该位于当前目录(即Makefile所在的目录)或者源代码文件(runtime_api.c和driver_intf.c)所在的目录。 解决步骤: 1. 确认rtapi.h文件是否存在于当前目录(Makefile所在目录)或源代码所在目录。 2. 如果rtapi.h不在当前目录,而是位于其他目录,例如include目录,则需要修改Makefile中的编译选项,添加该目录:-I../include 或 -I./include(根据实际路径)。 3. 如果rtapi.h在项目中不存在,则需要获取该头文件(可能是从其他地方复制或下载)。 另外,引用[2]提到了预编译头文件的问题,但我们的Makefile中没有使用预编译头文件(没有/Yu选项),所以这个问题可能不适用。但如果你在源代码中包含头文件的方式不正确,也可能导致问题。确保在.c文件中包含rtapi.h时,路径正确且位于文件开头(如果使用了预编译头,但这里没有)。 具体操作: 1. 检查rtapi.h是否存在: - 在Makefile所在目录下执行:ls rtapi.h 或者查看文件资源管理器。 2. 如果不存在,尝试在项目目录中搜索:find . -name rtapi.h 3. 如果还找不到,可能需要联系项目提供者或重新获取该文件。 如果找到了rtapi.h,但是不在当前目录,例如在./include目录下,那么修改Makefile中的编译选项,将-I./改为-I./include,或者添加额外的-I选项: 修改为:gcc $(CCFLAGS) -I./ -I./include -c $< -o $@ 另外,在源代码中,应该这样包含: #include "rtapi.h" 而不是 #include <rtapi.h> 因为使用双引号会先在当前目录(或指定的-I目录)中查找,而尖括号通常在系统目录中查找。 如果上述步骤都检查无误,那么问题应该解决。 注意:如果rtapi.h还依赖于其他头文件,也要确保这些头文件都存在。 总结:无法打开源文件rtapi.h的主要原因是编译器在指定的头文件搜索路径中找不到该文件。解决方案是确保文件存在,并通过-I选项指定正确的路径。 根据以上分析,给出具体步骤: 步骤1:确认rtapi.h文件位置 - 在Makefile所在目录下查看是否有rtapi.h。 - 如果没有,在项目目录中搜索。 步骤2:调整头文件路径 - 如果rtapi.h在子目录(如include)中,修改Makefile中的-I选项,添加该子目录。 步骤3:检查源代码中包含语句 - 确保源代码中包含语句为:`#include "rtapi.h"` 步骤4:重新编译 如果问题依旧,请检查是否还有其他依赖。 另外,注意Makefile中的OBJS_DIR指定了目标文件输出目录,但头文件不影响,所以不用考虑。 如果rtapi.h在系统目录中(如/usr/include),那么可能需要安装相应的开发包。但根据Makefile内容,这个项目应该是自包含的,所以rtapi.h应该在项目内。 如果用户已经确认rtapi.h存在,但仍然报错,可以尝试用绝对路径指定头文件目录(不推荐,但可测试): 例如:-I/home/user/project/include 但最好使用相对路径,便于移植。 根据引用[1]的Makefile,我们还可以看到: 在编译目标文件时,使用了:-I./ 所以,确保rtapi.h就在当前目录(Makefile所在目录)或与.c文件同一目录。 如果rtapi.h在objs目录(或其他目录)下,那么需要调整路径。 注意:不要将头文件放在objs目录,因为objs是存放目标文件的。头文件应放在src或include目录。 因此,建议的目录结构是: project/ Makefile include/ (存放rtapi.h等头文件) src/ (存放.c文件) objs/ (存放.o文件) 然后修改Makefile: RTAPI_SRCS = src/runtime_api.c src/driver_intf.c # 或者使用变量指定目录 CCFLAGS中添加:-I./include 这样,在源代码中写 #include "rtapi.h" 就能在include目录下找到。 如果用户的项目结构不同,需要相应调整。 最后,如果用户按照以上步骤操作后仍然无法解决,可能需要提供更多关于项目结构的信息。 </think> ### 无法打开源文件 `rtapi.h` 的解决方案 #### 核心原因分析 编译器找不到 `rtapi.h` 头文件,通常由以下原因导致: 1. **文件缺失或路径错误**:`rtapi.h` 未放在编译器搜索路径中 2. **编译参数缺失**:未通过 `-I` 指定头文件目录 3. **依赖未满足**:需安装相关开发库(如涉及系统库) --- #### 解决方案步骤(基于引用[1]的Makefile) 1. **确认头文件位置** 检查 `rtapi.h` 是否存在于以下位置: - Makefile 所在目录(`./`) - 项目子目录(如 `include/`) ```bash # 在项目根目录执行: find . -name rtapi.h ``` 2. **添加头文件搜索路径** 若 `rtapi.h` 在自定义目录(如 `./include`),修改 Makefile: ```makefile # 修改编译选项(在现有 -I./ 后追加路径) CCFLAGS = -shared -Wall -fPIC -Wl,-soname,libcudart.so.4 -g -I./include ``` > **关键点**:`-I` 路径需指向 `rtapi.h` 所在目录[^1] 3. **检查源码包含语句** 确保源文件(如 `runtime_api.c`)正确包含头文件: ```c // 使用双引号优先搜索本地目录 #include "rtapi.h" ``` 而非 `#include <rtapi.h>`(尖括号优先搜索系统路径) 4. **验证编译流程** 重新编译并检查错误: ```bash make clean # 清理旧编译 make # 重新编译 ``` 5. **处理依赖库(特殊情形)** 若 `rtapi.h` 来自外部库: - 安装开发包(如 `libcudart-dev`) - 添加系统路径:`-I/usr/include/cuda` --- #### 故障排查表 | 现象 | 解决方案 | |------|----------| | `rtapi.h: No such file` | 检查文件路径和 `-I` 参数 | | 头文件存在但报错 | 确认包含语句为 `#include "rtapi.h"` | | 链接阶段失败 | 补充 `-L` 库路径和 `-l` 库名 | > **注意**:预编译头问题(如引用[2])通常不适用于此场景,除非项目启用了 `/Yu` 选项且包含顺序错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值