/* * convert milliseconds to fast ticks */ uvlong ms2fastticks(ulong ms) { if(!tod.init) todinit(); return (tod.hz*ms)/1000ULL; }
/* * Set the time of day struct */ void todset(vlong t, vlong delta, int n) { if(!tod.init) todinit(); ilock(&tod); if(t >= 0){ tod.off = t; tod.last = fastticks(nil); tod.lasttime = 0; tod.delta = 0; tod.sstart = tod.send; } else { if(n <= 0) n = 1; n *= HZ; if(delta < 0 && n > -delta) n = -delta; if(delta > 0 && n > delta) n = delta; if (n == 0) { iprint("todset: n == 0, delta == %lld\n", delta); delta = 0; } else delta /= n; tod.sstart = MACHP(0)->ticks; tod.send = tod.sstart + n; tod.delta = delta; } iunlock(&tod); }
/* * Set the time of day struct */ void todset(int64_t t, int64_t delta, int n) { if(!tod.init) todinit(); ilock(&tod.Lock); if(t >= 0){ tod.off = t; tod.last = fastticks(nil); tod.lasttime = 0; tod.delta = 0; tod.sstart = tod.send; } else { if(n <= 0) n = 1; n *= HZ; if(delta < 0 && n > -delta) n = -delta; if(delta > 0 && n > delta) n = delta; delta = delta/n; tod.sstart = sys->ticks; tod.send = tod.sstart + n; tod.delta = delta; } iunlock(&tod.Lock); }
/* * convert milliseconds to fast ticks */ uint64_t ms2fastticks(uint32_t ms) { if(!tod.init) todinit(); return (tod.hz*ms)/1000ULL; }
/* * convert fast ticks to ns */ uvlong fastticks2ns(uvlong ticks) { uvlong res; if(!tod.init) todinit(); mul64fract(&res, ticks, tod.multiplier); return res; }
/* * convert nanoseconds to fast ticks */ uvlong ns2fastticks(uvlong ns) { uvlong res; if(!tod.init) todinit(); mul64fract(&res, ns, tod.divider); return res; }
/* * convert fast ticks to ns */ uint64_t fastticks2ns(uint64_t ticks) { uint64_t res; if(!tod.init) todinit(); mul64fract(&res, ticks, tod.multiplier); return res; }
/* * convert nanoseconds to fast ticks */ uint64_t ns2fastticks(uint64_t ns) { uint64_t res; if(!tod.init) todinit(); mul64fract(&res, ns, tod.divider); return res; }
static void consinit(void) { #if 0 todinit(); #endif /* * at 115200 baud, the 1024 char buffer takes 56 ms to process, * processing it every 22 ms should be fine */ #if 0 addclock0link(kbdputcclock, 22); #endif }
/* * get time of day */ vlong todget(vlong *ticksp) { uvlong x; vlong ticks, diff; ulong t; if(!tod.init) todinit(); /* * we don't want time to pass twixt the measuring of fastticks * and grabbing tod.last. Also none of the vlongs are atomic so * we have to look at them inside the lock. */ ilock(&tod); tod.cnt++; ticks = fastticks(nil); /* add in correction */ if(tod.sstart != tod.send){ t = MACHP(0)->ticks; if(t >= tod.send) t = tod.send; tod.off = tod.off + tod.delta*(t - tod.sstart); tod.sstart = t; } /* convert to epoch */ diff = ticks - tod.last; if(diff < 0) diff = 0; mul64fract(&x, diff, tod.multiplier); x += tod.off; /* time can't go backwards */ if(x < tod.lasttime) x = tod.lasttime; else tod.lasttime = x; iunlock(&tod); if(ticksp != nil) *ticksp = ticks; return x; }
void timersinit(void) { Timer *t; /* * T->tf == nil means the HZ clock for this processor. */ todinit(); t = malloc(sizeof(*t)); t->tmode = Tperiodic; t->tt = nil; t->tns = 1000000000/HZ; t->tf = nil; timeradd(t); }
static void consinit(void) { todinit(); }
static void consinit(void) { todinit(); randominit(); }