mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-09-20 17:44:20 +08:00
[kernel] add rt_thread_close()
* [thread] Add rt_thread_close() This patch introduces a new function `rt_thread_close()` to enhances the usability and maintainability by providing a dedicated mechanism for closing threads. - A new function `rt_thread_close()` is added to the API, providing a standardized approach for closing threads. - The `rt_thread_close()` function removes a thread from the thread queue, updates its status to indicate closure, and performs the thread timer detaching which is a embedded timer in thread object. - Additionally, the `rt_thread_detach()` function is modified to utilize `rt_thread_close()` internally, streamlining the thread detachment process. Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
+51
-48
@@ -79,25 +79,14 @@ RT_OBJECT_HOOKLIST_DEFINE(rt_thread_inited);
|
||||
static void _thread_exit(void)
|
||||
{
|
||||
struct rt_thread *thread;
|
||||
rt_sched_lock_level_t slvl;
|
||||
rt_base_t critical_level;
|
||||
|
||||
/* get current thread */
|
||||
thread = rt_thread_self();
|
||||
|
||||
critical_level = rt_enter_critical();
|
||||
rt_sched_lock(&slvl);
|
||||
|
||||
/* remove from schedule */
|
||||
rt_sched_remove_thread(thread);
|
||||
|
||||
/* remove it from timer list */
|
||||
rt_timer_detach(&thread->thread_timer);
|
||||
|
||||
/* change stat */
|
||||
rt_sched_thread_close(thread);
|
||||
|
||||
rt_sched_unlock(slvl);
|
||||
rt_thread_close(thread);
|
||||
|
||||
/* insert to defunct thread list */
|
||||
rt_thread_defunct_enqueue(thread);
|
||||
@@ -410,6 +399,52 @@ rt_err_t rt_thread_startup(rt_thread_t thread)
|
||||
}
|
||||
RTM_EXPORT(rt_thread_startup);
|
||||
|
||||
/**
|
||||
* @brief This function will close a thread. The thread object will be removed from
|
||||
* thread queue and detached/deleted from the system object management.
|
||||
* It's different from rt_thread_delete or rt_thread_detach that this will not enqueue
|
||||
* the closing thread to cleanup queue.
|
||||
*
|
||||
* @param thread is the thread to be closed.
|
||||
*
|
||||
* @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
|
||||
* If the return value is any other values, it means this operation failed.
|
||||
*/
|
||||
rt_err_t rt_thread_close(rt_thread_t thread)
|
||||
{
|
||||
rt_sched_lock_level_t slvl;
|
||||
rt_uint8_t thread_status;
|
||||
|
||||
/* forbid scheduling on current core if closing current thread */
|
||||
RT_ASSERT(thread != rt_thread_self() || rt_critical_level());
|
||||
|
||||
/* before checking status of scheduler */
|
||||
rt_sched_lock(&slvl);
|
||||
|
||||
/* check if thread is already closed */
|
||||
thread_status = rt_sched_thread_get_stat(thread);
|
||||
if (thread_status != RT_THREAD_CLOSE)
|
||||
{
|
||||
if (thread_status != RT_THREAD_INIT)
|
||||
{
|
||||
/* remove from schedule */
|
||||
rt_sched_remove_thread(thread);
|
||||
}
|
||||
|
||||
/* release thread timer */
|
||||
rt_timer_detach(&(thread->thread_timer));
|
||||
|
||||
/* change stat */
|
||||
rt_sched_thread_close(thread);
|
||||
}
|
||||
|
||||
/* scheduler works are done */
|
||||
rt_sched_unlock(slvl);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
RTM_EXPORT(rt_thread_close);
|
||||
|
||||
static rt_err_t _thread_detach(rt_thread_t thread);
|
||||
|
||||
/**
|
||||
@@ -435,8 +470,6 @@ RTM_EXPORT(rt_thread_detach);
|
||||
static rt_err_t _thread_detach(rt_thread_t thread)
|
||||
{
|
||||
rt_err_t error;
|
||||
rt_sched_lock_level_t slvl;
|
||||
rt_uint8_t thread_status;
|
||||
rt_base_t critical_level;
|
||||
|
||||
/**
|
||||
@@ -445,42 +478,12 @@ static rt_err_t _thread_detach(rt_thread_t thread)
|
||||
*/
|
||||
critical_level = rt_enter_critical();
|
||||
|
||||
/* before checking status of scheduler */
|
||||
rt_sched_lock(&slvl);
|
||||
error = rt_thread_close(thread);
|
||||
|
||||
/* check if thread is already closed */
|
||||
thread_status = rt_sched_thread_get_stat(thread);
|
||||
if (thread_status != RT_THREAD_CLOSE)
|
||||
{
|
||||
if (thread_status != RT_THREAD_INIT)
|
||||
{
|
||||
/* remove from schedule */
|
||||
rt_sched_remove_thread(thread);
|
||||
}
|
||||
_thread_detach_from_mutex(thread);
|
||||
|
||||
/* release thread timer */
|
||||
rt_timer_detach(&(thread->thread_timer));
|
||||
|
||||
/* change stat */
|
||||
rt_sched_thread_close(thread);
|
||||
|
||||
/* scheduler works are done */
|
||||
rt_sched_unlock(slvl);
|
||||
|
||||
_thread_detach_from_mutex(thread);
|
||||
|
||||
/* insert to defunct thread list */
|
||||
rt_thread_defunct_enqueue(thread);
|
||||
|
||||
error = RT_EOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_sched_unlock(slvl);
|
||||
|
||||
/* already closed */
|
||||
error = RT_EOK;
|
||||
}
|
||||
/* insert to defunct thread list */
|
||||
rt_thread_defunct_enqueue(thread);
|
||||
|
||||
rt_exit_critical_safe(critical_level);
|
||||
return error;
|
||||
|
||||
Reference in New Issue
Block a user