Skip to content

Commit

Permalink
Merge pull request LMMS#4335 from LMMS/fix/job-queue
Browse files Browse the repository at this point in the history
Fix job queue crash
  • Loading branch information
lukas-w authored May 2, 2018
2 parents 7ffd681 + bd608ed commit 12798cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions include/MixerWorkerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MixerWorkerThread : public QThread

JobQueue() :
m_items(),
m_queueSize( 0 ),
m_writeIndex( 0 ),
m_itemsDone( 0 ),
m_opMode( Static )
{
Expand All @@ -62,9 +62,9 @@ class MixerWorkerThread : public QThread
void wait();

private:
#define JOB_QUEUE_SIZE 1024
#define JOB_QUEUE_SIZE 8192
QAtomicPointer<ThreadableJob> m_items[JOB_QUEUE_SIZE];
AtomicInt m_queueSize;
AtomicInt m_writeIndex;
AtomicInt m_itemsDone;
OperationMode m_opMode;

Expand Down
17 changes: 12 additions & 5 deletions src/core/MixerWorkerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "MixerWorkerThread.h"

#include "denormals.h"
#include <QDebug>
#include <QMutex>
#include <QWaitCondition>
#include "ThreadableJob.h"
Expand All @@ -39,7 +40,7 @@ QList<MixerWorkerThread *> MixerWorkerThread::workerThreads;
// implementation of internal JobQueue
void MixerWorkerThread::JobQueue::reset( OperationMode _opMode )
{
m_queueSize = 0;
m_writeIndex = 0;
m_itemsDone = 0;
m_opMode = _opMode;
}
Expand All @@ -54,7 +55,13 @@ void MixerWorkerThread::JobQueue::addJob( ThreadableJob * _job )
// update job state
_job->queue();
// actually queue the job via atomic operations
m_items[m_queueSize.fetchAndAddOrdered(1)] = _job;
auto index = m_writeIndex.fetchAndAddOrdered(1);
if (index < JOB_QUEUE_SIZE) {
m_items[index] = _job;
} else {
qWarning() << "Job queue is full!";
m_itemsDone.fetchAndAddOrdered(1);
}
}
}

Expand All @@ -63,10 +70,10 @@ void MixerWorkerThread::JobQueue::addJob( ThreadableJob * _job )
void MixerWorkerThread::JobQueue::run()
{
bool processedJob = true;
while( processedJob && (int) m_itemsDone < (int) m_queueSize )
while( processedJob && (int) m_itemsDone < (int) m_writeIndex )
{
processedJob = false;
for( int i = 0; i < m_queueSize; ++i )
for( int i = 0; i < m_writeIndex && i < JOB_QUEUE_SIZE; ++i )
{
ThreadableJob * job = m_items[i].fetchAndStoreOrdered( NULL );
if( job )
Expand All @@ -86,7 +93,7 @@ void MixerWorkerThread::JobQueue::run()

void MixerWorkerThread::JobQueue::wait()
{
while( (int) m_itemsDone < (int) m_queueSize )
while( (int) m_itemsDone < (int) m_writeIndex )
{
#if defined(LMMS_HOST_X86) || defined(LMMS_HOST_X86_64)
asm( "pause" );
Expand Down

0 comments on commit 12798cf

Please sign in to comment.