1 // core/time.cc -- Clocks and timers
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
23 #include <kern/arch.h>
24 #include <kern/time.h>
25 #include <lowlevel/clock.h>
26 #include <System/Exceptions.h>
31 void MonotonicClock::get_time(Time *time)
34 throw_idl(InvalidArgument, 0, nullarray);
36 AutoSpinLockRecIRQ autolock(lock);
37 s32 nanos = get_nanos(ll_getclock() - last_tick_llclock);
39 *time = last_tick_time;
42 while (time->nanos >= 1000000000) {
43 time->nanos -= 1000000000;
48 void MonotonicClock::get_resolution(Time *res)
51 throw_idl(InvalidArgument, 0, nullarray);
55 if (llclocks_per_second >= 1000000000)
58 res->nanos = 1000000000 / llclocks_per_second;
61 void MonotonicClock::tick()
63 unsigned long irq = lock.lock_recirq();
65 while (ll_getclock() - last_tick_llclock > llclocks_per_tick) {
66 last_tick_llclock += llclocks_per_tick;
67 last_tick_time += clock_tick_increment;
70 Time next_tick = last_tick_time + clock_tick_increment;
71 lock.unlock_recirq(irq);
73 tick_timer.arm(next_tick);
76 void MonotonicClock::new_timer(Timer *timer)
79 throw_idl(InvalidArgument, 0, nullarray);
86 void MonotonicClock::timer_tick(KTimerEntry *entry)
88 MonotonicClock *clock = static_cast<MonotonicClock *>(entry->data);
92 void MonotonicClock::init_tick_timer()
94 tick_timer.mux = monotonic_timers;
95 tick_timer.func = timer_tick;
96 tick_timer.data = this;
98 last_tick_llclock = ll_getclock();
99 last_tick_time.seconds = 0;
100 last_tick_time.nanos = 0;
102 Time next_tick = clock_tick_increment;
103 tick_timer.arm(next_tick);
108 void HWTimer::get_expiry(Time *EXPIRY, uint8_t *ARMED)
116 MonotonicClock monotonic_clock;
117 TimerMux *monotonic_timers;
122 hw_timer->calibrate();
123 monotonic_timers = new TimerMux(monotonic_clock, *hw_timer);
124 monotonic_clock.init_tick_timer();
128 #include <servers/core/time/footer.cc>