I hate timers

This commit is contained in:
wheremyfoodat 2023-08-14 17:24:53 +03:00
parent 1354b0f7fa
commit 8881467505
4 changed files with 69 additions and 12 deletions

View file

@ -35,6 +35,7 @@ class Kernel {
std::vector<KernelObject> objects;
std::vector<Handle> portHandles;
std::vector<Handle> mutexHandles;
std::vector<Handle> timerHandles;
// Thread indices, sorted by priority
std::vector<int> threadIndices;
@ -84,6 +85,7 @@ private:
void releaseMutex(Mutex* moo);
void cancelTimer(Timer* timer);
void signalTimer(Handle timerHandle, Timer* timer);
void updateTimer(Handle timerHandle, Timer* timer);
// Wake up the thread with the highest priority out of all threads in the waitlist
// Returns the index of the woken up thread
@ -182,6 +184,14 @@ public:
void requireReschedule() { needReschedule = true; }
void evalReschedule() {
for (auto handle : timerHandles) {
const auto object = getObject(handle, KernelObjectType::Timer);
if (object != nullptr) {
Timer* timer = object->getData<Timer>();
updateTimer(handle, timer);
}
}
if (needReschedule) {
needReschedule = false;
rescheduleThreads();

View file

@ -167,11 +167,13 @@ struct Timer {
u64 waitlist; // Refer to the getWaitlist function below for documentation
ResetType resetType = ResetType::OneShot;
u64 initialDelay; // Number of ns until the timer fires for the first time
u64 interval; // Number of ns until the timer fires for the second and future times
bool fired; // Has this Timer been signalled?
u64 startTick; // CPU tick the timer started
u64 currentDelay; // Number of ns until the timer fires next time
u64 interval; // Number of ns until the timer fires for the second and future times
bool fired; // Has this timer been signalled?
bool running; // Is this timer running or stopped?
Timer(ResetType type) : resetType(type), initialDelay(0), interval(0), waitlist(0), fired(false) {}
Timer(ResetType type) : resetType(type), startTick(0), currentDelay(0), interval(0), waitlist(0), fired(false), running(false) {}
};
struct MemoryBlock {