Browse Source

Merge pull request #2259 from RT-Thread/fix_thread_stat

[Kernel] Fix rt_schedule_insert_thread issue
Bernard Xiong 6 years ago
parent
commit
806eb49f71
1 changed files with 14 additions and 10 deletions
  1. 14 10
      src/scheduler.c

+ 14 - 10
src/scheduler.c

@@ -351,7 +351,7 @@ void rt_schedule(void)
                 RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
 
                 rt_schedule_remove_thread(to_thread);
-                to_thread->stat = RT_THREAD_RUNNING;
+                to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
 
                 /* switch to new thread */
                 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
@@ -412,6 +412,7 @@ void rt_schedule(void)
 
         if (rt_thread_ready_priority_group != 0)
         {
+            /* need_insert_from_thread: need to insert from_thread to ready queue */
             int need_insert_from_thread = 0;
 
             to_thread = _get_highest_priority_thread(&highest_ready_priority);
@@ -443,7 +444,7 @@ void rt_schedule(void)
                 }
 
                 rt_schedule_remove_thread(to_thread);
-                to_thread->stat = RT_THREAD_RUNNING;
+                to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
 
                 /* switch to new thread */
                 RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
@@ -485,7 +486,7 @@ void rt_schedule(void)
             else
             {
                 rt_schedule_remove_thread(rt_current_thread);
-                rt_current_thread->stat = RT_THREAD_RUNNING;
+                rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
             }
         }
     }
@@ -556,7 +557,7 @@ void rt_scheduler_do_irq_switch(void *context)
                 RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
 
                 rt_schedule_remove_thread(to_thread);
-                to_thread->stat = RT_THREAD_RUNNING;
+                to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
 
 #ifdef RT_USING_OVERFLOW_CHECK
                 _rt_scheduler_stack_check(to_thread);
@@ -595,14 +596,16 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
     /* disable interrupt */
     level = rt_hw_interrupt_disable();
 
-    /* change stat */
-    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
-
+    /* it should be RUNNING thread */
     if (thread->oncpu != RT_CPU_DETACHED)
     {
+        thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
         goto __exit;
     }
 
+    /* READY thread, insert to ready queue */
+    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
+
     cpu_id   = rt_hw_cpu_id();
     bind_cpu = thread->bind_cpu ;
 
@@ -655,14 +658,15 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
     /* disable interrupt */
     temp = rt_hw_interrupt_disable();
 
-    /* change stat */
-    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
-
+    /* it's current thread, it should be RUNNING thread */
     if (thread == rt_current_thread)
     {
+        thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
         goto __exit;
     }
 
+    /* READY thread, insert to ready queue */
+    thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
     /* insert thread to ready list */
     rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
                           &(thread->tlist));