@@ -810,44 +810,53 @@ class AudioGraphProcessor::Pimpl
810810 if (level.empty ())
811811 continue ;
812812
813- activeGraph = &graph;
814- activeLevel = &level;
815- activeNumSamples = numSamples;
813+ const auto generation = workGeneration.load (std::memory_order_relaxed) + 1 ;
814+
815+ activeGraph.store (&graph, std::memory_order_relaxed);
816+ activeLevel.store (&level, std::memory_order_relaxed);
817+ activeNumSamples.store (numSamples, std::memory_order_relaxed);
816818 nextJobIndex.store (0 , std::memory_order_relaxed);
817819 remainingJobs.store (static_cast <int > (level.size ()), std::memory_order_release);
818- workGeneration.fetch_add (1 , std::memory_order_release);
820+ activeGeneration.store (generation, std::memory_order_release);
821+ workGeneration.store (generation, std::memory_order_release);
819822 workerReadyEvent.reset ();
820823 workerReadyEvent.signal ();
821824
822- drainActiveJobs ( );
825+ drainActiveJobs (generation );
823826
824827 while (remainingJobs.load (std::memory_order_acquire) > 0 )
825828 ;
826829 }
827830
828- activeGraph = nullptr ;
829- activeLevel = nullptr ;
830- activeNumSamples = 0 ;
831+ activeGraph.store (nullptr , std::memory_order_relaxed);
832+ activeLevel.store (nullptr , std::memory_order_relaxed);
833+ activeNumSamples.store (0 , std::memory_order_relaxed);
834+ activeGeneration.store (0 , std::memory_order_release);
831835 }
832836
833- void drainActiveJobs ( )
837+ void drainActiveJobs ( int generation )
834838 {
835- auto * graph = activeGraph;
836- auto * level = activeLevel;
839+ if (activeGeneration.load (std::memory_order_acquire) != generation)
840+ return ;
841+
842+ auto * graph = activeGraph.load (std::memory_order_relaxed);
843+ auto * level = activeLevel.load (std::memory_order_relaxed);
837844
838845 if (graph == nullptr || level == nullptr )
839846 return ;
840847
848+ const int numSamples = activeNumSamples.load (std::memory_order_relaxed);
849+
841850 for (;;)
842851 {
843852 const int jobIndex = nextJobIndex.fetch_add (1 );
844853
845854 if (jobIndex >= static_cast <int > (level->size ()))
846855 break ;
847856
848- processNode (*graph, (*level)[static_cast <size_t > (jobIndex)], activeNumSamples );
857+ processNode (*graph, (*level)[static_cast <size_t > (jobIndex)], numSamples );
849858
850- remainingJobs.fetch_sub (1 , std::memory_order_release );
859+ remainingJobs.fetch_sub (1 , std::memory_order_acq_rel );
851860 }
852861 }
853862
@@ -1059,9 +1068,10 @@ class AudioGraphProcessor::Pimpl
10591068 std::atomic<int > workGeneration { 0 };
10601069 std::atomic<int > nextJobIndex { 0 };
10611070 std::atomic<int > remainingJobs { 0 };
1062- CompiledGraph* activeGraph = nullptr ;
1063- std::vector<int >* activeLevel = nullptr ;
1064- int activeNumSamples = 0 ;
1071+ std::atomic<CompiledGraph*> activeGraph { nullptr };
1072+ std::atomic<std::vector<int >*> activeLevel { nullptr };
1073+ std::atomic<int > activeNumSamples { 0 };
1074+ std::atomic<int > activeGeneration { 0 };
10651075};
10661076
10671077// ==============================================================================
@@ -1095,7 +1105,7 @@ void AudioGraphProcessor::Pimpl::WorkerThread::run()
10951105 lastGeneration = generation;
10961106 ScopedNoDenormals noDenormals;
10971107 owner.joinWorkgroup (workgroupToken);
1098- owner.drainActiveJobs ( );
1108+ owner.drainActiveJobs (generation );
10991109 }
11001110}
11011111
0 commit comments