@@ -122,6 +122,22 @@ class AudioGraphProcessor::Pimpl final : private AudioProcessor::Listener
122122 std::atomic<bool >& flag;
123123 };
124124
125+ struct ScopedProcessBlock
126+ {
127+ explicit ScopedProcessBlock (std::atomic<int >& counterIn) noexcept
128+ : counter (counterIn)
129+ {
130+ counter.fetch_add (1 , std::memory_order_acq_rel);
131+ }
132+
133+ ~ScopedProcessBlock ()
134+ {
135+ counter.fetch_sub (1 , std::memory_order_acq_rel);
136+ }
137+
138+ std::atomic<int >& counter;
139+ };
140+
125141 struct DelayLine
126142 {
127143 void initialise (GraphSignalType signalTypeIn, int numChannels, int delaySamplesIn, int maxBlockSize, size_t midiReserveBytes = 4096 )
@@ -301,7 +317,7 @@ class AudioGraphProcessor::Pimpl final : private AudioProcessor::Listener
301317
302318 delete pendingPlan.exchange (compiled.release ());
303319 hasPublishedPlan.store (true );
304- deleteRetiredPlans ();
320+ deleteRetiredPlansIfUnused ();
305321
306322 if (oldLatencySamples != newLatencySamples)
307323 owner.updateHostDisplay (AudioProcessor::ChangeDetails ().withLatencyChanged (true ));
@@ -400,6 +416,7 @@ class AudioGraphProcessor::Pimpl final : private AudioProcessor::Listener
400416 void processBlock (AudioBuffer<float >& audioBuffer, MidiBuffer& midiBuffer)
401417 {
402418 ScopedNoDenormals noDenormals;
419+ const ScopedProcessBlock scopedProcessBlock (activeProcessBlocks);
403420 swapPendingPlan ();
404421
405422 auto * graph = currentPlan;
@@ -1096,6 +1113,12 @@ class AudioGraphProcessor::Pimpl final : private AudioProcessor::Listener
10961113 }
10971114 }
10981115
1116+ void deleteRetiredPlansIfUnused ()
1117+ {
1118+ if (activeProcessBlocks.load (std::memory_order_acquire) == 0 )
1119+ deleteRetiredPlans ();
1120+ }
1121+
10991122 void resizeWorkers (int newNumThreads)
11001123 {
11011124 while (static_cast <int > (workers.size ()) > newNumThreads)
@@ -1190,6 +1213,7 @@ class AudioGraphProcessor::Pimpl final : private AudioProcessor::Listener
11901213 float lastCompiledSampleRate = 0 .0f ;
11911214 int lastCompiledMaxBlockSize = 0 ;
11921215 std::atomic<bool > hasPublishedPlan { false };
1216+ std::atomic<int > activeProcessBlocks { 0 };
11931217 std::atomic<CompiledGraph*> pendingPlan { nullptr };
11941218 std::atomic<CompiledGraph*> retiredPlans { nullptr };
11951219 CompiledGraph* currentPlan = nullptr ;
0 commit comments