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

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

mercurial