diff --git a/trinity/ContinueOnMainThread.cpp b/trinity/ContinueOnMainThread.cpp index 64376c68..fc808491 100644 --- a/trinity/ContinueOnMainThread.cpp +++ b/trinity/ContinueOnMainThread.cpp @@ -2,11 +2,13 @@ #include "StdAfx.h" #include "ContinueOnMainThread.h" +#include namespace { std::vector> mainThreadActions; std::mutex mainThreadActionsMutex; +int invocations = 0; } void ContinueOnMainThread( std::function&& action ) @@ -20,13 +22,32 @@ void ExecuteMainThreadActions() CCP_STATS_ZONE( __FUNCTION__ ); static std::vector> actionsToProcess; + + invocations++; + if( invocations > 1 ) { - std::lock_guard lock( mainThreadActionsMutex ); - actionsToProcess.swap( mainThreadActions ); + return; } - for( auto& action : actionsToProcess ) + + ON_BLOCK_EXIT( [] { + invocations = 0; + actionsToProcess.clear(); + } ); + + while( invocations > 0 ) { - action(); + { + std::lock_guard lock( mainThreadActionsMutex ); + actionsToProcess.swap( mainThreadActions ); + } + { + ScopedBlockTrap blockTrap; + for( auto& action : actionsToProcess ) + { + action(); + } + } + actionsToProcess.clear(); + invocations--; } - actionsToProcess.clear(); }