samer@0: samer@0: package saml.sched; samer@0: samer@0: public class HRTimer { samer@0: samer@2: private static long min_sleep=1200000; // 1.2 ms samer@2: private static long wake_before=500000; // 0.5ms samer@0: samer@0: public static void setMinimumSleepTime(long t) { min_sleep=t; } samer@0: public static void setWakePrealarm(long t) { wake_before=t; } samer@0: samer@0: public static long now() { return System.nanoTime(); } samer@0: public static long sleepUntil(long t1) throws Exception { samer@0: long t0=System.nanoTime(); samer@0: long dt=t1-t0; samer@0: // System.out.println("need to wait for " + dt +"ns"); samer@0: if (dt>min_sleep) { samer@0: long sleep_time=dt-wake_before; samer@0: long millis=sleep_time/1000000; samer@0: int nanos=(int)(sleep_time-millis*1000000); samer@0: // System.out.println("will sleep for "+millis+"ms + "+nanos+"ns"); samer@0: Thread.sleep(millis,nanos); samer@0: } samer@0: samer@0: // go into a tight loop for the last few microseconds samer@0: t0=System.nanoTime(); samer@0: // System.out.println("need to spin for "+(t1-t0)+"ns"); samer@0: while (t0