文章目录
重点
1.创建静态线程rt_thread_init——删除静态线程rt_thread_detach
创建动态线程rt_thread_create——删除静态线程rt_thread_delete
2.线程删除会判断线程是否为初始态,是则不删除,如何删除初始态线程?
3.线程不能写成死循环,必须有让出处理器的延时操作
一、线程是什么?
在 RT-Thread 中,任务对应的程序实体就是线程,线程是实现任务的载体,它是 RT-Thread 中最基本的调度单位,它描述了一个任务执行的运行环境,也描述了这个任务所处的优先等级,重要的任务可设置相对较高的优先级,非重要的任务可以设置较低的优先级,不同的任务还可以设置相同的优先级,轮流运行。
二、线程的工作机制
1.线程控制块
这是线程控制快的基本信息
/* 线程控制块 */
struct rt_thread
{
/* rt 对象 */
char name[RT_NAME_MAX]; //线程名称
rt_uint8_t type; // 对象类型
rt_uint8_t flags; // 标志位
rt_list_t list; // 对象列表
rt_list_t tlist; // 线程列表
/* 栈指针与入口指针 */
void *sp; //栈指针
void *entry; //入口函数指针
void *parameter; //参数
void *stack_addr; //栈地址指针
rt_uint32_t stack_size; //栈大小
/* 错误代码 */
rt_err_t error; //线程错误代码
rt_uint8_t stat; // 线程状态
/* 优先级 */
rt_uint8_t current_priority; //当前优先级
rt_uint8_t init_priority; //初始优先级
rt_uint32_t number_mask;
......
rt_ubase_t init_tick; //线程初始化计数值
rt_ubase_t remaining_tick; //线程剩余计数值
struct rt_timer thread_timer; //内置线程定时器
void (*cleanup)(struct rt_thread *tid); //线程退出清除函数
rt_uint32_t user_data; //用户数据
};
2.线程API
1.思维图
2.创建静态线程
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,
rt_uint8_t priority,
rt_uint32_t tick)
{
/* init thread list */
rt_list_init(&(thread->tlist));//线程链表初始化
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);//将内存的内容设置为指定的值#
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(void *)((char *)thread->stack_addr + thread->stack_size - 4),
(void *)rt_thread_exit);//sp指针放在栈顶
/* 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;
/* 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;
thread->sig_ret = RT_NULL;
thread->sig_vectors = RT_NULL;
thread->si_list = RT_NULL;
#endif
#ifdef RT_USING_LWP
thread->lwp = RT_NULL;
#endif
RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
return RT_EOK;
}
3启动线程
rt_err_t rt_thread_startup(rt_thread_t thread)
{
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);//核查是否为线程并且是否符合线程规定
/* set current priority to init priority */
thread->current_priority = thread->init_priority;//系统当前优先级为初始优先级
/* calculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; //优先级右移3位(除以8)(优先级大于32位的操作)
thread->number_mask = 1L << thread->number;//long字型32位左移number位,制作掩码(优先级组数,一组32个优先级)
thread->high_mask = 1L << (thread->current_priority & 0x07); //制作掩码(优先级数)
#else
thread->number_mask = 1L << thread->current_priority;
#endif
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
thread->name, thread->init_priority));
/* change thread stat */
thread->stat = RT_THREAD_SUSPEND;//rt_thread_resume只会恢复状态为susupend的线程
/* then resume it */
rt_thread_resume(thread);//将线程添加到就绪列表
if (rt_thread_self() != RT_NULL)
{
/* do a scheduling */
rt_schedule();
}
return RT_EOK;
}
RTM_EXPORT(rt_thread_startup);
4.静态线程脱离
rt_err_t rt_thread_detach(rt_thread_t thread)
{
rt_base_t lock;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);//检测类型状态
RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)//判断是否为初始态,如果是线程并未启动,不删除
{
/* remove from schedule */
rt_schedule_remove_thread(thread);
}
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));//移除线程定时器
/* change stat */
thread->stat = RT_THREAD_CLOSE;//线程状态为关闭
/* detach object */
rt_object_detach((rt_object_t)thread);//对象移除
if (thread->cleanup != RT_NULL)//线程没清理干净则插入僵尸线程链表
{
/* disable interrupt */
lock = rt_hw_interrupt_disable();
/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
/* enable interrupt */
rt_hw_interrupt_enable(lock);
}
return RT_EOK;
}
RTM_EXPORT(rt_thread_detach);
5.创建动态线程
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)
{
struct rt_thread *thread;
void *stack_start;
thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
name); //创建对象
if (thread == RT_NULL)
return RT_NULL;
stack_start = (void *)RT_KERNEL_MALLOC(stack_size);//分配内存
if (stack_start == RT_NULL)
{
/* allocate stack failure */
rt_object_delete((rt_object_t)thread);//未分配内存删除对象
return RT_NULL;
}
_rt_thread_init(thread,
name,
entry,
parameter,
stack_start,
stack_size,
priority,
tick);
return thread;
}
RTM_EXPORT(rt_thread_create);
6.动态线程删除
rt_err_t rt_thread_delete(rt_thread_t thread)
·1{
rt_base_t lock;
/* thread check */
RT_ASSERT(thread != RT_NULL);//核查线程是否为空,检查类型
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread) == RT_FALSE);
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)//判断是否为初始态,如果是线程并未启动,不删除
{
/* remove from schedule */
rt_schedule_remove_thread(thread);
}
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));//移除线程定时器
/* change stat */
thread->stat = RT_THREAD_CLOSE;//状态设置为关闭
/* disable interrupt */
lock = rt_hw_interrupt_disable();//关中断1
/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));//插入僵尸队列
/* enable interrupt */
rt_hw_interrupt_enable(lock);//开中断1
return RT_EOK;
}
RTM_EXPORT(rt_thread_delete);
#endif
7.使线程让出处理器资源
rt_err_t rt_thread_yield(void)
{
register rt_base_t level;
struct rt_thread *thread;
/* disable interrupt */
level = rt_hw_interrupt_disable();//关中断1
/* set to current thread */
thread = rt_current_thread;//获得当前线程
/* if the thread stat is READY and on ready queue list */
if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
thread->tlist.next != thread->tlist.prev)//判断获得线程是否位于就绪链表第一个
{
/* remove thread from thread list */
rt_list_remove(&(thread->tlist));//移除就绪链表
/* put thread to end of ready queue */
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
&(thread->tlist)); //重新插入就绪链表
/* enable interrupt */
rt_hw_interrupt_enable(level);//开中断1
rt_schedule();
return RT_EOK;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);//开中断1
return RT_EOK;
}
RTM_EXPORT(rt_thread_yield);
8.使线程休眠几个节拍
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
register rt_base_t temp;
struct rt_thread *thread;
/* disable interrupt */
temp = rt_hw_interrupt_disable();//关中断1
/* set to current thread */
thread = rt_current_thread;
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
/* suspend thread */
rt_thread_suspend(thread);//挂起线程
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);//重置延时节拍参数
rt_timer_start(&(thread->thread_timer));//开启线程定时器
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断1
rt_schedule();
/* clear error number of this thread to RT_EOK */
if (thread->error == -RT_ETIMEOUT)//错误号修正
thread->error = RT_EOK;
return RT_EOK;
}
9.线程节拍延时
rt_err_t rt_thread_delay(rt_tick_t tick)
{
return rt_thread_sleep(tick);//效果一样
}
RTM_EXPORT(rt_thread_delay);
10.线程毫秒延时
rt_err_t rt_thread_mdelay(rt_int32_t ms)
{
rt_tick_t tick;
tick = rt_tick_from_millisecond(ms);//换算下ms到节拍
return rt_thread_sleep(tick);
}
RTM_EXPORT(rt_thread_mdelay);
11.修改线程参数
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
{
register rt_base_t temp;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
switch (cmd)//接收指令,并运行
{
case RT_THREAD_CTRL_CHANGE_PRIORITY://改变线程优先级
/* disable interrupt */
temp = rt_hw_interrupt_disable();
/* for ready thread, change queue */
if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY)
{
/* remove thread from schedule queue first */
rt_schedule_remove_thread(thread);
/* change thread priority */
thread->current_priority = *(rt_uint8_t *)arg;
/* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32 //优先级参数是否合法判断
thread->number = thread->current_priority >> 3; /* 5bit */
thread->number_mask = 1 << thread->number;
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
#endif
/* insert thread to schedule queue again */
rt_schedule_insert_thread(thread);
}
else
{
thread->current_priority = *(rt_uint8_t *)arg;
/* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 5bit */
thread->number_mask = 1 << thread->number;
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
#endif
}
/* enable interrupt */
rt_hw_interrupt_enable(temp);
break;
case RT_THREAD_CTRL_STARTUP://开启线程
return rt_thread_startup(thread);
#ifdef RT_USING_HEAP
case RT_THREAD_CTRL_CLOSE://删除线程
return rt_thread_delete(thread);
#endif
default:
break;
}
return RT_EOK;
}
RTM_EXPORT(rt_thread_control);
12.挂起线程
rt_err_t rt_thread_suspend(rt_thread_t thread)
{
register rt_base_t temp;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)//线程不是就绪状态发送当前状态日志
{
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
thread->stat));
return -RT_ERROR;
}
/* disable interrupt */
temp = rt_hw_interrupt_disable();//关中断1
/* change thread stat */
thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);//设置线程标志位
rt_schedule_remove_thread(thread);//调度器移除线程
/* stop thread timer anyway */
rt_timer_stop(&(thread->thread_timer));//关闭线程定时器
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断1
RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread));
return RT_EOK;
}
RTM_EXPORT(rt_thread_suspend);
13.恢复挂起线程
rt_err_t rt_thread_resume(rt_thread_t thread)
{
register rt_base_t temp;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: %s\n", thread->name));
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_SUSPEND)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread resume: thread disorder, %d\n",
thread->stat));
return -RT_ERROR;
}
/* disable interrupt */
temp = rt_hw_interrupt_disable();//开中断1
/* remove from suspend list */
rt_list_remove(&(thread->tlist));//线程挂起队列中移除
rt_timer_stop(&thread->thread_timer);//停止线程定时器
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断1
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);//将线程
RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
return RT_EOK;
}
RTM_EXPORT(rt_thread_resume);
14.线程超时等待资源使用
void rt_thread_timeout(void *parameter)
{
struct rt_thread *thread;
thread = (struct rt_thread *)parameter;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
/* set error number */
thread->error = -RT_ETIMEOUT;//为什么设置为负数???
/* remove from suspend list */
rt_list_remove(&(thread->tlist));//线程移出队列
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);//线程加入就绪链表
/* do schedule */
rt_schedule();//启动调度器
}
RTM_EXPORT(rt_thread_timeout);
15.查找线程
rt_thread_t rt_thread_find(char *name)
{
struct rt_object_information *information;
struct rt_object *object;
struct rt_list_node *node;
/* enter critical */
if (rt_thread_self() != RT_NULL)
rt_enter_critical(); //打开调度锁
/* try to find device object */
information = rt_object_get_information(RT_Object_Class_Thread);//获得线程对象(储存再对象容器中)结构体参数
RT_ASSERT(information != RT_NULL);
for (node = information->object_list.next; //对象链表中遍历
node != &(information->object_list);
node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);//取出对象节点参数
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
{
/* leave critical */
if (rt_thread_self() != RT_NULL)
rt_exit_critical();//关调度锁并返回找到线程
return (rt_thread_t)object;
}
}
/* leave critical */
if (rt_thread_self() != RT_NULL)
rt_exit_critical(); //关调度锁并返回未找到
/* not found */
return RT_NULL;
}
RTM_EXPORT(rt_thread_find);
16.空闲线程
空闲线程是一个线程状态永远为就绪态的线程
3. rtthread例程下载
https://gitee.com/rtthread/docs-online/raw/master/rt-thread-version/rt-thread-standard/tutorial/quick-start/stm32f103-simulator/rtthread_simulator_v0.1.0.7z
例程来源链接https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/tutorial/quick-start/stm32f103-simulator/stm32f103-simulator?id=%e5%85%b6%e4%bb%96%e4%be%8b%e5%ad%90