summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
authorMan, Jianting (Meco) <[email protected]>2021-08-17 16:56:10 -0500
committerGitHub <[email protected]>2021-08-17 16:56:10 -0500
commit3e6578916f892e4456d9dff4bbd44590a0cb4bc8 (patch)
treeb98d9fe601ce62e17b37f5b971f0cc9f965fa0e3 /src/thread.c
parent37a9231e6219b94f7cde8bdea9251e260c4bed18 (diff)
parent196f340e5d845efd005b57a2089e0642266a79be (diff)
Merge branch 'master' into security
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c112
1 files changed, 67 insertions, 45 deletions
diff --git a/src/thread.c b/src/thread.c
index 34e96077d1..7e0c10bfef 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,7 +74,7 @@ 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)
@@ -84,7 +82,7 @@ static void _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 _thread_exit(void)
{
struct rt_thread *thread;
register rt_base_t level;
@@ -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 */
@@ -141,14 +139,14 @@ void rt_thread_exit(void)
rt_hw_interrupt_enable(level);
}
-static rt_err_t _rt_thread_init(struct rt_thread *thread,
- const char *name,
- void (*entry)(void *parameter),
- void *parameter,
- void *stack_start,
- rt_uint32_t stack_size,
- rt_uint8_t priority,
- rt_uint32_t tick)
+static rt_err_t _thread_init(struct rt_thread *thread,
+ const char *name,
+ void (*entry)(void *parameter),
+ void *parameter,
+ void *stack_start,
+ rt_uint32_t stack_size,
+ rt_uint8_t priority,
+ rt_uint32_t tick)
{
/* init thread list */
rt_list_init(&(thread->tlist));
@@ -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 *)_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 *)_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));
@@ -272,14 +274,14 @@ rt_err_t rt_thread_init(struct rt_thread *thread,
/* initialize thread object */
rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
- return _rt_thread_init(thread,
- name,
- entry,
- parameter,
- stack_start,
- stack_size,
- priority,
- tick);
+ return _thread_init(thread,
+ name,
+ entry,
+ parameter,
+ stack_start,
+ stack_size,
+ priority,
+ tick);
}
RTM_EXPORT(rt_thread_init);
@@ -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);
@@ -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;
}
@@ -425,7 +440,7 @@ rt_thread_t rt_thread_create(const char *name,
return RT_NULL;
}
- _rt_thread_init(thread,
+ _thread_init(thread,
name,
entry,
parameter,
@@ -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();
}