Merge pull request #8068 from heinrich5991/pr_ddnet_job_atomics

Fix race conditions in job state handling
This commit is contained in:
Dennis Felsing 2024-03-05 21:57:11 +00:00 committed by GitHub
commit b6689bc012
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -83,9 +83,8 @@ void CJobPool::RunLoop()
if(pJob) if(pJob)
{ {
// do the job if we have one IJob::EJobState OldStateQueued = IJob::STATE_QUEUED;
const IJob::EJobState OldStateQueued = pJob->m_State.exchange(IJob::STATE_RUNNING); if(!pJob->m_State.compare_exchange_strong(OldStateQueued, IJob::STATE_RUNNING))
if(OldStateQueued != IJob::STATE_QUEUED)
{ {
if(OldStateQueued == IJob::STATE_ABORTED) if(OldStateQueued == IJob::STATE_ABORTED)
{ {
@ -109,10 +108,13 @@ void CJobPool::RunLoop()
} }
// do not change state to done if job was not completed successfully // do not change state to done if job was not completed successfully
const IJob::EJobState OldStateRunning = pJob->m_State.exchange(IJob::STATE_DONE); IJob::EJobState OldStateRunning = IJob::STATE_RUNNING;
if(OldStateRunning != IJob::STATE_RUNNING) if(!pJob->m_State.compare_exchange_strong(OldStateRunning, IJob::STATE_DONE))
{ {
pJob->m_State = OldStateRunning; if(OldStateRunning != IJob::STATE_ABORTED)
{
dbg_assert(false, "Job state invalid, must be either running or aborted");
}
} }
} }
else if(m_Shutdown) else if(m_Shutdown)