langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/CompilerPool.java
changeset 26110 6ce251a87137
parent 26086 cfcea23d2d19
parent 26109 0430c63da650
child 26111 915a71858aef
equal deleted inserted replaced
26086:cfcea23d2d19 26110:6ce251a87137
     1 /*
       
     2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.sjavac.server;
       
    27 
       
    28 import java.util.concurrent.ExecutorService;
       
    29 import java.util.concurrent.Executors;
       
    30 import java.util.concurrent.Semaphore;
       
    31 import java.util.Stack;
       
    32 import java.util.concurrent.Future;
       
    33 
       
    34 import com.sun.tools.sjavac.comp.JavacServiceImpl;
       
    35 
       
    36 /** The compiler pool maintains compiler threads.
       
    37  *
       
    38  * <p><b>This is NOT part of any supported API.
       
    39  * If you write code that depends on this, you do so at your own
       
    40  * risk.  This code and its internal interfaces are subject to change
       
    41  * or deletion without notice.</b></p>
       
    42  */
       
    43 public class CompilerPool {
       
    44     // The javac server that created this pool.
       
    45     private JavacServer javacServer;
       
    46     // A semaphore protecting the poolsize number of threads.
       
    47     private Semaphore available;
       
    48     // The stack of compiler threads.
       
    49     private Stack<CompilerThread> compilers = new Stack<>();
       
    50     // And the executor server to spawn threads.
       
    51     private final ExecutorService executorPool;
       
    52     // How many requests are active right now?
       
    53     private int concurrentRequests = 0;
       
    54     // When was the last request finished?
       
    55     private long lastRequestFinished = 0;
       
    56     // The total number of requests to this pool.
       
    57     private int numRequests = 0;
       
    58     // Protect access to the three above values.
       
    59     private static final Object conc = new Object();
       
    60 
       
    61     /**
       
    62      * Return the javac server that this pool belongs to.
       
    63      */
       
    64     public JavacServer getJavacServer() {
       
    65         return javacServer;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Return how many threads are running at this very moment.
       
    70      */
       
    71     public int numActiveRequests()
       
    72     {
       
    73         synchronized (conc) {
       
    74             return concurrentRequests;
       
    75         }
       
    76     }
       
    77 
       
    78     /**
       
    79      * Return when the last request was finished.
       
    80      * I.e. the pool has been idle since.
       
    81      */
       
    82     public long lastRequestFinished()
       
    83     {
       
    84         synchronized (conc) {
       
    85             return lastRequestFinished;
       
    86         }
       
    87     }
       
    88 
       
    89     /**
       
    90      * Up the number of active requests.
       
    91      */
       
    92     public int startRequest() {
       
    93         int n;
       
    94         synchronized (conc) {
       
    95             concurrentRequests++;
       
    96             numRequests++;
       
    97             n = numRequests;
       
    98         }
       
    99         return n;
       
   100     }
       
   101 
       
   102     /**
       
   103      * Down the number of active requests. Return the current time.
       
   104      */
       
   105     public long stopRequest() {
       
   106         synchronized (conc) {
       
   107             concurrentRequests--;
       
   108             lastRequestFinished = System.currentTimeMillis();
       
   109         }
       
   110         return lastRequestFinished;
       
   111     }
       
   112 
       
   113     /**
       
   114      * Create a new compiler pool.
       
   115      */
       
   116     CompilerPool(int poolsize, JavacServer server) {
       
   117         available = new Semaphore(poolsize, true);
       
   118         javacServer = server;
       
   119         executorPool = Executors.newFixedThreadPool(poolsize);
       
   120         lastRequestFinished = System.currentTimeMillis();
       
   121     }
       
   122 
       
   123     /**
       
   124      * Execute a compiler thread.
       
   125      */
       
   126     public void execute(CompilerThread ct) {
       
   127         executorPool.execute(ct);
       
   128     }
       
   129 
       
   130     /**
       
   131      * Execute a minor task, for example generating bytecodes and writing them to disk,
       
   132      * that belong to a major compiler thread task.
       
   133      */
       
   134     public Future<?> executeSubtask(CompilerThread t, Runnable r) {
       
   135         return executorPool.submit(r);
       
   136     }
       
   137 
       
   138     /**
       
   139      * Shutdown the pool.
       
   140      */
       
   141     public void shutdown() {
       
   142         executorPool.shutdown();
       
   143     }
       
   144 
       
   145     /**
       
   146      * Acquire a compiler thread from the pool, or block until a thread is available.
       
   147      * If the pools is empty, create a new thread, but never more than is "available".
       
   148      */
       
   149     public CompilerThread grabCompilerThread() throws InterruptedException {
       
   150         available.acquire();
       
   151         if (compilers.empty()) {
       
   152             return new CompilerThread(this, new JavacServiceImpl(javacServer));
       
   153         }
       
   154         return compilers.pop();
       
   155     }
       
   156 
       
   157     /**
       
   158      * Return the specified compiler thread to the pool.
       
   159      */
       
   160     public void returnCompilerThread(CompilerThread h) {
       
   161         compilers.push(h);
       
   162         available.release();
       
   163     }
       
   164 }
       
   165