The Sparta Modeling Framework
Loading...
Searching...
No Matches
GlobalEvent.hpp
Go to the documentation of this file.
1// <GlobalEvent.h> -*- C++ -*-
2
3
9#pragma once
10
11#include <memory>
17
18namespace sparta
19{
20 template<SchedulingPhase sched_phase_T>
21 class GlobalEvent;
22
33 {
34 public:
35 GlobalEventProxy() = default;
36 GlobalEventProxy(const GlobalEventProxy&) = default;
37 GlobalEventProxy & operator=(const GlobalEventProxy&) = default;
38
39 template<SchedulingPhase sched_phase_T = SchedulingPhase::Update>
41 phase_(sched_phase_T),
42 ev_handler_(handler)
43 {}
44
51 void operator()(void) const
52 {
53 if(!ev_handler_.expired()) {
54 auto sptr = ev_handler_.lock();
55 (*(sptr->tracked_object))();
56 }
57 }
58
59 const SchedulingPhase & getSchedulingPhase() const { return phase_; }
60
61 ~GlobalEventProxy() = default;
62
63 private:
64 SchedulingPhase phase_;
65 std::weak_ptr<utils::LifeTracker<SpartaHandler>> ev_handler_;
66 };
67
92 template<SchedulingPhase sched_phase_T = SchedulingPhase::Update>
94 {
95 public:
102 GlobalEvent(const Clock * clk,
103 const SpartaHandler & event_handler) :
104 local_clk_(clk),
105 event_handler_(event_handler),
106 ev_sched_ptr_(clk->getScheduler()->getGlobalPhasedPayloadEventPtr<sched_phase_T>())
107 {}
108
109 GlobalEvent(const GlobalEvent& rhs) :
110 local_clk_(rhs.local_clk_),
111 event_handler_(rhs.event_handler_),
112 ev_handler_lifetime_(&event_handler_),
113 ev_sched_ptr_(rhs.ev_sched_ptr_)
114 {
115 }
116
117 GlobalEvent(GlobalEvent&& rhs) :
118 local_clk_(rhs.local_clk_),
119 event_handler_(std::move(rhs.event_handler_)),
120 ev_handler_lifetime_(&event_handler_),
121 ev_sched_ptr_(rhs.ev_sched_ptr_)
122 {
123 rhs.ev_handler_lifetime_.reset();
124 }
125
126 GlobalEvent& operator=(const GlobalEvent& rhs) {
127 local_clk_ = rhs.local_clk_;
128 event_handler_ = rhs.event_handler_;
129 ev_handler_lifetime_ = utils::LifeTracker<SpartaHandler>(&event_handler_);
130 ev_sched_ptr_ = rhs.ev_sched_ptr_;
131
132 return *this;
133 }
134
135 GlobalEvent& operator=(GlobalEvent&& rhs) {
136 local_clk_ = rhs.local_clk_;
137 event_handler_ = std::move(rhs.event_handler_);
138 ev_handler_lifetime_ = utils::LifeTracker<SpartaHandler>(&event_handler_);
139 ev_sched_ptr_ = rhs.ev_sched_ptr_;
140
141 rhs.ev_handler_lifetime_.reset();
142
143 return *this;
144 }
145
146 void schedule(const Clock::Cycle & delay, const Clock * clk) {
147 sparta_assert(ev_sched_ptr_ != nullptr);
148 sparta_assert(ev_sched_ptr_->getSchedulingPhase() == sched_phase_T);
149
150 ev_sched_ptr_->preparePayload(GlobalEventProxy(ev_handler_lifetime_))->schedule(delay, clk);
151 }
152
153 void schedule(const Clock::Cycle & delay) {
154 sparta_assert(ev_sched_ptr_ != nullptr);
155 sparta_assert(ev_sched_ptr_->getSchedulingPhase() == sched_phase_T);
156
157 ev_sched_ptr_->preparePayload(GlobalEventProxy(ev_handler_lifetime_))->schedule(delay, local_clk_);
158 }
159
160 void resetHandler(const SpartaHandler & event_handler) {
161 event_handler_ = event_handler;
162 }
163
164 ~GlobalEvent() {
165 //std::cout << "Destroy GlobalEvent!\n";
166 }
167
168 friend class GlobalEventProxy;
169
170 private:
171 const Clock * local_clk_ = nullptr;
172 SpartaHandler event_handler_;
173 utils::LifeTracker<SpartaHandler> ev_handler_lifetime_{&event_handler_};
174 PhasedPayloadEvent<GlobalEventProxy> * ev_sched_ptr_;
175 };
176
177}
File that defines the Clock class.
File that defines the LifeTracker class.
File that defines the PhasedPayloadEvent class. Suggest using sparta::PayloadEvent instead.
File that defines the phases used in simulation.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
File that contains the macro used to generate the class callbacks.
A representation of simulated time.
Definition Clock.hpp:51
A helper class of GlobalEvent.
void operator()(void) const
function call operator of GlobalEventProxy
A type of "global" reusable event.
GlobalEvent(const Clock *clk, const SpartaHandler &event_handler)
Create a GlobalEvent.
A class that will track the lifetime of the object it points to.
Macros for handling exponential backoff.
SchedulingPhase
The SchedulingPhases used for events (Tick, Update, PortUpdate, etc)