forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceSetEngine.h
More file actions
161 lines (112 loc) · 4.52 KB
/
SourceSetEngine.h
File metadata and controls
161 lines (112 loc) · 4.52 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
// 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)
#pragma once
#include "react/detail/Defs.h"
#include <atomic>
#include <vector>
#include "tbb/queuing_mutex.h"
#include "tbb/spin_mutex.h"
#include "tbb/task_group.h"
#include "react/common/Containers.h"
#include "react/common/SourceIdSet.h"
#include "react/common/Types.h"
#include "react/detail/EngineBase.h"
#include "react/detail/ReactiveDomain.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
namespace sourceset {
class Node;
using std::atomic;
using std::vector;
using tbb::task_group;
using tbb::queuing_mutex;
using tbb::spin_mutex;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Turn
///////////////////////////////////////////////////////////////////////////////////////////////////
class Turn : public TurnBase
{
public:
using SourceIdSetT = SourceIdSet<ObjectId>;
Turn(TurnIdT id, TurnFlagsT flags);
void AddSourceId(ObjectId id);
SourceIdSetT& Sources() { return sources_; }
private:
SourceIdSetT sources_;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Node
///////////////////////////////////////////////////////////////////////////////////////////////////
class Node : public IReactiveNode
{
public:
using SourceIdSetT = Turn::SourceIdSetT;
using NudgeMutexT = queuing_mutex;
using ShiftMutexT = spin_mutex;
Node();
void AddSourceId(ObjectId id);
void AttachSuccessor(Node& node);
void DetachSuccessor(Node& node);
void DynamicAttachTo(Node& parent, Turn& turn);
void DynamicDetachFrom(Node& parent, Turn& turn);
void Destroy();
void Pulse(Turn& turn, bool updated);
bool IsDependency(Turn& turn);
bool CheckCurrentTurn(Turn& turn);
void Nudge(Turn& turn, bool update, bool invalidate);
void CheckForCycles(ObjectId startId) const;
private:
enum
{
kFlag_Visited = 1 << 0,
kFlag_Updated = 1 << 1,
kFlag_Invaliated = 1 << 2
};
NodeVector<Node> predecessors_;
NodeVector<Node> successors_;
SourceIdSetT sources_;
uint curTurnId_;
short tickThreshold_;
uchar flags_;
NudgeMutexT nudgeMutex_;
ShiftMutexT shiftMutex_;
void invalidateSources();
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TTurn>
class EngineBase : public IReactiveEngine<Node,TTurn>
{
public:
void OnNodeCreate(Node& node);
void OnNodeAttach(Node& node, Node& parent);
void OnNodeDetach(Node& node, Node& parent);
void OnNodeDestroy(Node& node);
void OnTurnInputChange(Node& node, TTurn& turn);
void OnTurnPropagate(TTurn& turn);
void OnNodePulse(Node& node, TTurn& turn);
void OnNodeIdlePulse(Node& node, TTurn& turn);
void OnDynamicNodeAttach(Node& node, Node& parent, TTurn& turn);
void OnDynamicNodeDetach(Node& node, Node& parent, TTurn& turn);
private:
vector<Node*> changedInputs_;
};
class BasicEngine : public EngineBase<Turn> {};
class QueuingEngine : public DefaultQueuingEngine<EngineBase,Turn> {};
} // ~namespace sourceset
/****************************************/ REACT_IMPL_END /***************************************/
/*****************************************/ REACT_BEGIN /*****************************************/
struct parallel;
struct parallel_queue;
template <typename TMode>
class SourceSetEngine;
template <> class SourceSetEngine<parallel> : public REACT_IMPL::sourceset::BasicEngine {};
template <> class SourceSetEngine<parallel_queue> : public REACT_IMPL::sourceset::QueuingEngine {};
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <typename> struct EnableParallelUpdating;
template <> struct EnableParallelUpdating<SourceSetEngine<parallel>> : std::true_type {};
template <> struct EnableParallelUpdating<SourceSetEngine<parallel_queue>> : std::true_type {};
/****************************************/ REACT_IMPL_END /***************************************/