-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathContinueOnMainThread.cpp
More file actions
53 lines (45 loc) · 985 Bytes
/
Copy pathContinueOnMainThread.cpp
File metadata and controls
53 lines (45 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright © 2025 CCP ehf.
#include "StdAfx.h"
#include "ContinueOnMainThread.h"
#include <ScopedBlockTrap.h>
namespace
{
std::vector<std::function<void()>> mainThreadActions;
std::mutex mainThreadActionsMutex;
int invocations = 0;
}
void ContinueOnMainThread( std::function<void()>&& action )
{
std::lock_guard<std::mutex> lock( mainThreadActionsMutex );
mainThreadActions.push_back( std::move( action ) );
}
void ExecuteMainThreadActions()
{
TRINITY_STATS_ZONE( __FUNCTION__ );
static std::vector<std::function<void()>> actionsToProcess;
invocations++;
if( invocations > 1 )
{
return;
}
ON_BLOCK_EXIT( [] {
invocations = 0;
actionsToProcess.clear();
} );
while( invocations > 0 )
{
{
std::lock_guard<std::mutex> lock( mainThreadActionsMutex );
actionsToProcess.swap( mainThreadActions );
}
{
ScopedBlockTrap blockTrap;
for( auto& action : actionsToProcess )
{
action();
}
}
actionsToProcess.clear();
invocations--;
}
}