forked from uchan-nos/mikanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.hpp
49 lines (40 loc) · 1.17 KB
/
timer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include <cstdint>
#include <queue>
#include <vector>
#include <limits>
#include "message.hpp"
void InitializeLAPICTimer();
void StartLAPICTimer();
uint32_t LAPICTimerElapsed();
void StopLAPICTimer();
class Timer {
public:
Timer(unsigned long timeout, int value, uint64_t task_id);
unsigned long Timeout() const { return timeout_; }
int Value() const { return value_; }
uint64_t TaskID() const { return task_id_; }
private:
unsigned long timeout_;
int value_;
uint64_t task_id_;
};
/** @brief タイマー優先度を比較する。タイムアウトが遠いほど優先度低。 */
inline bool operator<(const Timer& lhs, const Timer& rhs) {
return lhs.Timeout() > rhs.Timeout();
}
class TimerManager {
public:
TimerManager();
void AddTimer(const Timer& timer);
bool Tick();
unsigned long CurrentTick() const { return tick_; }
private:
volatile unsigned long tick_{0};
std::priority_queue<Timer> timers_{};
};
extern TimerManager* timer_manager;
extern unsigned long lapic_timer_freq;
const int kTimerFreq = 100;
const int kTaskTimerPeriod = static_cast<int>(kTimerFreq * 0.02);
const int kTaskTimerValue = std::numeric_limits<int>::max();