From 97646f8eb4ea3713c2d4e0675828ca97b21102c5 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Thu, 3 Jul 2008 20:29:00 +0000
Subject: [PATCH] change: thread interruption code such that we can give a
 guarantee that the ICleaner will not be interrupted

SVN: 7030
---
 .../cisd/common/concurrent/TerminableCallable.java         | 7 ++++---
 .../ch/systemsx/cisd/common/concurrent/ThreadGuard.java    | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java b/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java
index a857ecde506..abc8e2fc42f 100644
--- a/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java
+++ b/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java
@@ -183,9 +183,10 @@ public final class TerminableCallable<V> implements Callable<V>, ITerminable
          * Note that this method is <i>always</i> called, no matter what the cause is. If you want
          * to perform clean up only for some causes, check <var>cause</var> first.
          * <p>
-         * Note that the current Thread may be in an interrupted state when this method is called or
-         * may get interrupted during the method runs. It is advised not to use methods that throw
-         * an {@link InterruptedException} or equivalent in this method.
+         * <i>It is guaranteed that, if the callable is terminated with the
+         * {@link TerminableCallable#terminate(long)} or the {@link TerminableCallable#terminate()}
+         * method, this call will be interrupted by neither {@link Thread#interrupt()} nor
+         * <code>Thread.stop()</code>.</i>
          * <p>
          * <strong>Don't perform any time consuming operations in this method and avoid any
          * operations that can fail with an exception.</strong>
diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java b/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java
index df3414b417f..9691503906e 100644
--- a/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java
+++ b/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java
@@ -78,13 +78,14 @@ final class ThreadGuard
         }
     }
 
-    private synchronized Thread getThreadForTermination()
+    private synchronized Thread tryInterruptAndGetThread()
     {
         if (state == State.RUNNING)
         {
             final Thread t = thread;
             thread = null;
             state = State.TERMINATING;
+            t.interrupt();
             return t;
         } else
         {
@@ -159,6 +160,7 @@ final class ThreadGuard
     {
         state = State.FINISHING;
         thread = null;
+        Thread.interrupted(); // Clear interrupted flag in case we are in mode TERMINATING.
     }
 
     /**
@@ -237,10 +239,9 @@ final class ThreadGuard
             return true;
         }
         final long start = System.currentTimeMillis();
-        final Thread t = getThreadForTermination();
+        final Thread t = tryInterruptAndGetThread();
         if (t != null)
         {
-            t.interrupt();
             if (waitForFinished(waitInterruptMillis))
             {
                 return true;
-- 
GitLab