summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
authorBernard Xiong <[email protected]>2019-12-21 06:40:47 +0800
committerGitHub <[email protected]>2019-12-21 06:40:47 +0800
commit0c11cd86717295bc4c41d7421e4119a62bbbfa11 (patch)
tree594994078d07de40ba37c47d5d5c536a51bfe0fc /src/thread.c
parent3491b2f0f470d3834763b9d78071ce7804597c94 (diff)
parenta383385a3c0a676cee4471fd3cb4f372a36e1023 (diff)
Merge pull request #3274 from armink/lts-v3.1.x
Lts v3.1.x
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c71
1 files changed, 64 insertions, 7 deletions
diff --git a/src/thread.c b/src/thread.c
index 940d24e6f2..ab4696b696 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -142,7 +142,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
(void *)rt_thread_exit);
#else
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
- (void *)((char *)thread->stack_addr + thread->stack_size - 4),
+ (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
(void *)rt_thread_exit);
#endif
@@ -169,7 +169,7 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
thread->cleanup = 0;
thread->user_data = 0;
- /* init thread timer */
+ /* initialize thread timer */
rt_timer_init(&(thread->thread_timer),
thread->name,
rt_thread_timeout,
@@ -230,7 +230,7 @@ rt_err_t rt_thread_init(struct rt_thread *thread,
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(stack_start != RT_NULL);
- /* init thread object */
+ /* initialize thread object */
rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);
return _rt_thread_init(thread,
@@ -269,7 +269,7 @@ rt_err_t rt_thread_startup(rt_thread_t thread)
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 init priority */
+ /* set current priority to initialize priority */
thread->current_priority = thread->init_priority;
/* calculate priority attribute */
@@ -422,12 +422,12 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
/* release thread timer */
rt_timer_detach(&(thread->thread_timer));
- /* change stat */
- thread->stat = RT_THREAD_CLOSE;
-
/* disable interrupt */
lock = rt_hw_interrupt_disable();
+ /* change stat */
+ thread->stat = RT_THREAD_CLOSE;
+
/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
@@ -535,6 +535,63 @@ rt_err_t rt_thread_delay(rt_tick_t tick)
RTM_EXPORT(rt_thread_delay);
/**
+ * This function will let current thread delay until (*tick + inc_tick).
+ *
+ * @param tick the tick of last wakeup.
+ * @param inc_tick the increment tick
+ *
+ * @return RT_EOK
+ */
+rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
+{
+ register rt_base_t level;
+ struct rt_thread *thread;
+
+ RT_ASSERT(tick != RT_NULL);
+
+ /* set to current thread */
+ thread = rt_thread_self();
+ RT_ASSERT(thread != RT_NULL);
+ RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
+
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
+
+ if (rt_tick_get() - *tick < inc_tick)
+ {
+ *tick = rt_tick_get() - *tick + inc_tick;
+
+ /* suspend thread */
+ rt_thread_suspend(thread);
+
+ /* reset the timeout of thread timer and start it */
+ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick);
+ rt_timer_start(&(thread->thread_timer));
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
+
+ rt_schedule();
+
+ /* clear error number of this thread to RT_EOK */
+ if (thread->error == -RT_ETIMEOUT)
+ {
+ thread->error = RT_EOK;
+ }
+ }
+ else
+ {
+ rt_hw_interrupt_enable(level);
+ }
+
+ /* get the wakeup tick */
+ *tick = rt_tick_get();
+
+ return RT_EOK;
+}
+RTM_EXPORT(rt_thread_delay_until);
+
+/**
* This function will let current thread delay for some milliseconds.
*
* @param tick the delay time