summaryrefslogtreecommitdiff
path: root/src/thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/thread.c')
-rw-r--r--src/thread.c135
1 files changed, 110 insertions, 25 deletions
diff --git a/src/thread.c b/src/thread.c
index df9f151834..49e2022bf7 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -107,11 +107,11 @@ void rt_thread_exit(void)
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
}
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
-
/* switch to next task */
rt_schedule();
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
}
static rt_err_t _rt_thread_init(struct rt_thread *thread,
@@ -141,7 +141,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 - sizeof(rt_ubase_t)),
+ (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
(void *)rt_thread_exit);
#endif
@@ -172,13 +172,14 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
/* lock init */
thread->scheduler_lock_nest = 0;
thread->cpus_lock_nest = 0;
+ thread->critical_lock_nest = 0;
#endif /*RT_USING_SMP*/
/* initialize cleanup function and user data */
thread->cleanup = 0;
thread->user_data = 0;
- /* init thread timer */
+ /* initialize thread timer */
rt_timer_init(&(thread->thread_timer),
thread->name,
rt_thread_timeout,
@@ -191,7 +192,9 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
thread->sig_mask = 0x00;
thread->sig_pending = 0x00;
+#ifndef RT_USING_SMP
thread->sig_ret = RT_NULL;
+#endif
thread->sig_vectors = RT_NULL;
thread->si_list = RT_NULL;
#endif
@@ -239,7 +242,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,
@@ -290,7 +293,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 */
@@ -335,6 +338,9 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
RT_ASSERT(rt_object_is_systemobject((rt_object_t)thread));
+ if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
+ return RT_EOK;
+
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
{
/* remove from schedule */
@@ -347,17 +353,17 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
/* change stat */
thread->stat = RT_THREAD_CLOSE;
- /* detach object */
- rt_object_detach((rt_object_t)thread);
-
- if (thread->cleanup != RT_NULL)
+ if ((rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE) &&
+ thread->cleanup == RT_NULL)
+ {
+ rt_object_detach((rt_object_t)thread);
+ }
+ else
{
/* disable interrupt */
lock = rt_hw_interrupt_disable();
-
/* insert to defunct thread list */
rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));
-
/* enable interrupt */
rt_hw_interrupt_enable(lock);
}
@@ -366,7 +372,6 @@ rt_err_t rt_thread_detach(rt_thread_t thread)
}
RTM_EXPORT(rt_thread_detach);
-
#ifdef RT_USING_HEAP
/**
* This function will create a thread object and allocate thread object memory
@@ -435,6 +440,9 @@ rt_err_t rt_thread_delete(rt_thread_t thread)
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);
+ if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
+ return RT_EOK;
+
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
{
/* remove from schedule */
@@ -444,12 +452,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));
@@ -470,7 +478,15 @@ RTM_EXPORT(rt_thread_delete);
*/
rt_err_t rt_thread_yield(void)
{
+ struct rt_thread *thread;
+ rt_base_t lock;
+
+ thread = rt_thread_self();
+ lock = rt_hw_interrupt_disable();
+ thread->remaining_tick = thread->init_tick;
+ thread->stat |= RT_THREAD_STAT_YIELD;
rt_schedule();
+ rt_hw_interrupt_enable(lock);
return RT_EOK;
}
@@ -529,9 +545,66 @@ 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 = *tick + inc_tick - rt_tick_get();
+
+ /* 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
+ * @param ms the delay ms time
*
* @return RT_EOK
*/
@@ -614,9 +687,17 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
case RT_THREAD_CTRL_STARTUP:
return rt_thread_startup(thread);
-#ifdef RT_USING_HEAP
case RT_THREAD_CTRL_CLOSE:
- return rt_thread_delete(thread);
+
+ if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)
+ {
+ return rt_thread_detach(thread);
+ }
+#ifdef RT_USING_HEAP
+ else
+ {
+ return rt_thread_delete(thread);
+ }
#endif
#ifdef RT_USING_SMP
@@ -655,6 +736,7 @@ RTM_EXPORT(rt_thread_control);
*/
rt_err_t rt_thread_suspend(rt_thread_t thread)
{
+ register rt_base_t stat;
register rt_base_t temp;
/* thread check */
@@ -663,22 +745,25 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
RT_DEBUG_LOG(RT_DEBUG_THREAD, ("thread suspend: %s\n", thread->name));
- if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
+ 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));
-
return -RT_ERROR;
}
- RT_ASSERT(thread == rt_thread_self());
-
/* 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 */
- thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
rt_schedule_remove_thread(thread);
+ thread->stat = RT_THREAD_SUSPEND | (thread->stat & ~RT_THREAD_STAT_MASK);
/* stop thread timer anyway */
rt_timer_stop(&(thread->thread_timer));