forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathELMEngine.cpp
More file actions
169 lines (137 loc) · 4.44 KB
/
ELMEngine.cpp
File metadata and controls
169 lines (137 loc) · 4.44 KB
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
// 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/ELMEngine.h"
#include <cstdint>
#include "tbb/parallel_for.h"
#include "tbb/blocked_range.h"
#include "react/common/Types.h"
#include "react/common/GraphData.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
namespace elm {
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Turn
///////////////////////////////////////////////////////////////////////////////////////////////////
Turn::Turn(TurnIdT id, TurnFlagsT flags) :
TurnBase(id, flags)
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Node
///////////////////////////////////////////////////////////////////////////////////////////////////
Node::Node() :
Counter(0),
ShouldUpdate(false)
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TTurn>
void EngineBase<TTurn>::OnNodeCreate(Node& node)
{
if (node.IsInputNode())
inputNodes_.insert(&node);
}
template <typename TTurn>
void EngineBase<TTurn>::OnNodeDestroy(Node& node)
{
if (node.IsInputNode())
inputNodes_.erase(&node);
}
template <typename TTurn>
void EngineBase<TTurn>::OnNodeAttach(Node& node, Node& parent)
{
parent.Successors.Add(node);
}
template <typename TTurn>
void EngineBase<TTurn>::OnNodeDetach(Node& node, Node& parent)
{
parent.Successors.Remove(node);
}
template <typename TTurn>
void EngineBase<TTurn>::OnTurnInputChange(Node& node, TTurn& turn)
{
node.LastTurnId = turn.Id();
}
template <typename TTurn>
void EngineBase<TTurn>::OnTurnPropagate(TTurn& turn)
{
for (auto* node : inputNodes_)
nudgeChildren(*node, node->LastTurnId == turn.Id(), turn);
tasks_.wait();
}
template <typename TTurn>
void EngineBase<TTurn>::OnNodePulse(Node& node, TTurn& turn)
{
nudgeChildren(node, true, turn);
}
template <typename TTurn>
void EngineBase<TTurn>::OnNodeIdlePulse(Node& node, TTurn& turn)
{
nudgeChildren(node, false, turn);
}
template <typename TTurn>
void EngineBase<TTurn>::OnDynamicNodeAttach(Node& node, Node& parent, TTurn& turn)
{
bool shouldTick = false;
{// parent.ShiftMutex
NodeShiftMutexT::scoped_lock lock(parent.ShiftMutex);
parent.Successors.Add(node);
if (parent.LastTurnId == turn.Id())
{
shouldTick = true;
}
else
{
node.ShouldUpdate = true;
node.Counter.store(node.DependencyCount() - 1, std::memory_order_relaxed);
}
}// ~parent.ShiftMutex
if (shouldTick)
node.Tick(&turn);
}
template <typename TTurn>
void EngineBase<TTurn>::OnDynamicNodeDetach(Node& node, Node& parent, TTurn& turn)
{// parent.ShiftMutex
NodeShiftMutexT::scoped_lock lock(parent.ShiftMutex);
parent.Successors.Remove(node);
}// ~parent.ShiftMutex
template <typename TTurn>
void EngineBase<TTurn>::processChild(Node& node, bool update, TTurn& turn)
{
// Invalidated, this node has to be ticked
if (node.ShouldUpdate)
{
// Reset flag
node.ShouldUpdate = false;
node.Tick(&turn);
}
// No tick required
else
{
nudgeChildren(node, false, turn);
}
}
template <typename TTurn>
void EngineBase<TTurn>::nudgeChildren(Node& node, bool update, TTurn& turn)
{// node.ShiftMutex
NodeShiftMutexT::scoped_lock lock(node.ShiftMutex);
for (auto* succ : node.Successors)
{
if (update)
succ->ShouldUpdate = true;
// Delay tick?
if (succ->Counter.fetch_add(1, std::memory_order_relaxed) < succ->DependencyCount() - 1)
continue;
succ->Counter.store(0, std::memory_order_relaxed);
tasks_.run(std::bind(&EngineBase<TTurn>::processChild, this, std::ref(*succ), update, std::ref(turn)));
}
node.LastTurnId = turn.Id();
}// ~node.ShiftMutex
// Explicit instantiation
template class EngineBase<Turn>;
template class EngineBase<DefaultQueueableTurn<Turn>>;
} // ~namespace elm
/****************************************/ REACT_IMPL_END /***************************************/