Ensure delayed flushes complete during shutdown

This commit is contained in:
Cameron Gutman 2022-10-25 02:43:55 -05:00
parent 5bfe56472f
commit a13f8e7937

View file

@ -188,6 +188,9 @@ ComputerManager::~ComputerManager()
// Wait for it to terminate (and finish any pending flush) // Wait for it to terminate (and finish any pending flush)
m_DelayedFlushThread->wait(); m_DelayedFlushThread->wait();
delete m_DelayedFlushThread; delete m_DelayedFlushThread;
// Delayed flushes should have completed by now
Q_ASSERT(!m_NeedsDelayedFlush);
} }
QWriteLocker lock(&m_Lock); QWriteLocker lock(&m_Lock);
@ -220,7 +223,7 @@ ComputerManager::~ComputerManager()
} }
void DelayedFlushThread::run() { void DelayedFlushThread::run() {
while (!QThread::currentThread()->isInterruptionRequested()) { for (;;) {
// Wait for a delayed flush request or an interruption // Wait for a delayed flush request or an interruption
{ {
QMutexLocker locker(&m_ComputerManager->m_DelayedFlushMutex); QMutexLocker locker(&m_ComputerManager->m_DelayedFlushMutex);
@ -229,13 +232,15 @@ void DelayedFlushThread::run() {
m_ComputerManager->m_DelayedFlushCondition.wait(&m_ComputerManager->m_DelayedFlushMutex); m_ComputerManager->m_DelayedFlushCondition.wait(&m_ComputerManager->m_DelayedFlushMutex);
} }
// Reset the delayed flush flag to ensure any racing saveHosts() call will set it again // Bail without flushing if we woke up for an interruption alone.
m_ComputerManager->m_NeedsDelayedFlush = false; // If we have both an interruption and a flush request, do the flush.
if (!m_ComputerManager->m_NeedsDelayedFlush) {
Q_ASSERT(QThread::currentThread()->isInterruptionRequested());
break;
} }
if (QThread::currentThread()->isInterruptionRequested()) { // Reset the delayed flush flag to ensure any racing saveHosts() call will set it again
// Bail without flushing if we woke up for an interruption m_ComputerManager->m_NeedsDelayedFlush = false;
break;
} }
// Perform the flush // Perform the flush