langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/IdleResetSjavac.java
changeset 27233 ceaad401ef69
parent 26107 a4a156a33c94
child 32335 7df616378cf3
equal deleted inserted replaced
27232:85cb761ea65c 27233:ceaad401ef69
    28 import java.net.URI;
    28 import java.net.URI;
    29 import java.util.List;
    29 import java.util.List;
    30 import java.util.Set;
    30 import java.util.Set;
    31 import java.util.Timer;
    31 import java.util.Timer;
    32 import java.util.TimerTask;
    32 import java.util.TimerTask;
    33 import java.util.concurrent.atomic.AtomicInteger;
       
    34 
    33 
    35 /**
    34 /**
    36  * An sjavac implementation that keeps track of idleness and shuts down the
    35  * An sjavac implementation that keeps track of idleness and shuts down the
    37  * given Terminable upon idleness timeout.
    36  * given Terminable upon idleness timeout.
    38  *
    37  *
    45  *  deletion without notice.</b>
    44  *  deletion without notice.</b>
    46  */
    45  */
    47 public class IdleResetSjavac implements Sjavac {
    46 public class IdleResetSjavac implements Sjavac {
    48 
    47 
    49     private final Sjavac delegate;
    48     private final Sjavac delegate;
    50     private final AtomicInteger outstandingCalls = new AtomicInteger();
       
    51     private final Terminable toShutdown;
    49     private final Terminable toShutdown;
    52     private final Timer idlenessTimer = new Timer();
    50     private final Timer idlenessTimer = new Timer();
    53     private final long idleTimeout;
    51     private final long idleTimeout;
       
    52     private int outstandingCalls = 0;
    54 
    53 
    55     // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
    54     // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
    56     private TimerTask idlenessTimerTask;
    55     private TimerTask idlenessTimerTask;
    57 
    56 
    58     public IdleResetSjavac(Sjavac delegate,
    57     public IdleResetSjavac(Sjavac delegate,
    92         } finally {
    91         } finally {
    93             endCall();
    92             endCall();
    94         }
    93         }
    95     }
    94     }
    96 
    95 
    97     private void startCall() {
    96     private synchronized void startCall() {
    98         // Was there no outstanding calls before this call?
    97         // Was there no outstanding calls before this call?
    99         if (outstandingCalls.incrementAndGet() == 1) {
    98         if (++outstandingCalls == 1) {
   100             // Then the timer task must have been scheduled
    99             // Then the timer task must have been scheduled
   101             if (idlenessTimerTask == null)
   100             if (idlenessTimerTask == null)
   102                 throw new IllegalStateException("Idle timeout already cancelled");
   101                 throw new IllegalStateException("Idle timeout already cancelled");
   103             // Cancel timeout task
   102             // Cancel timeout task
   104             idlenessTimerTask.cancel();
   103             idlenessTimerTask.cancel();
   105             idlenessTimerTask = null;
   104             idlenessTimerTask = null;
   106         }
   105         }
   107     }
   106     }
   108 
   107 
   109     private void endCall() {
   108     private synchronized void endCall() {
   110         if (outstandingCalls.decrementAndGet() == 0) {
   109         if (--outstandingCalls == 0) {
   111             // No more outstanding calls. Schedule timeout.
   110             // No more outstanding calls. Schedule timeout.
   112             scheduleTimeout();
   111             scheduleTimeout();
   113         }
   112         }
   114     }
   113     }
   115 
   114