]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/include/kern/time.h
minor doc updates
[polintos/scott/priv.git] / kernel / include / kern / time.h
1 #ifndef _KERN_TIME_H
2 #define _KERN_TIME_H
3
4 #include <kernel/time.h>
5 #include <util/spinlock.h>
6
7 namespace Time {
8         // The clock timer ticks 20 times per second.  This timer is only used
9         // for maintaining the wall clock time; user timers, rescheduling, and
10         // other purposes are dealt with separately, so this timer only needs
11         // to run often enough to avoid overrun problems.
12         
13         static const int clock_tick_rate = 20;
14         
15         static const Time clock_tick_increment = {
16                 0, 1000000000 / clock_tick_rate
17         };
18
19         // KTimerEntry overrides the standard TimerEntry in order
20         // to bypass the asynchronous Notifier, so that low-level
21         // things like rescheduling timers can work.
22         
23         struct KTimerEntry : public TimerEntry {
24         public:
25                 KTimerEntry()
26                 {
27                 }
28                 
29                 KTimerEntry(TimerMux *MUX) : TimerEntry(MUX)
30                 {
31                 }
32         
33                 typedef void (*functype)(KTimerEntry *kte);
34                 functype func;
35                 void *data;
36                 
37                 void action()
38                 {
39                         func(this);
40                 }
41         };
42
43         // This clock does not change when the wall clock is changed, but
44         // rather increases monotonically.  Most timers should use this
45         // rather than wall time.
46
47         class MonotonicClock {
48                 s64 llclocks_per_second;
49                 s64 llclocks_per_tick;
50         
51                 // The clock's value as of the last tick.
52                 
53                 Time last_tick_time;
54                 s64 last_tick_llclock;
55                 
56                 Lock::SpinLock lock;
57                 
58                 s32 get_nanos(s32 llclocks)
59                 {
60                         return llclocks * 1000000000LL / llclocks_per_second;
61                 }
62                 
63                 KTimerEntry tick_timer;
64
65                 static void timer_tick(KTimerEntry *entry);
66         
67         public:
68                 #include <servers/core/time/Time/MonotonicClock.h>
69                 
70                 MonotonicClock()
71                 {
72                         init_iface();
73                 }
74                 
75                 virtual ~MonotonicClock()
76                 {
77                 }
78                 
79                 void get_time(Time *time);
80                 void get_resolution(Time *res);
81                 
82                 void new_timer(Timer *timer);
83                 
84                 void calibrate(u64 llclocks_per_second_init)
85                 {
86                         llclocks_per_second = llclocks_per_second_init;
87                         llclocks_per_tick = llclocks_per_second / clock_tick_rate;
88                         
89                         printf("MonotonicClock::calibrate: %lld llclocks per second, %lld per tick\n",
90                                llclocks_per_second, llclocks_per_tick);
91                 }
92                 
93                 // This is called every clock tick to update last_tick_time
94                 // and last_tick_nanos.
95                 
96                 void tick();
97
98                 void init_tick_timer();
99         };
100         
101         extern MonotonicClock monotonic_clock;
102         
103         class HWTimer {
104         protected:
105                 Time expiry;
106                 bool armed;
107         
108         public:
109                 #include <servers/core/time/Time/HWTimer.h>
110         
111                 HWTimer()
112                 {
113                         init_iface();
114                         expiry.seconds = 0;
115                         expiry.nanos = 0;
116                 }
117         
118                 virtual ~HWTimer()
119                 {
120                 }
121                 
122                 // If a hardware timer is slow to reprogram, it may ignore
123                 // arm() if time is later than the currently programmed time.
124                 // Returns true if the timer action should be run immediately.
125                 
126                 virtual void arm(Time new_expiry) = 0;
127                 virtual void disarm() = 0;
128                 virtual void calibrate() = 0;
129                 
130                 void get_expiry(Time *expiry, uint8_t *armed);
131
132                 void set_action(System::Events::Event *event)
133                 {
134                         BUG();
135                 }
136         };
137         
138         extern HWTimer *hw_timer;
139         extern TimerMux *monotonic_timers;
140         extern KTimerEntry *tick_timer;
141         void init();
142 }
143
144 #endif