创建线程的内部过程:
创建线程的过程,就是构造栈的过程。
线程创建时输入的各种参数
rt_thread_t rt_thread_create(
const char* name, //线程名字
void (*entry)(void* parameter), //入口函数
void* parameter, //入口函数参数
rt_uint32_t stack_size, //栈大小
rt_uint8_t priority, //线程优先级
rt_uint32_t tick //线程时间片大小
);
rt_thread 用 thread 来表示一个线程,
thread的重要成员如下:
thread -> entry : 函数指针
thread -> paramater : 函数参数
thread -> stack_addr :栈的起始地址
thread -> stack_size :栈的大小
thread -> sp :栈顶
thread -> init_priority :初始化优先级
thread -> current_priority :当前优先级
thread -> init_tick :一次能运行多少个tick
thread -> remaining_tick :当此运行还剩多少个tick
具体分析rt_thread_create函数
它在运行过程中做了以下几件事情
1、分配线程结构体:
这一步创建了返回的线程句柄,也就是上面提到的 thread 结构体
thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread, name);
2、分配栈:
这一步确定了栈的大小,也就是 thread -> stack_size
stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
3、初始化栈:
thread -> sp 确定栈顶位置,
thread->sp = (void *)rt_hw_stack_init