|
任务创建中,首先要初始化,那做了哪些工作? 主要是这个函数_thread_init 具体实现分析下
记录遇到一个着实看不懂的代码(见第7)
1. 初始化链表
rt_list_init(&(thread->tlist));
这个链表有意思,用这个链表把一个个任务放在一个链表里面
struct rt_list_node
{
struct rt_list_node *next; /**< point to next node. */
struct rt_list_node *prev; /**< point to prev node. */
};
2. 把任务的入口函数,参数,地址和大小赋值给任务控制块
thread->entry = (void *)entry;
thread->parameter = parameter;
/* stack init */
thread->stack_addr = stack_start;
thread->stack_size = stack_size;
/* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size);
3. 任务栈指针的处理,待研究
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
(void *)_thread_exit);
4. 任务优先级
thread->init_priority = priority;
thread->current_priority = priority;
thread->number_mask = 0;
5. 任务的tick,这个什么用途?tick is the time slice if there are same priority thread,如果有相同优先级
thread->init_tick = tick;
thread->remaining_tick = tick;
6. 初始化定时器,这个定时器有啥用途呢?
rt_timer_init(&(thread->thread_timer),
thread->name,
_thread_timeout,
thread,
0,
RT_TIMER_FLAG_ONE_SHOT);
7. RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); 是什么意思?初始化钩子函数
根据以下代码
static void (*rt_thread_inited_hook) (rt_thread_t thread);
#define RT_OBJECT_HOOK_CALL(func, argv) __on_##func argv
组合成 __on_rt_thread_inited_hook(thread),初始化钩子函数
果然,之后找到原文
#ifndef __on_rt_thread_inited_hook
#define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
#endif
往下继续找宏定义
define __ON_HOOK_ARGS(__hook, argv) do {if ((__hook) != RT_NULL) __hook argv; } while (0)
怎么感觉绕了一大圈,绕不懂了,搞这么复杂干啥,我就很不懂啊,按照我的理解,直接写成下面的不好吗
#define RT_OBJECT_HOOK_CALL(func, argv) do {if ((func) != RT_NULL) func argv; } while (0)