summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
authorWayne Lin <[email protected]>2021-07-12 18:17:25 +0800
committerWayne Lin <[email protected]>2021-07-12 18:17:25 +0800
commit735285360929c97d49ed51dbc41dfeec570a35b3 (patch)
tree9c1b5303717e80442037fd43f4144ff67c86bc5c /src/thread.c
parent978ea520d982ede86feee1f5f08b04bddd6efd1e (diff)
parent19f1c82bc8355879d157805748b23e6dabed701d (diff)
Merge branch 'master' of https://github.com/RT-Thread/rt-thread into nuvoton
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c86
1 files changed, 54 insertions, 32 deletions
diff --git a/src/thread.c b/src/thread.c
index 34e96077d1..60e6ab91d1 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -32,8 +32,6 @@
#include <rthw.h>
#include <rtthread.h>
-extern rt_list_t rt_thread_defunct;
-
#ifdef RT_USING_HOOK
static void (*rt_thread_suspend_hook)(rt_thread_t thread);
static void (*rt_thread_resume_hook) (rt_thread_t thread);
@@ -76,15 +74,15 @@ void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
rt_thread_inited_hook = hook;
}
-#endif
+#endif /* RT_USING_HOOK */
/* must be invoke witch rt_hw_interrupt_disable */
-static void _thread_cleanup_execute(rt_thread_t thread)
+static void _rt_thread_cleanup_execute(rt_thread_t thread)
{
register rt_base_t level;
#ifdef RT_USING_MODULE
struct rt_dlmodule *module = RT_NULL;
-#endif
+#endif /* RT_USING_MODULE */
level = rt_hw_interrupt_disable();
#ifdef RT_USING_MODULE
module = (struct rt_dlmodule*)thread->module_id;
@@ -92,18 +90,18 @@ static void _thread_cleanup_execute(rt_thread_t thread)
{
dlmodule_destroy(module);
}
-#endif
+#endif /* RT_USING_MODULE */
/* invoke thread cleanup */
if (thread->cleanup != RT_NULL)
thread->cleanup(thread);
#ifdef RT_USING_SIGNALS
rt_thread_free_sig(thread);
-#endif
+#endif /* RT_USING_SIGNALS */
rt_hw_interrupt_enable(level);
}
-void rt_thread_exit(void)
+static void _rt_thread_exit(void)
{
struct rt_thread *thread;
register rt_base_t level;
@@ -114,7 +112,7 @@ void rt_thread_exit(void)
/* disable interrupt */
level = rt_hw_interrupt_disable();
- _thread_cleanup_execute(thread);
+ _rt_thread_cleanup_execute(thread);
/* remove from schedule */
rt_schedule_remove_thread(thread);
@@ -131,7 +129,7 @@ void rt_thread_exit(void)
else
{
/* insert to defunct thread list */
- rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
+ rt_thread_defunct_enqueue(thread);
}
/* switch to next task */
@@ -165,12 +163,12 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
#ifdef ARCH_CPU_STACK_GROWS_UPWARD
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(void *)((char *)thread->stack_addr),
- (void *)rt_thread_exit);
+ (void *)_rt_thread_exit);
#else
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
- (void *)rt_thread_exit);
-#endif
+ (void *)_rt_thread_exit);
+#endif /* ARCH_CPU_STACK_GROWS_UPWARD */
/* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
@@ -181,7 +179,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
#if RT_THREAD_PRIORITY_MAX > 32
thread->number = 0;
thread->high_mask = 0;
-#endif
+#endif /* RT_THREAD_PRIORITY_MAX > 32 */
/* tick init */
thread->init_tick = tick;
@@ -200,7 +198,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
thread->scheduler_lock_nest = 0;
thread->cpus_lock_nest = 0;
thread->critical_lock_nest = 0;
-#endif /*RT_USING_SMP*/
+#endif /* RT_USING_SMP */
/* initialize cleanup function and user data */
thread->cleanup = 0;
@@ -221,13 +219,17 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
#ifndef RT_USING_SMP
thread->sig_ret = RT_NULL;
-#endif
+#endif /* RT_USING_SMP */
thread->sig_vectors = RT_NULL;
thread->si_list = RT_NULL;
-#endif
+#endif /* RT_USING_SIGNALS */
#ifdef RT_USING_LWP
thread->lwp = RT_NULL;
+#endif /* RT_USING_LWP */
+
+#ifdef RT_USING_CPU_USAGE
+ thread->duration_tick = 0;
#endif
RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
@@ -302,7 +304,7 @@ rt_thread_t rt_thread_self(void)
extern rt_thread_t rt_current_thread;
return rt_current_thread;
-#endif
+#endif /* RT_USING_SMP */
}
RTM_EXPORT(rt_thread_self);
@@ -330,7 +332,7 @@ rt_err_t rt_thread_startup(rt_thread_t thread)
thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1L << thread->current_priority;
-#endif
+#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));
@@ -358,6 +360,8 @@ RTM_EXPORT(rt_thread_startup);
*/
rt_err_t rt_thread_detach(rt_thread_t thread)
{
+ rt_base_t lock;
+
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
@@ -372,7 +376,7 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
rt_schedule_remove_thread(thread);
}
- _thread_cleanup_execute(thread);
+ _rt_thread_cleanup_execute(thread);
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));
@@ -380,8 +384,19 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
/* change stat */
thread->stat = RT_THREAD_CLOSE;
- /* detach thread object */
- rt_object_detach((rt_object_t)thread);
+ if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
+ {
+ rt_object_detach((rt_object_t)thread);
+ }
+ else
+ {
+ /* disable interrupt */
+ lock = rt_hw_interrupt_disable();
+ /* insert to defunct thread list */
+ rt_thread_defunct_enqueue(thread);
+ /* enable interrupt */
+ rt_hw_interrupt_enable(lock);
+ }
return RT_EOK;
}
@@ -464,7 +479,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
rt_schedule_remove_thread(thread);
}
- _thread_cleanup_execute(thread);
+ _rt_thread_cleanup_execute(thread);
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));
@@ -476,7 +491,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
thread->stat = RT_THREAD_CLOSE;
/* insert to defunct thread list */
- rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
+ rt_thread_defunct_enqueue(thread);
/* enable interrupt */
rt_hw_interrupt_enable(lock);
@@ -484,7 +499,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
return RT_EOK;
}
RTM_EXPORT(rt_thread_delete);
-#endif
+#endif /* RT_USING_HEAP */
/**
* This function will let current thread yield processor, and scheduler will
@@ -682,7 +697,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
- #endif
+ #endif /* RT_THREAD_PRIORITY_MAX > 32 */
/* insert thread to schedule queue again */
rt_schedule_insert_thread(thread);
@@ -698,7 +713,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */
#else
thread->number_mask = 1 << thread->current_priority;
- #endif
+ #endif /* RT_THREAD_PRIORITY_MAX > 32 */
}
/* enable interrupt */
@@ -724,7 +739,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
{
rt_err = rt_thread_delete(thread);
}
- #endif
+ #endif /* RT_USING_HEAP */
rt_schedule();
return rt_err;
}
@@ -744,7 +759,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
break;
}
- #endif /*RT_USING_SMP*/
+ #endif /* RT_USING_SMP */
default:
break;
@@ -839,12 +854,12 @@ rt_err_t rt_thread_resume(rt_thread_t thread)
rt_timer_stop(&thread->thread_timer);
- /* enable interrupt */
- rt_hw_interrupt_enable(temp);
-
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);
+ /* enable interrupt */
+ rt_hw_interrupt_enable(temp);
+
RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread));
return RT_EOK;
}
@@ -859,6 +874,7 @@ RTM_EXPORT(rt_thread_resume);
void rt_thread_timeout(void *parameter)
{
struct rt_thread *thread;
+ register rt_base_t temp;
thread = (struct rt_thread *)parameter;
@@ -867,6 +883,9 @@ void rt_thread_timeout(void *parameter)
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);
+ /* disable interrupt */
+ temp = rt_hw_interrupt_disable();
+
/* set error number */
thread->error = -RT_ETIMEOUT;
@@ -876,6 +895,9 @@ void rt_thread_timeout(void *parameter)
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);
+ /* enable interrupt */
+ rt_hw_interrupt_enable(temp);
+
/* do schedule */
rt_schedule();
}