The Pedigree Project 0.1
IrqEventCounter.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_KERNEL_MACHINE_IRQEVENTCOUNTER_H
9#define PEDIGREE_KERNEL_MACHINE_IRQEVENTCOUNTER_H
10#include "pedigree/kernel/Atomic.h"
11#include "pedigree/kernel/processor/types.h"
12
13#include <config.h>
14
23 public:
24 IrqEventCounter() : m_Count(0) {}
25
33 bool recordFromInterrupt(size_t occurrences = 1) {
34 if (!occurrences) {
35 return true;
36 }
37
38 constexpr size_t Maximum = ~static_cast<size_t>(0);
39 const size_t count = m_Count.value();
40 if (occurrences > (Maximum - count)) {
41 return false;
42 }
43
44 // There is one hard producer. A concurrent consumer can only reset
45 // the counter to zero, which creates capacity rather than invalidating
46 // this preflight check.
47 m_Count += occurrences;
48 return true;
49 }
50
52 size_t takeAll() {
53 size_t count = m_Count.value();
54 while (count && !m_Count.compareAndSwap(count, 0)) {
55 count = m_Count.value();
56 }
57 return count;
58 }
59
60 bool pending() const {
61 return m_Count.value() != 0;
62 }
63
64 void reset() {
65 m_Count = 0;
66 }
67
68 private:
69 Atomic<size_t> m_Count;
70
71 IrqEventCounter(const IrqEventCounter&) = delete;
72 IrqEventCounter& operator=(const IrqEventCounter&) = delete;
73};
74
75#endif
bool recordFromInterrupt(size_t occurrences=1)