On Mon, Jan 28, 2013 at 4:01 PM, Doug Lea <d...@cs.oswego.edu> wrote:

> I also noticed that I had failed to include the simple
> toString-based informal debug/monitoring aids that we've
> been doing for synchronizers.


Which suggests looking for other missing methods by comparing lock-like
classes.
Looking at the implementation of toString and comparing with RRWL suggests
(untested):

Index: src/main/java/util/concurrent/locks/StampedLock.java
===================================================================
RCS file:
/export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/locks/StampedLock.java,v
retrieving revision 1.16
diff -u -r1.16 StampedLock.java
--- src/main/java/util/concurrent/locks/StampedLock.java 28 Jan 2013
23:54:42 -0000 1.16
+++ src/main/java/util/concurrent/locks/StampedLock.java 29 Jan 2013
07:56:20 -0000
@@ -886,6 +886,23 @@
         }
     }

+    private int getReadLockCount(long s) {
+        long readers;
+        if ((readers = s & RBITS) >= RFULL)
+            readers = RFULL + readerOverflow;
+        return (int) readers;
+    }
+
+    /**
+     * Queries the number of read locks held for this lock. This
+     * method is designed for use in monitoring system state, not for
+     * synchronization control.
+     * @return the number of read locks held
+     */
+    public int getReadLockCount() {
+        return getReadLockCount(state);
+    }
+
     /**
      * Returns a string identifying this lock, as well as its lock
      * state.  The state, in brackets, includes the String {@code
@@ -896,15 +913,11 @@
      * @return a string identifying this lock, as well as its lock state
      */
     public String toString() {
-        long readers;
         long s = state;
-        if ((readers = s & RBITS) >= RFULL)
-            readers = RFULL + readerOverflow;
-        return super.toString() + ((s & WBIT) != 0L ?
-                                   "[Write-locked]" :
-                                   readers == 0 ?
-                                   "[Unlocked]" :
-                                   "[Read-locks:" + readers + "]");
+        return super.toString() +
+            ((s & ABITS) == 0L ? "[Unlocked]" :
+             (s & WBIT) != 0L ? "[Write-locked]" :
+             "[Read-locks:" + getReadLockCount(s) + "]");
     }

     // internals

Reply via email to