commit-classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Patch: Thread.holdLock implementation in java


From: Dalibor Topic
Subject: Patch: Thread.holdLock implementation in java
Date: Thu, 17 Jun 2004 00:42:07 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040608

Hi all,

I've implemented VMThread.holdLock in java. It's is probably quite slow, and wakes up the ocassional thread without much use, but hey, it's in java, so that's one less method to implement. OK to commit?

cheers,
dalibor topic
2003-06-16  Dalibor Topic  <address@hidden>

        * vm/reference/java/lang/VMThread.java
        (holdsLock) Implemented in java.
Index: vm/reference/java/lang/VMThread.java
===================================================================
RCS file: /cvsroot/classpath/classpath/vm/reference/java/lang/VMThread.java,v
retrieving revision 1.1
diff -u -r1.1 VMThread.java
--- vm/reference/java/lang/VMThread.java        29 Aug 2003 12:43:07 -0000      
1.1
+++ vm/reference/java/lang/VMThread.java        16 Jun 2004 22:39:19 -0000
@@ -58,7 +58,6 @@
  * <li>static native void yield();
  * <li>static native void sleep(long ms, int ns) throws InterruptedException;
  * <li>static native boolean interrupted();
- * <li>static native boolean holdsLock(Object obj);
  * </ul>
  * All other methods may be implemented to make Thread handling more efficient
  * or to implement some optional (and sometimes deprecated) behaviour. Default
@@ -66,6 +65,7 @@
  * for a specific VM.
  * 
  * @author Jeroen Frijters (address@hidden)
+ * @author Dalibor Topic (address@hidden)
  */
 final class VMThread
 {
@@ -389,5 +389,22 @@
      * @return true if the current thread is currently synchronized on obj
      * @throws NullPointerException if obj is null
      */
-    static native boolean holdsLock(Object obj);
+    static boolean holdsLock(Object obj) 
+    {
+      /* Use obj.notify to check if the current thread holds
+       * the monitor of the object.
+       * If it doesn't, notify will throw an exception.
+       */
+      try 
+       {
+         obj.notify();
+         // okay, current thread holds lock
+         return true;
+       }
+      catch (IllegalMonitorStateException e)
+       {
+         // it doesn't hold the lock
+         return false;
+       }
+    }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]