summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c97
1 files changed, 53 insertions, 44 deletions
diff --git a/src/thread.c b/src/thread.c
index 2f06cedd44..d1cc848777 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -29,13 +29,24 @@
* 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
*/
#include <rthw.h>
#include <rtthread.h>
#include <stddef.h>
-#ifdef RT_USING_HOOK
+#ifndef __on_rt_thread_inited_hook
+ #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread))
+#endif
+#ifndef __on_rt_thread_suspend_hook
+ #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread))
+#endif
+#ifndef __on_rt_thread_resume_hook
+ #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread))
+#endif
+
+#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
static void (*rt_thread_suspend_hook)(rt_thread_t thread);
static void (*rt_thread_resume_hook) (rt_thread_t thread);
static void (*rt_thread_inited_hook) (rt_thread_t thread);
@@ -106,6 +117,43 @@ static void _thread_exit(void)
rt_schedule();
}
+/**
+ * @brief This function is the timeout function for thread, normally which is invoked
+ * when thread is timeout to wait some resource.
+ *
+ * @param parameter is the parameter of thread timeout function
+ */
+static void _thread_timeout(void *parameter)
+{
+ struct rt_thread *thread;
+ register rt_base_t temp;
+
+ thread = (struct rt_thread *)parameter;
+
+ /* thread 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);
+
+ /* disable interrupt */
+ temp = rt_hw_interrupt_disable();
+
+ /* set error number */
+ thread->error = -RT_ETIMEOUT;
+
+ /* remove from suspend list */
+ rt_list_remove(&(thread->tlist));
+
+ /* insert to schedule ready list */
+ rt_schedule_insert_thread(thread);
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(temp);
+
+ /* do schedule */
+ rt_schedule();
+}
+
static rt_err_t _thread_init(struct rt_thread *thread,
const char *name,
void (*entry)(void *parameter),
@@ -173,7 +221,7 @@ static rt_err_t _thread_init(struct rt_thread *thread,
/* initialize thread timer */
rt_timer_init(&(thread->thread_timer),
thread->name,
- rt_thread_timeout,
+ _thread_timeout,
thread,
0,
RT_TIMER_FLAG_ONE_SHOT);
@@ -757,8 +805,7 @@ RTM_EXPORT(rt_thread_control);
/**
* @brief This function will suspend the specified thread and change it to suspend state.
*
- * @note If suspend self thread, after this function call, the
- * rt_schedule() must be invoked.
+ * @note This function only can suspend current thread itself.
*
* @param thread is the thread to be suspended.
*
@@ -773,14 +820,14 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
/* thread 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());
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
stat = thread->stat & RT_THREAD_STAT_MASK;
if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING))
{
- RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n",
- thread->stat));
+ RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: thread disorder, 0x%2x\n", thread->stat));
return -RT_ERROR;
}
@@ -853,44 +900,6 @@ rt_err_t rt_thread_resume(rt_thread_t thread)
RTM_EXPORT(rt_thread_resume);
/**
- * @brief This function is the timeout function for thread, normally which is invoked
- * when thread is timeout to wait some resource.
- *
- * @param parameter is the parameter of thread timeout function
- */
-void rt_thread_timeout(void *parameter)
-{
- struct rt_thread *thread;
- register rt_base_t temp;
-
- thread = (struct rt_thread *)parameter;
-
- /* thread 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);
-
- /* disable interrupt */
- temp = rt_hw_interrupt_disable();
-
- /* set error number */
- thread->error = -RT_ETIMEOUT;
-
- /* remove from suspend list */
- rt_list_remove(&(thread->tlist));
-
- /* insert to schedule ready list */
- rt_schedule_insert_thread(thread);
-
- /* enable interrupt */
- rt_hw_interrupt_enable(temp);
-
- /* do schedule */
- rt_schedule();
-}
-RTM_EXPORT(rt_thread_timeout);
-
-/**
* @brief This function will find the specified thread.
*
* @note Please don't invoke this function in interrupt status.