Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/library/searchthread.cpp

Go to the documentation of this file.
00001 // searchthread.cpp
00002 // Created 10/22/2009 by RJ Ryan (rryan@mit.edu)
00003 
00004 #include <QThread>
00005 #include <QMutexLocker>
00006 #include <QSet>
00007 #include <QtDebug>
00008 
00009 #include "library/searchthread.h"
00010 
00011 #include "library/trackmodel.h"
00012 
00013 SearchThread::SearchThread(QObject* parent)
00014         : QThread(parent),
00015           m_bQuit(false) {
00016     start();
00017 }
00018 
00019 SearchThread::~SearchThread() {
00020     stop();
00021     wait();
00022 }
00023 
00024 void SearchThread::enqueueSearch(TrackModel* pModel, QString search) {
00025     if (pModel == NULL)
00026         return;
00027 
00028     QMutexLocker lock(&m_mutex);
00029     m_searchQueue.enqueue(QPair<TrackModel*,QString>(pModel, search));
00030     m_waitCondition.wakeAll();
00031 }
00032 
00033 void SearchThread::stop() {
00034     QMutexLocker lock(&m_mutex);
00035     m_bQuit = true;
00036     m_waitCondition.wakeAll();
00037 }
00038 
00039 void SearchThread::run() {
00040     unsigned static id = 0; //the id of this thread, for debugging purposes //XXX copypasta (should factor this out somehow), -kousu 2/2009
00041     QThread::currentThread()->setObjectName(QString("SearchThread %1").arg(++id));
00042 
00043     while (!m_bQuit) {
00044         m_mutex.lock();
00045         if (m_searchQueue.isEmpty()) {
00046             m_waitCondition.wait(&m_mutex);
00047         }
00048         if (m_bQuit)
00049             break;
00050 
00051         QQueue<QPair<TrackModel*, QString> > searches;
00052         QSet<TrackModel*> processed;
00053         while (!m_searchQueue.isEmpty()) {
00054             QPair<TrackModel*, QString> pair = m_searchQueue.takeLast();
00055             // If we already executed a more recent search than this one, ignore
00056             // this one.
00057             if (processed.contains(pair.first))
00058                 continue;
00059             searches.enqueue(pair);
00060             processed.insert(pair.first);
00061 
00062         }
00063         // Don't need to hold the lock while doing each search.
00064         m_mutex.unlock();
00065 
00066         while (!searches.isEmpty()) {
00067             QPair<TrackModel*, QString> pair = searches.takeFirst();
00068             pair.first->search(pair.second);
00069         }
00070     }
00071     m_mutex.unlock();
00072 
00073 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines