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.laitinen@tii.ae>
This commit is contained in:
Jukka Laitinen 2025-07-21 22:46:42 +03:00 committed by Donny(董九柱)
parent 3ba6047e29
commit f55e6f6383

View file

@ -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--;
}
}
}