feat: enhance timer
This commit is contained in:
parent
43c87e5904
commit
54d48e7d91
5 changed files with 133 additions and 8 deletions
|
|
@ -1,15 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include "acpi/madt.h"
|
||||
#include "util/optional.h"
|
||||
|
||||
namespace apic {
|
||||
|
||||
struct lapic_timer_calibration_t {
|
||||
uint64_t tsc_frequency_hz;
|
||||
struct duration_t {
|
||||
uint64_t timer_count;
|
||||
};
|
||||
|
||||
optional<lapic_timer_calibration_t> calibrateTimer(const acpi::Madt& madt);
|
||||
struct timestamp_t {
|
||||
uint64_t timer_count;
|
||||
};
|
||||
|
||||
constexpr duration_t operator+(duration_t left, duration_t right) {
|
||||
return duration_t{left.timer_count + right.timer_count};
|
||||
}
|
||||
|
||||
constexpr duration_t operator-(duration_t left, duration_t right) {
|
||||
return duration_t{left.timer_count - right.timer_count};
|
||||
}
|
||||
|
||||
constexpr duration_t operator*(duration_t duration, uint64_t scalar) {
|
||||
return duration_t{duration.timer_count * scalar};
|
||||
}
|
||||
|
||||
constexpr duration_t operator*(uint64_t scalar, duration_t duration) {
|
||||
return duration_t{duration.timer_count * scalar};
|
||||
}
|
||||
|
||||
constexpr timestamp_t operator+(timestamp_t timestamp, duration_t duration) {
|
||||
return timestamp_t{timestamp.timer_count + duration.timer_count};
|
||||
}
|
||||
|
||||
constexpr timestamp_t operator+(duration_t duration, timestamp_t timestamp) {
|
||||
return timestamp_t{timestamp.timer_count + duration.timer_count};
|
||||
}
|
||||
|
||||
constexpr timestamp_t operator-(timestamp_t timestamp, duration_t duration) {
|
||||
return timestamp_t{timestamp.timer_count - duration.timer_count};
|
||||
}
|
||||
|
||||
constexpr duration_t operator-(timestamp_t left, timestamp_t right) {
|
||||
return duration_t{left.timer_count - right.timer_count};
|
||||
}
|
||||
|
||||
bool calibrateTimer(const acpi::Madt& madt);
|
||||
|
||||
duration_t seconds(uint64_t count);
|
||||
duration_t milliseconds(uint64_t count);
|
||||
duration_t microseconds(uint64_t count);
|
||||
duration_t nanoseconds(uint64_t count);
|
||||
|
||||
timestamp_t now();
|
||||
|
||||
// TODO: when dynamic allocation and lists are available
|
||||
// struct Timer {};
|
||||
|
|
|
|||
|
|
@ -23,3 +23,10 @@ auto construct() {
|
|||
return Type{forward<decltype(args)>(args)...};
|
||||
};
|
||||
}
|
||||
|
||||
template<typename V>
|
||||
auto constant(V v) {
|
||||
return [v](auto _) {
|
||||
return v;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -183,3 +183,24 @@ public:
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct as_optional {
|
||||
using type = optional<T>;
|
||||
};
|
||||
template<typename T>
|
||||
struct as_optional<optional<T>> {
|
||||
using type = optional<T>;
|
||||
};
|
||||
template<typename T>
|
||||
using as_optional_t = typename as_optional<T>::type;
|
||||
|
||||
template<typename Func>
|
||||
auto lift(Func func) {
|
||||
return [func](const auto&... optionals) -> as_optional_t<decltype(func(optionals.value()...))> {
|
||||
if ((optionals.has_value() && ...)) {
|
||||
return func(optionals.value()...);
|
||||
}
|
||||
return nullopt;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue