public void testAwait_Timeout(AwaitMethod awaitMethod) { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); sync.acquire(); assertAwaitTimesOut(c, awaitMethod); sync.release(); }
@Override public void initialize() { sync = new Mutex(); assertTrue(sync.getSharedQueuedThreads().isEmpty()); sync.acquire(1); assertTrue(sync.getSharedQueuedThreads().isEmpty()); }
public void thread2() throws InterruptedException { waitForTick(1); getThread(1).interrupt(); sync.acquire(1); c.signal(); sync.release(1); }
/** tryAcquire on a released sync succeeds */ public void testTryAcquire() { Mutex sync = new Mutex(); assertTrue(sync.tryAcquire()); assertTrue(sync.isHeldExclusively()); sync.release(); assertFalse(sync.isHeldExclusively()); }
/** owns is true for a condition created by sync else false */ public void testOwns() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); assertTrue(sync.owns(c)); assertFalse(sync2.owns(c)); }
public void testSignal(final AwaitMethod awaitMethod) { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); final BooleanLatch acquired = new BooleanLatch(); Thread t = newStartedThread( new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(); assertTrue(acquired.releaseShared(0)); await(c, awaitMethod); sync.release(); } }); acquired.acquireShared(0); sync.acquire(); assertHasWaitersLocked(sync, c, t); assertHasExclusiveQueuedThreads(sync, NO_THREADS); c.signal(); assertHasWaitersLocked(sync, c, NO_THREADS); assertHasExclusiveQueuedThreads(sync, t); sync.release(); awaitTermination(t); }
/** getWaitQueueLength(null) throws NullPointerException */ public void testGetWaitQueueLengthNPE() { final Mutex sync = new Mutex(); try { sync.getWaitQueueLength(null); shouldThrow(); } catch (NullPointerException success) { } }
/** hasWaiters(null) throws NullPointerException */ public void testHasWaitersNPE() { final Mutex sync = new Mutex(); try { sync.hasWaiters(null); shouldThrow(); } catch (NullPointerException success) { } }
/** * If this method is called with a mutex object which is locked by the calling thread, that mutex * object is unlocked and the function call returns. Furthermore: If exactly one thread was * blocking on that particular mutex object, then that thread stops blocking, obtains a lock on * that mutex object, and its lockMutex(Object) call returns. If more than one thread was blocking * on that particular mutex objet, then exactly one of the blocking threads is selected somehow. * That lucky thread stops blocking, obtains a lock on the mutex object, and its lockMutex(Object) * call returns. All other threads blocking on that particular mutex object continue to block. If * this method is called with a mutex object which is not locked, then the method call throws an * exception with the error code PKCS11Constants.CKR_MUTEX_NOT_LOCKED. If this method is called * with a mutex object which is locked by some thread other than the calling thread, the behavior * of this method call is undefined. * * @param mutex The mutex object to unlock. * @exception PKCS11Exception If the wrapper should return a differnet value than CKR_OK to the * library. It gets the error-code and returns it as CK_RV. * @preconditions (mutex <> null) * @postconditions */ public void unlockMutex(Object mutex) throws PKCS11Exception { try { Mutex castedMutex = (Mutex) mutex; castedMutex.unlock(); } catch (ClassCastException ex) { throw new PKCS11Exception(PKCS11Constants.CKR_MUTEX_BAD); } }
/** isQueued(null) throws NullPointerException */ public void testIsQueuedNPE() { final Mutex sync = new Mutex(); try { sync.isQueued(null); shouldThrow(); } catch (NullPointerException success) { } }
/** getWaitingThreads throws NPE if null */ public void testGetWaitingThreadsNPE() { final Mutex sync = new Mutex(); try { sync.getWaitingThreads(null); shouldThrow(); } catch (NullPointerException success) { } }
public void thread2() throws InterruptedException { waitForTick(1); sync.acquire(1); c.signal(); sync.release(1); getThread(1).join(SHORT_DELAY_MS); assertFalse(getThread(1).isAlive()); }
public void thread1() throws InterruptedException { sync.acquire(1); assertFalse(sync.hasWaiters(c)); assertEquals(0, sync.getWaitQueueLength(c)); c.await(); assertTick(1); sync.release(1); }
/** Checks that condition c has exactly the given waiter threads. */ void assertHasWaitersLocked(Mutex sync, ConditionObject c, Thread... threads) { assertEquals(threads.length > 0, sync.hasWaiters(c)); assertEquals(threads.length, sync.getWaitQueueLength(c)); assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty()); assertEquals(threads.length, sync.getWaitingThreads(c).size()); assertEquals( new HashSet<Thread>(sync.getWaitingThreads(c)), new HashSet<Thread>(Arrays.asList(threads))); }
/** Calling signalAll without holding sync throws IllegalMonitorStateException */ public void testSignalAll_IMSE() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); try { c.signalAll(); shouldThrow(); } catch (IllegalMonitorStateException success) { } }
public void thread2() throws InterruptedException { waitForTick(2); sync.acquire(1); assertFalse(sync.getWaitingThreads(c).isEmpty()); c.await(); assertTick(3); sync.release(1); }
/** getWaitingThreads throws IllegalMonitorStateException if not synced */ public void testGetWaitingThreadsIMSE() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); try { sync.getWaitingThreads(c); shouldThrow(); } catch (IllegalMonitorStateException success) { } assertHasWaitersUnlocked(sync, c, NO_THREADS); }
public void thread0() { waitForTick(1); assertTrue(sync.hasQueuedThreads()); waitForTick(3); assertTrue(sync.hasQueuedThreads()); getThread(1).interrupt(); waitForTick(4); assertTrue(sync.hasQueuedThreads()); sync.release(1); }
public void thread1() { try { sync.acquire(1); c.awaitNanos(1000 * 1000 * 1000); // 1 sec sync.release(1); fail("should throw exception"); } catch (InterruptedException success) { assertTick(1); } }
public void thread2() throws InterruptedException { waitForTick(1); sync.acquire(1); assertTrue(sync.hasWaiters(c)); assertEquals(1, sync.getWaitQueueLength(c)); c.await(); assertTick(2); sync.release(1); }
public void thread1() { try { sync.acquire(1); java.util.Date d = new java.util.Date(); c.awaitUntil(new java.util.Date(d.getTime() + 10000)); sync.release(1); fail("should throw exception"); } catch (InterruptedException success) { assertTick(1); } }
/** getWaitQueueLength throws IllegalArgumentException if not owned */ public void testGetWaitQueueLengthIAE() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); try { sync2.getWaitQueueLength(c); shouldThrow(); } catch (IllegalArgumentException success) { } assertHasWaitersUnlocked(sync, c, NO_THREADS); }
public void execute(final Runnable run) { Object prev = IN.get(); assertEquals("No previous value set", null, prev); IN.set(MutexWrapTest.this); try { run.run(); } finally { IN.set(prev); } assertFalse("No read", m.isReadAccess()); assertFalse("No write", m.isWriteAccess()); }
public static void main(String[] argv) throws Exception { mbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class); if (!mbean.isSynchronizerUsageSupported()) { System.out.println("Monitoring of synchronizer usage not supported"); return; } thread.setDaemon(true); thread.start(); // wait until myThread acquires mutex and lock owner is set. while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } long[] ids = new long[] {thread.getId()}; // validate the local access ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); // validate the remote access infos = mbean.getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); boolean found = false; infos = mbean.dumpAllThreads(true, true); for (ThreadInfo ti : infos) { if (ti.getThreadId() == thread.getId()) { thread.checkThreadInfo(ti); found = true; } } if (!found) { throw new RuntimeException("No ThreadInfo found for MyThread"); } System.out.println("Test passed"); }
public void thread3() { waitForTick(1); assertTrue(sync.hasContended()); waitForTick(3); assertTrue(sync.hasContended()); getThread(1).interrupt(); waitForTick(4); assertTrue(sync.hasContended()); sync.release(1); }
public void thread3() { waitForTick(1); assertTrue(sync.getSharedQueuedThreads().isEmpty()); waitForTick(3); assertTrue(sync.getSharedQueuedThreads().isEmpty()); getThread(1).interrupt(); waitForTick(4); assertTrue(sync.getSharedQueuedThreads().isEmpty()); sync.release(1); }
/** tryAcquire on exclusively held sync fails */ public void testTryAcquireWhenSynced() { final Mutex sync = new Mutex(); sync.acquire(); Thread t = newStartedThread( new CheckedRunnable() { public void realRun() { assertFalse(sync.tryAcquire()); } }); awaitTermination(t); sync.release(); }
public void thread3() { waitForTick(1); assertTrue(sync.getExclusiveQueuedThreads().contains(getThread(1))); waitForTick(3); assertTrue(sync.getExclusiveQueuedThreads().contains(getThread(1))); assertTrue(sync.getExclusiveQueuedThreads().contains(getThread(2))); getThread(1).interrupt(); waitForTick(4); assertFalse(sync.getExclusiveQueuedThreads().contains(getThread(1))); assertTrue(sync.getExclusiveQueuedThreads().contains(getThread(2))); sync.release(1); }
/** * If this method is called on with a mutex object which is not locked, the calling thread obtains * a lock on that mutex object and returns. If this method is called with a mutex object which is * locked by some thread other than the calling thread, the calling thread blocks and waits for * that mutex to be unlocked. If this method is called with a a mutex object which is locked by * the calling + thread, the behavior of this method call is undefined. * * @param mutex The mutex object to lock. * @exception PKCS11Exception If the wrapper should return a differnet value than CKR_OK to the * library. It gets the error-code and returns it as CK_RV. * @preconditions (mutex <> null) * @postconditions */ public void lockMutex(Object mutex) throws PKCS11Exception { try { Mutex castedMutex = (Mutex) mutex; while (true) { try { castedMutex.lock(); break; } catch (InterruptedException ex) { // try again, until we succeed } } } catch (ClassCastException ex) { throw new PKCS11Exception(PKCS11Constants.CKR_MUTEX_BAD); } }
/** Calling await without holding sync throws IllegalMonitorStateException */ public void testAwait_IMSE() { final Mutex sync = new Mutex(); final ConditionObject c = sync.newCondition(); for (AwaitMethod awaitMethod : AwaitMethod.values()) { long startTime = System.nanoTime(); try { await(c, awaitMethod); shouldThrow(); } catch (IllegalMonitorStateException success) { } catch (InterruptedException e) { threadUnexpectedException(e); } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); } }