ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r576176 - in /ant/core/trunk/src/main/org/apache/tools/ant: taskdefs/Parallel.java taskdefs/Redirector.java taskdefs/WaitFor.java util/Watchdog.java
Date Sun, 16 Sep 2007 21:08:09 GMT
Author: jkf
Date: Sun Sep 16 14:08:08 2007
New Revision: 576176

URL: http://svn.apache.org/viewvc?rev=576176&view=rev
Log:
Improved InterruptException handling, especially from Parallel task. Should also solve Pr
42924.

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Parallel.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Redirector.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/WaitFor.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/Watchdog.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Parallel.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Parallel.java?rev=576176&r1=576175&r2=576176&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Parallel.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Parallel.java Sun Sep 16 14:08:08
2007
@@ -248,6 +248,7 @@
         TaskRunnable[] runnables = new TaskRunnable[numTasks];
         stillRunning = true;
         timedOut = false;
+        boolean interrupted = false;
 
         int threadNumber = 0;
         for (Enumeration e = nestedTasks.elements(); e.hasMoreElements();
@@ -314,50 +315,48 @@
                 timeoutThread.start();
             }
 
-            // now find available running slots for the remaining threads
-            outer:
-            while (threadNumber < numTasks && stillRunning) {
-                for (int i = 0; i < maxRunning; i++) {
-                    if (running[i] == null || running[i].isFinished()) {
-                        running[i] = runnables[threadNumber++];
-                        Thread thread =  new Thread(group, running[i]);
-                        thread.start();
-                        // continue on outer while loop to get another
-                        // available slot
-                        continue outer;
+            try {
+                // now find available running slots for the remaining threads
+                outer: while (threadNumber < numTasks && stillRunning) {
+                    for (int i = 0; i < maxRunning; i++) {
+                        if (running[i] == null || running[i].isFinished()) {
+                            running[i] = runnables[threadNumber++];
+                            Thread thread = new Thread(group, running[i]);
+                            thread.start();
+                            // continue on outer while loop to get another
+                            // available slot
+                            continue outer;
+                        }
                     }
-                }
 
-                // if we got here all slots in use, so sleep until
-                // something happens
-                try {
+                    // if we got here all slots in use, so sleep until
+                    // something happens
                     semaphore.wait();
-                } catch (InterruptedException ie) {
-                    // doesn't java know interruptions are rude?
-                    // just pretend it didn't happen and go about out business.
-                    // sheesh!
                 }
-            }
 
-            // are all threads finished
-            outer2:
-            while (stillRunning) {
-                for (int i = 0; i < maxRunning; ++i) {
-                    if (running[i] != null && !running[i].isFinished()) {
-                        //System.out.println("Thread " + i + " is still alive ");
-                        // still running - wait for it
-                        try {
+                // are all threads finished
+                outer2: while (stillRunning) {
+                    for (int i = 0; i < maxRunning; ++i) {
+                        if (running[i] != null && !running[i].isFinished()) {
+                            // System.out.println("Thread " + i + " is still
+                            // alive ");
+                            // still running - wait for it
                             semaphore.wait();
-                        } catch (InterruptedException ie) {
-                            // who would interrupt me at a time like this?
+                            continue outer2;
                         }
-                        continue outer2;
                     }
+                    stillRunning = false;
                 }
-                stillRunning = false;
+            } catch (InterruptedException ie) {
+                interrupted = true;
             }
+
+            killAll(running);
         }
 
+        if (interrupted){
+            throw new BuildException("Parallel execution interrupted.");
+        }
         if (timedOut) {
             throw new BuildException("Parallel execution timed out");
         }
@@ -383,6 +382,34 @@
     }
 
     /**
+     * Doesn't do anything if all threads where already gone,
+     * else it tries to kill the threads 3 times.
+     * @param running The list of tasks that may currently be running.
+     */
+    private void killAll(TaskRunnable[] running) {
+        boolean oneAlive;
+        int tries = 0;
+        do
+        {
+            oneAlive = false;
+            for (int i = 0; i < running.length; i++)
+            {
+                if (running[i] != null && ! running[i].isFinished())
+                {
+                    running[i].interrupt();
+                    Thread.yield();
+                    oneAlive = true;
+                }
+            }
+            if (oneAlive)
+            {
+                tries++;
+                Thread.yield();
+            }
+        } while (oneAlive && tries < 100);        
+    }
+
+    /**
      * Determine the number of processors. Only effective on later VMs
      *
      * @return the number of processors available or 0 if not determinable.
@@ -409,6 +436,7 @@
         private Throwable exception;
         private Task task;
         private boolean finished;
+        private volatile Thread thread;
 
         /**
          * Construct a new TaskRunnable.<p>
@@ -425,6 +453,7 @@
          */
         public void run() {
             try {
+                thread = Thread.currentThread();
                 task.perform();
             } catch (Throwable t) {
                 exception = t;
@@ -453,6 +482,11 @@
          */
         boolean isFinished() {
             return finished;
+        }
+
+        void interrupt()
+        {
+            thread.interrupt();
         }
     }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Redirector.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Redirector.java?rev=576176&r1=576175&r2=576176&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Redirector.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Redirector.java Sun Sep 16 14:08:08
2007
@@ -781,7 +781,11 @@
                 }
                 wait(1000);
             } catch (InterruptedException eyeEx) {
-                // Ignore exception
+                Thread[] thread = new Thread[threadGroup.activeCount()];
+                threadGroup.enumerate(thread);
+                for (int i = 0; i < thread.length && thread[i] != null; i++) {
+                    thread[i].interrupt();
+                }
             }
         }
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/WaitFor.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/WaitFor.java?rev=576176&r1=576175&r2=576176&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/WaitFor.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/WaitFor.java Sun Sep 16 14:08:08
2007
@@ -135,21 +135,22 @@
         long savedMaxWaitMillis = maxWaitMillis;
         long savedCheckEveryMillis = checkEveryMillis;
         try {
-            maxWaitMillis *= maxWaitMultiplier;
-            checkEveryMillis *= checkEveryMultiplier;
-            long start = System.currentTimeMillis();
-            long end = start + maxWaitMillis;
+            try {
+                maxWaitMillis *= maxWaitMultiplier;
+                checkEveryMillis *= checkEveryMultiplier;
+                long start = System.currentTimeMillis();
+                long end = start + maxWaitMillis;
 
-            while (System.currentTimeMillis() < end) {
-                if (c.eval()) {
-                    processSuccess();
-                    return;
-                }
-                try {
+                while (System.currentTimeMillis() < end) {
+                    if (c.eval()) {
+                        processSuccess();
+                        return;
+                    }
                     Thread.sleep(checkEveryMillis);
-                } catch (InterruptedException e) {
-                    // ignore
                 }
+            } catch (InterruptedException e) {
+                log("Task " + getTaskName()
+                        + " interrupted, treating as timed out.");
             }
             processTimeout();
         } finally {

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/Watchdog.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/Watchdog.java?rev=576176&r1=576175&r2=576176&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/Watchdog.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/Watchdog.java Sun Sep 16 14:08:08 2007
@@ -110,13 +110,14 @@
      */
     public synchronized void run() {
         final long until = System.currentTimeMillis() + timeout;
-        long now;
-        while (!stopped && until > (now = System.currentTimeMillis())) {
-            try {
+        long now = until - 1;
+        try {
+            while (!stopped && until > now) {
+                now = System.currentTimeMillis();
                 wait(until - now);
-            } catch (InterruptedException e) {
-                // Ignore exception
             }
+        } catch (InterruptedException e) {
+            // Ignore exception
         }
         if (!stopped) {
             fireTimeoutOccured();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message