Skip to content

Commit

Permalink
code cleanup: use QMutexLocker::mutex() in QWaitCondition::wait()
Browse files Browse the repository at this point in the history
  • Loading branch information
devernay committed Dec 28, 2021
1 parent c5696f3 commit 7cb1459
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Engine/BlockingBackgroundRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ BlockingBackgroundRender::blockingRender(bool enableRenderStats,
_running = false;
} else {
while (_running) {
_runningCond.wait(&_runningMutex);
_runningCond.wait(locker.mutex());
}
}
}
Expand Down
42 changes: 29 additions & 13 deletions Engine/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class DeleterThread
_entriesQueueNotEmptyCond.wakeOne();
}
while (mustQuit) {
mustQuitCond.wait(&mustQuitMutex);
mustQuitCond.wait(k.mutex());
}
}

Expand Down Expand Up @@ -181,7 +181,7 @@ class DeleterThread
return;
}
while ( _entriesQueue.empty() ) {
_entriesQueueNotEmptyCond.wait(&_entriesQueueMutex);
_entriesQueueNotEmptyCond.wait(k.mutex());
}

assert( !_entriesQueue.empty() );
Expand Down Expand Up @@ -275,7 +275,7 @@ class CacheCleanerThread
_requestsQueueNotEmptyCond.wakeOne();
}
while (mustQuit) {
mustQuitCond.wait(&mustQuitMutex);
mustQuitCond.wait(k.mutex());
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ class CacheCleanerThread
return;
}
while ( _requestsQueues.empty() ) {
_requestsQueueNotEmptyCond.wait(&_requestQueueMutex);
_requestsQueueNotEmptyCond.wait(k.mutex());
}

assert( !_requestsQueues.empty() );
Expand Down Expand Up @@ -417,6 +417,10 @@ class Cache
{
return it->second;
}
static const std::list<CachedValue> & getValueFromIterator(ConstCacheIterator it)
{
return it->second;
}

#else // cache use STL

Expand All @@ -431,6 +435,10 @@ class Cache
{
return it->second;
}
static const std::list<EntryTypePtr> & getValueFromIterator(ConstCacheIterator it)
{
return it->second;
}

#endif // NATRON_CACHE_USE_BOOST

Expand All @@ -448,6 +456,10 @@ class Cache
{
return it->second;
}
static const std::list<EntryTypePtr> & getValueFromIterator(ConstCacheIterator it)
{
return it->second;
}

#else // cache use STL and tree (std map)

Expand All @@ -458,6 +470,10 @@ class Cache
{
return it->second.first;
}
static const std::list<EntryTypePtr> & getValueFromIterator(ConstCacheIterator it)
{
return it->second.first;
}

#endif // NATRON_CACHE_USE_BOOST

Expand Down Expand Up @@ -839,7 +855,7 @@ class Cache
//_memoryCacheSize member will get updated while images are being destroyed by the parallel thread.
//we wait for cache memory occupation to be < 100% to be sure we don't hit swap here
while ( occupationPercentage >= 1. && _deleterThread.isWorking() ) {
_memoryFullCondition.wait(&_sizeLock);
_memoryFullCondition.wait(k.mutex());
occupationPercentage = _maximumCacheSize == 0 ? 0.99 : (double)_memoryCacheSize / _maximumCacheSize;
}
}
Expand Down Expand Up @@ -1579,8 +1595,8 @@ class Cache
std::string holderID = holder->getCacheID();
QMutexLocker locker(&_lock);

