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 conditions:
12 // * Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimers.
15 // * Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimers in the
17 // documentation and/or other materials provided with the distribution.
19 // * The names of the Software's authors and/or contributors
20 // may not be used to endorse or promote products derived from
21 // this Software without specific prior written permission.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
25 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
31 #include <kern/arch.h>
32 #include <kern/time.h>
33 #include <lowlevel/clock.h>
34 #include <System/Exceptions.h>
39 void MonotonicClock::get_time(Time *time)
42 throw_idl(InvalidArgument, 0, nullarray);
44 AutoSpinLockRecIRQ autolock(lock);
45 s32 nanos = get_nanos(ll_getclock() - last_tick_llclock);
47 *time = last_tick_time;
50 while (time->nanos >= 1000000000) {
51 time->nanos -= 1000000000;
56 void MonotonicClock::get_resolution(Time *res)
59 throw_idl(InvalidArgument, 0, nullarray);
63 if (llclocks_per_second >= 1000000000)
66 res->nanos = 1000000000 / llclocks_per_second;
69 void MonotonicClock::tick()
71 unsigned long irq = lock.lock_recirq();
73 while (ll_getclock() - last_tick_llclock > llclocks_per_tick) {
74 last_tick_llclock += llclocks_per_tick;
75 last_tick_time += clock_tick_increment;
78 Time next_tick = last_tick_time + clock_tick_increment;
79 lock.unlock_recirq(irq);
81 tick_timer.arm(next_tick);
84 void MonotonicClock::new_timer(Timer *timer)
87 throw_idl(InvalidArgument, 0, nullarray);
94 void MonotonicClock::timer_tick(KTimerEntry *entry)
96 MonotonicClock *clock = static_cast<MonotonicClock *>(entry->data);
100 void MonotonicClock::init_tick_timer()
102 tick_timer.mux = monotonic_timers;
103 tick_timer.func = timer_tick;
104 tick_timer.data = this;
106 last_tick_llclock = ll_getclock();
107 last_tick_time.seconds = 0;
108 last_tick_time.nanos = 0;
110 Time next_tick = clock_tick_increment;
111 tick_timer.arm(next_tick);
116 void HWTimer::get_expiry(Time *EXPIRY, uint8_t *ARMED)
124 MonotonicClock monotonic_clock;
125 TimerMux *monotonic_timers;
130 hw_timer->calibrate();
131 monotonic_timers = new TimerMux(monotonic_clock, *hw_timer);
132 monotonic_clock.init_tick_timer();
136 #include <servers/core/time/footer.cc>