Mixer/FxMixer: separated MixerWorkerThread and ThreadableJob into files

Declarations and implementations of MixerWorkerThread and ThreadableJob
have been moved into separate source files.

Furthermore there were some improvements to MixerWorkerThreads.
MixerWorkerThread::processJobQueue() does not return until the job
queue completely has been processed. This way each thread can "help"
to finish processing the queue and does not get back to sleep until
all of the work is done.

Management of the queue is now done via an array of QAtomicPointers.
Items that are non-NULL still need to be processed while NULL-items
were taken from the queue (i.e. in progress or done). Thus we do not
need to deal with ThreadableJob-states within MixerWorkerThread anymore.
This commit is contained in:
Tobias Doerffel
2009-10-14 01:22:31 +02:00
parent c4647a58ac
commit a9d24d34f2
9 changed files with 329 additions and 214 deletions

View File

@@ -22,10 +22,10 @@
*
*/
#include <QtXml/QDomElement>
#include "FxMixer.h"
#include "MixerWorkerThread.h"
#include "Cpu.h"
#include "Effect.h"
#include "song.h"
@@ -60,7 +60,7 @@ FxChannel::~FxChannel()
void FxChannel::doProcessing(sampleFrame * _buf)
void FxChannel::doProcessing( sampleFrame * _buf )
{
FxMixer * fxm = engine::fxMixer();
const fpp_t fpp = engine::getMixer()->framesPerPeriod();
@@ -97,7 +97,7 @@ void FxChannel::doProcessing(sampleFrame * _buf)
const float v = m_volumeModel.value();
m_fxChain.startRunning();
m_stillRunning = m_fxChain.processAudioBuffer( _buf, fpp);
m_stillRunning = m_fxChain.processAudioBuffer( _buf, fpp );
m_peakLeft = engine::getMixer()->peakValueLeft( _buf, fpp ) * v;
m_peakRight = engine::getMixer()->peakValueRight( _buf, fpp ) * v;
}
@@ -113,13 +113,13 @@ void FxChannel::doProcessing(sampleFrame * _buf)
{
// if parent.unstarted and every parent.leaf.done:
FxChannel * parent = fxm->effectChannel(m_sends[i]);
if( parent->m_state == ThreadableJob::Unstarted )
if( parent->state() == ThreadableJob::Unstarted )
{
bool everyLeafDone = true;
for( int j=0; j<parent->m_receives.size(); ++j )
{
if( fxm->effectChannel(parent->m_receives[j])->m_state !=
ThreadableJob::Done )
if( fxm->effectChannel( parent->m_receives[j] )->state() !=
ThreadableJob::Done )
{
everyLeafDone = false;
break;
@@ -435,15 +435,6 @@ void FxMixer::mixToChannel( const sampleFrame * _buf, fx_ch_t _ch )
void FxMixer::processChannel( fx_ch_t _ch, sampleFrame * _buf )
{
m_fxChannels[_ch]->process(_buf);
}
void FxMixer::prepareMasterMix()
{
engine::getMixer()->clearAudioBuffer( m_fxChannels[0]->m_buffer,
@@ -492,7 +483,7 @@ void FxMixer::masterMix( sampleFrame * _buf )
// to be processed.
MixerWorkerThread::resetJobQueue();
addChannelLeaf( 0, _buf );
while( m_fxChannels[0]->m_state != ThreadableJob::Done )
while( m_fxChannels[0]->state() != ThreadableJob::Done )
{
MixerWorkerThread::startAndWaitForJobs();
}

View File

@@ -0,0 +1,138 @@
/*
* MixerWorkerThread.cpp - implementation of MixerWorkerThread
*
* Copyright (c) 2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "MixerWorkerThread.h"
#include "Cpu.h"
#include "engine.h"
#include "mixer.h"
MixerWorkerThread::JobQueue MixerWorkerThread::s_jobQueue;
MixerWorkerThread::MixerWorkerThread( int _worker_num, mixer * _mixer ) :
QThread( _mixer ),
m_workingBuf( CPU::allocFrames( _mixer->framesPerPeriod() ) ),
m_workerNum( _worker_num ),
m_quit( false ),
m_mixer( _mixer ),
m_queueReadyWaitCond( &m_mixer->m_queueReadyWaitCond )
{
resetJobQueue();
}
MixerWorkerThread::~MixerWorkerThread()
{
CPU::freeFrames( m_workingBuf );
}
void MixerWorkerThread::quit()
{
m_quit = true;
}
void MixerWorkerThread::processJobQueue()
{
while( s_jobQueue.itemsDone != s_jobQueue.queueSize )
{
for( int i = 0; i < s_jobQueue.queueSize; ++i )
{
ThreadableJob * job =
s_jobQueue.items[i].fetchAndStoreOrdered( NULL );
if( job )
{
job->process( m_workingBuf );
s_jobQueue.itemsDone.fetchAndAddOrdered( 1 );
}
}
}
}
void MixerWorkerThread::resetJobQueue()
{
s_jobQueue.queueSize = 0;
s_jobQueue.itemsDone = 0;
}
void MixerWorkerThread::addJob( ThreadableJob * _job )
{
if( _job->requiresProcessing() )
{
// update job state
_job->queue();
// actually queue the job via atomic operations
s_jobQueue.items[s_jobQueue.queueSize.fetchAndAddOrdered(1)] = _job;
}
}
void MixerWorkerThread::startJobs()
{
// TODO: this is dirty!
engine::getMixer()->m_queueReadyWaitCond.wakeAll();
}
void MixerWorkerThread::waitForJobs()
{
// TODO: this is dirty!
mixer * m = engine::getMixer();
m->m_workers[m->m_numWorkers]->processJobQueue();
}
void MixerWorkerThread::run()
{
QMutex m;
while( m_quit == false )
{
m.lock();
m_queueReadyWaitCond->wait( &m );
processJobQueue();
m.unlock();
}
}

View File

@@ -26,6 +26,7 @@
#include "mixer.h"
#include "FxMixer.h"
#include "MixerWorkerThread.h"
#include "play_handle.h"
#include "song.h"
#include "templates.h"
@@ -61,13 +62,6 @@
#endif
MixerWorkerThread::JobQueue MixerWorkerThread::s_jobQueue;
mixer::mixer() :
m_framesPerPeriod( DEFAULT_BUFFER_SIZE ),
m_workingBuf( NULL ),