Skip to content

Commit

Permalink
Change how extensions and task generation works to always be a task (#…
Browse files Browse the repository at this point in the history
…118)

Changes how extensions work so they always run on a task object rather
than on different types.
By changing the order of construction, all of the reactions can be given
full context.
  • Loading branch information
TrentHouliston authored Aug 14, 2024
1 parent e2dae5b commit f56d4eb
Show file tree
Hide file tree
Showing 37 changed files with 319 additions and 301 deletions.
16 changes: 12 additions & 4 deletions src/PowerPlant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "dsl/word/Shutdown.hpp"
#include "dsl/word/Startup.hpp"
#include "dsl/word/emit/Direct.hpp"
#include "extension/ChronoController.hpp"
#include "extension/IOController.hpp"
#include "extension/NetworkController.hpp"
#include "message/CommandLineArguments.hpp"
#include "message/LogMessage.hpp"
#include "threading/ReactionTask.hpp"
Expand All @@ -55,6 +58,11 @@ PowerPlant::PowerPlant(Configuration config, int argc, const char* argv[]) : sch
// Store our static variable
powerplant = this;

// Install the extension controllers
install<extension::ChronoController>();
install<extension::IOController>();
install<extension::NetworkController>();

// Emit our arguments if any.
message::CommandLineArguments args;
for (int i = 0; i < argc; ++i) {
Expand Down Expand Up @@ -112,13 +120,13 @@ void PowerPlant::submit(std::unique_ptr<threading::ReactionTask>&& task, const b
if (task) {
try {
const std::shared_ptr<threading::ReactionTask> t(std::move(task));
submit(t->id, t->priority, t->group_descriptor, t->thread_pool_descriptor, immediate, [t]() { t->run(); });
submit(t->id, t->priority, t->group_descriptor, t->pool_descriptor, immediate, [t]() { t->run(); });
}
catch (const std::exception& ex) {
task->parent.reactor.log<NUClear::ERROR>("There was an exception while submitting a reaction", ex.what());
task->parent->reactor.log<NUClear::ERROR>("There was an exception while submitting a reaction", ex.what());
}
catch (...) {
task->parent.reactor.log<NUClear::ERROR>("There was an unknown exception while submitting a reaction");
task->parent->reactor.log<NUClear::ERROR>("There was an unknown exception while submitting a reaction");
}
}
}
Expand All @@ -130,7 +138,7 @@ void PowerPlant::log(const LogLevel& level, std::string message) {
// 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,
current_task != nullptr ? current_task->parent->reactor.log_level : LogLevel::UNKNOWN,
std::move(message),
current_task != nullptr ? current_task->stats : nullptr));
}
Expand Down
26 changes: 13 additions & 13 deletions src/dsl/Parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,36 @@ namespace dsl {
return DSL::template bind<Parse<Sentence...>>(r, std::forward<Arguments>(args)...);
}

static auto get(threading::Reaction& r)
static auto get(threading::ReactionTask& task)
-> decltype(std::conditional_t<fusion::has_get<DSL>::value, DSL, fusion::NoOp>::template get<
Parse<Sentence...>>(r)) {
Parse<Sentence...>>(task)) {
return std::conditional_t<fusion::has_get<DSL>::value, DSL, fusion::NoOp>::template get<Parse<Sentence...>>(
r);
task);
}

