Skip to content

Commit e80cd36

Browse files
authored
Merge pull request #42 from lf-lang/input-actions
Keep track of input actions in the environment
2 parents e5d7cfd + f7249af commit e80cd36

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

examples/count/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Count : public Reactor {
3737
};
3838

3939
auto main() -> int {
40-
Environment env{4, true};
40+
Environment env{4};
4141

4242
Count count{&env};
4343
env.assemble();

examples/hello/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Hello : public Reactor {
3131
};
3232

3333
auto main() -> int {
34-
Environment env{4, false, false, 5s};
34+
Environment env{4, false, 5s};
3535

3636
Hello hello{&env};
3737
env.assemble();

include/reactor-cpp/action.hh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef REACTOR_CPP_ACTION_HH
1010
#define REACTOR_CPP_ACTION_HH
1111

12+
#include "environment.hh"
13+
#include "fwd.hh"
1214
#include "logical_time.hh"
1315
#include "reactor.hh"
1416
#include "value_ptr.hh"
@@ -37,6 +39,12 @@ protected:
3739
: ReactorElement(name, ReactorElement::Type::Action, container)
3840
, min_delay_(min_delay)
3941
, logical_(logical) {}
42+
BaseAction(const std::string& name, Environment* environment, bool logical, Duration min_delay)
43+
: ReactorElement(name, ReactorElement::Type::Action, environment)
44+
, min_delay_(min_delay)
45+
, logical_(logical) {
46+
environment->register_input_action(this);
47+
}
4048

4149
public:
4250
[[nodiscard]] auto inline triggers() const noexcept -> const auto& { return triggers_; }
@@ -63,6 +71,8 @@ protected:
6371

6472
Action(const std::string& name, Reactor* container, bool logical, Duration min_delay)
6573
: BaseAction(name, container, logical, min_delay) {}
74+
Action(const std::string& name, Environment* environment, bool logical, Duration min_delay)
75+
: BaseAction(name, environment, logical, min_delay) {}
6676

6777
public:
6878
// Normally, we should lock the mutex while moving to make this
@@ -107,6 +117,8 @@ template <> class Action<void> : public BaseAction {
107117
protected:
108118
Action(const std::string& name, Reactor* container, bool logical, Duration min_delay)
109119
: BaseAction(name, container, logical, min_delay) {}
120+
Action(const std::string& name, Environment* environment, bool logical, Duration min_delay)
121+
: BaseAction(name, environment, logical, min_delay) {}
110122

111123
public:
112124
template <class Dur = Duration> void schedule(Dur delay = Dur::zero());
@@ -118,7 +130,11 @@ public:
118130
template <class T> class PhysicalAction : public Action<T> {
119131
public:
120132
PhysicalAction(const std::string& name, Reactor* container)
121-
: Action<T>(name, container, false, Duration::zero()) {}
133+
: Action<T>(name, container, false, Duration::zero()) {
134+
// all physical actions act as input actions to the program as they can be
135+
// scheduled from external threads
136+
container->environment()->register_input_action(this);
137+
}
122138
};
123139

124140
template <class T> class LogicalAction : public Action<T> {

include/reactor-cpp/environment.hh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string>
1414
#include <vector>
1515

16+
#include "fwd.hh"
1617
#include "reactor-cpp/logging.hh"
1718
#include "reactor-cpp/time.hh"
1819
#include "reactor.hh"
@@ -36,10 +37,12 @@ private:
3637
const log::NamedLogger log_;
3738
const unsigned int num_workers_{default_number_worker};
3839
unsigned int max_reaction_index_{default_max_reaction_index};
39-
const bool run_forever_{default_run_forever};
40+
bool run_forever_{default_run_forever};
4041
const bool fast_fwd_execution_{default_fast_fwd_execution};
4142

4243
std::set<Reactor*> top_level_reactors_{};
44+
/// Set of actions that act as an input to the reactor program in this environment
45+
std::set<BaseAction*> input_actions_{};
4346
std::set<Reaction*> reactions_{};
4447
std::vector<Dependency> dependencies_{};
4548

@@ -64,13 +67,14 @@ private:
6467
auto startup(const TimePoint& start_time) -> std::thread;
6568

6669
public:
67-
explicit Environment(unsigned int num_workers, bool run_forever = default_run_forever,
68-
bool fast_fwd_execution = default_fast_fwd_execution, const Duration& timeout = Duration::max());
70+
explicit Environment(unsigned int num_workers, bool fast_fwd_execution = default_fast_fwd_execution,
71+
const Duration& timeout = Duration::max());
6972
explicit Environment(const std::string& name, Environment* containing_environment);
7073

7174
auto name() -> const std::string& { return name_; }
7275

7376
void register_reactor(Reactor* reactor);
77+
void register_input_action(BaseAction* action);
7478
void assemble();
7579
auto startup() -> std::thread;
7680
void sync_shutdown();

include/reactor-cpp/trace.hh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ TRACEPOINT_EVENT(
6262
ctf_integer(unsigned long long, timestamp_ns, tag_arg.time_point().time_since_epoch().count())
6363
ctf_integer(unsigned long, timestamp_microstep, tag_arg.micro_step())))
6464

65-
TRACEPOINT_EVENT(reactor_cpp, trigger_reaction,
66-
TP_ARGS(const std::string&, reactor_name_arg, const std::string&, reaction_name_arg,
67-
const reactor::LogicalTime&, tag_arg),
68-
TP_FIELDS(ctf_string(reactor_name, reactor_name_arg.c_str()) ctf_string(reaction_name,
69-
reaction_name_arg.c_str())
70-
ctf_integer(unsigned long long, timestamp_ns, tag_arg.time_point().time_since_epoch().count())
71-
ctf_integer(unsigned long, timestamp_microstep, tag_arg.micro_step())))
65+
TRACEPOINT_EVENT(
66+
reactor_cpp, trigger_reaction,
67+
TP_ARGS(const std::string&, reactor_name_arg, const std::string&, reaction_name_arg, const reactor::LogicalTime&,
68+
tag_arg),
69+
TP_FIELDS(ctf_string(reactor_name, reactor_name_arg.c_str()) ctf_string(reaction_name, reaction_name_arg.c_str())
70+
ctf_integer(unsigned long long, timestamp_ns, tag_arg.time_point().time_since_epoch().count())
71+
ctf_integer(unsigned long, timestamp_microstep, tag_arg.micro_step())))
7272

7373
#endif /* REACTOR_CPP_TRACE_HH */
7474

lib/environment.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323

2424
namespace reactor {
2525

26-
Environment::Environment(unsigned int num_workers, bool run_forever, bool fast_fwd_execution, const Duration& timeout)
26+
Environment::Environment(unsigned int num_workers, bool fast_fwd_execution, const Duration& timeout)
2727
: log_("Environment")
2828
, num_workers_(num_workers)
29-
, run_forever_(run_forever)
3029
, fast_fwd_execution_(fast_fwd_execution)
3130
, top_environment_(this)
3231
, scheduler_(this)
@@ -36,7 +35,6 @@ Environment::Environment(const std::string& name, Environment* containing_enviro
3635
: name_(name)
3736
, log_("Environment " + name)
3837
, num_workers_(containing_environment->num_workers_)
39-
, run_forever_(containing_environment->run_forever_)
4038
, fast_fwd_execution_(containing_environment->fast_fwd_execution_)
4139
, containing_environment_(containing_environment)
4240
, top_environment_(containing_environment_->top_environment_)
@@ -52,6 +50,13 @@ void Environment::register_reactor(Reactor* reactor) {
5250
reactor_assert(top_level_reactors_.insert(reactor).second);
5351
}
5452

53+
void Environment::register_input_action(BaseAction* action) {
54+
reactor_assert(action != nullptr);
55+
validate(this->phase() == Phase::Construction, "Input actions may only be registered during construction phase!");
56+
reactor_assert(input_actions_.insert(action).second);
57+
run_forever_ = true;
58+
}
59+
5560
void recursive_assemble(Reactor* container) { // NOLINT
5661
container->assemble();
5762
for (auto* reactor : container->reactors()) {

0 commit comments

Comments
 (0)