1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-01 08:29:27 +02:00

Condition variable needs the associated mutex locked only when waiting.

This commit is contained in:
Paul Licameli 2016-04-12 20:41:10 -04:00
parent 61177a15ad
commit da23526860

View File

@ -56,7 +56,7 @@ ODManager::ODManager()
{ {
mTerminate = false; mTerminate = false;
mTerminated = false; mTerminated = false;
mPause= gPause; mPause = gPause;
//must set up the queue condition //must set up the queue condition
mQueueNotEmptyCond = new ODCondition(&mQueueNotEmptyCondLock); mQueueNotEmptyCond = new ODCondition(&mQueueNotEmptyCondLock);
@ -86,11 +86,9 @@ void ODManager::AddTask(ODTask* task)
paused=mPause; paused=mPause;
mPauseLock.Unlock(); mPauseLock.Unlock();
mQueueNotEmptyCondLock.Lock();
//don't signal if we are paused since if we wake up the loop it will start processing other tasks while paused //don't signal if we are paused since if we wake up the loop it will start processing other tasks while paused
if(!paused) if(!paused)
mQueueNotEmptyCond->Signal(); mQueueNotEmptyCond->Signal();
mQueueNotEmptyCondLock.Unlock();
} }
void ODManager::SignalTaskQueueLoop() void ODManager::SignalTaskQueueLoop()
@ -100,11 +98,9 @@ void ODManager::SignalTaskQueueLoop()
mPauseLock.Lock(); mPauseLock.Lock();
paused=mPause; paused=mPause;
mPauseLock.Unlock(); mPauseLock.Unlock();
mQueueNotEmptyCondLock.Lock();
//don't signal if we are paused //don't signal if we are paused
if(!paused) if(!paused)
mQueueNotEmptyCond->Signal(); mQueueNotEmptyCond->Signal();
mQueueNotEmptyCondLock.Unlock();
} }
///removes a task from the active task queue ///removes a task from the active task queue
@ -280,10 +276,11 @@ void ODManager::Start()
// JKC: If there are no tasks ready to run, or we're paused then // JKC: If there are no tasks ready to run, or we're paused then
// we wait for there to be tasks in the queue. // we wait for there to be tasks in the queue.
mQueueNotEmptyCondLock.Lock(); {
if( (!tasksInArray) || paused) ODLocker locker{ mQueueNotEmptyCondLock };
mQueueNotEmptyCond->Wait(); if( (!tasksInArray) || paused)
mQueueNotEmptyCondLock.Unlock(); mQueueNotEmptyCond->Wait();
}
//if there is some ODTask running, then there will be something in the queue. If so then redraw to show progress //if there is some ODTask running, then there will be something in the queue. If so then redraw to show progress
mQueuesMutex.Lock(); mQueuesMutex.Lock();
@ -327,10 +324,9 @@ void ODManager::Pauser::Pause(bool pause)
pMan->mPause = pause; pMan->mPause = pause;
pMan->mPauseLock.Unlock(); pMan->mPauseLock.Unlock();
//we should check the queue again. if(!pause)
pMan->mQueueNotEmptyCondLock.Lock(); //we should check the queue again.
pMan->mQueueNotEmptyCond->Signal(); pMan->mQueueNotEmptyCond->Signal();
pMan->mQueueNotEmptyCondLock.Unlock();
} }
else else
{ {
@ -360,9 +356,7 @@ void ODManager::Quit()
wxThread::Sleep(200); wxThread::Sleep(200);
//signal the queue not empty condition since the ODMan thread will wait on the queue condition //signal the queue not empty condition since the ODMan thread will wait on the queue condition
pMan->mQueueNotEmptyCondLock.Lock();
pMan->mQueueNotEmptyCond->Signal(); pMan->mQueueNotEmptyCond->Signal();
pMan->mQueueNotEmptyCondLock.Unlock();
pMan->mTerminatedMutex.Lock(); pMan->mTerminatedMutex.Lock();
} }