The Pedigree Project 0.1
IntervalTimerState.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef PEDIGREE_MODULES_POSIX_INTERVALTIMERSTATE_H
9#define PEDIGREE_MODULES_POSIX_INTERVALTIMERSTATE_H
10#include "pedigree/kernel/time/Time.h"
11
12#include <config.h>
13
14namespace PosixIntervalTimerState {
16 Time::Timestamp value;
17 bool armed;
18 bool expired;
19};
20
22 Consumption timer;
23 Time::Timestamp baseline;
24};
25
33inline Consumption consume(Time::Timestamp value, Time::Timestamp interval, bool armed,
34 Time::Timestamp elapsed) {
35 if (!armed || !elapsed) {
36 return {value, armed, false};
37 }
38 if (elapsed < value) {
39 return {value - elapsed, true, false};
40 }
41 if (!interval) {
42 return {0, false, true};
43 }
44
45 const Time::Timestamp overshoot = elapsed - value;
46 const Time::Timestamp phase = overshoot % interval;
47 return {phase ? interval - phase : interval, true, true};
48}
49
57inline AbsoluteConsumption consumeAbsolute(Time::Timestamp value, Time::Timestamp interval,
58 bool armed, Time::Timestamp baseline,
59 Time::Timestamp current) {
60 if (current <= baseline) {
61 return {{value, armed, false}, baseline};
62 }
63
64 return {consume(value, interval, armed, current - baseline), current};
65}
66} // namespace PosixIntervalTimerState
67
68#endif