author | serb |
Fri, 17 Apr 2015 16:54:13 +0300 | |
changeset 30469 | bac0a7ff7e1e |
parent 29922 | 7b9c1e1532cf |
child 30488 | 3a8007752b94 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18178
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.awt; |
|
27 |
||
28 |
import java.awt.AWTEvent; |
|
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
29 |
|
23893 | 30 |
import java.security.AccessController; |
31 |
import java.security.PrivilegedAction; |
|
2 | 32 |
import java.util.HashSet; |
33 |
import java.util.IdentityHashMap; |
|
34 |
import java.util.Map; |
|
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
35 |
import java.util.Set; |
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
36 |
|
29922 | 37 |
import sun.misc.InnocuousThread; |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
2
diff
changeset
|
38 |
import sun.util.logging.PlatformLogger; |
24520
e8afd90fcb69
8035169: Move ThreadGroupUtils from the sun.misc package
pchelko
parents:
23900
diff
changeset
|
39 |
import sun.awt.util.ThreadGroupUtils; |
2 | 40 |
|
41 |
/** |
|
42 |
* This class is to let AWT shutdown automatically when a user is done |
|
43 |
* with AWT. It tracks AWT state using the following parameters: |
|
44 |
* <ul> |
|
45 |
* <li><code>peerMap</code> - the map between the existing peer objects |
|
46 |
* and their associated targets |
|
47 |
* <li><code>toolkitThreadBusy</code> - whether the toolkit thread |
|
48 |
* is waiting for a new native event to appear in its queue |
|
49 |
* or is dispatching an event |
|
50 |
* <li><code>busyThreadSet</code> - a set of all the event dispatch |
|
51 |
* threads that are busy at this moment, i.e. those that are not |
|
52 |
* waiting for a new event to appear in their event queue. |
|
53 |
* </ul><p> |
|
54 |
* AWT is considered to be in ready-to-shutdown state when |
|
55 |
* <code>peerMap</code> is empty and <code>toolkitThreadBusy</code> |
|
56 |
* is false and <code>busyThreadSet</code> is empty. |
|
57 |
* The internal AWTAutoShutdown logic secures that the single non-daemon |
|
58 |
* thread (<code>blockerThread</code>) is running when AWT is not in |
|
59 |
* ready-to-shutdown state. This blocker thread is to prevent AWT from |
|
60 |
* exiting since the toolkit thread is now daemon and all the event |
|
61 |
* dispatch threads are started only when needed. Once it is detected |
|
62 |
* that AWT is in ready-to-shutdown state this blocker thread waits |
|
63 |
* for a certain timeout and if AWT state doesn't change during timeout |
|
64 |
* this blocker thread terminates all the event dispatch threads and |
|
65 |
* exits. |
|
66 |
*/ |
|
67 |
public final class AWTAutoShutdown implements Runnable { |
|
68 |
||
69 |
private static final AWTAutoShutdown theInstance = new AWTAutoShutdown(); |
|
70 |
||
71 |
/** |
|
72 |
* This lock object is used to synchronize shutdown operations. |
|
73 |
*/ |
|
74 |
private final Object mainLock = new Object(); |
|
75 |
||
76 |
/** |
|
77 |
* This lock object is to secure that when a new blocker thread is |
|
78 |
* started it will be the first who acquire the main lock after |
|
79 |
* the thread that created the new blocker released the main lock |
|
80 |
* by calling lock.wait() to wait for the blocker to start. |
|
81 |
*/ |
|
82 |
private final Object activationLock = new Object(); |
|
83 |
||
84 |
/** |
|
85 |
* This set keeps references to all the event dispatch threads that |
|
86 |
* are busy at this moment, i.e. those that are not waiting for a |
|
87 |
* new event to appear in their event queue. |
|
88 |
* Access is synchronized on the main lock object. |
|
89 |
*/ |
|
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
90 |
private final Set<Thread> busyThreadSet = new HashSet<>(7); |
2 | 91 |
|
92 |
/** |
|
93 |
* Indicates whether the toolkit thread is waiting for a new native |
|
94 |
* event to appear or is dispatching an event. |
|
95 |
*/ |
|
96 |
private boolean toolkitThreadBusy = false; |
|
97 |
||
98 |
/** |
|
99 |
* This is a map between components and their peers. |
|
100 |
* we should work with in under activationLock&mainLock lock. |
|
101 |
*/ |
|
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
102 |
private final Map<Object, Object> peerMap = new IdentityHashMap<>(); |
2 | 103 |
|
104 |
/** |
|
105 |
* References the alive non-daemon thread that is currently used |
|
106 |
* for keeping AWT from exiting. |
|
107 |
*/ |
|
108 |
private Thread blockerThread = null; |
|
109 |
||
110 |
/** |
|
111 |
* We need this flag to secure that AWT state hasn't changed while |
|
112 |
* we were waiting for the safety timeout to pass. |
|
113 |
*/ |
|
114 |
private boolean timeoutPassed = false; |
|
115 |
||
116 |
/** |
|
117 |
* Once we detect that AWT is ready to shutdown we wait for a certain |
|
118 |
* timeout to pass before stopping event dispatch threads. |
|
119 |
*/ |
|
120 |
private static final int SAFETY_TIMEOUT = 1000; |
|
121 |
||
122 |
/** |
|
123 |
* Constructor method is intentionally made private to secure |
|
124 |
* a single instance. Use getInstance() to reference it. |
|
125 |
* |
|
126 |
* @see AWTAutoShutdown#getInstance |
|
127 |
*/ |
|
128 |
private AWTAutoShutdown() {} |
|
129 |
||
130 |
/** |
|
131 |
* Returns reference to a single AWTAutoShutdown instance. |
|
132 |
*/ |
|
133 |
public static AWTAutoShutdown getInstance() { |
|
134 |
return theInstance; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Notify that the toolkit thread is not waiting for a native event |
|
139 |
* to appear in its queue. |
|
140 |
* |
|
141 |
* @see AWTAutoShutdown#notifyToolkitThreadFree |
|
142 |
* @see AWTAutoShutdown#setToolkitBusy |
|
143 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
144 |
*/ |
|
145 |
public static void notifyToolkitThreadBusy() { |
|
146 |
getInstance().setToolkitBusy(true); |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Notify that the toolkit thread is waiting for a native event |
|
151 |
* to appear in its queue. |
|
152 |
* |
|
153 |
* @see AWTAutoShutdown#notifyToolkitThreadFree |
|
154 |
* @see AWTAutoShutdown#setToolkitBusy |
|
155 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
156 |
*/ |
|
157 |
public static void notifyToolkitThreadFree() { |
|
158 |
getInstance().setToolkitBusy(false); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Add a specified thread to the set of busy event dispatch threads. |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
163 |
* If this set already contains the specified thread or the thread is null, |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
164 |
* the call leaves this set unchanged and returns silently. |
2 | 165 |
* |
166 |
* @param thread thread to be added to this set, if not present. |
|
167 |
* @see AWTAutoShutdown#notifyThreadFree |
|
168 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
169 |
*/ |
|
170 |
public void notifyThreadBusy(final Thread thread) { |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
171 |
if (thread == null) { |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
172 |
return; |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
173 |
} |
2 | 174 |
synchronized (activationLock) { |
175 |
synchronized (mainLock) { |
|
176 |
if (blockerThread == null) { |
|
177 |
activateBlockerThread(); |
|
178 |
} else if (isReadyToShutdown()) { |
|
179 |
mainLock.notifyAll(); |
|
180 |
timeoutPassed = false; |
|
181 |
} |
|
182 |
busyThreadSet.add(thread); |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Remove a specified thread from the set of busy event dispatch threads. |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
189 |
* If this set doesn't contain the specified thread or the thread is null, |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
190 |
* the call leaves this set unchanged and returns silently. |
2 | 191 |
* |
192 |
* @param thread thread to be removed from this set, if present. |
|
193 |
* @see AWTAutoShutdown#notifyThreadBusy |
|
194 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
195 |
*/ |
|
196 |
public void notifyThreadFree(final Thread thread) { |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
197 |
if (thread == null) { |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
198 |
return; |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
2
diff
changeset
|
199 |
} |
2 | 200 |
synchronized (activationLock) { |
201 |
synchronized (mainLock) { |
|
202 |
busyThreadSet.remove(thread); |
|
203 |
if (isReadyToShutdown()) { |
|
204 |
mainLock.notifyAll(); |
|
205 |
timeoutPassed = false; |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Notify that the peermap has been updated, that means a new peer |
|
213 |
* has been created or some existing peer has been disposed. |
|
214 |
* |
|
215 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
216 |
*/ |
|
217 |
void notifyPeerMapUpdated() { |
|
218 |
synchronized (activationLock) { |
|
219 |
synchronized (mainLock) { |
|
220 |
if (!isReadyToShutdown() && blockerThread == null) { |
|
23900
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
221 |
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { |
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
222 |
activateBlockerThread(); |
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
223 |
return null; |
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
224 |
}); |
2 | 225 |
} else { |
226 |
mainLock.notifyAll(); |
|
227 |
timeoutPassed = false; |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Determine whether AWT is currently in ready-to-shutdown state. |
|
235 |
* AWT is considered to be in ready-to-shutdown state if |
|
236 |
* <code>peerMap</code> is empty and <code>toolkitThreadBusy</code> |
|
237 |
* is false and <code>busyThreadSet</code> is empty. |
|
238 |
* |
|
239 |
* @return true if AWT is in ready-to-shutdown state. |
|
240 |
*/ |
|
241 |
private boolean isReadyToShutdown() { |
|
242 |
return (!toolkitThreadBusy && |
|
243 |
peerMap.isEmpty() && |
|
244 |
busyThreadSet.isEmpty()); |
|
245 |
} |
|
246 |
||
247 |
/** |
|
248 |
* Notify about the toolkit thread state change. |
|
249 |
* |
|
250 |
* @param busy true if the toolkit thread state changes from idle |
|
251 |
* to busy. |
|
252 |
* @see AWTAutoShutdown#notifyToolkitThreadBusy |
|
253 |
* @see AWTAutoShutdown#notifyToolkitThreadFree |
|
254 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
255 |
*/ |
|
256 |
private void setToolkitBusy(final boolean busy) { |
|
257 |
if (busy != toolkitThreadBusy) { |
|
258 |
synchronized (activationLock) { |
|
259 |
synchronized (mainLock) { |
|
260 |
if (busy != toolkitThreadBusy) { |
|
261 |
if (busy) { |
|
262 |
if (blockerThread == null) { |
|
263 |
activateBlockerThread(); |
|
264 |
} else if (isReadyToShutdown()) { |
|
265 |
mainLock.notifyAll(); |
|
266 |
timeoutPassed = false; |
|
267 |
} |
|
268 |
toolkitThreadBusy = busy; |
|
269 |
} else { |
|
270 |
toolkitThreadBusy = busy; |
|
271 |
if (isReadyToShutdown()) { |
|
272 |
mainLock.notifyAll(); |
|
273 |
timeoutPassed = false; |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Implementation of the Runnable interface. |
|
284 |
* Incapsulates the blocker thread functionality. |
|
285 |
* |
|
286 |
* @see AWTAutoShutdown#isReadyToShutdown |
|
287 |
*/ |
|
288 |
public void run() { |
|
289 |
Thread currentThread = Thread.currentThread(); |
|
290 |
boolean interrupted = false; |
|
291 |
synchronized (mainLock) { |
|
292 |
try { |
|
293 |
/* Notify that the thread is started. */ |
|
294 |
mainLock.notifyAll(); |
|
295 |
while (blockerThread == currentThread) { |
|
296 |
mainLock.wait(); |
|
297 |
timeoutPassed = false; |
|
298 |
/* |
|
299 |
* This loop is introduced to handle the following case: |
|
300 |
* it is possible that while we are waiting for the |
|
301 |
* safety timeout to pass AWT state can change to |
|
302 |
* not-ready-to-shutdown and back to ready-to-shutdown. |
|
303 |
* In this case we have to wait once again. |
|
304 |
* NOTE: we shouldn't break into the outer loop |
|
305 |
* in this case, since we may never be notified |
|
306 |
* in an outer infinite wait at this point. |
|
307 |
*/ |
|
308 |
while (isReadyToShutdown()) { |
|
309 |
if (timeoutPassed) { |
|
310 |
timeoutPassed = false; |
|
311 |
blockerThread = null; |
|
312 |
break; |
|
313 |
} |
|
314 |
timeoutPassed = true; |
|
315 |
mainLock.wait(SAFETY_TIMEOUT); |
|
316 |
} |
|
317 |
} |
|
318 |
} catch (InterruptedException e) { |
|
319 |
interrupted = true; |
|
320 |
} finally { |
|
321 |
if (blockerThread == currentThread) { |
|
322 |
blockerThread = null; |
|
323 |
} |
|
324 |
} |
|
325 |
} |
|
326 |
if (!interrupted) { |
|
327 |
AppContext.stopEventDispatchThreads(); |
|
328 |
} |
|
329 |
} |
|
330 |
||
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
331 |
@SuppressWarnings("serial") |
2 | 332 |
static AWTEvent getShutdownEvent() { |
11264
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
333 |
return new AWTEvent(getInstance(), 0) { |
54f2f4c6bd30
7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents:
5506
diff
changeset
|
334 |
}; |
2 | 335 |
} |
336 |
||
337 |
/** |
|
338 |
* Creates and starts a new blocker thread. Doesn't return until |
|
339 |
* the new blocker thread starts. |
|
23900
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
340 |
* |
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
341 |
* Must be called with {@link sun.security.util.SecurityConstants#MODIFY_THREADGROUP_PERMISSION} |
2 | 342 |
*/ |
343 |
private void activateBlockerThread() { |
|
29922 | 344 |
Thread thread; |
345 |
String name = "AWT-Shutdown"; |
|
346 |
if (System.getSecurityManager() == null) { |
|
347 |
thread = new Thread(ThreadGroupUtils.getRootThreadGroup(), this, name); |
|
348 |
} else { |
|
349 |
thread = new InnocuousThread(this, name); |
|
350 |
} |
|
23900
fd98305f0d19
8031477: [macosx] Loading AWT native library fails
pchelko
parents:
23893
diff
changeset
|
351 |
thread.setContextClassLoader(null); |
2 | 352 |
thread.setDaemon(false); |
353 |
blockerThread = thread; |
|
354 |
thread.start(); |
|
355 |
try { |
|
356 |
/* Wait for the blocker thread to start. */ |
|
357 |
mainLock.wait(); |
|
358 |
} catch (InterruptedException e) { |
|
359 |
System.err.println("AWT blocker activation interrupted:"); |
|
360 |
e.printStackTrace(); |
|
361 |
} |
|
362 |
} |
|
363 |
||
26004
7507a1b93f67
6521783: Unnecessary final modifier for a method in a final class
serb
parents:
24520
diff
changeset
|
364 |
void registerPeer(final Object target, final Object peer) { |
2 | 365 |
synchronized (activationLock) { |
366 |
synchronized (mainLock) { |
|
367 |
peerMap.put(target, peer); |
|
368 |
notifyPeerMapUpdated(); |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
||
26004
7507a1b93f67
6521783: Unnecessary final modifier for a method in a final class
serb
parents:
24520
diff
changeset
|
373 |
void unregisterPeer(final Object target, final Object peer) { |
2 | 374 |
synchronized (activationLock) { |
375 |
synchronized (mainLock) { |
|
376 |
if (peerMap.get(target) == peer) { |
|
377 |
peerMap.remove(target); |
|
378 |
notifyPeerMapUpdated(); |
|
379 |
} |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
||
26004
7507a1b93f67
6521783: Unnecessary final modifier for a method in a final class
serb
parents:
24520
diff
changeset
|
384 |
Object getPeer(final Object target) { |
2 | 385 |
synchronized (activationLock) { |
386 |
synchronized (mainLock) { |
|
387 |
return peerMap.get(target); |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
26004
7507a1b93f67
6521783: Unnecessary final modifier for a method in a final class
serb
parents:
24520
diff
changeset
|
392 |
void dumpPeers(final PlatformLogger aLog) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16839
diff
changeset
|
393 |
if (aLog.isLoggable(PlatformLogger.Level.FINE)) { |
16839
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
394 |
synchronized (activationLock) { |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
395 |
synchronized (mainLock) { |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
396 |
aLog.fine("Mapped peers:"); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
397 |
for (Object key : peerMap.keySet()) { |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
398 |
aLog.fine(key + "->" + peerMap.get(key)); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
11264
diff
changeset
|
399 |
} |
2 | 400 |
} |
401 |
} |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
} // class AWTAutoShutdown |