static bool precondition(threading::Reaction& r) {
static bool precondition(threading::ReactionTask& task) {
return std::conditional_t<fusion::has_precondition<DSL>::value, DSL, fusion::NoOp>::template precondition<
Parse<Sentence...>>(r);
Parse<Sentence...>>(task);
}

static int priority(threading::Reaction& r) {
static int priority(threading::ReactionTask& task) {
return std::conditional_t<fusion::has_priority<DSL>::value, DSL, fusion::NoOp>::template priority<
Parse<Sentence...>>(r);
Parse<Sentence...>>(task);
}

static util::GroupDescriptor group(threading::Reaction& r) {
static util::GroupDescriptor group(threading::ReactionTask& task) {
return std::conditional_t<fusion::has_group<DSL>::value, DSL, fusion::NoOp>::template group<
Parse<Sentence...>>(r);
Parse<Sentence...>>(task);
}

static util::ThreadPoolDescriptor pool(threading::Reaction& r) {
static util::ThreadPoolDescriptor pool(threading::ReactionTask& task) {
return std::conditional_t<fusion::has_pool<DSL>::value, DSL, fusion::NoOp>::template pool<
Parse<Sentence...>>(r);
Parse<Sentence...>>(task);
}

static void postcondition(threading::ReactionTask& r) {
static void postcondition(threading::ReactionTask& task) {
std::conditional_t<fusion::has_postcondition<DSL>::value, DSL, fusion::NoOp>::template postcondition<
Parse<Sentence...>>(r);
Parse<Sentence...>>(task);
}
};

Expand Down
14 changes: 7 additions & 7 deletions src/dsl/fusion/GetFusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ namespace dsl {
*/
template <typename Function, typename DSL>
struct GetCaller {
static auto call(threading::Reaction& reaction) -> decltype(Function::template get<DSL>(reaction)) {
return Function::template get<DSL>(reaction);
static auto call(threading::ReactionTask& task) -> decltype(Function::template get<DSL>(task)) {
return Function::template get<DSL>(task);
}
};

Expand Down Expand Up @@ -86,19 +86,19 @@ namespace dsl {
struct GetFuser<std::tuple<Word1, WordN...>> {

template <typename DSL, typename U = Word1>
static auto get(threading::Reaction& reaction)
static auto get(threading::ReactionTask& task)
-> decltype(util::FunctionFusion<std::tuple<Word1, WordN...>,
decltype(std::forward_as_tuple(reaction)),
decltype(std::forward_as_tuple(task)),
GetCaller,
std::tuple<DSL>,
1>::call(reaction)) {
1>::call(task)) {

// Perform our function fusion
return util::FunctionFusion<std::tuple<Word1, WordN...>,
decltype(std::forward_as_tuple(reaction)),
decltype(std::forward_as_tuple(task)),
GetCaller,
std::tuple<DSL>,
1>::call(reaction);
1>::call(task);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/dsl/fusion/GroupFusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ namespace dsl {
struct GroupFuser<std::tuple<Word>> {

template <typename DSL>
static util::GroupDescriptor group(threading::Reaction& reaction) {
static util::GroupDescriptor group(threading::ReactionTask& task) {

// Return our group
return Word::template group<DSL>(reaction);
return Word::template group<DSL>(task);
}
};

Expand All @@ -89,7 +89,7 @@ namespace dsl {
struct GroupFuser<std::tuple<Word1, Word2, WordN...>> {

template <typename DSL>
static void group(const threading::Reaction& /*reaction*/) {
static void group(const threading::ReactionTask& /*task*/) {
throw std::invalid_argument("Can not be a member of more than one group");
}
};
Expand Down
22 changes: 11 additions & 11 deletions src/dsl/fusion/NoOp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ namespace dsl {
}

template <typename DSL>
static std::tuple<> get(const threading::Reaction& /*reaction*/) {
static std::tuple<> get(const threading::ReactionTask& /*task*/) {
return {};
}

template <typename DSL>
static bool precondition(const threading::Reaction& /*reaction*/) {
static bool precondition(const threading::ReactionTask& /*task*/) {
return true;
}

template <typename DSL>
static int priority(const threading::Reaction& /*reaction*/) {
static int priority(const threading::ReactionTask& /*task*/) {
return word::Priority::NORMAL::value;
}

template <typename DSL>
static util::GroupDescriptor group(const threading::Reaction& /*reaction*/) {
return util::GroupDescriptor{};
static util::GroupDescriptor group(const threading::ReactionTask& /*task*/) {
return {};
}

template <typename DSL>
static util::ThreadPoolDescriptor pool(const threading::Reaction& /*reaction*/) {
static util::ThreadPoolDescriptor pool(const threading::ReactionTask& /*task*/) {
return util::ThreadPoolDescriptor{};
}

Expand All @@ -86,15 +86,15 @@ namespace dsl {

static std::tuple<> bind(const std::shared_ptr<threading::Reaction>&);

static std::tuple<> get(threading::Reaction&);
static std::tuple<> get(threading::ReactionTask&);

static bool precondition(threading::Reaction&);
static bool precondition(threading::ReactionTask&);

static int priority(threading::Reaction&);
static int priority(threading::ReactionTask&);

static util::GroupDescriptor group(threading::Reaction&);
static util::GroupDescriptor group(threading::ReactionTask&);

static util::ThreadPoolDescriptor pool(threading::Reaction&);
static util::ThreadPoolDescriptor pool(threading::ReactionTask&);

static void postcondition(threading::ReactionTask&);
};
Expand Down
8 changes: 4 additions & 4 deletions src/dsl/fusion/PoolFusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <algorithm>
#include <stdexcept>

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "../operation/DSLProxy.hpp"
#include "has_pool.hpp"

Expand Down Expand Up @@ -77,10 +77,10 @@ namespace dsl {
struct PoolFuser<std::tuple<Word>> {

template <typename DSL>
static util::ThreadPoolDescriptor pool(threading::Reaction& reaction) {
static util::ThreadPoolDescriptor pool(threading::ReactionTask& task) {

// Return our pool
return Word::template pool<DSL>(reaction);
return Word::template pool<DSL>(task);
}
};

Expand All @@ -89,7 +89,7 @@ namespace dsl {
struct PoolFuser<std::tuple<Word1, Word2, WordN...>> {

template <typename DSL>
static util::ThreadPoolDescriptor pool(const threading::Reaction& /*reaction*/) {
static util::ThreadPoolDescriptor pool(const threading::ReactionTask& /*task*/) {
throw std::invalid_argument("Can not be a member of more than one pool");
}
};
Expand Down
12 changes: 6 additions & 6 deletions src/dsl/fusion/PreconditionFusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_PRECONDITION_FUSION_HPP
#define NUCLEAR_DSL_FUSION_PRECONDITION_FUSION_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "../operation/DSLProxy.hpp"
#include "has_precondition.hpp"

Expand Down Expand Up @@ -75,10 +75,10 @@ namespace dsl {
struct PreconditionFuser<std::tuple<Word>> {

template <typename DSL>
static bool precondition(threading::Reaction& reaction) {
static bool precondition(threading::ReactionTask& task) {

// Run our remaining precondition
return Word::template precondition<DSL>(reaction);
return Word::template precondition<DSL>(task);
}
};

Expand All @@ -87,11 +87,11 @@ namespace dsl {
struct PreconditionFuser<std::tuple<Word1, Word2, WordN...>> {

template <typename DSL>
static bool precondition(threading::Reaction& reaction) {
static bool precondition(threading::ReactionTask& task) {

// Perform a recursive and operation ending with the first false
return Word1::template precondition<DSL>(reaction)
&& PreconditionFuser<std::tuple<Word2, WordN...>>::template precondition<DSL>(reaction);
return Word1::template precondition<DSL>(task)
&& PreconditionFuser<std::tuple<Word2, WordN...>>::template precondition<DSL>(task);
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/dsl/fusion/PriorityFusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_PRIORITY_FUSION_HPP
#define NUCLEAR_DSL_FUSION_PRIORITY_FUSION_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "../operation/DSLProxy.hpp"
#include "has_priority.hpp"

Expand Down Expand Up @@ -74,10 +74,10 @@ namespace dsl {
struct PriorityFuser<std::tuple<Word>> {

template <typename DSL>
static int priority(threading::Reaction& reaction) {
static int priority(threading::ReactionTask& task) {

// Return our priority
return Word::template priority<DSL>(reaction);
return Word::template priority<DSL>(task);
}
};

Expand All @@ -86,11 +86,11 @@ namespace dsl {
struct PriorityFuser<std::tuple<Word1, Word2, WordN...>> {

template <typename DSL>
static int priority(threading::Reaction& reaction) {
static int priority(threading::ReactionTask& task) {

// Choose our maximum priority
return std::max(Word1::template priority<DSL>(reaction),
PriorityFuser<std::tuple<Word2, WordN...>>::template priority<DSL>(reaction));
return std::max(Word1::template priority<DSL>(task),
PriorityFuser<std::tuple<Word2, WordN...>>::template priority<DSL>(task));
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/dsl/fusion/has_get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_HAS_GET_HPP
#define NUCLEAR_DSL_FUSION_HAS_GET_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "NoOp.hpp"

namespace NUClear {
Expand All @@ -42,7 +42,8 @@ namespace dsl {
using no = std::false_type;

template <typename U>
static auto test(int) -> decltype(U::template get<ParsedNoOp>(std::declval<threading::Reaction&>()), yes());
static auto test(int) -> decltype(U::template get<ParsedNoOp>(std::declval<threading::ReactionTask&>()),
yes());
template <typename>
static no test(...);

Expand Down
4 changes: 2 additions & 2 deletions src/dsl/fusion/has_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_HAS_GROUP_HPP
#define NUCLEAR_DSL_FUSION_HAS_GROUP_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "NoOp.hpp"

namespace NUClear {
Expand All @@ -42,7 +42,7 @@ namespace dsl {
using no = std::false_type;

template <typename U>
static auto test(int) -> decltype(U::template group<ParsedNoOp>(std::declval<threading::Reaction&>()),
static auto test(int) -> decltype(U::template group<ParsedNoOp>(std::declval<threading::ReactionTask&>()),
yes());
template <typename>
static no test(...);
Expand Down
4 changes: 2 additions & 2 deletions src/dsl/fusion/has_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_HAS_POOL_HPP
#define NUCLEAR_DSL_FUSION_HAS_POOL_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "NoOp.hpp"

namespace NUClear {
Expand All @@ -42,7 +42,7 @@ namespace dsl {
using no = std::false_type;

template <typename U>
static auto test(int) -> decltype(U::template pool<ParsedNoOp>(std::declval<threading::Reaction&>()),
static auto test(int) -> decltype(U::template pool<ParsedNoOp>(std::declval<threading::ReactionTask&>()),
yes());
template <typename>
static no test(...);
Expand Down
4 changes: 2 additions & 2 deletions src/dsl/fusion/has_precondition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef NUCLEAR_DSL_FUSION_HAS_PRECONDITION_HPP
#define NUCLEAR_DSL_FUSION_HAS_PRECONDITION_HPP

#include "../../threading/Reaction.hpp"
#include "../../threading/ReactionTask.hpp"
#include "NoOp.hpp"

namespace NUClear {
Expand All @@ -43,7 +43,7 @@ namespace dsl {

template <typename U>
static auto test(int)
-> decltype(U::template precondition<ParsedNoOp>(std::declval<threading::Reaction&>()), yes());
-> decltype(U::template precondition<ParsedNoOp>(std::declval<threading::ReactionTask&>()), yes());
template <typename>
static no test(...);

Expand Down
Loading

0 comments on commit f56d4eb

Please sign in to comment.