/** * Creates and returns a new lock. */ public synchronized OrderedLock newLock() { return new OrderedLock(this); }
/** * Re-acquires a suspended lock and reverts to the correct lock depth. */ public void resume() { //spin until the lock is successfully acquired //NOTE: spinning here allows the UI thread to service pending syncExecs //if the UI thread is waiting to acquire a lock. while (true) { try { if (lock.acquire(Long.MAX_VALUE)) break; } catch (InterruptedException e) { //ignore and loop } } lock.setDepth(depth); } }
/** * Returns null if acquired and a Semaphore object otherwise. If a * waiting semaphore already exists for this thread, it will be returned, * otherwise a new semaphore will be created, enqueued, and returned. */ private synchronized Semaphore createSemaphore() { return attempt() ? null : enqueue(new Semaphore(Thread.currentThread())); }
public boolean acquire(long delay) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); boolean success = false; if (delay <= 0) return attempt(); Semaphore semaphore = createSemaphore(); if (semaphore == null) return true; if (DEBUG) System.out.println("[" + Thread.currentThread() + "] Operation waiting to be executed... " + this); //$NON-NLS-1$ //$NON-NLS-2$ success = doAcquire(semaphore, delay); manager.resumeSuspendedLocks(Thread.currentThread()); if (DEBUG) System.out.println("[" + Thread.currentThread() + //$NON-NLS-1$ (success ? "] Operation started... " : "] Operation timed out... ") + this); //$NON-NLS-1$ //$NON-NLS-2$ //} return success; }
removeFromQueue(semaphore); depth++; manager.addLockThread(currentOperationThread, this); semaphore = createSemaphore(); if (semaphore == null) return true; return updateOperationQueue(semaphore, success);
removeFromQueue(semaphore); depth++; manager.addLockThread(currentOperationThread, this); semaphore = createSemaphore(); if (semaphore == null) return true; updateCurrentOperation(); } else { removeFromQueue(semaphore); manager.removeLockWaitThread(Thread.currentThread(), this);
/** * This lock has just been granted to a new thread (the thread waited for it). * Remove the request from the queue and update both the graph and the lock. */ private synchronized void updateCurrentOperation() { operations.dequeue(); setCurrentOperationThread(Thread.currentThread()); }
/** * We have finished waiting on the given semaphore. Update the operation queue according * to whether we succeeded in obtaining the lock. * * @param semaphore The semaphore that we waited on * @param acquired <code>true</code> if we successfully acquired the semaphore, and <code>false</code> otherwise * @return whether the lock was successfully obtained */ private synchronized boolean updateOperationQueue(Semaphore semaphore, boolean acquired) { // Bug 311863 - Semaphore may have been released concurrently, so check again before discarding it if (!acquired) acquired = semaphore.attempt(); if (acquired) { depth++; updateCurrentOperation(); } else { removeFromQueue(semaphore); manager.removeLockWaitThread(Thread.currentThread(), this); } return acquired; }
/** * Suspend this lock by granting the lock to the next lock in the queue. * Return the depth of the suspended lock. */ protected int forceRelease() { int oldDepth = depth; doRelease(); return oldDepth; }
/** * Suspends ownership of the given lock, and returns the saved state. */ protected static LockState suspend(OrderedLock lock) { LockState state = new LockState(); state.lock = lock; state.depth = lock.forceRelease(); return state; }
public void acquire() { //spin until the lock is successfully acquired //NOTE: spinning here allows the UI thread to service pending syncExecs //if the UI thread is waiting to acquire a lock. while (true) { try { if (acquire(Long.MAX_VALUE)) return; } catch (InterruptedException e) { //ignore and loop } } }
@Override public boolean acquire(long delay) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); boolean success = false; if (delay <= 0) return attempt(); Semaphore semaphore = createSemaphore(); if (semaphore == null) return true; if (DEBUG) System.out.println("[" + Thread.currentThread() + "] Operation waiting to be executed... " + this); //$NON-NLS-1$ //$NON-NLS-2$ success = doAcquire(semaphore, delay); manager.resumeSuspendedLocks(Thread.currentThread()); if (DEBUG) System.out.println("[" + Thread.currentThread() + //$NON-NLS-1$ (success ? "] Operation started... " : "] Operation timed out... ") + this); //$NON-NLS-1$ //$NON-NLS-2$ //} if (!success && Thread.interrupted()) throw new InterruptedException(); return success; }
removeFromQueue(semaphore); depth++; manager.addLockThread(currentOperationThread, this); semaphore = createSemaphore(); if (semaphore == null) return true; return updateOperationQueue(semaphore, success);
/** * This lock has just been granted to a new thread (the thread waited for it). * Remove the request from the queue and update both the graph and the lock. */ private synchronized void updateCurrentOperation() { operations.dequeue(); setCurrentOperationThread(Thread.currentThread()); } }
/** * We have finished waiting on the given semaphore. Update the operation queue according * to whether we succeeded in obtaining the lock. * * @param semaphore The semaphore that we waited on * @param acquired <code>true</code> if we successfully acquired the semaphore, and <code>false</code> otherwise * @return whether the lock was successfully obtained */ private synchronized boolean updateOperationQueue(Semaphore semaphore, boolean acquired) { // Bug 311863 - Semaphore may have been released concurrently, so check again before discarding it if (!acquired) acquired = semaphore.attempt(); if (acquired) { depth++; updateCurrentOperation(); } else { removeFromQueue(semaphore); manager.removeLockWaitThread(Thread.currentThread(), this); } return acquired; }
/** * Suspend this lock by granting the lock to the next lock in the queue. * Return the depth of the suspended lock. */ protected int forceRelease() { int oldDepth = depth; doRelease(); return oldDepth; }
/** * Suspends ownership of the given lock, and returns the saved state. */ protected static LockState suspend(OrderedLock lock) { LockState state = new LockState(); state.lock = lock; state.depth = lock.forceRelease(); return state; }
public void acquire() { //spin until the lock is successfully acquired //NOTE: spinning here allows the UI thread to service pending syncExecs //if the UI thread is waiting to acquire a lock. boolean interrupted = false; while (true) { try { if (acquire(Long.MAX_VALUE)) break; } catch (InterruptedException e) { interrupted = true; } } //preserve thread interrupt state if (interrupted) Thread.currentThread().interrupt(); }
@Override public boolean acquire(long delay) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); boolean success = false; if (delay <= 0) return attempt(); Semaphore semaphore = createSemaphore(); if (semaphore == null) return true; if (DEBUG) System.out.println("[" + Thread.currentThread() + "] Operation waiting to be executed... " + this); //$NON-NLS-1$ //$NON-NLS-2$ success = doAcquire(semaphore, delay); manager.resumeSuspendedLocks(Thread.currentThread()); if (DEBUG) System.out.println("[" + Thread.currentThread() + //$NON-NLS-1$ (success ? "] Operation started... " : "] Operation timed out... ") + this); //$NON-NLS-1$ //$NON-NLS-2$ //} if (!success && Thread.interrupted()) throw new InterruptedException(); return success; }
removeFromQueue(semaphore); depth++; manager.addLockThread(currentOperationThread, this); semaphore = createSemaphore(); if (semaphore == null) return true; return updateOperationQueue(semaphore, success);