/** * Tries to acquire write lock for a given transaction. If <CODE>this.writeCount</CODE> is greater * than the currents tx's write count or the read count is greater than the currents tx's read * count the transaction has to wait and the {@link RagManager#checkWaitOn} method is invoked for * deadlock detection. * * <p>If the lock can be acquires the lock count is updated on <CODE>this</CODE> and the * transaction lock element (tle). * * @throws DeadlockDetectedException if a deadlock is detected */ synchronized void acquireWriteLock(Object tx) throws DeadlockDetectedException { TxLockElement tle = getOrCreateLockElement(tx); try { tle.movedOn = false; boolean shouldAddWait = true; Thread currentThread = currentThread(); while (totalWriteCount > tle.writeCount || totalReadCount > tle.readCount) { ragManager.checkWaitOn(this, tx); if (shouldAddWait) { waitingThreadList.addFirst(new WaitElement(tle, WRITE, currentThread)); } try { wait(); shouldAddWait = false; } catch (InterruptedException e) { interrupted(); shouldAddWait = true; } ragManager.stopWaitOn(this, tx); } registerWriteLockAcquired(tx, tle); } finally { // if deadlocked, remove marking so lock is removed when empty tle.movedOn = true; marked--; } }
private void waitIndefinitely() { while (true) { try { sleep(MAX_VALUE); } catch (InterruptedException e) { interrupted(); // meh } } }
private void deadlockGuardedWait( Object tx, TxLockElement tle, LockType lockType) { // given: we must be in a synchronized block here ragManager.checkWaitOn(this, tx); waitingThreadList.addFirst(new WaitElement(tle, lockType, currentThread())); try { wait(); } catch (InterruptedException e) { interrupted(); } ragManager.stopWaitOn(this, tx); }
public void testNoOpServiceStartStopAndWaitUninterruptible() throws Exception { NoOpService service = new NoOpService(); currentThread().interrupt(); try { service.startAndWait(); assertEquals(State.RUNNING, service.state()); service.stopAndWait(); assertEquals(State.TERMINATED, service.state()); assertTrue(currentThread().isInterrupted()); } finally { Thread.interrupted(); // clear interrupt for future tests } }
public int waitFor() throws InterruptedException { waitForInterruptibly(handle); if (Thread.interrupted()) throw new InterruptedException(); return exitValue(); }