Skip to content

Commit

Permalink
Clean up to Circuit buffer count management
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusTomlinson committed Oct 29, 2023
1 parent beae17e commit 31b73f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
68 changes: 37 additions & 31 deletions src/Circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Circuit
{
public:
int pauseCount = 0;
int currentThreadNo = 0;

int bufferCount = 0;
int currentBuffer = 0;

AutoTickThread autoTickThread;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions tests/components/PassThrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ class PassThrough : public Component
protected:
virtual void Process_( SignalBus& inputs, SignalBus& outputs ) override
{
const auto* in = inputs.GetValue<int>( 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)
}
};

Expand Down

0 comments on commit 31b73f3

Please sign in to comment.