This is an automated email from the ASF dual-hosted git repository. jiuzhudong pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit f55e6f63838a7b2381ebeeb4d9a9077f09890e2e Author: Jukka Laitinen <jukka.laiti...@tii.ae> AuthorDate: Mon Jul 21 22:46:42 2025 +0300 sched: Remove race condition in sched_unlock When exiting schedlock, that task should first take the critical section and only after that decrease the lockcount to 0. Otherwise an interrupt might cause a re-schedule before the task enters the critical section, which makes the following code meaningless. Signed-off-by: Jukka Laitinen <jukka.laiti...@tii.ae> --- sched/sched/sched_unlock.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sched/sched/sched_unlock.c b/sched/sched/sched_unlock.c index 4c7d6814542..dabd07cbc80 100644 --- a/sched/sched/sched_unlock.c +++ b/sched/sched/sched_unlock.c @@ -69,10 +69,12 @@ void sched_unlock(void) * then pre-emption has been re-enabled. */ - if (rtcb != NULL && --rtcb->lockcount == 0) + if (rtcb != NULL && rtcb->lockcount == 1) { irqstate_t flags = enter_critical_section_wo_note(); + rtcb->lockcount = 0; + /* Note that we no longer have pre-emption disabled. */ nxsched_critmon_preemption(rtcb, false, return_address(0)); @@ -164,5 +166,9 @@ void sched_unlock(void) leave_critical_section_wo_note(flags); } + else + { + rtcb->lockcount--; + } } }