< prev index next >

src/java.base/share/classes/java/util/concurrent/locks/ReentrantLock.java

Print this page

        

*** 35,44 **** --- 35,46 ---- package java.util.concurrent.locks; import java.util.Collection; import java.util.concurrent.TimeUnit; + + import jdk.internal.misc.Strands; import jdk.internal.vm.annotation.ReservedStackAccess; /** * A reentrant mutual exclusion {@link Lock} with the same basic * behavior and semantics as the implicit monitor lock accessed using
*** 122,177 **** * Performs non-fair tryLock. tryAcquire is implemented in * subclasses, but both need nonfair try for trylock method. */ @ReservedStackAccess final boolean nonfairTryAcquire(int acquires) { ! final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) { ! setExclusiveOwnerThread(current); return true; } } ! else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) // overflow ! throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } @ReservedStackAccess protected final boolean tryRelease(int releases) { int c = getState() - releases; ! if (Thread.currentThread() != getExclusiveOwnerThread()) throw new IllegalMonitorStateException(); boolean free = false; if (c == 0) { free = true; ! setExclusiveOwnerThread(null); } setState(c); return free; } protected final boolean isHeldExclusively() { // While we must in general read state before owner, // we don't need to do so to check if current thread is owner ! return getExclusiveOwnerThread() == Thread.currentThread(); } final ConditionObject newCondition() { return new ConditionObject(); } // Methods relayed from outer class ! final Thread getOwner() { ! return getState() == 0 ? null : getExclusiveOwnerThread(); } final int getHoldCount() { return isHeldExclusively() ? getState() : 0; } --- 124,179 ---- * Performs non-fair tryLock. tryAcquire is implemented in * subclasses, but both need nonfair try for trylock method. */ @ReservedStackAccess final boolean nonfairTryAcquire(int acquires) { ! final Object current = Strands.currentStrand(); int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) { ! setExclusiveOwner(current); return true; } } ! else if (current == getExclusiveOwner()) { int nextc = c + acquires; if (nextc < 0) // overflow ! throw new Error("Maximum lock count esetExclusiveOwnexceeded"); setState(nextc); return true; } return false; } @ReservedStackAccess protected final boolean tryRelease(int releases) { int c = getState() - releases; ! if (Strands.currentStrand() != getExclusiveOwner()) throw new IllegalMonitorStateException(); boolean free = false; if (c == 0) { free = true; ! setExclusiveOwner(null); } setState(c); return free; } protected final boolean isHeldExclusively() { // While we must in general read state before owner, // we don't need to do so to check if current thread is owner ! return getExclusiveOwner() == Strands.currentStrand(); } final ConditionObject newCondition() { return new ConditionObject(); } // Methods relayed from outer class ! final Object getOwner() { ! return getState() == 0 ? null : getExclusiveOwner(); } final int getHoldCount() { return isHeldExclusively() ? getState() : 0; }
*** 209,228 **** * Fair version of tryAcquire. Don't grant access unless * recursive call or no waiters or is first. */ @ReservedStackAccess protected final boolean tryAcquire(int acquires) { ! final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { ! setExclusiveOwnerThread(current); return true; } } ! else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) throw new Error("Maximum lock count exceeded"); setState(nextc); return true; --- 211,230 ---- * Fair version of tryAcquire. Don't grant access unless * recursive call or no waiters or is first. */ @ReservedStackAccess protected final boolean tryAcquire(int acquires) { ! final Object current = Strands.currentStrand(); int c = getState(); if (c == 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { ! setExclusiveOwner(current); return true; } } ! else if (current == getExclusiveOwner()) { int nextc = c + acquires; if (nextc < 0) throw new Error("Maximum lock count exceeded"); setState(nextc); return true;
*** 591,603 **** * This method is designed to facilitate construction of * subclasses that provide more extensive lock monitoring * facilities. * * @return the owner, or {@code null} if not owned */ protected Thread getOwner() { ! return sync.getOwner(); } /** * Queries whether any threads are waiting to acquire this lock. Note that * because cancellations may occur at any time, a {@code true} --- 593,606 ---- * This method is designed to facilitate construction of * subclasses that provide more extensive lock monitoring * facilities. * * @return the owner, or {@code null} if not owned + * @throws ClassCastException if owned by a fiber */ protected Thread getOwner() { ! return (Thread) sync.getOwner(); } /** * Queries whether any threads are waiting to acquire this lock. Note that * because cancellations may occur at any time, a {@code true}
*** 733,743 **** * {@linkplain Thread#getName name} of the owning thread. * * @return a string identifying this lock, as well as its lock state */ public String toString() { ! Thread o = sync.getOwner(); return super.toString() + ((o == null) ? "[Unlocked]" : ! "[Locked by thread " + o.getName() + "]"); } } --- 736,752 ---- * {@linkplain Thread#getName name} of the owning thread. * * @return a string identifying this lock, as well as its lock state */ public String toString() { ! Object o = sync.getOwner(); ! String name = ""; ! if (o instanceof Thread) { ! name = "thread " + ((Thread) o).getName(); ! } else if (o instanceof Fiber) { ! name = "Fiber"; ! } return super.toString() + ((o == null) ? "[Unlocked]" : ! "[Locked by " + name + "]"); } }
< prev index next >