赞
踩
准备:
1 去官网下载 RT-Thread 源码
2 准备 source insight 4.0 / 随意文本阅读器
3 下载官方的 API 手册并打开
4 打开 rt-thread-master/src/thread.c 和 rt-thread-master/rtthread.h 两个文件
1 根据源码解读线程的初始化
在 rt_thread_init() 和 rt_thread_create() 函数中均最后引用了 rt_thread_init()
所以咱先对这个函数进行深入解读
//参数介绍可以看文档 static rt_err_t _rt_thread_init(struct rt_thread *thread, //这个线程的相关描述 const char *name, //线程名称 void (*entry)(void *parameter), //线程入口函数 void *parameter, //入口参数 void *stack_start, //保存线程上下文信息的栈地址 rt_uint32_t stack_size, // 线程栈的长度(初期推荐 1K=1024 byte/ 2K=2048 byte) rt_uint8_t priority, // 线程优先级(理论255 最低/0最高)具体得看 rtconfig.h 定义 RT_THREAD_PRIORITY_MAX rt_uint32_t tick) // 这个参数指定线程一次调度能够运行的最大时间长度 { /* init thread list */ rt_list_init(&(thread->tlist)); //线程列表所在前后线程均指向自己 {list->next = list->prev = list} 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); //栈初始化(后续说明 rt_memset 下一篇) #ifdef ARCH_CPU_STACK_GROWS_UPWARD //栈地址由低向高增长 thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (后续说明rt_hw_stack_init 下一篇) (void *)((char *)thread->stack_addr), (void *)rt_thread_exit); #else //RT-Thread 3.1.0 以前的版本,均只支持栈由高地址向低地址增长的方式 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 *)rt_thread_exit); #endif /* priority init */ RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);//判断优先级符合要求 thread->init_priority = priority; thread->current_priority = priority; //动态优先级(是可变的) thread->number_mask = 0; #if RT_THREAD_PRIORITY_MAX > 32 thread->number = 0; thread->high_mask = 0; #endif /* tick init */ thread->init_tick = tick; thread->remaining_tick = tick; /* error and flags */ thread->error = RT_EOK; thread->stat = RT_THREAD_INIT; #ifdef RT_USING_SMP /* not bind on any cpu */ thread->bind_cpu = RT_CPUS_NR; thread->oncpu = RT_CPU_DETACHED; /* lock init */ thread->scheduler_lock_nest = 0; thread->cpus_lock_nest = 0; #endif /*RT_USING_SMP*/ /* initialize cleanup function and user data */ thread->cleanup = 0; thread->user_data = 0; /* init thread timer */ rt_timer_init(&(thread->thread_timer), //下一篇讲解 thread->name, rt_thread_timeout, thread, 0, RT_TIMER_FLAG_ONE_SHOT); /* initialize signal */ #ifdef RT_USING_SIGNALS thread->sig_mask = 0x00; thread->sig_pending = 0x00; #ifndef RT_USING_SMP //对称多核处理器(用于区分单核和多核芯片) thread->sig_ret = RT_NULL; #endif thread->sig_vectors = RT_NULL; thread->si_list = RT_NULL; #endif #ifdef RT_USING_LWP //轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元 thread->lwp = RT_NULL; #endif RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); [^1] //对象钩子函数,在创建,删除,获取和放置等对象时调用 //就是通知内核空了调用一下这个线程 return RT_EOK; }
1 RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); 2 #define RT_OBJECT_HOOK_CALL(func, argv) \ do {if ((func) != RT_NULL) func argv;} while (0) 3 static void (*rt_thread_inited_hook) (rt_thread_t thread) 合并 do { if ((rt_thread_inited_hook) != RT_NULL) { rt_thread_inited_hook(thread); //宏定义中,程序是照搬的 } while (0) } 可以自己测试以下程序就够明白了 #include <stdio.h> #define TEST_FUN(func, argv) \ do {if ((func) != NULL) func argv;} while (0) void test_A (int t) { printf("%d", t); } int main() { int B = 10; TEST_FUN(test_A, B); // 或者 TEST_FUN(test_A, (B)); 这两种都试试看,你就理解宏定义中变量替代的方式了重点在于 (B) return 0; // 输出 10 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。