rt-thread线程源码分析(三)

2014-11-24 07:57:45 · 作者: · 浏览: 6
ck_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_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;
}
3 线程的脱离及删除
3.1 脱离线程
[cpp]
/**
* This function will detach a thread. The thread object will be removed from
* thread queue and detached/deleted from system object management.
*
* @param thread the thread to be deleted
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_detach(rt_thread_t thread)
{
rt_base_t lock;
/* thread check */
RT_ASSERT(thread != RT_NULL);
/* remove from schedule */
rt_schedule_remove_thread(thread);//将线程从调度器中移除
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));//脱离定时器
/* change stat */
thread->stat = RT_THREAD_CLOSE;//将线程的状态设置为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_thread_defunct链表在系统空闲时将被空闲线程来处理
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));//将线程加入到rt_thread_defunct链表中
/* enable interrupt */
rt_hw_interrupt_enable(lock);//开中断
}
return RT_EOK;
}
3.2 删除线程
[cpp]
/**
* This function will delete a thread. The thread object will be removed from
* thread queue and detached/deleted from system object management.
*
* @param thread the thread to be deleted
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_thread_delete(rt_thread_t thread)
{
rt_base_t lock;
/* thread check */
RT_ASSERT(thread != RT_NULL);
/* remove from schedule */
rt_schedule_remove_thread(thread);//从调度器中移除线程
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));//脱离定时器
/* change stat */
thread->stat = RT_THREAD_CLOSE;//线程状态设置为RT_THREAD_CLOSE
/* 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;
}
4 启动线程
[cpp]
/**
* This function will start a thread and put it to system ready queue
*
* @param thread the thread to be sta