Skip to content

Commit

Permalink
Merge pull request #1446 from daschuer/lp1740448
Browse files Browse the repository at this point in the history
Fix  "Loading Track .." forever
  • Loading branch information
uklotzde authored Mar 16, 2018
2 parents 18bbf6b + 343fb1f commit bd456c2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
21 changes: 15 additions & 6 deletions src/engine/engineworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include "engine/engineworkerscheduler.h"

EngineWorker::EngineWorker()
: m_pScheduler(NULL) {
: m_pScheduler(nullptr) {
m_notReady.test_and_set();
}

EngineWorker::~EngineWorker() {
Expand All @@ -15,13 +16,21 @@ void EngineWorker::run() {
}

void EngineWorker::setScheduler(EngineWorkerScheduler* pScheduler) {
DEBUG_ASSERT(m_pScheduler == nullptr);
m_pScheduler = pScheduler;
pScheduler->addWorker(this);
}

bool EngineWorker::workReady() {
if (m_pScheduler) {
m_pScheduler->workerReady(this);
return true;
void EngineWorker::workReady() {
m_notReady.clear();
VERIFY_OR_DEBUG_ASSERT(m_pScheduler) {
return;
}
m_pScheduler->workerReady();
}

void EngineWorker::wakeIfReady() {
if (!m_notReady.test_and_set()) {
m_semaRun.release();
}
return false;
}
9 changes: 4 additions & 5 deletions src/engine/engineworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#ifndef ENGINEWORKER_H
#define ENGINEWORKER_H

#include <QAtomicInt>
#include <atomic>
#include <QObject>
#include <QSemaphore>
#include <QThread>
Expand All @@ -25,16 +25,15 @@ class EngineWorker : public QThread {
virtual void run();

void setScheduler(EngineWorkerScheduler* pScheduler);
bool workReady();
void wake() {
m_semaRun.release();
}
void workReady();
void wakeIfReady();

protected:
QSemaphore m_semaRun;

private:
EngineWorkerScheduler* m_pScheduler;
std::atomic_flag m_notReady;
};

#endif /* ENGINEWORKER_H */
37 changes: 18 additions & 19 deletions src/engine/engineworkerscheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@

EngineWorkerScheduler::EngineWorkerScheduler(QObject* pParent)
: m_bWakeScheduler(false),
m_scheduleFIFO(MAX_ENGINE_WORKERS),
m_bQuit(false) {
Q_UNUSED(pParent);
}

EngineWorkerScheduler::~EngineWorkerScheduler() {
m_mutex.lock();
m_bQuit = true;
m_waitCondition.wakeAll();
m_mutex.unlock();
wait();
}

void EngineWorkerScheduler::workerReady(EngineWorker* pWorker) {
if (pWorker) {
// If the write fails, we really can't do much since we should not block
// in this slot. Write the address of the variable pWorker, since it is
// a 1-element array.
m_scheduleFIFO.write(&pWorker, 1);
m_bWakeScheduler = true;
}
void EngineWorkerScheduler::workerReady() {
m_bWakeScheduler = true;
}

void EngineWorkerScheduler::addWorker(EngineWorker* pWorker) {
DEBUG_ASSERT(pWorker);
QMutexLocker locker(&m_mutex);
m_workers.push_back(pWorker);
}

void EngineWorkerScheduler::runWorkers() {
Expand All @@ -45,17 +42,19 @@ void EngineWorkerScheduler::runWorkers() {
void EngineWorkerScheduler::run() {
while (!m_bQuit) {
Event::start("EngineWorkerScheduler");
EngineWorker* pWorker = NULL;
while (m_scheduleFIFO.read(&pWorker, 1) == 1) {
if (pWorker) {
pWorker->wake();
{
QMutexLocker lock(&m_mutex);
for(const auto& pWorker: m_workers) {
pWorker->wakeIfReady();
}
}
Event::end("EngineWorkerScheduler");
m_mutex.lock();
if (!m_bQuit) {
m_waitCondition.wait(&m_mutex); // unlock mutex and wait
{
QMutexLocker lock(&m_mutex);
if (!m_bQuit) {
// Wait for next runWorkers() call
m_waitCondition.wait(&m_mutex); // unlock mutex and wait
}
}
m_mutex.unlock();
}
}
6 changes: 4 additions & 2 deletions src/engine/engineworkerscheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class EngineWorkerScheduler : public QThread {
EngineWorkerScheduler(QObject* pParent=NULL);
virtual ~EngineWorkerScheduler();

void addWorker(EngineWorker* pWorker);
void runWorkers();
void workerReady(EngineWorker* worker);
void workerReady();

protected:
void run();
Expand All @@ -30,7 +31,8 @@ class EngineWorkerScheduler : public QThread {
// runWorkers was run. This should only be touched from the engine callback.
bool m_bWakeScheduler;

FIFO<EngineWorker*> m_scheduleFIFO;
std::vector<EngineWorker*> m_workers;

QWaitCondition m_waitCondition;
QMutex m_mutex;
volatile bool m_bQuit;
Expand Down
2 changes: 1 addition & 1 deletion src/mixer/basetrackplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void BaseTrackPlayerImpl::disconnectLoadedTrack() {
}

void BaseTrackPlayerImpl::slotLoadTrack(TrackPointer pNewTrack, bool bPlay) {
qDebug() << "BaseTrackPlayerImpl::slotLoadTrack";
qDebug() << "BaseTrackPlayerImpl::slotLoadTrack" << getGroup();
// Before loading the track, ensure we have access. This uses lazy
// evaluation to make sure track isn't NULL before we dereference it.
if (pNewTrack && !Sandbox::askForAccess(pNewTrack->getCanonicalLocation())) {
Expand Down
10 changes: 5 additions & 5 deletions src/widget/woverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ void WOverview::slotAnalyzerProgress(int progress) {
}

void WOverview::slotTrackLoaded(TrackPointer pTrack) {
if (m_pCurrentTrack == pTrack) {
m_trackLoaded = true;
update();
}
DEBUG_ASSERT(m_pCurrentTrack == pTrack);
m_trackLoaded = true;
update();
}

void WOverview::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {
//qDebug() << this << "WOverview::slotLoadingTrack" << pNewTrack << pOldTrack;
if (m_pCurrentTrack != nullptr && pOldTrack == m_pCurrentTrack) {
DEBUG_ASSERT(m_pCurrentTrack == pOldTrack);
if (m_pCurrentTrack != nullptr) {
disconnect(m_pCurrentTrack.get(), SIGNAL(waveformSummaryUpdated()),
this, SLOT(slotWaveformSummaryUpdated()));
disconnect(m_pCurrentTrack.get(), SIGNAL(analyzerProgress(int)),
Expand Down

0 comments on commit bd456c2

Please sign in to comment.