< prev index next >

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

Print this page

        

@@ -35,10 +35,12 @@
 
 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,56 +124,56 @@
          * 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();
+            final Object current = Strands.currentStrand();
             int c = getState();
             if (c == 0) {
                 if (compareAndSetState(0, acquires)) {
-                    setExclusiveOwnerThread(current);
+                    setExclusiveOwner(current);
                     return true;
                 }
             }
-            else if (current == getExclusiveOwnerThread()) {
+            else if (current == getExclusiveOwner()) {
                 int nextc = c + acquires;
                 if (nextc < 0) // overflow
-                    throw new Error("Maximum lock count exceeded");
+                    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 (Thread.currentThread() != getExclusiveOwnerThread())
+            if (Strands.currentStrand() != getExclusiveOwner())
                 throw new IllegalMonitorStateException();
             boolean free = false;
             if (c == 0) {
                 free = true;
-                setExclusiveOwnerThread(null);
+                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 getExclusiveOwnerThread() == Thread.currentThread();
+            return getExclusiveOwner() == Strands.currentStrand();
         }
 
         final ConditionObject newCondition() {
             return new ConditionObject();
         }
 
         // Methods relayed from outer class
 
-        final Thread getOwner() {
-            return getState() == 0 ? null : getExclusiveOwnerThread();
+        final Object getOwner() {
+            return getState() == 0 ? null : getExclusiveOwner();
         }
 
         final int getHoldCount() {
             return isHeldExclusively() ? getState() : 0;
         }

@@ -209,20 +211,20 @@
          * 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();
+            final Object current = Strands.currentStrand();
             int c = getState();
             if (c == 0) {
                 if (!hasQueuedPredecessors() &&
                     compareAndSetState(0, acquires)) {
-                    setExclusiveOwnerThread(current);
+                    setExclusiveOwner(current);
                     return true;
                 }
             }
-            else if (current == getExclusiveOwnerThread()) {
+            else if (current == getExclusiveOwner()) {
                 int nextc = c + acquires;
                 if (nextc < 0)
                     throw new Error("Maximum lock count exceeded");
                 setState(nextc);
                 return true;

@@ -591,13 +593,14 @@
      * 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 sync.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,11 +736,17 @@
      * {@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();
+        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 thread " + o.getName() + "]");
+                                   "[Locked by " + name + "]");
     }
 }
< prev index next >