|
1 /* |
|
2 * Copyright (c) 2012, 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 /** 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(); |
|
58 |
|
59 /** |
|
60 * Return the javac server that this pool belongs to. |
|
61 */ |
|
62 public JavacServer getJavacServer() { |
|
63 return javacServer; |
|
64 } |
|
65 |
|
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 } |
|
75 |
|
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 } |
|
86 |
|
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 } |
|
99 |
|
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 } |
|
110 |
|
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 } |
|
120 |
|
121 /** |
|
122 * Execute a compiler thread. |
|
123 */ |
|
124 public void execute(CompilerThread ct) { |
|
125 executorPool.execute(ct); |
|
126 } |
|
127 |
|
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 } |
|
135 |
|
136 /** |
|
137 * Shutdown the pool. |
|
138 */ |
|
139 public void shutdown() { |
|
140 executorPool.shutdown(); |
|
141 } |
|
142 |
|
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 } |
|
154 |
|
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 } |
|
163 |