Skip to content

Commit

Permalink
Do not busy wait when writing buffer in mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Serrano Polo committed Jul 2, 2016
1 parent 46c2aa3 commit 8497d39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
3 changes: 2 additions & 1 deletion include/Mixer.h
Expand Up @@ -409,14 +409,15 @@ class EXPORT Mixer : public QObject
bool m_clearSignal;

bool m_changesSignal;
bool m_waitForMixer;
unsigned int m_changes;
QMutex m_changesMutex;
QMutex m_doChangesMutex;
QMutex m_waitChangesMutex;
QWaitCondition m_changesMixerCondition;
QWaitCondition m_changesRequestCondition;

bool m_waitingForWrite;

friend class LmmsCore;
friend class MixerWorkerThread;

Expand Down
12 changes: 0 additions & 12 deletions include/fifo_buffer.h
Expand Up @@ -57,18 +57,6 @@ class fifoBuffer
m_reader_sem.release();
}

bool tryWrite( T _element )
{
if( m_writer_sem.tryAcquire() )
{
m_buffer[m_writer_index++] = _element;
m_writer_index %= m_size;
m_reader_sem.release();
return true;
}
return false;
}

T read()
{
m_reader_sem.acquire();
Expand Down
31 changes: 15 additions & 16 deletions src/core/Mixer.cpp
Expand Up @@ -84,9 +84,9 @@ Mixer::Mixer( bool renderOnly ) :
m_metronomeActive(false),
m_clearSignal( false ),
m_changesSignal( false ),
m_waitForMixer( true ),
m_changes( 0 ),
m_doChangesMutex( QMutex::Recursive )
m_doChangesMutex( QMutex::Recursive ),
m_waitingForWrite( false )
{
for( int i = 0; i < 2; ++i )
{
Expand Down Expand Up @@ -739,14 +739,13 @@ void Mixer::requestChangeInModel()
m_changesMutex.unlock();

m_doChangesMutex.lock();
if ( m_isProcessing && m_waitForMixer )
m_waitChangesMutex.lock();
if ( m_isProcessing && !m_waitingForWrite && !m_changesSignal )
{
m_waitForMixer = false;
m_waitChangesMutex.lock();
m_changesSignal = true;
m_changesRequestCondition.wait( &m_waitChangesMutex );
m_waitChangesMutex.unlock();
}
m_waitChangesMutex.unlock();
}


Expand All @@ -763,7 +762,6 @@ void Mixer::doneChangeInModel()

if( !moreChanges )
{
m_waitForMixer = true;
m_changesSignal = false;
m_changesMixerCondition.wakeOne();
}
Expand Down Expand Up @@ -1077,15 +1075,16 @@ void Mixer::fifoWriter::run()

void Mixer::fifoWriter::write( surroundSampleFrame * buffer )
{
while( !m_fifo->tryWrite( buffer ) )
{
if( m_mixer->m_changesSignal )
{
m_mixer->runChangesInModel();
continue;
}
yieldCurrentThread();
}
m_mixer->m_waitChangesMutex.lock();
m_mixer->m_waitingForWrite = true;
m_mixer->m_waitChangesMutex.unlock();
m_mixer->runChangesInModel();

m_fifo->write( buffer );

m_mixer->m_doChangesMutex.lock();
m_mixer->m_waitingForWrite = false;
m_mixer->m_doChangesMutex.unlock();
}


Expand Down

0 comments on commit 8497d39

Please sign in to comment.