harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wjwashb...@apache.org
Subject svn commit: r549899 - /harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java
Date Fri, 22 Jun 2007 17:35:43 GMT
Author: wjwashburn
Date: Fri Jun 22 10:35:42 2007
New Revision: 549899

URL: http://svn.apache.org/viewvc?view=rev&rev=549899
Log:
harmony-3684:  this patch fixes a problem with ThreadGroup.setmaxPriority


Modified:
    harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java

Modified: harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java?view=diff&rev=549899&r1=549898&r2=549899
==============================================================================
--- harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java
(original)
+++ harmony/enhanced/drlvm/trunk/vm/vmcore/src/kernel_classes/javasrc/java/lang/ThreadGroup.java
Fri Jun 22 10:35:42 2007
@@ -32,7 +32,7 @@
  * @com.intel.drl.spec_ref
  */
 
-public class ThreadGroup implements Thread.UncaughtExceptionHandler{
+public class ThreadGroup implements Thread.UncaughtExceptionHandler {
 
     /**
      * Indent used to print information about thread group
@@ -40,6 +40,12 @@
     private final static String LISTING_INDENT = "    ";
 
     /**
+     * ThreadGroup lock object
+     */
+    private static class ThreadGroupLock {};
+    private final static ThreadGroupLock lock = new ThreadGroupLock();
+
+    /**
      * This group's max priority
      */
     int maxPriority = Thread.MAX_PRIORITY;
@@ -119,7 +125,7 @@
         int count = 0;
         List groupsListCopy = null;  // a copy of subgroups list
         Object[] threadsCopy = null; // a copy of threads list
-        synchronized (this) {
+        synchronized (lock) {
             if (destroyed) {
                 return 0;
             }
@@ -145,7 +151,7 @@
     public int activeGroupCount() {
         int count;
         List groupsListCopy = null; // a copy of subgroups list
-        synchronized (this) {
+        synchronized (lock) {
             if (destroyed) {
                 return 0;
             }
@@ -180,18 +186,20 @@
     /**
      * @com.intel.drl.spec_ref
      */
-    public synchronized final void destroy() {
+    public final void destroy() {
         checkAccess();
-        if (destroyed) {
-			throw new IllegalThreadStateException(
-					"The thread group " + name + " is already destroyed!");
-		}
-        if (!nonsecureDestroy()) {
-            throw new IllegalThreadStateException("The thread group " + name +
-                    " is not empty");
-        } else {
-            if (parent != null) {
-                parent.remove(this);
+        synchronized (lock) {
+            if (destroyed) {
+                throw new IllegalThreadStateException(
+                        "The thread group " + name + " is already destroyed!");
+            }
+            if (!nonsecureDestroy()) {
+                throw new IllegalThreadStateException("The thread group " + name +
+                        " is not empty");
+            } else {
+                if (parent != null) {
+                    parent.remove(this);
+                }
             }
         }
     }
@@ -315,7 +323,7 @@
     /**
      * @com.intel.drl.spec_ref
      */
-    public synchronized final void setMaxPriority(int priority) {
+    public final void setMaxPriority(int priority) {
         checkAccess();
 
         /*
@@ -330,10 +338,11 @@
             this.maxPriority = Thread.MIN_PRIORITY;
             return;
         }
-        this.maxPriority = (parent != null && parent.maxPriority < priority)
+        int new_priority = (parent != null && parent.maxPriority < priority)
                             ? parent.maxPriority
                             : priority;
-        nonsecureSetMaxPriority(this.maxPriority);
+
+        nonsecureSetMaxPriority(new_priority);
     }
 
     /**
@@ -385,28 +394,32 @@
     /**
      * Adds a thread to this thread group
      */
-    synchronized void add(Thread thread) {
-        if (destroyed) {
-            throw new IllegalThreadStateException(
-                "The thread group is already destroyed!");
+    void add(Thread thread) {
+        synchronized (lock) {
+            if (destroyed) {
+                throw new IllegalThreadStateException(
+                        "The thread group is already destroyed!");
+            }
+            threads.put(thread, this);
         }
-        threads.put(thread, this);
     }
 
     /**
      * Removes a thread from this thread group
      */
-    synchronized void remove(Thread thread) {
-        if (destroyed) {
-            return;
-        }
-        threads.remove(thread);
-        thread.group = null;
-        if (daemon && threads.isEmpty() && groups.isEmpty()) {
-            // destroy this group
-            if (parent != null) {
-                parent.remove(this);
-                destroyed = true;
+    void remove(Thread thread) {
+        synchronized (lock) {
+            if (destroyed) {
+                return;
+            }
+            threads.remove(thread);
+            thread.group = null;
+            if (daemon && threads.isEmpty() && groups.isEmpty()) {
+                // destroy this group
+                if (parent != null) {
+                    parent.remove(this);
+                    destroyed = true;
+                }
             }
         }
     }
@@ -414,12 +427,14 @@
     /**
      * Adds a subgroup to this thread group
      */
-    private synchronized void add(ThreadGroup group) {
-        if (destroyed) {
-            throw new IllegalThreadStateException(
-                "The thread group is already destroyed!");
+    private void add(ThreadGroup group) {
+        synchronized (lock) {
+            if (destroyed) {
+                throw new IllegalThreadStateException(
+                        "The thread group is already destroyed!");
+            }
+            groups.add(group);
         }
-        groups.add(group);
     }
 
     /**
@@ -447,7 +462,7 @@
         ArrayList<Thread> threadsCopy = new ArrayList<Thread>(threads.size());
         ArrayList<ThreadGroup> groupsCopy = new ArrayList<ThreadGroup>(groups.size());
 
-        synchronized (this) {
+        synchronized (lock) {
             if (destroyed) {
                 return new Object[] {null, null};
             }
@@ -490,7 +505,7 @@
     private int enumerate(Thread[] list, int offset, boolean recurse) {
         List groupsListCopy = null;  // a copy of subgroups list
         Object[] threadsCopy = null; // a copy of threads list
-        synchronized (this) {
+        synchronized (lock) {
             if (destroyed) {
                 return offset;
             }
@@ -535,7 +550,7 @@
             return offset;
         }
         int firstGroupIdx = offset;
-        synchronized (this) {
+        synchronized (lock) {
             for (Iterator it = groups.iterator(); it.hasNext();) {
                 list[offset++] = (ThreadGroup)it.next();
                 if (offset == list.length) {
@@ -562,7 +577,7 @@
         prefix += LISTING_INDENT;
         List groupsListCopy = null;   // a copy of subgroups list
         Object[] threadsCopy = null;  // a copy of threads list
-        synchronized (this) {
+        synchronized (lock) {
             threadsCopy = copyThreads();
             groupsListCopy = (List)groups.clone();
         }
@@ -582,20 +597,22 @@
      * will be thrown.
      * @return false if this ThreadGroup is not empty
      */
-    private synchronized boolean nonsecureDestroy() {
+    private boolean nonsecureDestroy() {
         boolean thisGroupIsEmpty = true;
-        if (!threads.isEmpty()) {
-            return false;
-        }
-        for (Iterator<ThreadGroup> it = groups.iterator(); it.hasNext();) {
-            if (it.next().nonsecureDestroy()) {
-                it.remove();
-            } else {
-                thisGroupIsEmpty = false;
+        synchronized (lock) {
+            if (!threads.isEmpty()) {
+                return false;
+            }
+            for (Iterator<ThreadGroup> it = groups.iterator(); it.hasNext();) {
+                if (it.next().nonsecureDestroy()) {
+                    it.remove();
+                } else {
+                    thisGroupIsEmpty = false;
+                }
+            }
+            if (groups.isEmpty()) {
+                destroyed = true;
             }
-        }
-        if (groups.isEmpty()) {
-            destroyed = true;
         }
         return thisGroupIsEmpty;
     }
@@ -604,13 +621,15 @@
      * Interrupts this thread group without any security checks. We add this
      * method to avoid calls to the checkAccess() method on subgroups
      */
-    private synchronized void nonsecureInterrupt() {
-        Object[] threadsCopy = copyThreads(); // a copy of threads list
-        for (int i = 0; i < threadsCopy.length; i++) {
-            ((Thread) threadsCopy[i]).interrupt();
-        }
-        for (Iterator it = groups.iterator(); it.hasNext();) {
-            ((ThreadGroup)it.next()).nonsecureInterrupt();
+    private void nonsecureInterrupt() {
+        synchronized (lock) {
+            Object[] threadsCopy = copyThreads(); // a copy of threads list
+            for (int i = 0; i < threadsCopy.length; i++) {
+                ((Thread) threadsCopy[i]).interrupt();
+            }
+            for (Iterator it = groups.iterator(); it.hasNext();) {
+                ((ThreadGroup)it.next()).nonsecureInterrupt();
+            }
         }
     }
 
@@ -618,13 +637,15 @@
      * Resumes this thread group without any security checks. We add this method
      * to avoid calls to the checkAccess() method on subgroups
      */
-    private synchronized void nonsecureResume() {
-        Object[] threadsCopy = copyThreads();
-        for (int i = 0; i < threadsCopy.length; i++) {
-            ((Thread) threadsCopy[i]).resume();
-        }
-        for (Iterator it = groups.iterator(); it.hasNext();) {
-            ((ThreadGroup)it.next()).nonsecureResume();
+    private void nonsecureResume() {
+        synchronized (lock) {
+            Object[] threadsCopy = copyThreads();
+            for (int i = 0; i < threadsCopy.length; i++) {
+                ((Thread) threadsCopy[i]).resume();
+            }
+            for (Iterator it = groups.iterator(); it.hasNext();) {
+                ((ThreadGroup)it.next()).nonsecureResume();
+            }
         }
     }
 
@@ -632,24 +653,29 @@
      * Sets the maximum priority allowed for this thread group and its subgroups.
      * We add this method to avoid calls to the checkAccess() method on subgroups
      */
-    private synchronized void nonsecureSetMaxPriority(int priority) {
-        for (Iterator it = ((List)groups.clone()).iterator(); it.hasNext();) {
-            ((ThreadGroup)it.next()).nonsecureSetMaxPriority(priority);
+    private void nonsecureSetMaxPriority(int priority) {
+        synchronized (lock) {
+            this.maxPriority = priority;
+
+            for (Iterator it = groups.iterator(); it.hasNext();) {
+                ((ThreadGroup)it.next()).nonsecureSetMaxPriority(priority);
+            }
         }
-        this.maxPriority = priority;
     }
 
     /**
      * Stops this thread group without any security checks.
      * We add this method to avoid calls to the checkAccess() method on subgroups
      */
-    private synchronized void nonsecureStop() {
-        Object[] threadsCopy = copyThreads();
-        for (int i = 0; i < threadsCopy.length; i++) {
-            ((Thread) threadsCopy[i]).stop();
-        }
-        for (Iterator it = groups.iterator(); it.hasNext();) {
-            ((ThreadGroup)it.next()).nonsecureStop();
+    private void nonsecureStop() {
+        synchronized (lock) {
+            Object[] threadsCopy = copyThreads();
+            for (int i = 0; i < threadsCopy.length; i++) {
+                ((Thread) threadsCopy[i]).stop();
+            }
+            for (Iterator it = groups.iterator(); it.hasNext();) {
+                ((ThreadGroup)it.next()).nonsecureStop();
+            }
         }
     }
 
@@ -657,13 +683,15 @@
      * Suspends this thread group without any security checks.
      * We add this method to avoid calls to the checkAccess() method on subgroups
      */
-    private synchronized void nonsecureSuspend() {
-        Object[] threadsCopy = copyThreads(); // a copy of threads list
-        for (int i = 0; i < threadsCopy.length; i++) {
-            ((Thread) threadsCopy[i]).suspend();
-        }
-        for (Iterator it = groups.iterator(); it.hasNext();) {
-            ((ThreadGroup)it.next()).nonsecureSuspend();
+    private void nonsecureSuspend() {
+        synchronized (lock) {
+            Object[] threadsCopy = copyThreads(); // a copy of threads list
+            for (int i = 0; i < threadsCopy.length; i++) {
+                ((Thread) threadsCopy[i]).suspend();
+            }
+            for (Iterator it = groups.iterator(); it.hasNext();) {
+                ((ThreadGroup)it.next()).nonsecureSuspend();
+            }
         }
     }
 
@@ -672,13 +700,15 @@
      *
      * @param group group to be removed from this one
      */
-    private synchronized void remove(ThreadGroup group) {
-    	groups.remove(group);
-        if (daemon && threads.isEmpty() && groups.isEmpty()) {
-        	// destroy this group
-            if (parent != null) {
-                parent.remove(this);
-                destroyed = true;
+    private void remove(ThreadGroup group) {
+        synchronized (lock) {
+            groups.remove(group);
+            if (daemon && threads.isEmpty() && groups.isEmpty()) {
+                // destroy this group
+                if (parent != null) {
+                    parent.remove(this);
+                    destroyed = true;
+                }
             }
         }
     }



Mime
View raw message