Skip to content

Commit 933e3b3

Browse files
committed
snapshot
1 parent e0df434 commit 933e3b3

File tree

12 files changed

+165
-9
lines changed

12 files changed

+165
-9
lines changed

examples/ports/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Adder : public Reactor {
8585
r_add.declare_antidependency(&sum);
8686
}
8787

88-
void add() {
88+
void add(Adder* self) {
8989
if (i1.is_present() && i2.is_present()) {
9090
sum.set(*i1.get() + *i2.get());
9191
std::cout << "setting sum\n";

include/reactor-cpp/logging.hh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ private:
3030

3131
// NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
3232
inline static std::mutex mutex_{};
33-
3433
Lock lock_{};
3534

3635
public:

include/reactor-cpp/multiport.hh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ namespace reactor {
2323

2424
class BaseMultiport { // NOLINT cppcoreguidelines-special-member-functions,-warnings-as-errors
2525
private:
26-
std::atomic<std::size_t> size_{0};
26+
std::atomic<std::size_t> present_ports_size_{0};
2727
std::vector<std::size_t> present_ports_{};
2828

2929
// record that the port with the given index has been set
3030
void set_present(std::size_t index);
3131

3232
// reset the list of set port indexes
33-
void reset() noexcept { size_.store(0, std::memory_order_relaxed); }
33+
void reset() noexcept { present_ports_size_.store(0, std::memory_order_relaxed); }
3434

3535
[[nodiscard]] auto get_set_callback(std::size_t index) noexcept -> PortCallback;
3636
const PortCallback clean_callback_{[this]([[maybe_unused]] const BasePort& port) { this->reset(); }};
@@ -39,7 +39,7 @@ private:
3939

4040
protected:
4141
[[nodiscard]] auto present_ports() const -> const auto& { return present_ports_; }
42-
[[nodiscard]] auto present_ports_size() const -> auto { return size_.load(); }
42+
[[nodiscard]] auto present_ports_size() const -> auto { return present_ports_size_.load(); }
4343

4444
void present_ports_reserve(size_t n) { present_ports_.reserve(n); }
4545

@@ -50,11 +50,15 @@ public:
5050
~BaseMultiport() = default;
5151
};
5252

53+
template <class T>
54+
class MutationChangeMultiportSize;
55+
5356
template <class T, class A = std::allocator<T>>
5457
class Multiport : public BaseMultiport { // NOLINT cppcoreguidelines-special-member-functions
5558
protected:
5659
std::vector<T> ports_{}; // NOLINT cppcoreguidelines-non-private-member-variables-in-classes
5760

61+
friend MutationChangeMultiportSize<T>;
5862
public:
5963
using value_type = typename A::value_type;
6064
using size_type = typename A::size_type;

include/reactor-cpp/reaction.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private:
2727
std::set<BasePort*> dependencies_;
2828

2929
const int priority_;
30+
const bool mutation_;
3031
unsigned int index_{};
3132

3233
std::function<void(void)> body_{nullptr};
@@ -37,7 +38,7 @@ private:
3738
void set_deadline_impl(Duration deadline, const std::function<void(void)>& handler);
3839

3940
public:
40-
Reaction(const std::string& name, int priority, Reactor* container, std::function<void(void)> body);
41+
Reaction(const std::string& name, int priority, bool mutation, Reactor* container, std::function<void(void)> body);
4142

4243
~Reaction() override = default;
4344

include/reactor-cpp/reactor-cpp.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
#include "reaction.hh"
2222
#include "reactor.hh"
2323
#include "time.hh"
24+
#include "scops.hh"
2425

2526
#endif // REACTOR_CPP_REACTOR_CPP_HH

include/reactor-cpp/scops.hh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2024 TU Dresden
3+
* All rights reserved.
4+
*
5+
* Authors:
6+
* Tassilo Tanneberger
7+
*/
8+
9+
#ifndef REACTOR_CPP_SCOPS_HH
10+
#define REACTOR_CPP_SCOPS_HH
11+
12+
#include "transaction.hh"
13+
#include "time.hh"
14+
15+
namespace reactor {
16+
class Reactor;
17+
18+
class Scope {
19+
private:
20+
reactor::Reactor* reactor;
21+
22+
public:
23+
Scope(reactor::Reactor* reactor)
24+
: reactor(reactor) {}
25+
26+
auto get_physical_time() const -> reactor::TimePoint { return reactor->get_physical_time(); }
27+
auto get_tag() const -> reactor::Tag { return reactor->get_tag(); }
28+
auto get_logical_time() const -> reactor::TimePoint { return reactor->get_logical_time(); }
29+
auto get_microstep() const -> reactor::mstep_t { return reactor->get_microstep(); }
30+
auto get_elapsed_logical_time() const -> reactor::Duration { return reactor->get_elapsed_logical_time(); }
31+
auto get_elapsed_physical_time() const -> reactor::Duration { return reactor->get_elapsed_physical_time(); }
32+
auto environment() const -> reactor::Environment* { return reactor->environment(); }
33+
void request_stop() const { return environment()->sync_shutdown(); }
34+
};
35+
36+
template<class HostReactor>
37+
class MutableScope : public Scope {
38+
public:
39+
HostReactor* self_ = nullptr;
40+
Environment* env_ = nullptr;
41+
Transaction transaction_;
42+
43+
explicit MutableScope(reactor::Reactor* reactor) : Scope(reactor), self_(reactor), env_(reactor->environment()) {}
44+
~MutableScope() = default;
45+
46+
void begin_transaction();
47+
void end_transaction();
48+
void mutate(const std::unique_ptr<Mutation>&& mutation);
49+
50+
};
51+
52+
}
53+
54+
#endif // REACTOR_CPP_SCOPS_HH

include/reactor-cpp/transaction.hh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2024 TU Dresden
3+
* All rights reserved.
4+
*
5+
* Authors:
6+
* Tassilo Tanneberger
7+
*/
8+
9+
#ifndef REACTOR_CPP_TRANSACTION_HH
10+
#define REACTOR_CPP_TRANSACTION_HH
11+
12+
#include <vector>
13+
14+
#include "multiport.hh"
15+
16+
namespace reactor {
17+
class Reactor;
18+
class Environment;
19+
20+
enum MutationResult {
21+
Success = 0,
22+
NotMatchingBankSize = 1,
23+
};
24+
25+
class Mutation {
26+
public:
27+
virtual auto run() -> MutationResult = 0 ;
28+
29+
};
30+
31+
template<class T>
32+
class MutationChangeMultiportSize : public Mutation {
33+
private:
34+
Multiport<T>* multiport_ = nullptr;
35+
std::size_t desired_size_ = 0;
36+
public:
37+
MutationChangeMultiportSize(Multiport<T>* multiport, std::size_t size);
38+
~MutationChangeMultiportSize() = default;
39+
void run() override;
40+
};
41+
42+
43+
class Transaction {
44+
private:
45+
Reactor* parent = nullptr;
46+
Environment* environment = nullptr;
47+
std::vector<Mutation*> mutations_{};
48+
49+
public:
50+
void reset();
51+
auto execute() -> MutationResult;
52+
53+
};
54+
}
55+
#endif // REACTOR_CPP_TRANSACTION_HH

lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ set(SOURCE_FILES
77
reaction.cc
88
reactor.cc
99
scheduler.cc
10+
scops.cc
1011
time.cc
12+
transaction.cc
1113
multiport.cc
1214
reactor_element.cc
1315
)

lib/multiport.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ auto reactor::BaseMultiport::get_set_callback(std::size_t index) noexcept -> rea
2121
}
2222

2323
void reactor::BaseMultiport::set_present(std::size_t index) {
24-
auto calculated_index = size_.fetch_add(1, std::memory_order_relaxed);
24+
auto calculated_index = present_ports_size_.fetch_add(1, std::memory_order_relaxed);
2525

2626
reactor_assert(calculated_index < present_ports_.size());
2727

lib/reaction.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
namespace reactor {
1919

20-
Reaction::Reaction(const std::string& name, int priority, Reactor* container, std::function<void(void)> body)
20+
Reaction::Reaction(const std::string& name, int priority, bool mutation, Reactor* container, std::function<void(void)> body)
2121
: ReactorElement(name, ReactorElement::Type::Reaction, container)
2222
, priority_(priority)
23-
, body_(std::move(std::move(body))) {
23+
, mutation_(mutation)
24+
, body_(std::move(std::move(body)))
25+
{
2426
reactor_assert(priority != 0);
2527
}
2628

0 commit comments

Comments
 (0)