forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPulseCountEngine.h
More file actions
157 lines (118 loc) · 4.74 KB
/
PulseCountEngine.h
File metadata and controls
157 lines (118 loc) · 4.74 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
// 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/task_group.h"
#include "tbb/spin_rw_mutex.h"
#include "tbb/task.h"
#include "react/common/Containers.h"
#include "react/common/Types.h"
#include "react/detail/EngineBase.h"
#include "react/detail/ReactiveDomain.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
namespace pulsecount {
using std::atomic;
using std::vector;
using tbb::task;
using tbb::empty_task;
using tbb::spin_rw_mutex;
using tbb::task_list;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Turn
///////////////////////////////////////////////////////////////////////////////////////////////////
struct Turn : public TurnBase
{
public:
Turn(TurnIdT id, TurnFlagsT flags);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Node
///////////////////////////////////////////////////////////////////////////////////////////////////
enum class ENodeMark
{
unmarked,
visited,
should_update
};
enum class ENodeState
{
unchanged,
changed,
deferred
};
class Node : public IReactiveNode
{
public:
using ShiftMutexT = spin_rw_mutex;
inline void IncCounter() { counter_.fetch_add(1, std::memory_order_relaxed); }
inline bool DecCounter() { return counter_.fetch_sub(1, std::memory_order_relaxed) > 1; }
inline void SetCounter(int c) { counter_.store(c, std::memory_order_relaxed); }
inline ENodeMark Mark() const
{
return mark_.load(std::memory_order_relaxed);
}
inline void SetMark(ENodeMark mark)
{
mark_.store(mark, std::memory_order_relaxed);
}
inline bool ExchangeMark(ENodeMark mark)
{
return mark_.exchange(mark, std::memory_order_relaxed) != mark;
}
ShiftMutexT ShiftMutex;
NodeVector<Node> Successors;
ENodeState State = ENodeState::unchanged;
uint Weight = 0;
private:
atomic<int> counter_ = 0;
atomic<ENodeMark> mark_ = ENodeMark::unmarked;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TTurn>
class EngineBase : public IReactiveEngine<Node,TTurn>
{
public:
using NodeShiftMutexT = Node::ShiftMutexT;
using NodeVectT = vector<Node*>;
void OnNodeAttach(Node& node, Node& parent);
void OnNodeDetach(Node& node, Node& parent);
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);
void HintUpdateDuration(Node& node, uint dur);
private:
NodeVectT changedInputs_;
empty_task* rootTask_ = new(task::allocate_root()) empty_task;
task_list spawnList_;
};
class BasicEngine : public EngineBase<Turn> {};
class QueuingEngine : public DefaultQueuingEngine<EngineBase,Turn> {};
} // ~namespace pulsecount
/****************************************/ REACT_IMPL_END /***************************************/
/*****************************************/ REACT_BEGIN /*****************************************/
struct parallel;
struct parallel_queue;
template <typename TMode>
class PulseCountEngine;
template <> class PulseCountEngine<parallel> :
public REACT_IMPL::pulsecount::BasicEngine {};
template <> class PulseCountEngine<parallel_queue> :
public REACT_IMPL::pulsecount::QueuingEngine {};
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <typename> struct EnableNodeUpdateTimer;
template <> struct EnableNodeUpdateTimer<PulseCountEngine<parallel>> : std::true_type {};
template <> struct EnableNodeUpdateTimer<PulseCountEngine<parallel_queue>> : std::true_type {};
template <typename> struct EnableParallelUpdating;
template <> struct EnableParallelUpdating<PulseCountEngine<parallel>> : std::true_type {};
template <> struct EnableParallelUpdating<PulseCountEngine<parallel_queue>> : std::true_type {};
/****************************************/ REACT_IMPL_END /***************************************/