forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_manager_impl.cc
66 lines (56 loc) · 2.25 KB
/
init_manager_impl.cc
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
#include "server/init_manager_impl.h"
#include <functional>
#include "common/common/assert.h"
#define TRACE_INIT_MANAGER(fmt, ...) \
ENVOY_LOG(debug, "InitManagerImpl({}): " fmt, description_, ##__VA_ARGS__)
namespace Envoy {
namespace Server {
InitManagerImpl::InitManagerImpl(absl::string_view description) : description_(description) {
TRACE_INIT_MANAGER("constructor");
}
InitManagerImpl::~InitManagerImpl() { TRACE_INIT_MANAGER("destructor"); }
void InitManagerImpl::initialize(std::function<void()> callback) {
ASSERT(state_ == State::NotInitialized);
if (targets_.empty()) {
TRACE_INIT_MANAGER("empty targets, initialized");
callback();
state_ = State::Initialized;
} else {
TRACE_INIT_MANAGER("initializing");
callback_ = callback;
state_ = State::Initializing;
// Target::initialize(...) method can modify the list to remove the item currently
// being initialized, so we increment the iterator before calling initialize.
for (auto iter = targets_.begin(); iter != targets_.end();) {
TargetWithDescription& target = *iter;
++iter;
initializeTarget(target);
}
}
}
void InitManagerImpl::initializeTarget(TargetWithDescription& target) {
TRACE_INIT_MANAGER("invoking initializeTarget {}", target.second);
target.first->initialize([this, &target]() -> void {
TRACE_INIT_MANAGER("completed initializeTarget {}", target.second);
ASSERT(std::find(targets_.begin(), targets_.end(), target) != targets_.end());
targets_.remove(target);
if (targets_.empty()) {
TRACE_INIT_MANAGER("initialized");
state_ = State::Initialized;
callback_();
}
});
}
void InitManagerImpl::registerTarget(Init::Target& target, absl::string_view description) {
TRACE_INIT_MANAGER("registerTarget {}", description);
ASSERT(state_ != State::Initialized);
ASSERT(std::find(targets_.begin(), targets_.end(),
TargetWithDescription{&target, std::string(description)}) == targets_.end(),
"Registered duplicate Init::Target");
targets_.emplace_back(&target, std::string(description));
if (state_ == State::Initializing) {
initializeTarget(targets_.back());
}
}
} // namespace Server
} // namespace Envoy