/* * called regularly to avoid calculation overflows */ static void todfix(void) { vlong ticks, diff; uvlong x; ticks = fastticks(nil); diff = ticks - tod.last; if(diff <= tod.hz) return; ilock(&tod); diff = ticks - tod.last; if(diff > tod.hz){ /* convert to epoch */ mul64fract(&x, diff, tod.multiplier); if(x > 30000000000ULL) iprint("todfix %llud\n", x); x += tod.off; /* protect against overflows */ tod.last = ticks; tod.off = x; } iunlock(&tod); }
/* * 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; }
/* * convert time of day to ticks */ uvlong tod2fastticks(vlong ns) { uvlong x; ilock(&tod); mul64fract(&x, ns-tod.off, tod.divider); x += tod.last; iunlock(&tod); return x; }
/* * convert time of day to ticks */ uint64_t tod2fastticks(int64_t ns) { uint64_t x; ilock(&tod.Lock); mul64fract(&x, ns-tod.off, tod.divider); x += tod.last; iunlock(&tod.Lock); return x; }
/* * 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; }
/* * called regularly to avoid calculation overflows */ static void todfix(void) { int64_t ticks, diff; uint64_t x; ticks = fastticks(nil); diff = ticks - tod.last; if(diff > tod.hz){ ilock(&tod.Lock); /* convert to epoch */ mul64fract(&x, diff, tod.multiplier); if(x > 30000000000ULL) print("todfix %llu\n", x); x += tod.off; /* protect against overflows */ tod.last = ticks; tod.off = x; iunlock(&tod.Lock); } }
/* * called regularly to avoid calculation overflows */ void todfix(void) { vlong ticks, diff; uvlong x; ticks = fastticks(nil); diff = ticks - tod.last; if(diff > tod.hz){ ilock(&tod); // convert to epoch mul64fract(&x, diff, tod.multiplier); if(x > 30000000000ULL) print("todfix %llud\n", x); x += tod.off; // protect against overflows tod.last = ticks; tod.off = x; iunlock(&tod); } }