Skip to content

Commit

Permalink
Split the files that can into hpp and cpp files (#117)
Browse files Browse the repository at this point in the history
Originally these were put into header only to support having the nuclear
base clock change. Now with the time travel features this is not needed
and is detrimental to the ability to build the code
  • Loading branch information
TrentHouliston authored Aug 13, 2024
1 parent 55f8c2d commit e2dae5b
Show file tree
Hide file tree
Showing 80 changed files with 1,164 additions and 1,075 deletions.
10 changes: 5 additions & 5 deletions docs/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Bind

.. codeblock:: c++
template <typename DSL>
static inline void bind(const std::shared_ptr<threading::Reaction>& reaction, /*More arguments can be declared*/)
static void bind(const std::shared_ptr<threading::Reaction>& reaction, /*More arguments can be declared*/)

This function is called when the reaction is bound, it should be thought of as the constructor. It is used to setup
anything that is required by the DSL word.
Expand All @@ -68,7 +68,7 @@ Get

.. codeblock:: c++
template <typename DSL>
static inline T get(threading::Reaction&)
static T get(threading::Reaction& reaction)

This is used to get the data for the callback. The returned value is passed to the callback.

Expand All @@ -84,7 +84,7 @@ Precondition

.. codeblock:: c++
template <typename DSL>
static inline bool precondition(threading::Reaction& reaction)
static bool precondition(threading::Reaction& reaction)

A precondition is used to test if the reaction should run. On a true return the reaction will run as normal. On a false
return the reaction will be dropped.
Expand All @@ -103,7 +103,7 @@ Reschedule

.. codeblock:: c++
template <typename DSL>
static inline std::unique_ptr<threading::ReactionTask> reschedule(std::unique_ptr<threading::ReactionTask>&& task)
static std::unique_ptr<threading::Reaction> reschedule(std::unique_ptr<threading::Reaction>&& task)

The ownership of the reaction task is passed to the DSL word. The task returned will be run instead of the passed in
reaction task. If the returned task is the one passed in the task will be run normally.
Expand Down Expand Up @@ -167,7 +167,7 @@ Now we define the `reschedule` to interrupt any new tasks if we are currently ru
multithreaded so a mutex is needed when accessing the static members.
.. codeblock:: c++
template <typename DSL>
static inline std::unique_ptr<threading::ReactionTask> reschedule(
static std::unique_ptr<threading::ReactionTask> reschedule(
std::unique_ptr<threading::ReactionTask>&& task) {

// Lock our mutex
Expand Down
6 changes: 3 additions & 3 deletions docs/networking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ checks explicitly for an explicit type. Be careful about multiple declarations.
For this partial specialisation three static methods need to be defined.

.. codeblock:: c++
static inline std::vector<uint8_t> serialise(const T& in)
static std::vector<uint8_t> serialise(const T& in)

static inline T deserialise(const std::vector<uint8_t>& in)
static T deserialise(const std::vector<uint8_t>& in)

static inline uint64_t hash()
static uint64_t hash()
68 changes: 61 additions & 7 deletions src/PowerPlant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,49 @@

#include "PowerPlant.hpp"

#include <exception>
#include <tuple>

#include "Reactor.hpp"
#include "dsl/store/DataStore.hpp"
#include "dsl/word/Shutdown.hpp"
#include "dsl/word/Startup.hpp"
#include "dsl/word/emit/Direct.hpp"
#include "message/CommandLineArguments.hpp"
#include "message/LogMessage.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.thread_count) {

// 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 our command line arguments
emit(std::make_unique<message::CommandLineArguments>(args));
}

PowerPlant::~PowerPlant() {
// Make sure reactors are destroyed before anything else
while (!reactors.empty()) {
Expand All @@ -50,6 +88,16 @@ void PowerPlant::start() {
scheduler.start();
}

void PowerPlant::add_idle_task(const NUClear::id_t& id,
const util::ThreadPoolDescriptor& pool_descriptor,
std::function<void()>&& task) {
scheduler.add_idle_task(id, pool_descriptor, std::move(task));
}

void PowerPlant::remove_idle_task(const NUClear::id_t& id, const util::ThreadPoolDescriptor& pool_descriptor) {
scheduler.remove_idle_task(id, pool_descriptor);
}

void PowerPlant::submit(const NUClear::id_t& id,
const int& priority,
const util::GroupDescriptor& group,
Expand All @@ -75,14 +123,19 @@ void PowerPlant::submit(std::unique_ptr<threading::ReactionTask>&& task, const b
}
}

void PowerPlant::add_idle_task(const NUClear::id_t& id,
const util::ThreadPoolDescriptor& pool_descriptor,
std::function<void()>&& task) {
scheduler.add_idle_task(id, pool_descriptor, std::move(task));
}
void PowerPlant::log(const LogLevel& level, std::string message) {
// Get the current task
const auto* current_task = threading::ReactionTask::get_current_task();

void PowerPlant::remove_idle_task(const NUClear::id_t& id, const util::ThreadPoolDescriptor& pool_descriptor) {
scheduler.remove_idle_task(id, pool_descriptor);
// Direct emit the log message so that any direct loggers can use it
emit<dsl::word::emit::Direct>(std::make_unique<message::LogMessage>(
level,
current_task != nullptr ? current_task->parent.reactor.log_level : LogLevel::UNKNOWN,
std::move(message),
current_task != nullptr ? current_task->stats : nullptr));
}
void PowerPlant::log(const LogLevel& level, std::stringstream& message) {
log(level, message.str());
}

void PowerPlant::shutdown() {
Expand All @@ -101,4 +154,5 @@ void PowerPlant::shutdown() {
bool PowerPlant::running() const {
return is_running.load();
}

} // namespace NUClear
Loading

0 comments on commit e2dae5b

Please sign in to comment.