Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/engine/engineworkerscheduler.cpp

Go to the documentation of this file.
00001 // engineworkerscheduler.cpp
00002 // Created 6/2/2010 by RJ Ryan (rryan@mit.edu)
00003 
00004 #include <QtDebug>
00005 #include <QMutexLocker>
00006 
00007 #include "engine/engineworker.h"
00008 #include "engine/engineworkerscheduler.h"
00009 
00010 EngineWorkerScheduler::EngineWorkerScheduler(QObject* pParent)
00011         : m_scheduleFIFO(MAX_ENGINE_WORKERS),
00012           m_bQuit(false) {
00013     Q_UNUSED(pParent);
00014     m_workerThreadPool.setMaxThreadCount(ENGINE_WORKER_THREAD_COUNT);
00015     // A timeout of 1 minute for threads in the pool.
00016     m_workerThreadPool.setExpiryTimeout(60000);
00017 }
00018 
00019 EngineWorkerScheduler::~EngineWorkerScheduler() {
00020     m_bQuit = true;
00021     m_waitCondition.wakeAll();
00022     m_workerThreadPool.waitForDone();
00023 }
00024 
00025 void EngineWorkerScheduler::bindWorker(EngineWorker* pWorker) {
00026     connect(pWorker, SIGNAL(workReady(EngineWorker*)),
00027             this, SLOT(workerReady(EngineWorker*)),
00028             Qt::DirectConnection);
00029     connect(pWorker, SIGNAL(workStarting(EngineWorker*)),
00030             this, SLOT(workerStarted(EngineWorker*)),
00031             Qt::DirectConnection);
00032     connect(pWorker, SIGNAL(workDone(EngineWorker*)),
00033             this, SLOT(workerFinished(EngineWorker*)),
00034             Qt::DirectConnection);
00035 }
00036 
00037 void EngineWorkerScheduler::workerReady(EngineWorker* pWorker) {
00038     if (pWorker) {
00039         // If the write fails, we really can't do much since we should not block
00040         // in this slot. Write the address of the variable pWorker, since it is
00041         // a 1-element array.
00042         m_scheduleFIFO.write(&pWorker, 1);
00043     }
00044 }
00045 
00046 void EngineWorkerScheduler::workerStarted(EngineWorker* pWorker) {
00047 }
00048 
00049 void EngineWorkerScheduler::workerFinished(EngineWorker* pWorker) {
00050     QMutexLocker locker(&m_mutex);
00051     m_activeWorkers.remove(pWorker);
00052 }
00053 
00054 void EngineWorkerScheduler::runWorkers() {
00055     m_waitCondition.wakeAll();
00056 }
00057 
00058 void EngineWorkerScheduler::run() {
00059     m_mutex.lock();
00060     while (!m_bQuit) {
00061         EngineWorker* pWorker = NULL;
00062         while (m_scheduleFIFO.read(&pWorker, 1) == 1) {
00063             if (pWorker && !m_activeWorkers.contains(pWorker)) {
00064                 m_activeWorkers.insert(pWorker);
00065                 m_workerThreadPool.start(pWorker);
00066             }
00067         }
00068         m_waitCondition.wait(&m_mutex);
00069     }
00070 }
00071 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines