The Sparta Modeling Framework
Loading...
Searching...
No Matches
PhasedSingleCycleUniqueEvent.hpp
Go to the documentation of this file.
1// <PhasedSingleCycleUniqueEvent.h> -*- C++ -*-
2
3
10#pragma once
11
12#include <array>
13#include <memory>
14#include <string>
21
22namespace sparta
23{
24
40 {
41 public:
55 const std::string & name,
56 SchedulingPhase sched_phase,
57 const SpartaHandler & consumer_event_handler) :
58 EventNode(event_set, name, sched_phase),
59 local_clk_(getClock()),
60 fancy_name_(name + "[" + consumer_event_handler.getName() + "]"),
61 single_cycle_event_scheduleable_(consumer_event_handler, 1 /*HARD CODED*/, sched_phase)
62 {
63 single_cycle_event_scheduleable_.setScheduleableClock(getClock());
64 single_cycle_event_scheduleable_.setLabel(fancy_name_.c_str());
65 }
66
69
72
75 void setContinuing(bool continuing) {
76 single_cycle_event_scheduleable_.setContinuing(continuing);
77 }
78
83 bool isContinuing() const {
84 return single_cycle_event_scheduleable_.isContinuing();
85 }
86
90 void cancel() {
91 single_cycle_event_scheduleable_.cancel();
92 }
93
101 bool isScheduled() const {
102 return single_cycle_event_scheduleable_.isScheduled();
103 }
104
106 virtual ~PhasedSingleCycleUniqueEvent() = default;
107
110
120 void schedule(Clock::Cycle rel_cycle = 0) {
121 sparta_assert(rel_cycle < 2,
122 "Cannot schedule sparta::SingleCycleUniqueEvent:'"
123 << getName() << "' in any relative time other than 0 or 1. rel_cycle given: "
124 << rel_cycle);
125
126 const auto to_be_scheduled_relative_tick =
127 local_clk_->getTick(Clock::Cycle(rel_cycle));
128
129 const auto to_be_scheduled_abs_tick =
130 local_scheduler_->calcIndexTime(to_be_scheduled_relative_tick);
131
132 if(SPARTA_EXPECT_TRUE(next_scheduled_tick_ < to_be_scheduled_abs_tick || is_init_tick_))
133 {
134#ifndef NDEBUG
135 // This is a handy debug assertion to see if
136 // SingleCycleUniqueEvent is actually only scheduled once.
137 //
138 sparta_assert(single_cycle_event_scheduleable_.isScheduled(rel_cycle) == false,
139 "Bummer! You found a bug in PhasedSingleCycleUniqueEvent. Please file a GitHub Issue");
140#endif
141 single_cycle_event_scheduleable_.
142 scheduleRelativeTick(to_be_scheduled_relative_tick, local_scheduler_);
143 prev_scheduled_tick_ = next_scheduled_tick_;
144 next_scheduled_tick_ = to_be_scheduled_abs_tick;
145 is_init_tick_ = false; //only allow one event in tick 0
146 }
147 else if((to_be_scheduled_abs_tick < next_scheduled_tick_) &&
148 (prev_scheduled_tick_ != to_be_scheduled_abs_tick))
149 {
150#ifndef NDEBUG
151 // This is a handy debug assertion to see if
152 // SingleCycleUniqueEvent is actually only scheduled once.
153 //
154 sparta_assert(single_cycle_event_scheduleable_.isScheduled(rel_cycle) == false,
155 "Bummer! You found a bug in PhasedSingleCycleUniqueEvent. Please file a GitHub Issue");
156 sparta_assert(prev_scheduled_tick_ < to_be_scheduled_abs_tick,
157 "Bummer! You found a bug in PhasedSingleCycleUniqueEvent. Please file a GitHub Issue")
158#endif
159 single_cycle_event_scheduleable_.
160 scheduleRelativeTick(to_be_scheduled_relative_tick, local_scheduler_);
161 prev_scheduled_tick_ = to_be_scheduled_abs_tick;
162 }
163 }
164
173 template<class ScheduleableT>
174 void precedes(ScheduleableT & consumer, const std::string & reason = "") {
175 single_cycle_event_scheduleable_.precedes(consumer, reason);
176 }
177
178#ifndef DO_NOT_DOCUMENT
179 // Used by EventNode and auto-precedence. Return the
180 // Scheduleable (this)
181 Scheduleable & getScheduleable() override {
182 return single_cycle_event_scheduleable_;
183 }
184#endif
185
186 private:
188 void createResource_() override {
189 local_clk_ = getClock();
190 local_scheduler_ = local_clk_->getScheduler();
191 }
192
194 const Clock * local_clk_ = nullptr;
195
197 Scheduler * local_scheduler_ = nullptr;
198
200 //std::array<Scheduler::Tick, 2> next_scheduled_tick_ = {0, 0};
201 Scheduler::Tick next_scheduled_tick_ = 0;
202 Scheduler::Tick prev_scheduled_tick_ = 0;
203
205 std::string fancy_name_;
206
208 Scheduleable single_cycle_event_scheduleable_;
209
211 bool is_init_tick_ = true;
212 };
213
214}
File that defines the Clock class.
File that defines the EventNode class.
File that defines the Scheduleable class.
A simple time-based, event precedence based scheduler.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
#define SPARTA_EXPECT_TRUE(x)
A macro for hinting to the compiler a particular condition should be considered most likely true.
File that contains the macro used to generate the class callbacks.
Basic Node framework in sparta device tree composite pattern.
Scheduler * getScheduler() const
Definition Clock.hpp:302
Scheduler::Tick getTick(const Cycle &cycle) const
Return the tick corresponding to the given cycle.
Definition Clock.hpp:214
EventNode is the base class for all event types in SPARTA. Not to be used by the modeler....
Definition EventNode.hpp:36
sparta::SchedulingPhase getSchedulingPhase() const
Get the scheduling phase of this event node.
Definition EventNode.hpp:93
virtual Scheduleable & getScheduleable()=0
Get the scheduleable associated with this event node.
An event that can only be schedule one cycle into the future.
bool isScheduled() const
Return true if this scheduleable was scheduled at all.
PhasedSingleCycleUniqueEvent(const PhasedSingleCycleUniqueEvent &)=delete
Disallow the copying of the PhasedSingleCycleUniqueEvent.
bool isContinuing() const
Is this Event continuing?
PhasedSingleCycleUniqueEvent(TreeNode *event_set, const std::string &name, SchedulingPhase sched_phase, const SpartaHandler &consumer_event_handler)
Create a PhasedSingleCycleUniqueEvent.
void precedes(ScheduleableT &consumer, const std::string &reason="")
Have this SingleCycleUniqueEvent precede another.
PhasedSingleCycleUniqueEvent & operator=(const PhasedSingleCycleUniqueEvent &)=delete
Disallow the assignment of the PhasedSingleCycleUniqueEvent.
void schedule(Clock::Cycle rel_cycle=0)
Schedule this PhasedSingleCycleUniqueEvent exactly zero or one cycle into the future....
virtual ~PhasedSingleCycleUniqueEvent()=default
Uniquely destroy.
void cancel()
Cancel the event for now and one cycle into the future.
A class that defines the basic scheduling interface to the Scheduler. Not intended to be used by mode...
void setContinuing(bool continuing)
This event, if continuing == true, will keep the simulation running.
void cancel()
Cancel all the times that this Scheduleable was placed on the Scheduler.
void precedes(Scheduleable &consumer, const std::string &reason="")
Have this Scheduleable precede another.
bool isScheduled() const
Return true if this scheduleable was scheduled at all.
void setLabel(const char *label)
Set a new label for this Scheduleable – used in debugging.
void setScheduleableClock(const Clock *clk)
Set the clock and scheduler of this Scheduleable.
bool isContinuing() const
Is this Event continuing?
uint64_t Tick
Typedef for our unit of time.
constexpr Tick calcIndexTime(const Tick rel_time) const
Const expression to calculate tick value for indexing.
Node in a composite tree representing a sparta Tree item.
Definition TreeNode.hpp:205
const Clock * getClock() override
Walks up parents (starting with self) until a parent with an associated local clock is found,...
const std::string & getName() const override
Gets the name of this node.
Macros for handling exponential backoff.
SchedulingPhase
The SchedulingPhases used for events (Tick, Update, PortUpdate, etc)