1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/sjavac/server/CompilerPool.java Mon Feb 04 18:08:53 2013 -0500 1.3 @@ -0,0 +1,163 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.sjavac.server; 1.30 + 1.31 +import java.util.concurrent.ExecutorService; 1.32 +import java.util.concurrent.Executors; 1.33 +import java.util.concurrent.Semaphore; 1.34 +import java.util.Stack; 1.35 +import java.util.concurrent.Future; 1.36 + 1.37 +/** The compiler pool maintains compiler threads. 1.38 + * 1.39 + * <p><b>This is NOT part of any supported API. 1.40 + * If you write code that depends on this, you do so at your own 1.41 + * risk. This code and its internal interfaces are subject to change 1.42 + * or deletion without notice.</b></p> 1.43 + */ 1.44 +public class CompilerPool { 1.45 + // The javac server that created this pool. 1.46 + private JavacServer javacServer; 1.47 + // A semaphore protecting the poolsize number of threads. 1.48 + private Semaphore available; 1.49 + // The stack of compiler threads. 1.50 + private Stack<CompilerThread> compilers = new Stack<CompilerThread>(); 1.51 + // And the executor server to spawn threads. 1.52 + private final ExecutorService executorPool; 1.53 + // How many requests are active right now? 1.54 + private int concurrentRequests = 0; 1.55 + // When was the last request finished? 1.56 + private long lastRequestFinished = 0; 1.57 + // The total number of requests to this pool. 1.58 + private int numRequests = 0; 1.59 + // Protect access to the three above values. 1.60 + private static final Object conc = new Object(); 1.61 + 1.62 + /** 1.63 + * Return the javac server that this pool belongs to. 1.64 + */ 1.65 + public JavacServer getJavacServer() { 1.66 + return javacServer; 1.67 + } 1.68 + 1.69 + /** 1.70 + * Return how many threads are running at this very moment. 1.71 + */ 1.72 + public int numActiveRequests() 1.73 + { 1.74 + synchronized (conc) { 1.75 + return concurrentRequests; 1.76 + } 1.77 + } 1.78 + 1.79 + /** 1.80 + * Return when the last request was finished. 1.81 + * I.e. the pool has been idle since. 1.82 + */ 1.83 + public long lastRequestFinished() 1.84 + { 1.85 + synchronized (conc) { 1.86 + return lastRequestFinished; 1.87 + } 1.88 + } 1.89 + 1.90 + /** 1.91 + * Up the number of active requests. 1.92 + */ 1.93 + public int startRequest() { 1.94 + int n; 1.95 + synchronized (conc) { 1.96 + concurrentRequests++; 1.97 + numRequests++; 1.98 + n = numRequests; 1.99 + } 1.100 + return n; 1.101 + } 1.102 + 1.103 + /** 1.104 + * Down the number of active requests. Return the current time. 1.105 + */ 1.106 + public long stopRequest() { 1.107 + synchronized (conc) { 1.108 + concurrentRequests--; 1.109 + lastRequestFinished = System.currentTimeMillis(); 1.110 + } 1.111 + return lastRequestFinished; 1.112 + } 1.113 + 1.114 + /** 1.115 + * Create a new compiler pool. 1.116 + */ 1.117 + CompilerPool(int poolsize, JavacServer server) { 1.118 + available = new Semaphore(poolsize, true); 1.119 + javacServer = server; 1.120 + executorPool = Executors.newFixedThreadPool(poolsize); 1.121 + lastRequestFinished = System.currentTimeMillis(); 1.122 + } 1.123 + 1.124 + /** 1.125 + * Execute a compiler thread. 1.126 + */ 1.127 + public void execute(CompilerThread ct) { 1.128 + executorPool.execute(ct); 1.129 + } 1.130 + 1.131 + /** 1.132 + * Execute a minor task, for example generating bytecodes and writing them to disk, 1.133 + * that belong to a major compiler thread task. 1.134 + */ 1.135 + public Future<?> executeSubtask(CompilerThread t, Runnable r) { 1.136 + return executorPool.submit(r); 1.137 + } 1.138 + 1.139 + /** 1.140 + * Shutdown the pool. 1.141 + */ 1.142 + public void shutdown() { 1.143 + executorPool.shutdown(); 1.144 + } 1.145 + 1.146 + /** 1.147 + * Acquire a compiler thread from the pool, or block until a thread is available. 1.148 + * If the pools is empty, create a new thread, but never more than is "available". 1.149 + */ 1.150 + public CompilerThread grabCompilerThread() throws InterruptedException { 1.151 + available.acquire(); 1.152 + if (compilers.empty()) { 1.153 + return new CompilerThread(this); 1.154 + } 1.155 + return compilers.pop(); 1.156 + } 1.157 + 1.158 + /** 1.159 + * Return the specified compiler thread to the pool. 1.160 + */ 1.161 + public void returnCompilerThread(CompilerThread h) { 1.162 + compilers.push(h); 1.163 + available.release(); 1.164 + } 1.165 +} 1.166 +