#pragma implementation "Timer.h" #include "Timer.h" #include #include #include static tms cpu; static Timer *timerChain = 0; #ifdef m68k double const Timer::hz = 60; #else double const Timer::hz = sysconf(_SC_CLK_TCK); #endif void Timer::wait(struct timeval &to) { if (!timerChain) return; long t = ticks(); if (timerChain) { t = timerChain->target - t; to.tv_sec = (long)(t / hz); long m = (long)(to.tv_sec * hz); if (t < m) { if (to.tv_sec == 0) { to.tv_sec = 0; to.tv_usec = 1; return; } m = (long)(--to.tv_sec * hz); } to.tv_usec = (long)((t - m) / hz * 1000000L); } } unsigned long Timer::ticks() { unsigned long lastclk = times(&cpu); for (Timer *p = timerChain; p && p->before(lastclk); p = timerChain) { timerChain = p->nextTimer; p->timerEvent(); // might alter timerChain, so we reload p } return lastclk; } void Timer::timer(unsigned long tval) { // remove from timerChain if (active) { for (Timer **p = &timerChain; *p; p = &(*p)->nextTimer) { if (*p == this) { *p = nextTimer; break; } } active = 0; } // add to timerChain if (tval) { target = ticks() + tval; Timer **p; for (p = &timerChain; *p; p = &(*p)->nextTimer) { if (before((*p)->target)) break; } nextTimer = *p; *p = this; active = 1; } }