FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp.react/src/engine/ToposortEngine.cpp at master · youngkan/cpp.react · GitHub
youngkan
/
cpp.react
Public
forked from
snakster/cpp.react
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp.react
/
src
/
engine
/
ToposortEngine.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
219 lines (184 loc) · 6.51 KB
Breadcrumbs
cpp.react
/
src
/
engine
/
ToposortEngine.cpp
Copy path
File metadata and controls
219 lines (184 loc) · 6.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
Copyright Sebastian Jeckel 2014.
//
Distributed under the Boost Software License, Version 1.0.
//
(See accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
#
include
"
react/engine/ToposortEngine.h
"
#
include
"
tbb/parallel_for.h
"
/*
*************************************
*/
REACT_IMPL_BEGIN
/*
************************************
*/
namespace
toposort
{
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ SeqTurn
//
/////////////////////////////////////////////////////////////////////////////////////////////////
SeqTurn::SeqTurn
(TurnIdT id, TransactionFlagsT flags) :
TurnBase
( id, flags )
{}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ ParTurn
//
/////////////////////////////////////////////////////////////////////////////////////////////////
ParTurn::ParTurn
(TurnIdT id, TransactionFlagsT flags) :
TurnBase
( id, flags )
{}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ EngineBase
//
/////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename
TNode,
typename
TTurn>
void
EngineBase<TNode,TTurn>::OnNodeAttach(TNode& node, TNode& parent)
{
parent.
Successors
.
Add
(node);
if
(node.
Level
<= parent.
Level
)
node.
Level
= parent.
Level
+
1
;
}
template
<
typename
TNode,
typename
TTurn>
void
EngineBase<TNode,TTurn>::OnNodeDetach(TNode& node, TNode& parent)
{
parent.
Successors
.
Remove
(node);
}
template
<
typename
TNode,
typename
TTurn>
void
EngineBase<TNode,TTurn>::OnInputChange(TNode& node, TTurn& turn)
{
processChildren
(node, turn);
}
template
<
typename
TNode,
typename
TTurn>
void
EngineBase<TNode,TTurn>::OnNodePulse(TNode& node, TTurn& turn)
{
processChildren
(node, turn);
}
//
Explicit instantiation
template
class
EngineBase
<SeqNode,SeqTurn>;
template
class
EngineBase
<ParNode,ParTurn>;
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ SeqEngineBase
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void
SeqEngineBase::Propagate
(SeqTurn& turn)
{
while
(scheduledNodes_.
FetchNext
())
{
for
(
auto
* curNode : scheduledNodes_.
NextValues
())
{
if
(curNode->
Level
< curNode->
NewLevel
)
{
curNode->
Level
= curNode->
NewLevel
;
invalidateSuccessors
(*curNode);
scheduledNodes_.
Push
(curNode);
continue
;
}
curNode->
Queued
=
false
;
curNode->
Tick
(&turn);
}
}
}
void
SeqEngineBase::OnDynamicNodeAttach
(SeqNode& node, SeqNode& parent, SeqTurn& turn)
{
this
->
OnNodeAttach
(node, parent);
invalidateSuccessors
(node);
//
Re-schedule this node
node.
Queued
=
true
;
scheduledNodes_.
Push
(&node);
}
void
SeqEngineBase::OnDynamicNodeDetach
(SeqNode& node, SeqNode& parent, SeqTurn& turn)
{
this
->
OnNodeDetach
(node, parent);
}
void
SeqEngineBase::processChildren
(SeqNode& node, SeqTurn& turn)
{
//
Add children to queue
for
(
auto
* succ : node.
Successors
)
{
if
(!succ->
Queued
)
{
succ->
Queued
=
true
;
scheduledNodes_.
Push
(succ);
}
}
}
void
SeqEngineBase::invalidateSuccessors
(SeqNode& node)
{
for
(
auto
* succ : node.
Successors
)
{
if
(succ->
NewLevel
<= node.
Level
)
succ->
NewLevel
= node.
Level
+
1
;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ ParEngineBase
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void
ParEngineBase::Propagate
(ParTurn& turn)
{
while
(topoQueue_.
FetchNext
())
{
//
using RangeT = tbb::blocked_range<vector<ParNode*>::const_iterator>;
using
RangeT = ParEngineBase::TopoQueueT::NextRangeT;
//
Iterate all nodes of current level and start processing them in parallel
tbb::parallel_for
(
topoQueue_.
NextRange
(),
[&] (
const
RangeT& range)
{
for
(
const
auto
& e : range)
{
auto
* curNode = e.
first
;
if
(curNode->
Level
< curNode->
NewLevel
)
{
curNode->
Level
= curNode->
NewLevel
;
invalidateSuccessors
(*curNode);
topoQueue_.
Push
(curNode);
continue
;
}
curNode->
Collected
=
false
;
//
Tick -> if changed: OnNodePulse -> adds child nodes to the queue
curNode->
Tick
(&turn);
}
}
);
if
(dynRequests_.
size
() >
0
)
{
for
(
auto
req : dynRequests_)
{
if
(req.
ShouldAttach
)
applyDynamicAttach
(*req.
Node
, *req.
Parent
, turn);
else
applyDynamicDetach
(*req.
Node
, *req.
Parent
, turn);
}
dynRequests_.
clear
();
}
}
}
void
ParEngineBase::OnDynamicNodeAttach
(ParNode& node, ParNode& parent, ParTurn& turn)
{
DynRequestData data{
true
, &node, &parent };
dynRequests_.
push_back
(data);
}
void
ParEngineBase::OnDynamicNodeDetach
(ParNode& node, ParNode& parent, ParTurn& turn)
{
DynRequestData data{
false
, &node, &parent };
dynRequests_.
push_back
(data);
}
void
ParEngineBase::applyDynamicAttach
(ParNode& node, ParNode& parent, ParTurn& turn)
{
this
->
OnNodeAttach
(node, parent);
invalidateSuccessors
(node);
//
Re-schedule this node
node.
Collected
=
true
;
topoQueue_.
Push
(&node);
}
void
ParEngineBase::applyDynamicDetach
(ParNode& node, ParNode& parent, ParTurn& turn)
{
this
->
OnNodeDetach
(node, parent);
}
void
ParEngineBase::processChildren
(ParNode& node, ParTurn& turn)
{
//
Add children to queue
for
(
auto
* succ : node.
Successors
)
if
(!succ->
Collected
.
exchange
(
true
, std::memory_order_relaxed))
topoQueue_.
Push
(succ);
}
void
ParEngineBase::invalidateSuccessors
(ParNode& node)
{
for
(
auto
* succ : node.
Successors
)
{
//
succ->InvalidateMutex
ParNode::InvalidateMutexT::scoped_lock
lock
(succ->
InvalidateMutex
);
if
(succ->
NewLevel
<= node.
Level
)
succ->
NewLevel
= node.
Level
+
1
;
}
//
~succ->InvalidateMutex
}
}
//
~namespace toposort
/*
**************************************
*/
REACT_IMPL_END
/*
*************************************
*/
Back
|
FazBrowse Home
|
New Git URL