src/share/classes/com/sun/tools/sjavac/server/CompilerPool.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1504
22e417cdddee
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

     1 /*
     2  * Copyright (c) 2012, 2013, 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  */
    26 package com.sun.tools.sjavac.server;
    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;
    34 /** The compiler pool maintains compiler threads.
    35  *
    36  * <p><b>This is NOT part of any supported API.
    37  * If you write code that depends on this, you do so at your own
    38  * risk.  This code and its internal interfaces are subject to change
    39  * or deletion without notice.</b></p>
    40  */
    41 public class CompilerPool {
    42     // The javac server that created this pool.
    43     private JavacServer javacServer;
    44     // A semaphore protecting the poolsize number of threads.
    45     private Semaphore available;
    46     // The stack of compiler threads.
    47     private Stack<CompilerThread> compilers = new Stack<CompilerThread>();
    48     // And the executor server to spawn threads.
    49     private final ExecutorService executorPool;
    50     // How many requests are active right now?
    51     private int concurrentRequests = 0;
    52     // When was the last request finished?
    53     private long lastRequestFinished = 0;
    54     // The total number of requests to this pool.
    55     private int numRequests = 0;
    56     // Protect access to the three above values.
    57     private static final Object conc = new Object();
    59     /**
    60      * Return the javac server that this pool belongs to.
    61      */
    62     public JavacServer getJavacServer() {
    63         return javacServer;
    64     }
    66     /**
    67      * Return how many threads are running at this very moment.
    68      */
    69     public int numActiveRequests()
    70     {
    71         synchronized (conc) {
    72             return concurrentRequests;
    73         }
    74     }
    76     /**
    77      * Return when the last request was finished.
    78      * I.e. the pool has been idle since.
    79      */
    80     public long lastRequestFinished()
    81     {
    82         synchronized (conc) {
    83             return lastRequestFinished;
    84         }
    85     }
    87     /**
    88      * Up the number of active requests.
    89      */
    90     public int startRequest() {
    91         int n;
    92         synchronized (conc) {
    93             concurrentRequests++;
    94             numRequests++;
    95             n = numRequests;
    96         }
    97         return n;
    98     }
   100     /**
   101      * Down the number of active requests. Return the current time.
   102      */
   103     public long stopRequest() {
   104         synchronized (conc) {
   105             concurrentRequests--;
   106             lastRequestFinished = System.currentTimeMillis();
   107         }
   108         return lastRequestFinished;
   109     }
   111     /**
   112      * Create a new compiler pool.
   113      */
   114     CompilerPool(int poolsize, JavacServer server) {
   115         available = new Semaphore(poolsize, true);
   116         javacServer = server;
   117         executorPool = Executors.newFixedThreadPool(poolsize);
   118         lastRequestFinished = System.currentTimeMillis();
   119     }
   121     /**
   122      * Execute a compiler thread.
   123      */
   124     public void execute(CompilerThread ct) {
   125         executorPool.execute(ct);
   126     }
   128     /**
   129      * Execute a minor task, for example generating bytecodes and writing them to disk,
   130      * that belong to a major compiler thread task.
   131      */
   132     public Future<?> executeSubtask(CompilerThread t, Runnable r) {
   133         return executorPool.submit(r);
   134     }
   136     /**
   137      * Shutdown the pool.
   138      */
   139     public void shutdown() {
   140         executorPool.shutdown();
   141     }
   143     /**
   144      * Acquire a compiler thread from the pool, or block until a thread is available.
   145      * If the pools is empty, create a new thread, but never more than is "available".
   146      */
   147     public CompilerThread grabCompilerThread() throws InterruptedException {
   148         available.acquire();
   149         if (compilers.empty()) {
   150             return new CompilerThread(this);
   151         }
   152         return compilers.pop();
   153     }
   155     /**
   156      * Return the specified compiler thread to the pool.
   157      */
   158     public void returnCompilerThread(CompilerThread h) {
   159         compilers.push(h);
   160         available.release();
   161     }
   162 }

mercurial