Skip to content

Commit

Permalink
Speeds up teardown of mythloggging threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-kristjansson committed Jul 23, 2011
1 parent e2e2d48 commit 7834f9e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
45 changes: 34 additions & 11 deletions mythtv/libs/libmythbase/mythlogging.cpp
@@ -1,5 +1,6 @@
#include <QMutex>
#include <QMutexLocker>
#include <QWaitCondition>
#include <QList>
#include <QQueue>
#include <QThread>
Expand Down Expand Up @@ -435,22 +436,30 @@ bool DatabaseLogger::logqmsg(LoggingItem_t *item)
return true;
}

DBLoggerThread::DBLoggerThread(DatabaseLogger *logger) :
m_logger(logger), m_queue(new QQueue<LoggingItem_t *>),
m_wait(new QWaitCondition()), aborted(false)
{
}

DBLoggerThread::~DBLoggerThread()
{
delete m_queue;
delete m_wait;
}

void DBLoggerThread::run(void)
{
threadRegister("DBLogger");
LoggingItem_t *item;

aborted = false;

QMutexLocker qLock(&m_queueMutex);

while(!aborted || !m_queue->isEmpty())
{
if (m_queue->isEmpty())
{
qLock.unlock();
msleep(100);
qLock.relock();
m_wait->wait(qLock.mutex(), 100);
continue;
}

Expand All @@ -464,7 +473,7 @@ void DBLoggerThread::run(void)
{
qLock.relock();
m_queue->prepend(item);
msleep(100);
m_wait->wait(qLock.mutex(), 100);
} else {
deleteItem(item);
qLock.relock();
Expand All @@ -475,6 +484,13 @@ void DBLoggerThread::run(void)
threadDeregister();
}

void DBLoggerThread::stop(void)
{
QMutexLocker qLock(&m_queueMutex);
aborted = true;
m_wait->wakeAll();
}

bool DatabaseLogger::isDatabaseReady()
{
bool ready = false;
Expand Down Expand Up @@ -583,7 +599,8 @@ void setThreadTid( LoggingItem_t *item )
}


LoggerThread::LoggerThread()
LoggerThread::LoggerThread() :
m_wait(new QWaitCondition()), aborted(false)
{
char *debug = getenv("VERBOSE_THREADS");
if (debug != NULL)
Expand All @@ -604,6 +621,8 @@ LoggerThread::~LoggerThread()
{
(*it)->deleteLater();
}

delete m_wait;
}

void LoggerThread::run(void)
Expand All @@ -612,17 +631,14 @@ void LoggerThread::run(void)
LoggingItem_t *item;

logThreadFinished = false;
aborted = false;

QMutexLocker qLock(&logQueueMutex);

while(!aborted || !logQueue.isEmpty())
{
if (logQueue.isEmpty())
{
qLock.unlock();
msleep(100);
qLock.relock();
m_wait->wait(qLock.mutex(), 100);
continue;
}

Expand Down Expand Up @@ -707,6 +723,13 @@ void LoggerThread::run(void)
logThreadFinished = true;
}

void LoggerThread::stop(void)
{
QMutexLocker qLock(&logQueueMutex);
aborted = true;
m_wait->wakeAll();
}

void deleteItem( LoggingItem_t *item )
{
if( !item )
Expand Down
16 changes: 9 additions & 7 deletions mythtv/libs/libmythbase/mythlogging.h
Expand Up @@ -198,16 +198,18 @@ class DatabaseLogger : public LoggerBase {
bool m_disabled;
};

class QWaitCondition;
class LoggerThread : public QThread {
Q_OBJECT

public:
LoggerThread();
~LoggerThread();
void run(void);
void stop(void) { aborted = true; };
void stop(void);
private:
bool aborted;
QWaitCondition *m_wait; // protected by logQueueMutex
bool aborted; // protected by logQueueMutex
};

#define MAX_QUEUE_LEN 1000
Expand All @@ -216,11 +218,10 @@ class DBLoggerThread : public QThread {
Q_OBJECT

public:
DBLoggerThread(DatabaseLogger *logger) : m_logger(logger),
m_queue(new QQueue<LoggingItem_t *>) {}
~DBLoggerThread() { delete m_queue; }
DBLoggerThread(DatabaseLogger *logger);
~DBLoggerThread();
void run(void);
void stop(void) { aborted = true; }
void stop(void);
bool enqueue(LoggingItem_t *item)
{
QMutexLocker qLock(&m_queueMutex);
Expand All @@ -236,7 +237,8 @@ class DBLoggerThread : public QThread {
DatabaseLogger *m_logger;
QMutex m_queueMutex;
QQueue<LoggingItem_t *> *m_queue;
bool aborted;
QWaitCondition *m_wait; // protected by m_queueMutex
bool aborted; // protected by m_queueMutex
};
#endif

Expand Down

0 comments on commit 7834f9e

Please sign in to comment.