-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPowerPlant.cpp
More file actions
114 lines (92 loc) · 3.99 KB
/
PowerPlant.cpp
File metadata and controls
114 lines (92 loc) · 3.99 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
/*
* MIT License
*
* Copyright (c) 2013 NUClear Contributors
*
* This file is part of the NUClear codebase.
* See https://github.com/Fastcode/NUClear for further info.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "PowerPlant.hpp"
#include <memory>
#include <utility>
#include "Configuration.hpp"
#include "Reactor.hpp"
#include "dsl/store/DataStore.hpp"
#include "dsl/word/Shutdown.hpp"
#include "dsl/word/Startup.hpp"
#include "dsl/word/emit/Inline.hpp"
#include "id.hpp"
#include "message/CommandLineArguments.hpp"
#include "threading/Reaction.hpp"
#include "threading/ReactionTask.hpp"
namespace NUClear {
namespace util {
struct GroupDescriptor;
struct ThreadPoolDescriptor;
} // namespace util
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
PowerPlant* PowerPlant::powerplant = nullptr;
// This is taking argc and argv as given by main so this should not take an array
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
PowerPlant::PowerPlant(Configuration config, int argc, const char* argv[])
: scheduler(config.default_pool_concurrency), logger(*this) {
// Stop people from making more then one powerplant
if (powerplant != nullptr) {
throw std::runtime_error("There is already a powerplant in existence (There should be a single PowerPlant)");
}
// Store our static variable
powerplant = this;
// Emit our arguments if any.
message::CommandLineArguments args;
for (int i = 0; i < argc; ++i) {
args.emplace_back(argv[i]);
}
// Emit the command line arguments so they are available for any With clauses.
emit(std::make_unique<message::CommandLineArguments>(args));
}
PowerPlant::~PowerPlant() {
// Make sure reactors are destroyed before anything else
while (!reactors.empty()) {
reactors.pop_back();
}
// Bye bye powerplant
powerplant = nullptr;
}
void PowerPlant::start() {
// Inline emit startup event and command line arguments
emit<dsl::word::emit::Inline>(std::make_unique<dsl::word::Startup>());
emit_shared<dsl::word::emit::Inline>(dsl::store::DataStore<message::CommandLineArguments>::get());
// Start all of the threads
scheduler.start();
}
void PowerPlant::add_idle_task(const std::shared_ptr<threading::Reaction>& reaction,
const std::shared_ptr<const util::ThreadPoolDescriptor>& pool_descriptor) {
scheduler.add_idle_task(reaction, pool_descriptor);
}
void PowerPlant::remove_idle_task(const NUClear::id_t& id,
const std::shared_ptr<const util::ThreadPoolDescriptor>& pool_descriptor) {
scheduler.remove_idle_task(id, pool_descriptor);
}
void PowerPlant::submit(std::unique_ptr<threading::ReactionTask>&& task) noexcept {
scheduler.submit(std::move(task));
}
void PowerPlant::shutdown(bool force) {
// Emit our shutdown event
emit(std::make_unique<dsl::word::Shutdown>());
// Shutdown the scheduler
scheduler.stop(force);
}
} // namespace NUClear