/** {@inheritDoc} */ @Override public void signal() { ctx.kernalContext().gateway().readLock(); try { if (!isHeldExclusively()) throw new IllegalMonitorStateException(); validate(false); addOutgoingSignal(name); } finally { ctx.kernalContext().gateway().readUnlock(); } }
/** {@inheritDoc} */ @Override public void signalAll() { ctx.kernalContext().gateway().readLock(); try { if (!isHeldExclusively()) throw new IllegalMonitorStateException(); sync.validate(false); addOutgoingSignalAll(name); } finally { ctx.kernalContext().gateway().readUnlock(); } } }
int c = getState(); c = getState(); if (compareAndSetGlobalState(0, acquires, current, fair)) { setExclusiveOwnerThread(current); while (!isHeldExclusively() && !interruptAll) Thread.yield(); else if (isHeldExclusively()) { int nextc = c + acquires; throw new Error("Maximum lock count exceeded."); setState(nextc); if (fair && !isQueued(current)) synchronizeQueue(false, current);
int c = getState(); c = getState(); if (compareAndSetGlobalState(0, acquires, current, fair)) { setExclusiveOwnerThread(current); while (!isHeldExclusively() && !interruptAll) Thread.yield(); else if (isHeldExclusively()) { int nextc = c + acquires; throw new Error("Maximum lock count exceeded."); setState(nextc); if (fair && !isQueued(current)) synchronizeQueue(false, current);
/** {@inheritDoc} */ @Override public void onUpdate(GridCacheLockState val) { // Called only on initialization, so it's safe to ignore update. if (sync == null) return; updateLock.lock(); try { // If this update is a result of unsuccessful acquire in fair mode, no local update should be done. if (!val.isChanged()) return; // Check if update came from this node. boolean loc = sync.isLockedLocally(val.getId()); // Process any incoming signals. boolean incomingSignals = sync.checkIncomingSignals(val); // Update permission count. sync.setPermits(val.get()); // Update owner's node id. sync.setCurrentOwnerNode(val.getId()); // Update owner's thread id. sync.setCurrentOwnerThread(val.getThreadId()); // Check if any threads waiting on this node need to be notified. if ((incomingSignals || sync.getPermits() == 0) && !loc) { // Try to notify any waiting threads. sync.release(0); } } finally{ updateLock.unlock(); } }
synchronized boolean checkIncomingSignals(GridCacheLockState state) { if (state.getSignals() == null) return false; LinkedList<String> signals = state.getSignals().get(thisNode); if (signals == null || signals.isEmpty()) return false; UUID tempUUID = getOwnerNode(); Thread tempThread = getExclusiveOwnerThread(); long tempThreadID = currentOwnerThreadId; // Temporarily allow current thread to signal condition object. // This is safe to do because: // 1. if release was called on this node, // it was called from currently active thread; // 2. if release came from a thread on any other node, // all threads on this node are already blocked. setCurrentOwnerNode(thisNode); setExclusiveOwnerThread(Thread.currentThread()); currentOwnerThreadId = Thread.currentThread().getId(); for (String signal: signals) conditionMap.get(signal).signal(); // Restore owner node and owner thread. setCurrentOwnerNode(tempUUID); setExclusiveOwnerThread(tempThread); currentOwnerThreadId = tempThreadID; return true; }
/** Interrupt every thread on this node waiting on this lock. */ private synchronized void interruptAll() { // First release all threads waiting on associated condition queues. if (!conditionMap.isEmpty()) { // Temporarily obtain ownership of the lock, // in order to signal all conditions. UUID tempUUID = getOwnerNode(); long tempThreadID = currentOwnerThreadId; setCurrentOwnerNode(thisNode); currentOwnerThreadId = Thread.currentThread().getId(); for (Condition c : conditionMap.values()) c.signalAll(); // Restore owner node and owner thread. setCurrentOwnerNode(tempUUID); currentOwnerThreadId = tempThreadID; } // Interrupt any future call to acquire/release on this sync object. interruptAll = true; // Interrupt any ongoing transactions. for (Thread t: getQueuedThreads()) t.interrupt(); }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try { initializeReentrantLock(); sync.acquireInterruptibly(1); sync.validate(true); } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }
/** {@inheritDoc} */ @Override public void onNodeRemoved(UUID nodeId) { updateLock.lock(); try { if (nodeId.equals(sync.getOwnerNode())) { if (!sync.failoverSafe) { sync.setBroken(true); sync.interruptAll(); } } // Try to notify any waiting threads. sync.release(0); } finally { updateLock.unlock(); } }
/** {@inheritDoc} */ @Override public void signalAll() { ctx.kernalContext().gateway().readLock(); try { if (!isHeldExclusively()) throw new IllegalMonitorStateException(); sync.validate(false); addOutgoingSignalAll(name); } finally { ctx.kernalContext().gateway().readUnlock(); } } }
/** {@inheritDoc} */ @Override public void signal() { ctx.kernalContext().gateway().readLock(); try { if (!isHeldExclusively()) throw new IllegalMonitorStateException(); validate(false); addOutgoingSignal(name); } finally { ctx.kernalContext().gateway().readUnlock(); } }
@Override public Sync call() throws Exception { try (GridNearTxLocal tx = CU.txStartInternal(ctx, cacheView, PESSIMISTIC, REPEATABLE_READ)) { GridCacheLockState val = cacheView.get(key); if (val == null) { if (log.isDebugEnabled()) log.debug("Failed to find reentrant lock with given name: " + name); return null; } tx.rollback(); return new Sync(val); } } });
/** * Checks if latest call to acquire/release was called on this node. * Should only be called from update method. * * @param newOwnerID ID of the node that is about to acquire this lock (or null). * @return true if acquire/release that triggered last update came from this node. */ protected boolean isLockedLocally(UUID newOwnerID) { return thisNode.equals(getOwnerNode()) || thisNode.equals(newOwnerID); }
/** {@inheritDoc} */ @Override public int getHoldCount() { try{ initializeReentrantLock(); return sync.getHoldCount(); } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** * Performs lock. */ final void lock() { acquire(1); }
/** {@inheritDoc} */ @Override public void onUpdate(GridCacheLockState val) { // Called only on initialization, so it's safe to ignore update. if (sync == null) return; updateLock.lock(); try { // If this update is a result of unsuccessful acquire in fair mode, no local update should be done. if (!val.isChanged()) return; // Check if update came from this node. boolean loc = sync.isLockedLocally(val.getId()); // Process any incoming signals. boolean incomingSignals = sync.checkIncomingSignals(val); // Update permission count. sync.setPermits(val.get()); // Update owner's node id. sync.setCurrentOwnerNode(val.getId()); // Update owner's thread id. sync.setCurrentOwnerThread(val.getThreadId()); // Check if any threads waiting on this node need to be notified. if ((incomingSignals || sync.getPermits() == 0) && !loc) { // Try to notify any waiting threads. sync.release(0); } } finally{ updateLock.unlock(); } }
synchronized boolean checkIncomingSignals(GridCacheLockState state) { if (state.getSignals() == null) return false; LinkedList<String> signals = state.getSignals().get(thisNode); if (signals == null || signals.isEmpty()) return false; UUID tempUUID = getOwnerNode(); Thread tempThread = getExclusiveOwnerThread(); long tempThreadID = currentOwnerThreadId; // Temporarily allow current thread to signal condition object. // This is safe to do because: // 1. if release was called on this node, // it was called from currently active thread; // 2. if release came from a thread on any other node, // all threads on this node are already blocked. setCurrentOwnerNode(thisNode); setExclusiveOwnerThread(Thread.currentThread()); currentOwnerThreadId = Thread.currentThread().getId(); for (String signal: signals) conditionMap.get(signal).signal(); // Restore owner node and owner thread. setCurrentOwnerNode(tempUUID); setExclusiveOwnerThread(tempThread); currentOwnerThreadId = tempThreadID; return true; }
/** Interrupt every thread on this node waiting on this lock. */ private synchronized void interruptAll() { // First release all threads waiting on associated condition queues. if (!conditionMap.isEmpty()) { // Temporarily obtain ownership of the lock, // in order to signal all conditions. UUID tempUUID = getOwnerNode(); long tempThreadID = currentOwnerThreadId; setCurrentOwnerNode(thisNode); currentOwnerThreadId = Thread.currentThread().getId(); for (Condition c : conditionMap.values()) c.signalAll(); // Restore owner node and owner thread. setCurrentOwnerNode(tempUUID); currentOwnerThreadId = tempThreadID; } // Interrupt any future call to acquire/release on this sync object. interruptAll = true; // Interrupt any ongoing transactions. for (Thread t: getQueuedThreads()) t.interrupt(); }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try { initializeReentrantLock(); sync.acquireInterruptibly(1); sync.validate(true); } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }
/** {@inheritDoc} */ @Override public void onNodeRemoved(UUID nodeId) { updateLock.lock(); try { if (nodeId.equals(sync.getOwnerNode())) { if (!sync.failoverSafe) { sync.setBroken(true); sync.interruptAll(); } } // Try to notify any waiting threads. sync.release(0); } finally { updateLock.unlock(); } }