From 31b73f3fa2cdc1c9398fb9115f385f356a79b5a6 Mon Sep 17 00:00:00 2001 From: Marcus Tomlinson Date: Sun, 29 Oct 2023 14:19:48 +0000 Subject: [PATCH] Clean up to Circuit buffer count management --- src/Circuit.cpp | 68 ++++++++++++++++++---------------- tests/components/PassThrough.h | 7 +--- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/Circuit.cpp b/src/Circuit.cpp index 7d5db0ba..df8e2419 100644 --- a/src/Circuit.cpp +++ b/src/Circuit.cpp @@ -45,7 +45,9 @@ class Circuit { public: int pauseCount = 0; - int currentThreadNo = 0; + + int bufferCount = 0; + int currentBuffer = 0; AutoTickThread autoTickThread; @@ -80,7 +82,7 @@ bool Circuit::AddComponent( const Component::SPtr& component ) } // components within the circuit need to have as many buffers as there are threads in the circuit - component->SetBufferCount( (int)p->circuitThreads.size(), p->currentThreadNo ); + component->SetBufferCount( p->bufferCount, p->currentBuffer ); PauseAutoTick(); p->components.emplace_back( component ); @@ -188,50 +190,54 @@ void Circuit::DisconnectAllComponents() void Circuit::SetBufferCount( int bufferCount ) { - if ( (size_t)bufferCount != p->circuitThreads.size() ) + if ( p->bufferCount == bufferCount ) { - PauseAutoTick(); + return; + } - // stop all threads - for ( auto& circuitThread : p->circuitThreads ) - { - circuitThread.Stop(); - } + PauseAutoTick(); - // resize thread array - p->circuitThreads.resize( bufferCount ); + p->bufferCount = bufferCount; - // initialise and start all threads - for ( size_t i = 0; i < p->circuitThreads.size(); ++i ) - { - p->circuitThreads[i].Start( &p->components, (int)i ); - } + // stop all threads + for ( auto& circuitThread : p->circuitThreads ) + { + circuitThread.Stop(); + } - if ( p->currentThreadNo >= bufferCount ) - { - p->currentThreadNo = 0; - } + // resize thread array + p->circuitThreads.resize( p->bufferCount ); - // set all components to the new buffer count - for ( auto& component : p->components ) - { - component->SetBufferCount( bufferCount, p->currentThreadNo ); - } + // initialise and start all threads + for ( int i = 0; i < p->bufferCount; ++i ) + { + p->circuitThreads[i].Start( &p->components, i ); + } - ResumeAutoTick(); + if ( p->currentBuffer >= p->bufferCount ) + { + p->currentBuffer = 0; } + + // set all components to the new buffer count + for ( auto& component : p->components ) + { + component->SetBufferCount( p->bufferCount, p->currentBuffer ); + } + + ResumeAutoTick(); } int Circuit::GetBufferCount() const { - return (int)p->circuitThreads.size(); + return p->bufferCount; } void Circuit::Tick() { // process in a single thread if this circuit has no threads // ========================================================= - if ( p->circuitThreads.empty() ) + if ( p->bufferCount == 0 ) { // tick all internal components for ( auto& component : p->components ) @@ -249,11 +255,11 @@ void Circuit::Tick() // ======================================================= else { - p->circuitThreads[p->currentThreadNo].SyncAndResume(); // sync and resume thread x + p->circuitThreads[p->currentBuffer].SyncAndResume(); // sync and resume thread x - if ( ++p->currentThreadNo == (int)p->circuitThreads.size() ) + if ( ++p->currentBuffer == p->bufferCount ) { - p->currentThreadNo = 0; + p->currentBuffer = 0; } } } diff --git a/tests/components/PassThrough.h b/tests/components/PassThrough.h index b4acf1c6..32a6ac4d 100644 --- a/tests/components/PassThrough.h +++ b/tests/components/PassThrough.h @@ -16,12 +16,7 @@ class PassThrough : public Component protected: virtual void Process_( SignalBus& inputs, SignalBus& outputs ) override { - const auto* in = inputs.GetValue( 0 ); - if ( in ) - { - outputs.MoveSignal( 0, *inputs.GetSignal( 0 ) ); // pass the signal through (no copy) - } - // else set no output + outputs.MoveSignal( 0, *inputs.GetSignal( 0 ) ); // pass the signal through (no copy) } };