summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c99
1 files changed, 51 insertions, 48 deletions
diff --git a/src/thread.c b/src/thread.c
index 1b489cf76f..170d61eec8 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -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,40 +399,24 @@ rt_err_t rt_thread_startup(rt_thread_t thread)
}
RTM_EXPORT(rt_thread_startup);
-static rt_err_t _thread_detach(rt_thread_t thread);
-
/**
- * @brief This function will detach a thread. The thread object will be removed from
+ * @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 deleted.
+ * @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_detach(rt_thread_t thread)
-{
- /* parameter 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));
-
- return _thread_detach(thread);
-}
-RTM_EXPORT(rt_thread_detach);
-
-static rt_err_t _thread_detach(rt_thread_t thread)
+rt_err_t rt_thread_close(rt_thread_t thread)
{
- rt_err_t error;
rt_sched_lock_level_t slvl;
rt_uint8_t thread_status;
- rt_base_t critical_level;
- /**
- * forbid scheduling on current core before returning since current thread
- * may be detached from scheduler.
- */
- critical_level = rt_enter_critical();
+ /* 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);
@@ -463,24 +436,54 @@ static rt_err_t _thread_detach(rt_thread_t thread)
/* change stat */
rt_sched_thread_close(thread);
+ }
- /* scheduler works are done */
- rt_sched_unlock(slvl);
+ /* scheduler works are done */
+ rt_sched_unlock(slvl);
- _thread_detach_from_mutex(thread);
+ return RT_EOK;
+}
+RTM_EXPORT(rt_thread_close);
- /* insert to defunct thread list */
- rt_thread_defunct_enqueue(thread);
+static rt_err_t _thread_detach(rt_thread_t thread);
- error = RT_EOK;
- }
- else
- {
- rt_sched_unlock(slvl);
+/**
+ * @brief This function will detach a thread. The thread object will be removed from
+ * thread queue and detached/deleted from the system object management.
+ *
+ * @param thread is the thread to be deleted.
+ *
+ * @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_detach(rt_thread_t thread)
+{
+ /* parameter 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));
- /* already closed */
- error = RT_EOK;
- }
+ return _thread_detach(thread);
+}
+RTM_EXPORT(rt_thread_detach);
+
+static rt_err_t _thread_detach(rt_thread_t thread)
+{
+ rt_err_t error;
+ rt_base_t critical_level;
+
+ /**
+ * forbid scheduling on current core before returning since current thread
+ * may be detached from scheduler.
+ */
+ critical_level = rt_enter_critical();
+
+ error = rt_thread_close(thread);
+
+ _thread_detach_from_mutex(thread);
+
+ /* insert to defunct thread list */
+ rt_thread_defunct_enqueue(thread);
rt_exit_critical_safe(critical_level);
return error;