rt-thread线程源码分析(六)
* @return RT_EOK
*/
rt_err_t rt_thread_delay(rt_tick_t tick)
{
return rt_thread_sleep(tick);
}
9 线程控制
[cpp]
* This function will control thread behaviors according to control command.
*
* @param thread the specified thread to be controlled
* @param cmd the control command, which includes
* RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
* RT_THREAD_CTRL_STARTUP for starting a thread;
* RT_THREAD_CTRL_CLOSE for delete a thread.
* @param arg the argument of control command
*
* @return RT_EOK
*/
rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg)
{
register rt_base_t temp;
/* thread check */
RT_ASSERT(thread != RT_NULL);
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_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;
}
10 查找线程
[cpp]
/**
* This function will find the specified thread.
*
* @param name the name of thread finding
*
* @return the found thread
*
* @note please don't invoke this function in interrupt status.
*/
rt_thread_t rt_thread_find(char *name)
{
struct rt_object_information *information;
struct rt_object *object;
struct rt_list_node *node;
extern struct rt_object_information rt_object_container[];
/* enter critical */
if (rt_thread_self() != RT_NULL)
rt_enter_critical();//进入临界区
/* try to find device object */
information = &rt_object_container[RT_Object_Class_Thread];//从内核对象容器中获取内核对象链表
for (node = informat