forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToposortEngine.h
More file actions
210 lines (162 loc) · 6.69 KB
/
ToposortEngine.h
File metadata and controls
210 lines (162 loc) · 6.69 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
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
// 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)
#ifndef REACT_DETAIL_ENGINE_TOPOSORTENGINE_H_INCLUDED
#define REACT_DETAIL_ENGINE_TOPOSORTENGINE_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <atomic>
#include <utility>
#include <type_traits>
#include <vector>
#include "tbb/concurrent_vector.h"
#include "tbb/spin_mutex.h"
#include "react/common/Containers.h"
#include "react/common/TopoQueue.h"
#include "react/common/Types.h"
#include "react/detail/EngineBase.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
namespace toposort {
using std::atomic;
using std::vector;
using tbb::concurrent_vector;
using tbb::spin_mutex;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Parameters
///////////////////////////////////////////////////////////////////////////////////////////////////
static const uint min_weight = 1;
static const uint grain_size = 100;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// SeqNode
///////////////////////////////////////////////////////////////////////////////////////////////////
class SeqNode : public IReactiveNode
{
public:
int Level { 0 };
int NewLevel { 0 };
bool Queued { false };
NodeVector<SeqNode> Successors;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ParNode
///////////////////////////////////////////////////////////////////////////////////////////////////
class ParNode : public IReactiveNode
{
public:
using InvalidateMutexT = spin_mutex;
int Level { 0 };
int NewLevel { 0 };
atomic<bool> Collected { false };
NodeVector<ParNode> Successors;
InvalidateMutexT InvalidateMutex;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ShiftRequestData
///////////////////////////////////////////////////////////////////////////////////////////////////
struct DynRequestData
{
bool ShouldAttach;
ParNode* Node;
ParNode* Parent;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ExclusiveSeqTurn
///////////////////////////////////////////////////////////////////////////////////////////////////
class SeqTurn : public TurnBase
{
public:
SeqTurn(TurnIdT id, TransactionFlagsT flags);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ExclusiveParTurn
///////////////////////////////////////////////////////////////////////////////////////////////////
class ParTurn : public TurnBase
{
public:
ParTurn(TurnIdT id, TransactionFlagsT flags);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Functors
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct GetLevelFunctor
{
int operator()(const T* x) const { return x->Level; }
};
template <typename T>
struct GetWeightFunctor
{
uint operator()(T* x) const { return x->IsHeavyweight() ? grain_size : 1; }
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode, typename TTurn>
class EngineBase : public IReactiveEngine<TNode,TTurn>
{
public:
void OnNodeAttach(TNode& node, TNode& parent);
void OnNodeDetach(TNode& node, TNode& parent);
void OnInputChange(TNode& node, TTurn& turn);
void OnNodePulse(TNode& node, TTurn& turn);
protected:
virtual void processChildren(TNode& node, TTurn& turn) = 0;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// SeqEngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
class SeqEngineBase : public EngineBase<SeqNode,SeqTurn>
{
public:
using TopoQueueT = TopoQueue<SeqNode*, GetLevelFunctor<SeqNode>>;
void Propagate(SeqTurn& turn);
void OnDynamicNodeAttach(SeqNode& node, SeqNode& parent, SeqTurn& turn);
void OnDynamicNodeDetach(SeqNode& node, SeqNode& parent, SeqTurn& turn);
private:
void invalidateSuccessors(SeqNode& node);
virtual void processChildren(SeqNode& node, SeqTurn& turn) override;
TopoQueueT scheduledNodes_;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ParEngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
class ParEngineBase : public EngineBase<ParNode,ParTurn>
{
public:
using DynRequestVectT = concurrent_vector<DynRequestData>;
using TopoQueueT = ConcurrentTopoQueue
<
ParNode*,
grain_size,
GetLevelFunctor<ParNode>,
GetWeightFunctor<ParNode>
>;
void Propagate(ParTurn& turn);
void OnDynamicNodeAttach(ParNode& node, ParNode& parent, ParTurn& turn);
void OnDynamicNodeDetach(ParNode& node, ParNode& parent, ParTurn& turn);
private:
void applyDynamicAttach(ParNode& node, ParNode& parent, ParTurn& turn);
void applyDynamicDetach(ParNode& node, ParNode& parent, ParTurn& turn);
void invalidateSuccessors(ParNode& node);
virtual void processChildren(ParNode& node, ParTurn& turn) override;
TopoQueueT topoQueue_;
DynRequestVectT dynRequests_;
};
} // ~namespace toposort
/****************************************/ REACT_IMPL_END /***************************************/
/*****************************************/ REACT_BEGIN /*****************************************/
template <REACT_IMPL::EPropagationMode>
class ToposortEngine;
template <> class ToposortEngine<REACT_IMPL::sequential_propagation> :
public REACT_IMPL::toposort::SeqEngineBase
{};
template <> class ToposortEngine<REACT_IMPL::parallel_propagation> :
public REACT_IMPL::toposort::ParEngineBase
{};
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <>
struct NodeUpdateTimerEnabled<ToposortEngine<parallel_propagation>> : std::true_type {};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_DETAIL_ENGINE_TOPOSORTENGINE_H_INCLUDED