summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
authorSherman <[email protected]>2022-01-26 17:48:46 +0800
committerSherman <[email protected]>2022-01-26 17:48:46 +0800
commit544c721d89ee7c6e7d3f88dd5bd555a221d8222e (patch)
treeaafdd34b49eca60159844a6712883cd01fd478b7 /src/thread.c
parentfc3c29df709798bcbf0c40e4146e628975c40b9e (diff)
parent032ce63ed39c1bbb9f68068c0cf8b876771f27c8 (diff)
Merge branch 'master' of github.com:RT-Thread/rt-thread
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c47
1 files changed, 26 insertions, 21 deletions
diff --git a/src/thread.c b/src/thread.c
index a024a3b7dc..30208f1dd1 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -28,7 +28,9 @@
* add support for tasks bound to cpu
* 2021-02-24 Meco Man rearrange rt_thread_control() - schedule the thread when close it
* 2021-11-15 THEWON Remove duplicate work between idle and _thread_exit
+ * 2021-12-27 Meco Man remove .init_priority
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to thread.c
+ * 2022-01-24 THEWON let rt_thread_sleep return thread->error when using signal
*/
#include <rthw.h>
@@ -129,7 +131,7 @@ static void _thread_timeout(void *parameter)
thread = (struct rt_thread *)parameter;
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
@@ -186,7 +188,6 @@ static rt_err_t _thread_init(struct rt_thread *thread,
/* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
- thread->init_priority = priority;
thread->current_priority = priority;
thread->number_mask = 0;
@@ -289,7 +290,7 @@ rt_err_t rt_thread_init(struct rt_thread *thread,
rt_uint8_t priority,
rt_uint32_t tick)
{
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(stack_start != RT_NULL);
@@ -340,14 +341,11 @@ RTM_EXPORT(rt_thread_self);
*/
rt_err_t rt_thread_startup(rt_thread_t thread)
{
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
- /* set current priority to initialize priority */
- thread->current_priority = thread->init_priority;
-
/* calculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = thread->current_priority >> 3; /* 5bit */
@@ -358,7 +356,7 @@ rt_err_t rt_thread_startup(rt_thread_t thread)
#endif /* RT_THREAD_PRIORITY_MAX > 32 */
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("startup a thread:%s with priority:%d\n",
- thread->name, thread->init_priority));
+ thread->name, thread->current_priority));
/* change thread stat */
thread->stat = RT_THREAD_SUSPEND;
/* then resume it */
@@ -386,7 +384,7 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
{
rt_base_t lock;
- /* thread check */
+ /* 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));
@@ -489,7 +487,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
{
rt_base_t lock;
- /* thread check */
+ /* 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) == RT_FALSE);
@@ -569,6 +567,9 @@ rt_err_t rt_thread_sleep(rt_tick_t tick)
/* disable interrupt */
temp = rt_hw_interrupt_disable();
+ /* reset thread error */
+ thread->error = RT_EOK;
+
/* suspend thread */
rt_thread_suspend(thread);
@@ -585,7 +586,7 @@ rt_err_t rt_thread_sleep(rt_tick_t tick)
if (thread->error == -RT_ETIMEOUT)
thread->error = RT_EOK;
- return RT_EOK;
+ return thread->error;
}
/**
@@ -628,6 +629,9 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
/* disable interrupt */
level = rt_hw_interrupt_disable();
+ /* reset thread error */
+ thread->error = RT_EOK;
+
cur_tick = rt_tick_get();
if (cur_tick - *tick < inc_tick)
{
@@ -660,7 +664,7 @@ rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
rt_hw_interrupt_enable(level);
}
- return RT_EOK;
+ return thread->error;
}
RTM_EXPORT(rt_thread_delay_until);
@@ -706,7 +710,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
{
register rt_base_t temp;
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
@@ -808,7 +812,11 @@ RTM_EXPORT(rt_thread_control);
/**
* @brief This function will suspend the specified thread and change it to suspend state.
*
- * @note This function only can suspend current thread itself.
+ * @note This function ONLY can suspend current thread itself.
+ * Do not use the rt_thread_suspend and rt_thread_resume functions to synchronize the activities of threads.
+ * You have no way of knowing what code a thread is executing when you suspend it.
+ * If you suspend a thread while it is executing a critical area which is protected by a mutex,
+ * other threads attempt to use that mutex and have to wait. Deadlocks can occur very easily.
*
* @param thread is the thread to be suspended.
*
@@ -820,7 +828,7 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
register rt_base_t stat;
register rt_base_t temp;
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_ASSERT(thread == rt_thread_self());
@@ -836,11 +844,6 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
/* disable interrupt */
temp = rt_hw_interrupt_disable();
- if (stat == RT_THREAD_RUNNING)
- {
- /* not suspend running status thread on other core */
- RT_ASSERT(thread == rt_thread_self());
- }
/* change thread stat */
rt_schedule_remove_thread(thread);
@@ -860,6 +863,8 @@ RTM_EXPORT(rt_thread_suspend);
/**
* @brief This function will resume a thread and put it to system ready queue.
*
+ * @note Do not use the rt_thread_suspend and rt_thread_resume functions to synchronize the activities of threads.
+ *
* @param thread is the thread to be resumed.
*
* @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
@@ -869,7 +874,7 @@ rt_err_t rt_thread_resume(rt_thread_t thread)
{
register rt_base_t temp;
- /* thread check */
+ /* parameter check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);