/** getState is true when acquired and false when not */
  public void testGetState() {
    final Mutex sync = new Mutex();
    sync.acquire();
    assertTrue(sync.isHeldExclusively());
    sync.release();
    assertFalse(sync.isHeldExclusively());

    final BooleanLatch acquired = new BooleanLatch();
    final BooleanLatch done = new BooleanLatch();
    Thread t =
        newStartedThread(
            new CheckedRunnable() {
              public void realRun() throws InterruptedException {
                sync.acquire();
                assertTrue(acquired.releaseShared(0));
                done.acquireShared(0);
                sync.release();
              }
            });

    acquired.acquireShared(0);
    assertTrue(sync.isHeldExclusively());
    assertTrue(done.releaseShared(0));
    awaitTermination(t);
    assertFalse(sync.isHeldExclusively());
  }
 /** tryAcquire on a released sync succeeds */
 public void testTryAcquire() {
   Mutex sync = new Mutex();
   assertTrue(sync.tryAcquire());
   assertTrue(sync.isHeldExclusively());
   sync.release();
   assertFalse(sync.isHeldExclusively());
 }
  /** A serialized AQS deserializes with current state, but no queued threads */
  public void testSerialization() {
    Mutex sync = new Mutex();
    assertFalse(serialClone(sync).isHeldExclusively());
    sync.acquire();
    Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
    waitForQueuedThread(sync, t);
    assertTrue(sync.isHeldExclusively());

    Mutex clone = serialClone(sync);
    assertTrue(clone.isHeldExclusively());
    assertHasExclusiveQueuedThreads(sync, t);
    assertHasExclusiveQueuedThreads(clone, NO_THREADS);
    t.interrupt();
    awaitTermination(t);
    sync.release();
    assertFalse(sync.isHeldExclusively());
    assertTrue(clone.isHeldExclusively());
    assertHasExclusiveQueuedThreads(sync, NO_THREADS);
    assertHasExclusiveQueuedThreads(clone, NO_THREADS);
  }
  /** acquireInterruptibly succeeds when released, else is interruptible */
  public void testAcquireInterruptibly() throws InterruptedException {
    final Mutex sync = new Mutex();
    final BooleanLatch threadStarted = new BooleanLatch();
    sync.acquireInterruptibly();
    Thread t =
        newStartedThread(
            new CheckedInterruptedRunnable() {
              public void realRun() throws InterruptedException {
                assertTrue(threadStarted.releaseShared(0));
                sync.acquireInterruptibly();
              }
            });

    threadStarted.acquireShared(0);
    waitForQueuedThread(sync, t);
    t.interrupt();
    awaitTermination(t);
    assertTrue(sync.isHeldExclusively());
  }
 /** isHeldExclusively is false upon construction */
 public void testIsHeldExclusively() {
   Mutex sync = new Mutex();
   assertFalse(sync.isHeldExclusively());
 }