Skip to content

Commit 4f674ef

Browse files
authored
Merge pull request #3786 from jesven/avoid_critical_deadlock
avoid deadlock (rt_hw_interrupt_disable and rt_enter_critical when en…
2 parents 18d1683 + 0e61949 commit 4f674ef

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/scheduler.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
#include <rtthread.h>
3434
#include <rthw.h>
3535

36-
#ifdef RT_USING_SMP
37-
rt_hw_spinlock_t _rt_critical_lock;
38-
#endif /*RT_USING_SMP*/
39-
4036
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
4137
rt_uint32_t rt_thread_ready_priority_group;
4238
#if RT_THREAD_PRIORITY_MAX > 32
@@ -851,20 +847,23 @@ void rt_enter_critical(void)
851847
if (!current_thread)
852848
{
853849
rt_hw_local_irq_enable(level);
854-
return ;
850+
return;
855851
}
856852

857853
/*
858854
* the maximal number of nest is RT_UINT16_MAX, which is big
859855
* enough and does not check here
860856
*/
861857

862-
/* lock scheduler for all cpus */
863-
if (current_thread->critical_lock_nest == 0)
864858
{
865-
rt_hw_spin_lock(&_rt_critical_lock);
859+
register rt_uint16_t lock_nest = current_thread->cpus_lock_nest;
860+
current_thread->cpus_lock_nest++;
861+
if (lock_nest == 0)
862+
{
863+
current_thread->scheduler_lock_nest ++;
864+
rt_hw_spin_lock(&_cpus_lock);
865+
}
866866
}
867-
868867
/* critical for local cpu */
869868
current_thread->critical_lock_nest ++;
870869

@@ -910,16 +909,18 @@ void rt_exit_critical(void)
910909
if (!current_thread)
911910
{
912911
rt_hw_local_irq_enable(level);
913-
return ;
912+
return;
914913
}
915914

916915
current_thread->scheduler_lock_nest --;
917916

918917
current_thread->critical_lock_nest --;
919918

920-
if (current_thread->critical_lock_nest == 0)
919+
current_thread->cpus_lock_nest--;
920+
if (current_thread->cpus_lock_nest == 0)
921921
{
922-
rt_hw_spin_unlock(&_rt_critical_lock);
922+
current_thread->scheduler_lock_nest --;
923+
rt_hw_spin_unlock(&_cpus_lock);
923924
}
924925

925926
if (current_thread->scheduler_lock_nest <= 0)

0 commit comments

Comments
 (0)