langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/IdleResetSjavac.java
author alundblad
Mon, 27 Oct 2014 14:55:47 +0100
changeset 27233 ceaad401ef69
parent 26107 a4a156a33c94
child 32335 7df616378cf3
permissions -rw-r--r--
8060715: There is a small race condition in IdleResetSjavac Summary: Synchronizes on method level instead of variable access level. Reviewed-by: jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     1
/*
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     4
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    10
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    15
 * accompanied this code).
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    16
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    20
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    23
 * questions.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    24
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    25
package com.sun.tools.sjavac.server;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    26
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    27
import java.io.File;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    28
import java.net.URI;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    29
import java.util.List;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    30
import java.util.Set;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    31
import java.util.Timer;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    32
import java.util.TimerTask;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    33
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    34
/**
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    35
 * An sjavac implementation that keeps track of idleness and shuts down the
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    36
 * given Terminable upon idleness timeout.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    37
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    38
 * An idleness timeout kicks in {@code idleTimeout} milliseconds after the last
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    39
 * request is completed.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    40
 *
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    41
 *  <p><b>This is NOT part of any supported API.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    42
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    43
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    44
 *  deletion without notice.</b>
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    45
 */
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    46
public class IdleResetSjavac implements Sjavac {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    47
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    48
    private final Sjavac delegate;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    49
    private final Terminable toShutdown;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    50
    private final Timer idlenessTimer = new Timer();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    51
    private final long idleTimeout;
27233
ceaad401ef69 8060715: There is a small race condition in IdleResetSjavac
alundblad
parents: 26107
diff changeset
    52
    private int outstandingCalls = 0;
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    53
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    54
    // Class invariant: idlenessTimerTask != null <-> idlenessTimerTask is scheduled
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    55
    private TimerTask idlenessTimerTask;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    56
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    57
    public IdleResetSjavac(Sjavac delegate,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    58
                            Terminable toShutdown,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    59
                            long idleTimeout) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    60
        this.delegate = delegate;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    61
        this.toShutdown = toShutdown;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    62
        this.idleTimeout = idleTimeout;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    63
        scheduleTimeout();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    64
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    65
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    66
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    67
    public SysInfo getSysInfo() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    68
        startCall();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    69
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    70
            return delegate.getSysInfo();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    71
        } finally {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    72
            endCall();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    73
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    74
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    75
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    76
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    77
    public CompilationResult compile(String protocolId,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    78
                                     String invocationId,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    79
                                     String[] args,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    80
                                     List<File> explicitSources,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    81
                                     Set<URI> sourcesToCompile,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    82
                                     Set<URI> visibleSources) {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    83
        startCall();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    84
        try {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    85
            return delegate.compile(protocolId,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    86
                                    invocationId,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    87
                                    args,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    88
                                    explicitSources,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    89
                                    sourcesToCompile,
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    90
                                    visibleSources);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    91
        } finally {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    92
            endCall();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    93
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    94
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    95
27233
ceaad401ef69 8060715: There is a small race condition in IdleResetSjavac
alundblad
parents: 26107
diff changeset
    96
    private synchronized void startCall() {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    97
        // Was there no outstanding calls before this call?
27233
ceaad401ef69 8060715: There is a small race condition in IdleResetSjavac
alundblad
parents: 26107
diff changeset
    98
        if (++outstandingCalls == 1) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
    99
            // Then the timer task must have been scheduled
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   100
            if (idlenessTimerTask == null)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   101
                throw new IllegalStateException("Idle timeout already cancelled");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   102
            // Cancel timeout task
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   103
            idlenessTimerTask.cancel();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   104
            idlenessTimerTask = null;
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   105
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   106
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   107
27233
ceaad401ef69 8060715: There is a small race condition in IdleResetSjavac
alundblad
parents: 26107
diff changeset
   108
    private synchronized void endCall() {
ceaad401ef69 8060715: There is a small race condition in IdleResetSjavac
alundblad
parents: 26107
diff changeset
   109
        if (--outstandingCalls == 0) {
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   110
            // No more outstanding calls. Schedule timeout.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   111
            scheduleTimeout();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   112
        }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   113
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   114
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   115
    private void scheduleTimeout() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   116
        if (idlenessTimerTask != null)
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   117
            throw new IllegalStateException("Idle timeout already scheduled");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   118
        idlenessTimerTask = new TimerTask() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   119
            public void run() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   120
                toShutdown.shutdown("Server has been idle for " + (idleTimeout / 1000) + " seconds.");
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   121
            }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   122
        };
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   123
        idlenessTimer.schedule(idlenessTimerTask, idleTimeout);
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   124
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   125
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   126
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   127
    public void shutdown() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   128
        idlenessTimer.cancel();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   129
        delegate.shutdown();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   130
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   131
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   132
    @Override
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   133
    public String serverSettings() {
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   134
        return delegate.serverSettings();
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   135
    }
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents:
diff changeset
   136
}