/** * 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 {
/** * 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; /* 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; }
static void rt_thread_exit(void) { struct rt_thread *thread; register rt_base_t level; /* get current thread */ thread = rt_current_thread; /* disable interrupt */ level = rt_hw_interrupt_disable(); /* remove from schedule */ rt_schedule_remove_thread(thread); /* change stat */ thread->stat = RT_THREAD_CLOSE; /* remove it from timer list */ rt_list_remove(&(thread->thread_timer.list)); rt_object_detach((rt_object_t)&(thread->thread_timer)); if ((rt_object_is_systemobject((rt_object_t)thread) == RT_EOK) && thread->cleanup == RT_NULL) { rt_object_detach((rt_object_t)thread); } else { /* insert to defunct thread list */ rt_list_insert_after(&rt_thread_defunct, &(thread->tlist)); } /* enable interrupt */ rt_hw_interrupt_enable(level); /* switch to next task */ rt_schedule(); }