2019-07-29 01:49:54 +00:00
|
|
|
//
|
|
|
|
// JustInTime.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/07/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef JustInTime_h
|
|
|
|
#define JustInTime_h
|
|
|
|
|
2021-11-11 14:24:32 +00:00
|
|
|
#include "ClockReceiver.hpp"
|
2019-07-29 20:38:57 +00:00
|
|
|
#include "../Concurrency/AsyncTaskQueue.hpp"
|
2021-03-27 03:44:15 +00:00
|
|
|
#include "ClockingHintSource.hpp"
|
2019-10-31 03:18:42 +00:00
|
|
|
#include "ForceInline.hpp"
|
2019-07-29 01:49:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
A JustInTimeActor holds (i) an embedded object with a run_for method; and (ii) an amount
|
|
|
|
of time since run_for was last called.
|
|
|
|
|
|
|
|
Time can be added using the += operator. The -> operator can be used to access the
|
|
|
|
embedded object. All time accumulated will be pushed to object before the pointer is returned.
|
|
|
|
|
|
|
|
Machines that accumulate HalfCycle time but supply to a Cycle-counted device may supply a
|
|
|
|
separate @c TargetTimeScale at template declaration.
|
2020-11-16 02:58:18 +00:00
|
|
|
|
|
|
|
If the held object implements get_next_sequence_point() then it'll be used to flush implicitly
|
|
|
|
as and when sequence points are hit. Callers can use will_flush() to predict these.
|
2021-03-27 03:44:15 +00:00
|
|
|
|
|
|
|
If the held object is a subclass of ClockingHint::Source, this template will register as an
|
|
|
|
observer and potentially stop clocking or stop delaying clocking until just-in-time references
|
|
|
|
as directed.
|
2021-04-04 04:25:22 +00:00
|
|
|
|
|
|
|
TODO: incorporate and codify AsyncJustInTimeActor.
|
2019-07-29 01:49:54 +00:00
|
|
|
*/
|
2021-04-04 21:33:49 +00:00
|
|
|
template <class T, class LocalTimeScale = HalfCycles, int multiplier = 1, int divider = 1> class JustInTimeActor:
|
2021-03-27 03:44:15 +00:00
|
|
|
public ClockingHint::Observer {
|
2020-11-17 00:00:11 +00:00
|
|
|
private:
|
2021-04-04 04:25:22 +00:00
|
|
|
/*!
|
|
|
|
A std::unique_ptr deleter which causes an update_sequence_point to occur on the actor supplied
|
|
|
|
to it at construction if it implements get_next_sequence_point(). Otherwise destruction is a no-op.
|
|
|
|
|
|
|
|
**Does not delete the object.**
|
|
|
|
|
|
|
|
This is used by the -> operators below, which provide a unique pointer to the enclosed object and
|
|
|
|
update their sequence points upon its destruction — i.e. after the caller has made whatever call
|
|
|
|
or calls as were relevant to the enclosed object.
|
|
|
|
*/
|
2020-11-17 00:00:11 +00:00
|
|
|
class SequencePointAwareDeleter {
|
|
|
|
public:
|
2021-04-04 21:33:49 +00:00
|
|
|
explicit SequencePointAwareDeleter(JustInTimeActor<T, LocalTimeScale, multiplier, divider> *actor) noexcept
|
2021-04-04 04:25:22 +00:00
|
|
|
: actor_(actor) {}
|
2020-11-17 00:00:11 +00:00
|
|
|
|
2021-04-04 04:25:22 +00:00
|
|
|
forceinline void operator ()(const T *const) const {
|
2020-11-17 00:00:11 +00:00
|
|
|
if constexpr (has_sequence_points<T>::value) {
|
|
|
|
actor_->update_sequence_point();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-04-04 21:33:49 +00:00
|
|
|
JustInTimeActor<T, LocalTimeScale, multiplier, divider> *const actor_;
|
2020-11-17 00:00:11 +00:00
|
|
|
};
|
|
|
|
|
2021-04-04 19:37:07 +00:00
|
|
|
// This block of SFINAE determines whether objects of type T accepts Cycles or HalfCycles.
|
|
|
|
using HalfRunFor = void (T::*const)(HalfCycles);
|
|
|
|
static uint8_t half_sig(...);
|
|
|
|
static uint16_t half_sig(HalfRunFor);
|
|
|
|
using TargetTimeScale =
|
|
|
|
std::conditional_t<
|
|
|
|
sizeof(half_sig(&T::run_for)) == sizeof(uint16_t),
|
|
|
|
HalfCycles,
|
|
|
|
Cycles>;
|
|
|
|
|
2019-07-29 01:49:54 +00:00
|
|
|
public:
|
|
|
|
/// Constructs a new JustInTimeActor using the same construction arguments as the included object.
|
2021-03-27 03:44:15 +00:00
|
|
|
template<typename... Args> JustInTimeActor(Args&&... args) : object_(std::forward<Args>(args)...) {
|
|
|
|
if constexpr (std::is_base_of<ClockingHint::Source, T>::value) {
|
|
|
|
object_.set_clocking_hint_observer(this);
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 01:49:54 +00:00
|
|
|
|
|
|
|
/// Adds time to the actor.
|
2021-04-04 01:26:43 +00:00
|
|
|
///
|
|
|
|
/// @returns @c true if adding time caused a flush; @c false otherwise.
|
|
|
|
forceinline bool operator += (LocalTimeScale rhs) {
|
2021-03-27 03:44:15 +00:00
|
|
|
if constexpr (std::is_base_of<ClockingHint::Source, T>::value) {
|
|
|
|
if(clocking_preference_ == ClockingHint::Preference::None) {
|
2021-04-04 01:26:43 +00:00
|
|
|
return false;
|
2021-03-27 03:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:58:19 +00:00
|
|
|
if constexpr (multiplier != 1) {
|
2019-10-29 01:35:10 +00:00
|
|
|
time_since_update_ += rhs * multiplier;
|
|
|
|
} else {
|
|
|
|
time_since_update_ += rhs;
|
|
|
|
}
|
2019-07-29 19:38:41 +00:00
|
|
|
is_flushed_ = false;
|
2020-11-16 02:58:18 +00:00
|
|
|
|
2021-03-27 03:44:15 +00:00
|
|
|
if constexpr (std::is_base_of<ClockingHint::Source, T>::value) {
|
|
|
|
if (clocking_preference_ == ClockingHint::Preference::RealTime) {
|
|
|
|
flush();
|
2021-04-04 01:26:43 +00:00
|
|
|
return true;
|
2021-03-27 03:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:58:18 +00:00
|
|
|
if constexpr (has_sequence_points<T>::value) {
|
2021-06-18 22:43:08 +00:00
|
|
|
time_until_event_ -= rhs * multiplier;
|
2020-11-17 00:00:11 +00:00
|
|
|
if(time_until_event_ <= LocalTimeScale(0)) {
|
2021-06-18 22:43:08 +00:00
|
|
|
time_overrun_ = time_until_event_ / divider;
|
2020-11-16 02:58:18 +00:00
|
|
|
flush();
|
2021-04-06 01:02:37 +00:00
|
|
|
update_sequence_point();
|
2021-04-04 01:26:43 +00:00
|
|
|
return true;
|
2020-11-16 02:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-04 01:26:43 +00:00
|
|
|
|
|
|
|
return false;
|
2019-07-29 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Flushes all accumulated time and returns a pointer to the included object.
|
2020-11-17 00:00:11 +00:00
|
|
|
///
|
|
|
|
/// If this object provides sequence points, checks for changes to the next
|
|
|
|
/// sequence point upon deletion of the pointer.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline auto operator->() {
|
2019-07-29 19:38:41 +00:00
|
|
|
flush();
|
2020-11-17 00:00:11 +00:00
|
|
|
return std::unique_ptr<T, SequencePointAwareDeleter>(&object_, SequencePointAwareDeleter(this));
|
2019-07-29 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 02:45:10 +00:00
|
|
|
/// Acts exactly as per the standard ->, but preserves constness.
|
2020-11-17 00:00:11 +00:00
|
|
|
///
|
|
|
|
/// Despite being const, this will flush the object and, if relevant, update the next sequence point.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline auto operator -> () const {
|
2021-04-04 21:33:49 +00:00
|
|
|
auto non_const_this = const_cast<JustInTimeActor<T, LocalTimeScale, multiplier, divider> *>(this);
|
2020-01-21 02:45:10 +00:00
|
|
|
non_const_this->flush();
|
2020-11-17 00:00:11 +00:00
|
|
|
return std::unique_ptr<const T, SequencePointAwareDeleter>(&object_, SequencePointAwareDeleter(non_const_this));
|
2020-01-21 02:45:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 02:55:41 +00:00
|
|
|
/// @returns a pointer to the included object, without flushing time.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline T *last_valid() {
|
2019-07-29 21:17:04 +00:00
|
|
|
return &object_;
|
|
|
|
}
|
|
|
|
|
2021-04-05 00:43:16 +00:00
|
|
|
/// @returns a const pointer to the included object, without flushing time.
|
|
|
|
[[nodiscard]] forceinline const T *last_valid() const {
|
|
|
|
return &object_;
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:35:07 +00:00
|
|
|
/// @returns the amount of time since the object was last flushed, in the target time scale.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline TargetTimeScale time_since_flush() const {
|
2020-12-01 23:35:07 +00:00
|
|
|
if constexpr (divider == 1) {
|
|
|
|
return time_since_update_;
|
|
|
|
}
|
|
|
|
return TargetTimeScale(time_since_update_.as_integral() / divider);
|
2020-11-17 02:55:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:24:21 +00:00
|
|
|
/// @returns the amount of time since the object was last flushed, plus the local time scale @c offset,
|
|
|
|
/// converted to the target time scale.
|
|
|
|
[[nodiscard]] forceinline TargetTimeScale time_since_flush(LocalTimeScale offset) const {
|
|
|
|
if constexpr (divider == 1) {
|
|
|
|
return time_since_update_ + offset;
|
|
|
|
}
|
|
|
|
return TargetTimeScale((time_since_update_ + offset).as_integral() / divider);
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:38:41 +00:00
|
|
|
/// Flushes all accumulated time.
|
2020-11-17 00:00:11 +00:00
|
|
|
///
|
|
|
|
/// This does not affect this actor's record of when the next sequence point will occur.
|
2019-10-31 03:18:42 +00:00
|
|
|
forceinline void flush() {
|
2019-10-13 22:19:39 +00:00
|
|
|
if(!is_flushed_) {
|
2020-11-26 21:11:03 +00:00
|
|
|
did_flush_ = is_flushed_ = true;
|
2019-12-31 03:58:19 +00:00
|
|
|
if constexpr (divider == 1) {
|
2020-01-23 00:32:23 +00:00
|
|
|
const auto duration = time_since_update_.template flush<TargetTimeScale>();
|
|
|
|
object_.run_for(duration);
|
2019-10-13 22:19:39 +00:00
|
|
|
} else {
|
2019-10-31 03:18:42 +00:00
|
|
|
const auto duration = time_since_update_.template divide<TargetTimeScale>(LocalTimeScale(divider));
|
|
|
|
if(duration > TargetTimeScale(0))
|
|
|
|
object_.run_for(duration);
|
2019-10-13 22:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 21:11:03 +00:00
|
|
|
/// Indicates whether a flush has occurred since the last call to did_flush().
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline bool did_flush() {
|
2020-11-26 21:11:03 +00:00
|
|
|
const bool did_flush = did_flush_;
|
|
|
|
did_flush_ = false;
|
|
|
|
return did_flush;
|
2020-11-16 02:58:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 01:02:37 +00:00
|
|
|
/// @returns a number in the range [-max, 0] indicating the offset of the most recent sequence
|
|
|
|
/// point from the final time at the end of the += that triggered the sequence point.
|
|
|
|
[[nodiscard]] forceinline LocalTimeScale last_sequence_point_overrun() {
|
|
|
|
return time_overrun_;
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:58:18 +00:00
|
|
|
/// @returns the number of cycles until the next sequence-point-based flush, if the embedded object
|
|
|
|
/// supports sequence points; @c LocalTimeScale() otherwise.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] LocalTimeScale cycles_until_implicit_flush() const {
|
2021-06-18 22:43:08 +00:00
|
|
|
return time_until_event_ / divider;
|
2020-11-08 00:40:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 21:11:03 +00:00
|
|
|
/// Indicates whether a sequence-point-caused flush will occur if the specified period is added.
|
2021-04-04 01:26:43 +00:00
|
|
|
[[nodiscard]] forceinline bool will_flush(LocalTimeScale rhs) const {
|
2020-11-26 21:11:03 +00:00
|
|
|
if constexpr (!has_sequence_points<T>::value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return rhs >= time_until_event_;
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:24:21 +00:00
|
|
|
/// Indicates the amount of time, in the local time scale, until the first local slot that falls wholly
|
2021-06-27 21:30:09 +00:00
|
|
|
/// after @c duration, if that delay were to occur in @c offset units of time from now.
|
|
|
|
[[nodiscard]] forceinline LocalTimeScale back_map(TargetTimeScale duration, TargetTimeScale offset) const {
|
2021-06-27 21:36:25 +00:00
|
|
|
// A 1:1 mapping is easy.
|
|
|
|
if constexpr (multiplier == 1 && divider == 1) {
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out when this query is placed, and the time to which it relates
|
|
|
|
const auto base = time_since_update_ + offset * divider;
|
|
|
|
const auto target = base + duration * divider;
|
|
|
|
|
|
|
|
// Figure out the number of whole input steps that is required to get
|
|
|
|
// past target, and subtract the number of whole input steps necessary
|
|
|
|
// to get to base.
|
2021-06-27 21:39:13 +00:00
|
|
|
const auto steps_to_base = base.as_integral() / multiplier;
|
|
|
|
const auto steps_to_target = (target.as_integral() + divider - 1) / multiplier;
|
2021-06-27 21:36:25 +00:00
|
|
|
|
|
|
|
return LocalTimeScale(steps_to_target - steps_to_base);
|
2021-06-27 21:24:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 00:00:11 +00:00
|
|
|
/// Updates this template's record of the next sequence point.
|
|
|
|
void update_sequence_point() {
|
|
|
|
if constexpr (has_sequence_points<T>::value) {
|
2021-06-28 01:36:41 +00:00
|
|
|
// Keep a fast path where no conversions will be applied; if conversions are
|
|
|
|
// going to be applied then do a direct max -> max translation rather than
|
|
|
|
// allowing the arithmetic to overflow.
|
|
|
|
if constexpr (divider == 1 && std::is_same_v<LocalTimeScale, TargetTimeScale>) {
|
|
|
|
time_until_event_ = object_.get_next_sequence_point();
|
|
|
|
} else {
|
|
|
|
const auto time = object_.get_next_sequence_point();
|
|
|
|
if(time == TargetTimeScale::max()) {
|
|
|
|
time_until_event_ = LocalTimeScale::max();
|
|
|
|
} else {
|
|
|
|
time_until_event_ = time * divider;
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 00:00:11 +00:00
|
|
|
assert(time_until_event_ > LocalTimeScale(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 00:43:16 +00:00
|
|
|
/// @returns A cached copy of the object's clocking preference.
|
|
|
|
ClockingHint::Preference clocking_preference() const {
|
|
|
|
return clocking_preference_;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:49:54 +00:00
|
|
|
private:
|
|
|
|
T object_;
|
2021-04-06 01:02:37 +00:00
|
|
|
LocalTimeScale time_since_update_, time_until_event_, time_overrun_;
|
2019-07-29 19:38:41 +00:00
|
|
|
bool is_flushed_ = true;
|
2020-11-26 21:11:03 +00:00
|
|
|
bool did_flush_ = false;
|
2020-11-16 02:58:18 +00:00
|
|
|
|
|
|
|
template <typename S, typename = void> struct has_sequence_points : std::false_type {};
|
|
|
|
template <typename S> struct has_sequence_points<S, decltype(void(std::declval<S &>().get_next_sequence_point()))> : std::true_type {};
|
2021-03-27 03:44:15 +00:00
|
|
|
|
|
|
|
ClockingHint::Preference clocking_preference_ = ClockingHint::Preference::JustInTime;
|
|
|
|
void set_component_prefers_clocking(ClockingHint::Source *, ClockingHint::Preference clocking) {
|
|
|
|
clocking_preference_ = clocking;
|
|
|
|
}
|
2019-07-29 01:49:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
2021-04-04 20:14:47 +00:00
|
|
|
An AsyncJustInTimeActor acts like a JustInTimeActor but additionally contains an AsyncTaskQueue.
|
2019-07-29 01:49:54 +00:00
|
|
|
Any time the amount of accumulated time crosses a threshold provided at construction time,
|
|
|
|
the object will be updated on the AsyncTaskQueue.
|
|
|
|
*/
|
2019-08-08 01:28:02 +00:00
|
|
|
template <class T, class LocalTimeScale = HalfCycles, class TargetTimeScale = LocalTimeScale> class AsyncJustInTimeActor {
|
2019-07-29 20:38:57 +00:00
|
|
|
public:
|
|
|
|
/// Constructs a new AsyncJustInTimeActor using the same construction arguments as the included object.
|
|
|
|
template<typename... Args> AsyncJustInTimeActor(TargetTimeScale threshold, Args&&... args) :
|
|
|
|
object_(std::forward<Args>(args)...),
|
|
|
|
threshold_(threshold) {}
|
2019-07-29 01:49:54 +00:00
|
|
|
|
2019-07-29 20:38:57 +00:00
|
|
|
/// Adds time to the actor.
|
|
|
|
inline void operator += (const LocalTimeScale &rhs) {
|
|
|
|
time_since_update_ += rhs;
|
|
|
|
if(time_since_update_ >= threshold_) {
|
|
|
|
time_since_update_ -= threshold_;
|
|
|
|
task_queue_.enqueue([this] () {
|
|
|
|
object_.run_for(threshold_);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
is_flushed_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flushes all accumulated time and returns a pointer to the included object.
|
|
|
|
inline T *operator->() {
|
|
|
|
flush();
|
|
|
|
return &object_;
|
|
|
|
}
|
|
|
|
|
2019-07-29 21:17:04 +00:00
|
|
|
/// Returns a pointer to the included object without flushing time.
|
|
|
|
inline T *last_valid() {
|
|
|
|
return &object_;
|
|
|
|
}
|
|
|
|
|
2019-07-29 20:38:57 +00:00
|
|
|
/// Flushes all accumulated time.
|
|
|
|
inline void flush() {
|
|
|
|
if(!is_flushed_) {
|
|
|
|
task_queue_.flush();
|
|
|
|
object_.run_for(time_since_update_.template flush<TargetTimeScale>());
|
|
|
|
is_flushed_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T object_;
|
|
|
|
LocalTimeScale time_since_update_;
|
|
|
|
TargetTimeScale threshold_;
|
|
|
|
bool is_flushed_ = true;
|
2022-07-16 18:41:04 +00:00
|
|
|
Concurrency::AsyncTaskQueue<true> task_queue_;
|
2019-07-29 01:49:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* JustInTime_h */
|