-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cc
More file actions
131 lines (108 loc) · 3.25 KB
/
node.cc
File metadata and controls
131 lines (108 loc) · 3.25 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
// Copyright 2016 Dino Wernli. All Rights Reserved. See LICENSE for licensing terms.
#include "node.h"
#include <assert.h>
#include <set>
#include <sstream>
#include <string>
namespace ccproducers {
NodeBase::NodeBase(int id, std::string name, std::set<NodeBase*> deps) :
id_(id), name_(name), deps_(deps), rdeps_({}), state_(NodeState::BLOCKED), finished_deps_({}) {
for (const auto& dep : deps) {
dep->AddReverseDep(this);
}
}
void NodeBase::Run() {
assert(!IsDone());
assert(CanRun());
RunProducer();
SetFinished();
for (const auto& rdep : rdeps_) {
rdep->ReportFinished(this);
}
}
bool NodeBase::IsDone() const {
std::lock_guard<std::recursive_mutex> lock(state_lock_);
return (state_ == NodeState::FINISHED);
}
bool NodeBase::CanRun() const {
std::lock_guard<std::recursive_mutex> lock(finished_deps_lock_);
return (deps_.size() == finished_deps_.size());
}
bool NodeBase::TrySetRunning() {
std::lock_guard<std::recursive_mutex> lock(state_lock_);
if (state_ == NodeState::BLOCKED) {
state_ = NodeState::RUNNING;
return true;
} else {
return false;
}
}
void NodeBase::Start() {
if (!CanRun()) {
std::cout << DebugPrefix() << "Can't run yet, ignoring" << std::endl;
return;
}
bool running = TrySetRunning();
if (!running) {
std::cout << DebugPrefix() << "Called start but could not set to running" << std::endl;
return;
}
std::cout << DebugPrefix() << "Kicking off async producer run" << std::endl;
async_future_ = std::async(std::launch::async, &NodeBase::Run, this);
std::cout << DebugPrefix() << "Starting node finished" << std::endl;
}
void NodeBase::SetFinished() {
std::lock_guard<std::recursive_mutex> lock(state_lock_);
assert(state_ == NodeState::RUNNING);
state_ = NodeState::FINISHED;
std::cout << DebugPrefix() << "Finished" << std::endl;
}
void NodeBase::AddReverseDep(NodeBase* rdep) {
rdeps_.insert(rdep);
}
std::set<NodeBase*> NodeBase::TransitiveDeps() {
std::set<NodeBase*> result;
TransitiveDepsInternal(&result);
return std::move(result);
}
void NodeBase::ReportFinished(NodeBase* node) {
assert(deps_.find(node) != deps_.end());
std::cout << DebugPrefix() << "Got report of finished node: " << node->name() << std::endl;
std::lock_guard<std::recursive_mutex> lock(finished_deps_lock_);
finished_deps_.insert(node);
// This node could have become ready as a result of this call.
if (CanRun()) {
Start();
}
}
std::string NodeBase::DebugPrefix() const {
std::stringstream stream;
stream << "["
<< "node=" << name() << ", "
<< "state=" << DebugState() << ", "
<< "deps=" << deps_.size() << ", "
<< "finished_deps=" << finished_deps_.size()
<< "] ";
return stream.str();
}
std::string NodeBase::DebugState() const {
std::lock_guard<std::recursive_mutex> lock(state_lock_);
if (state_ == NodeState::BLOCKED) {
return "blocked";
} else if (state_ == NodeState::FINISHED) {
return "finished";
} else {
return "running";
}
}
void NodeBase::TransitiveDepsInternal(std::set<NodeBase*>* result) {
if (result->find(this) != result->end()) {
// Already visited. Abort.
return;
}
result->insert(this);
for (NodeBase* dep : deps_) {
dep->TransitiveDepsInternal(result);
}
}
} // namespace ccproducers