/** Spin-waits until sync.isQueued(t) becomes true. */
 void waitForQueuedThread(AbstractQueuedLongSynchronizer sync, Thread t) {
   long startTime = System.nanoTime();
   while (!sync.isQueued(t)) {
     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
       throw new AssertionFailedError("timed out");
     Thread.yield();
   }
   assertTrue(t.isAlive());
 }
 /**
  * Delays, via Thread.sleep, for the given millisecond delay, but if the sleep is shorter than
  * specified, may re-sleep or yield until time elapses.
  */
 static void delay(long millis) throws InterruptedException {
   long startTime = System.nanoTime();
   long ns = millis * 1000 * 1000;
   for (; ; ) {
     if (millis > 0L) Thread.sleep(millis);
     else // too short to sleep
     Thread.yield();
     long d = ns - (System.nanoTime() - startTime);
     if (d > 0L) millis = d / (1000 * 1000);
     else break;
   }
 }
 /**
  * Spin-waits up to the specified number of milliseconds for the given thread to enter a wait
  * state: BLOCKED, WAITING, or TIMED_WAITING.
  */
 void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
   long startTime = System.nanoTime();
   for (; ; ) {
     Thread.State s = thread.getState();
     if (s == Thread.State.BLOCKED || s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING)
       return;
     else if (s == Thread.State.TERMINATED) fail("Unexpected thread termination");
     else if (millisElapsedSince(startTime) > timeoutMillis) {
       threadAssertTrue(thread.isAlive());
       return;
     }
     Thread.yield();
   }
 }