Skip to content

Commit 69994ca

Browse files
committed
[Kernel] Fix rt_schedule_insert_thread issue
When suspend current thread and then enable interrupt, it's possible resume "the current thread" immediately and insert to ready queue. The rt_schedule_insert_thread should change the status of "current thread" to RUNNING statue.
1 parent bef1d55 commit 69994ca

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/scheduler.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void rt_schedule(void)
351351
RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
352352

353353
rt_schedule_remove_thread(to_thread);
354-
to_thread->stat = RT_THREAD_RUNNING;
354+
to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
355355

356356
/* switch to new thread */
357357
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
@@ -412,6 +412,7 @@ void rt_schedule(void)
412412

413413
if (rt_thread_ready_priority_group != 0)
414414
{
415+
/* need_insert_from_thread: need to insert from_thread to ready queue */
415416
int need_insert_from_thread = 0;
416417

417418
to_thread = _get_highest_priority_thread(&highest_ready_priority);
@@ -443,7 +444,7 @@ void rt_schedule(void)
443444
}
444445

445446
rt_schedule_remove_thread(to_thread);
446-
to_thread->stat = RT_THREAD_RUNNING;
447+
to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
447448

448449
/* switch to new thread */
449450
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
@@ -485,7 +486,7 @@ void rt_schedule(void)
485486
else
486487
{
487488
rt_schedule_remove_thread(rt_current_thread);
488-
rt_current_thread->stat = RT_THREAD_RUNNING;
489+
rt_current_thread->stat = RT_THREAD_RUNNING | (rt_current_thread->stat & ~RT_THREAD_STAT_MASK);
489490
}
490491
}
491492
}
@@ -556,7 +557,7 @@ void rt_scheduler_do_irq_switch(void *context)
556557
RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (current_thread, to_thread));
557558

558559
rt_schedule_remove_thread(to_thread);
559-
to_thread->stat = RT_THREAD_RUNNING;
560+
to_thread->stat = RT_THREAD_RUNNING | (to_thread->stat & ~RT_THREAD_STAT_MASK);
560561

561562
#ifdef RT_USING_OVERFLOW_CHECK
562563
_rt_scheduler_stack_check(to_thread);
@@ -595,14 +596,16 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
595596
/* disable interrupt */
596597
level = rt_hw_interrupt_disable();
597598

598-
/* change stat */
599-
thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
600-
599+
/* it should be RUNNING thread */
601600
if (thread->oncpu != RT_CPU_DETACHED)
602601
{
602+
thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
603603
goto __exit;
604604
}
605605

606+
/* READY thread, insert to ready queue */
607+
thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
608+
606609
cpu_id = rt_hw_cpu_id();
607610
bind_cpu = thread->bind_cpu ;
608611

@@ -655,14 +658,15 @@ void rt_schedule_insert_thread(struct rt_thread *thread)
655658
/* disable interrupt */
656659
temp = rt_hw_interrupt_disable();
657660

658-
/* change stat */
659-
thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
660-
661+
/* it's current thread, it should be RUNNING thread */
661662
if (thread == rt_current_thread)
662663
{
664+
thread->stat = RT_THREAD_RUNNING | (thread->stat & ~RT_THREAD_STAT_MASK);
663665
goto __exit;
664666
}
665667

668+
/* READY thread, insert to ready queue */
669+
thread->stat = RT_THREAD_READY | (thread->stat & ~RT_THREAD_STAT_MASK);
666670
/* insert thread to ready list */
667671
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
668672
&(thread->tlist));

0 commit comments

Comments
 (0)