for (CacheIterator memIt = _memoryCache.begin(); memIt != _memoryCache.end(); ++memIt) {
std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
for (ConstCacheIterator memIt = _memoryCache.begin(); memIt != _memoryCache.end(); ++memIt) {
const std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
if ( !entries.empty() ) {
const EntryTypePtr & front = entries.front();

Expand All @@ -1592,8 +1608,8 @@ class Cache
}
}

for (CacheIterator memIt = _diskCache.begin(); memIt != _diskCache.end(); ++memIt) {
std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
for (ConstCacheIterator memIt = _diskCache.begin(); memIt != _diskCache.end(); ++memIt) {
const std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
if ( !entries.empty() ) {
const EntryTypePtr & front = entries.front();

Expand All @@ -1617,8 +1633,8 @@ class Cache
{
QMutexLocker locker(&_lock);

for (CacheIterator memIt = _memoryCache.begin(); memIt != _memoryCache.end(); ++memIt) {
std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
for (ConstCacheIterator memIt = _memoryCache.begin(); memIt != _memoryCache.end(); ++memIt) {
const std::list<EntryTypePtr> & entries = getValueFromIterator(memIt);
if ( !entries.empty() ) {
const EntryTypePtr & front = entries.front();

Expand All @@ -1634,8 +1650,8 @@ class Cache
}
}

for (CacheIterator dIt = _diskCache.begin(); dIt != _diskCache.end(); ++dIt) {
std::list<EntryTypePtr> & entries = getValueFromIterator(dIt);
for (ConstCacheIterator dIt = _diskCache.begin(); dIt != _diskCache.end(); ++dIt) {
const std::list<EntryTypePtr> & entries = getValueFromIterator(dIt);
if ( !entries.empty() ) {
const EntryTypePtr & front = entries.front();

Expand Down
4 changes: 2 additions & 2 deletions Engine/CacheSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Cache<EntryType>::save(CacheTOC* tableOfContents)
{
QMutexLocker l(&_lock); // must be locked

for (CacheIterator it = _diskCache.begin(); it != _diskCache.end(); ++it) {
std::list<EntryTypePtr> & listOfValues = getValueFromIterator(it);
for (ConstCacheIterator it = _diskCache.begin(); it != _diskCache.end(); ++it) {
const std::list<EntryTypePtr> & listOfValues = getValueFromIterator(it);
for (typename std::list<EntryTypePtr>::const_iterator it2 = listOfValues.begin(); it2 != listOfValues.end(); ++it2) {
if ( (*it2)->isStoredOnDisk() ) {
SerializedEntry serialization;
Expand Down
2 changes: 1 addition & 1 deletion Engine/EffectInstancePrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ EffectInstance::Implementation::waitForImageBeingRenderedElsewhere(const RectI &

QMutexLocker kk(&ibr->lock);
while (!ab && isBeingRenderedElseWhere && !ibr->failed && ibr->refCount > 1) {
ibr->cond.wait(&ibr->lock, 50);
ibr->cond.wait(kk.mutex(), 50);
restToRender.clear();
isBeingRenderedElseWhere = false;
img->getRestToRender_trimap(roi, restToRender, &isBeingRenderedElseWhere);
Expand Down
2 changes: 1 addition & 1 deletion Engine/ExistenceCheckThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ExistenceCheckerThread::quitThread()
assert(!_imp->mustQuit);
_imp->mustQuit = true;
while (_imp->mustQuit) {
_imp->mustQuitCond.wait(&_imp->mustQuitMutex);
_imp->mustQuitCond.wait(k.mutex());
}
}
wait();
Expand Down
6 changes: 3 additions & 3 deletions Engine/FileSystemModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ FileGathererThread::abortGathering()
QMutexLocker k(&_imp->abortRequestsMutex);
++_imp->abortRequests;
while (_imp->abortRequests > 0) {
_imp->abortRequestsCond.wait(&_imp->abortRequestsMutex);
_imp->abortRequestsCond.wait(k.mutex());
}
}

Expand All @@ -1487,7 +1487,7 @@ FileGathererThread::quitGatherer()
{
QMutexLocker k(&_imp->mustQuitMutex);
while (_imp->mustQuit) {
_imp->mustQuitCond.wait(&_imp->mustQuitMutex);
_imp->mustQuitCond.wait(k.mutex());
}
}
}
Expand Down Expand Up @@ -1544,7 +1544,7 @@ FileGathererThread::run()
{
QMutexLocker l(&_imp->startCountMutex);
while (_imp->startCount <= 0) {
_imp->startCountCond.wait(&_imp->startCountMutex);
_imp->startCountCond.wait(l.mutex());
}
_imp->startCount = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/GPUContextPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ GPUContextPool::attachGLContextToRender(bool checkIfGLLoaded)

#ifndef NATRON_RENDER_SHARED_CONTEXT
while (_imp->glContextPool.empty() && (int)_imp->attachedGLContexts.size() >= maxContexts) {
_imp->glContextPoolEmpty.wait(&_imp->contextPoolMutex);
_imp->glContextPoolEmpty.wait(k.mutex());
}
if ( _imp->glContextPool.empty() ) {
assert( (int)_imp->attachedGLContexts.size() < maxContexts );
Expand Down
8 changes: 4 additions & 4 deletions Engine/GenericSchedulerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ GenericSchedulerThreadPrivate::waitForThreadsToQuit_internal(bool allowBlockingF
{
QMutexLocker k(&mustQuitMutex);
while (mustQuit) {
mustQuitCond.wait(&mustQuitMutex);
mustQuitCond.wait(k.mutex());
}
}

Expand Down Expand Up @@ -408,7 +408,7 @@ GenericSchedulerThreadPrivate::waitForAbortToComplete_internal(bool allowBlockin
QMutexLocker k(&abortRequestedMutex);

while (abortRequested > 0) {
abortRequestedCond.wait(&abortRequestedMutex);
abortRequestedCond.wait(k.mutex());
}
}
_p->onWaitForAbortCompleted();
Expand Down Expand Up @@ -575,7 +575,7 @@ GenericSchedulerThread::run()
{
QMutexLocker l(&_imp->startRequestsMutex);
while (_imp->startRequests <= 0) {
_imp->startRequestsCond.wait(&_imp->startRequestsMutex);
_imp->startRequestsCond.wait(l.mutex());
}
///We got the request
if (_imp->startRequests > 0) {
Expand All @@ -602,7 +602,7 @@ GenericSchedulerThread::requestExecutionOnMainThread(const GenericThreadExecOnMa
Q_EMIT executionOnMainThreadRequested(inArgs);

while (_imp->executingOnMainThread) {
_imp->executingOnMainThreadCond.wait(&_imp->executingOnMainThreadMutex);
_imp->executingOnMainThreadCond.wait(processLocker.mutex());
}
}

Expand Down
4 changes: 2 additions & 2 deletions Engine/GenericSchedulerThreadWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ GenericWatcher::stopWatching()
_imp->startRequestsCond.wakeOne();
}
while (_imp->mustQuit) {
_imp->mustQuitCond.wait(&_imp->mustQuitMutex);
_imp->mustQuitCond.wait(quitLocker.mutex());
}
}

Expand Down Expand Up @@ -160,7 +160,7 @@ GenericWatcher::run()
{
QMutexLocker l(&_imp->startRequestsMutex);
while (_imp->startRequests <= 0) {
_imp->startRequestsCond.wait(&_imp->startRequestsMutex);
_imp->startRequestsCond.wait(l.mutex());
}
///We got the request, reset it back to 0
_imp->startRequests = 0;
Expand Down
4 changes: 2 additions & 2 deletions Engine/HistogramCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ HistogramCPU::quitAnyComputation()
computeHistogram(0, ImagePtr(), RectI(), 0, 0, 0, 0);
l.relock();
while (_imp->mustQuit) {
_imp->mustQuitCond.wait(&_imp->mustQuitMutex);
_imp->mustQuitCond.wait(l.mutex());
}
}
}
Expand Down Expand Up @@ -392,7 +392,7 @@ HistogramCPU::run()
{
QMutexLocker l(&_imp->requestMutex);
while ( _imp->requests.empty() ) {
_imp->requestCond.wait(&_imp->requestMutex);
_imp->requestCond.wait(l.mutex());
}

///get the last request
Expand Down
6 changes: 3 additions & 3 deletions Engine/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ Node::Implementation::abortPreview_blocking(bool allowPreviewRenders)
assert(!mustQuitPreview);
++mustQuitPreview;
while (mustQuitPreview) {
mustQuitPreviewCond.wait(&mustQuitPreviewMutex);
mustQuitPreviewCond.wait(l.mutex());
}
}
}
Expand Down Expand Up @@ -4404,7 +4404,7 @@ Node::lock(const ImagePtr & image)
std::find(_imp->imagesBeingRendered.begin(), _imp->imagesBeingRendered.end(), image);

while ( it != _imp->imagesBeingRendered.end() ) {
_imp->imagesBeingRenderedCond.wait(&_imp->imagesBeingRenderedMutex);
_imp->imagesBeingRenderedCond.wait(l.mutex());
it = std::find(_imp->imagesBeingRendered.begin(), _imp->imagesBeingRendered.end(), image);
}
///Okay the image is not used by any other thread, claim that we want to use it
Expand Down Expand Up @@ -5666,7 +5666,7 @@ Node::setNodeIsRenderingInternal(std::list<NodeWPtr>& markedNodes)
if ( QThread::currentThread() != qApp->thread() ) {
QMutexLocker k(&_imp->nodeIsDequeuingMutex);
while ( _imp->nodeIsDequeuing && !aborted() ) {
_imp->nodeIsDequeuingCond.wait(&_imp->nodeIsDequeuingMutex);
_imp->nodeIsDequeuingCond.wait(k.mutex());
}
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/OSGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ OSGLContext::setContextCurrent(const AbortableRenderInfoPtr& abortInfo
#ifdef NATRON_RENDER_SHARED_CONTEXT
QMutexLocker k(&_imp->renderOwningContextMutex);
while (_imp->renderOwningContext && _imp->renderOwningContext != abortInfo) {
_imp->renderOwningContextCond.wait(&_imp->renderOwningContextMutex);
_imp->renderOwningContextCond.wait(k.mutex());
}
_imp->renderOwningContext = abortInfo;
#ifdef DEBUG
Expand Down
8 changes: 4 additions & 4 deletions Engine/OutputSchedulerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ OutputSchedulerThread::pickFrameToRender(RenderThreadTask* thread,
///Notify that we're no longer doing work
thread->notifyIsRunning(false);

_imp->framesToRenderNotEmptyCond.wait(&_imp->framesToRenderMutex);
_imp->framesToRenderNotEmptyCond.wait(l.mutex());
}

if ( !_imp->framesToRender.empty() ) {
Expand Down Expand Up @@ -1443,7 +1443,7 @@ OutputSchedulerThread::threadLoopOnce(const GenericThreadStartArgsPtr &inArgs)
assert(state == eThreadStateActive);
QMutexLocker bufLocker (&_imp->bufMutex);
// Wait here for more frames to be rendered, we will be woken up once appendToBuffer(...) is called
_imp->bufEmptyCondition.wait(&_imp->bufMutex);
_imp->bufEmptyCondition.wait(bufLocker.mutex());
} else {
if ( !_imp->engine->isPlaybackAutoRestartEnabled() ) {
//Move the timeline to the last rendered frame to keep it in sync with what is displayed
Expand Down Expand Up @@ -3512,7 +3512,7 @@ struct ViewerCurrentFrameRequestSchedulerPrivate
QMutexLocker k(&currentFrameRenderTasksMutex);

while ( !currentFrameRenderTasks.empty() ) {
currentFrameRenderTasksCond.wait(&currentFrameRenderTasksMutex);
currentFrameRenderTasksCond.wait(k.mutex());
}
}

Expand Down Expand Up @@ -3684,7 +3684,7 @@ ViewerCurrentFrameRequestScheduler::threadLoopOnce(const GenericThreadStartArgsP
//_imp->producedFrames.clear();
break;
}
_imp->producedFramesNotEmpty.wait(&_imp->producedFramesMutex);
_imp->producedFramesNotEmpty.wait(k.mutex());

for (ProducedFrameSet::iterator it = _imp->producedFrames.begin(); it != _imp->producedFrames.end(); ++it) {
if (it->age == args->age) {
Expand Down
1 change: 1 addition & 0 deletions Engine/ProcessHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ ProcessInputChannel::ProcessInputChannel(const QString & mainProcessServerName)
ProcessInputChannel::~ProcessInputChannel()
{
if ( isRunning() ) {
assert( !_mustQuitMutex.tryLock() );
assert(!_mustQuit);
_mustQuit = true;
while (_mustQuit) {
Expand Down
2 changes: 1 addition & 1 deletion Engine/RotoContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,7 +2146,7 @@ RotoContext::evaluateNeatStrokeRender()
{
QMutexLocker k(&_imp->doingNeatRenderMutex);
while (_imp->doingNeatRender) {
_imp->doingNeatRenderCond.wait(&_imp->doingNeatRenderMutex);
_imp->doingNeatRenderCond.wait(k.mutex());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/ViewerInstancePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ GCC_DIAG_SUGGEST_OVERRIDE_ON
std::find(textureBeingRendered.begin(), textureBeingRendered.end(), entry);

while ( it != textureBeingRendered.end() ) {
textureBeingRenderedCond.wait(&textureBeingRenderedMutex);
textureBeingRenderedCond.wait(l.mutex());
it = std::find(textureBeingRendered.begin(), textureBeingRendered.end(), entry);
}
///Okay the image is not used by any other thread, claim that we want to use it
Expand Down

0 comments on commit 7cb1459

Please sign in to comment.