From 3d170bf8b92087d74cbe1a5a3b7bb2bb9bb140e8 Mon Sep 17 00:00:00 2001 From: Daniel Thor Kristjansson Date: Wed, 11 Jan 2012 10:22:48 -0500 Subject: [PATCH] Avoid high CPU usage in DB logger when DB is unavailable at startup. Commit [aab13b3] fixed a logic typo in the loop exit clause, but did not make a symetric change in the sleep clause, so we sometimes enter a condition where the we were consuming 100% CPU waiting for the DB. --- mythtv/libs/libmythbase/logging.cpp | 16 ++++++---------- mythtv/libs/libmythbase/logging.h | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/mythtv/libs/libmythbase/logging.cpp b/mythtv/libs/libmythbase/logging.cpp index beb3d3441ca..21419eb8bb7 100644 --- a/mythtv/libs/libmythbase/logging.cpp +++ b/mythtv/libs/libmythbase/logging.cpp @@ -524,20 +524,16 @@ void DBLoggerThread::run(void) // Wait a bit before we start logging to the DB.. If we wait too long, // then short-running tasks (like mythpreviewgen) will not log to the db // at all, and that's undesirable. - bool ready = false; - while ( !aborted && ( !gCoreContext || !ready ) ) + while (true) { - ready = m_logger->isDatabaseReady(); + if ((aborted || (gCoreContext && m_logger->isDatabaseReady()))) + break; - // Don't delay if aborted was set while we were checking database ready - if (!ready && !aborted && !gCoreContext) - { - QMutexLocker qLock(&m_queueMutex); - m_wait->wait(qLock.mutex(), 100); - } + QMutexLocker locker(&m_queueMutex); + m_wait->wait(locker.mutex(), 100); } - if (ready) + if (!aborted) { // We want the query to be out of scope before the RunEpilog() so // shutdown occurs correctly as otherwise the connection appears still diff --git a/mythtv/libs/libmythbase/logging.h b/mythtv/libs/libmythbase/logging.h index 8d758de8e54..cb3e97a467f 100644 --- a/mythtv/libs/libmythbase/logging.h +++ b/mythtv/libs/libmythbase/logging.h @@ -134,7 +134,7 @@ class DBLoggerThread : public MThread QMutex m_queueMutex; QQueue *m_queue; QWaitCondition *m_wait; // protected by m_queueMutex - bool aborted; // protected by m_queueMutex + volatile bool aborted; // protected by m_queueMutex }; #endif