private double RunOne(long n) { long t0 = HPTimer.Now(); _r(n); return(HPTimer.SecondsSince(t0)); }
public void Add(AlarmListener listener, Object state, int delay) { if (listener == null) { throw new ArgumentNullException("listener"); } if (delay <= 0) { throw new ArgumentException("delay <= 0"); } CheckIsStarted(); long due = HPTimer.Now() + delay * HPTimer.NS_PER_MILLISECOND; Alarm alarm = GetAlarm(listener); if (alarm != null) { // schedule is being adjusted Dequeue(alarm); alarm.setDue(due); alarm.setState(state); Enqueue(alarm); } else { alarm = new Alarm(listener, state, due); AddAlarm(listener, alarm); Enqueue(alarm); } NotifyWorker("add"); }
public bool Put(T obj, int maxDelay) { if (obj == null) { throw new ArgumentNullException("t == null"); } if (IsClosed()) { return(false); } // the queue is not closed. if (!IsFull()) { PutAndNotify(obj); return(true); } // the queue is not closed, the queue is full. if (maxDelay < 0) { return(false); } long now = HPTimer.Now(); long end = EndTime(now, maxDelay); int d; while ((d = RemTime(end, now)) > 0) { // the queue is not closed, the queue is full, and delay has not run out... Debug.Assert(!IsClosed()); Debug.Assert(IsFull()); System.Threading.Monitor.Wait(this, d); if (IsClosed()) { return(false); } // the queue is not closed. if (!IsFull()) { PutAndNotify(obj); return(true); } now = HPTimer.Now(); } return(false); }
private void Update(Alarm alarm, int delay) { long due = delay > 0 ? alarm.getDue() + delay * HPTimer.NS_PER_MILLISECOND : HPTimer.Now() - delay * HPTimer.NS_PER_MILLISECOND; alarm.setDue(due); Enqueue(alarm); NotifyWorker("update"); }
public void foo() { long x = HPTimer.Now(); for (int i = 0; i < 10000; i++) { // assert that values from HPTimer.Now() are always monotonically increasing long y = HPTimer.Now(); Assert.GreaterOrEqual(y, x); long diff = y - x; Assert.GreaterOrEqual(y, 0); int idiff = (int)diff; Assert.GreaterOrEqual(idiff, 0); x = y; Thread.Sleep(1); } }
public void waitUntilNotEqAndSet4() { Monitor <int?> m = new Monitor <int?>("a", null); Thread t = TestHelp.delayAndStart(200, new Thread(new ThreadStart(delegate() { m.Set(1); }))); long t0 = HPTimer.Now(); m.WaitUntilNotEqAndSet(null, 2); double t1 = (HPTimer.Now() - t0) / 1000000.0; TestHelp.AssertRelError(200, t1, REL_ERROR); t.Join(); }
public void get() { CircularQueue <int?> queue = new CircularQueue <int?>(1); // System.nanoTime(); HPTimer.Now(); Assert.IsNull(queue.Get(10)); assertRelError(1, 1, 1); foreach (int i in new int[] { 10, 20, 30, 50, 80, 130, 210, 340, 550, 890, 1440 }) { // Console.WriteLine( "get delay = {0}\n", i ); long t0 = HPTimer.Now(); Assert.IsNull(queue.Get(i)); /* double t = (HPTimer.Now() - t0) / 1000000.0; * assertRelError( i, t, 2 ); */ } }
public void waitUntilEqAndSet4() { Monitor <int?> m = new Monitor <int?>("a", null); Thread t = TestHelp.delayAndStart(200, new Thread(new ThreadStart(delegate() { m.Set(1); }))); long t0 = HPTimer.Now(); m.WaitUntilEqAndSet(1, 2); double t1 = (HPTimer.Now() - t0) / 1000000.0; // TODO is there a better way for choosing relative error? TestHelp.AssertRelError(200, t1, REL_ERROR); t.Join(); }
public T Get(int maxDelay) { if (!IsEmpty()) { return(GetAndNotify()); } if (IsClosed() || maxDelay < 0) { return(default(T)); //null; } // the queue is empty, not closed, and caller has requested a delay long now = HPTimer.Now(); long end = EndTime(now, maxDelay); Debug.Assert(end > now, "end > now"); int d; while ((d = RemTime(end, now)) > 0) { // the queue is empty, not closed, and delay has not run out... Debug.Assert(IsEmpty(), "IsEmpty()"); Debug.Assert(!IsClosed(), "!IsClosed()"); System.Threading.Monitor.Wait(this, d); if (!IsEmpty()) { return(GetAndNotify()); } if (IsClosed()) { return(default(T)); //null; } now = HPTimer.Now(); } return(default(T)); // null; }
public void WaitUntilEq(T desiredValue, int maxDelay) { CheckDelay(maxDelay); long now = HPTimer.Now(); long end = EndTime(now, maxDelay); int d; while (!Eq(value, desiredValue) && (d = RemTime(end, now)) > 0) { System.Threading.Monitor.Wait(this, d); now = HPTimer.Now(); } if (!Eq(value, desiredValue)) { throw new ThreadInterruptedException("timeout"); } }
public void one() { System.Console.WriteLine("one starting"); long x = HPTimer.Now(); for (int i = 0; i < 10000; i++) { // assert that values from HPTimer.Now() are always monotonically increasing long y = HPTimer.Now(); Assert.GreaterOrEqual(y, x); long diff = y - x; Assert.GreaterOrEqual(y, 0); int idiff = (int)diff; Assert.GreaterOrEqual(idiff, 0); x = y; Thread.Sleep(1); } System.Console.WriteLine("one done"); }
private Alarm getNextDueAlarm() { // ok, the worker needs to get the next due alarm and // then wait until its due time, then return it. if alerted // by notifyWorker, it should refresh the next due alarm. // one trick will be in excluding multiple workers from // coming in here at the same time. lock (getNextDueAlarmSync) { lock (this) { while (true) { // if (!IsStarted()) { return(null); } Alarm alarm = getFirst(); if (alarm == null) { try { //Console.WriteLine(" Waiting in getNextDueAlarm "); Monitor.Wait(this, Int32.MaxValue); //Console.WriteLine(" Done Waiting in getNextDueAlarm "); continue; } catch (Exception e) { Console.WriteLine(e); return(null); } } long delayNs = alarm.getDue() - HPTimer.Now(); if (delayNs > 0) { try { long delay = delayNs / HPTimer.NS_PER_MILLISECOND; if (delay > 0) { int d = (int)delay; Monitor.Wait(this, d); } continue; } catch (Exception e) { Console.WriteLine(e); return(null); } } // the alarm being returned has not been removed // from alarmsByListener. it is presumed that the // alarm will be set again. if not, it should be // removed. Dequeue(alarm); return(alarm); } } } }