author | darcy |
Wed, 15 Aug 2018 10:44:56 -0700 | |
changeset 51413 | 43e41800d579 |
parent 49186 | b8bfadfacded |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49176 | 2 |
* Copyright (c) 1999, 2018, 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 java.lang; |
|
27 |
||
28 |
||
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
29 |
import jdk.internal.misc.VM; |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
30 |
|
2 | 31 |
/** |
32 |
* Package-private utility class containing data structures and logic |
|
33 |
* governing the virtual-machine shutdown sequence. |
|
34 |
* |
|
35 |
* @author Mark Reinhold |
|
36 |
* @since 1.3 |
|
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
37 |
* |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
38 |
* @see java.io.Console |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
39 |
* @see ApplicationShutdownHooks |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
40 |
* @see java.io.DeleteOnExitHook |
2 | 41 |
*/ |
42 |
||
43 |
class Shutdown { |
|
44 |
||
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
45 |
// The system shutdown hooks are registered with a predefined slot. |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
46 |
// The list of shutdown hooks is as follows: |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
47 |
// (0) Console restore hook |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
48 |
// (1) ApplicationShutdownHooks that invokes all registered application |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
49 |
// shutdown hooks and waits until they finish |
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
50 |
// (2) DeleteOnExit hook |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
51 |
private static final int MAX_SYSTEM_HOOKS = 10; |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
52 |
private static final Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS]; |
2 | 53 |
|
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
54 |
// the index of the currently running shutdown hook to the hooks array |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
55 |
private static int currentRunningHook = -1; |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
56 |
|
2 | 57 |
/* The preceding static fields are protected by this lock */ |
58 |
private static class Lock { }; |
|
59 |
private static Object lock = new Lock(); |
|
60 |
||
61 |
/* Lock object for the native halt method */ |
|
62 |
private static Object haltLock = new Lock(); |
|
63 |
||
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
64 |
/** |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
65 |
* Add a new system shutdown hook. Checks the shutdown state and |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
66 |
* the hook itself, but does not do any security checks. |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
67 |
* |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
68 |
* The registerShutdownInProgress parameter should be false except |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
69 |
* registering the DeleteOnExitHook since the first file may |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
70 |
* be added to the delete on exit list by the application shutdown |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
71 |
* hooks. |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
72 |
* |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
73 |
* @params slot the slot in the shutdown hook array, whose element |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
74 |
* will be invoked in order during shutdown |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
75 |
* @params registerShutdownInProgress true to allow the hook |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
76 |
* to be registered even if the shutdown is in progress. |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
77 |
* @params hook the hook to be registered |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
78 |
* |
32227
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
25859
diff
changeset
|
79 |
* @throws IllegalStateException |
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
25859
diff
changeset
|
80 |
* if registerShutdownInProgress is false and shutdown is in progress; or |
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
25859
diff
changeset
|
81 |
* if registerShutdownInProgress is true and the shutdown process |
34721a47bc92
8132478: [tidy] three new warnings from java docs (java.net, javax.annotation)
avstepan
parents:
25859
diff
changeset
|
82 |
* already passes the given slot |
2 | 83 |
*/ |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
84 |
static void add(int slot, boolean registerShutdownInProgress, Runnable hook) { |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
85 |
if (slot < 0 || slot >= MAX_SYSTEM_HOOKS) { |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
86 |
throw new IllegalArgumentException("Invalid slot: " + slot); |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
87 |
} |
2 | 88 |
synchronized (lock) { |
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
89 |
if (hooks[slot] != null) |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
90 |
throw new InternalError("Shutdown hook at slot " + slot + " already registered"); |
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
91 |
|
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
92 |
if (!registerShutdownInProgress) { |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
93 |
if (currentRunningHook >= 0) |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
94 |
throw new IllegalStateException("Shutdown in progress"); |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
95 |
} else { |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
96 |
if (VM.isShutdown() || slot <= currentRunningHook) |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
97 |
throw new IllegalStateException("Shutdown in progress"); |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
98 |
} |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
99 |
|
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
100 |
hooks[slot] = hook; |
2 | 101 |
} |
102 |
} |
|
103 |
||
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
104 |
/* Run all system shutdown hooks. |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
105 |
* |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
106 |
* The system shutdown hooks are run in the thread synchronized on |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
107 |
* Shutdown.class. Other threads calling Runtime::exit, Runtime::halt |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
108 |
* or JNI DestroyJavaVM will block indefinitely. |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
109 |
* |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
110 |
* ApplicationShutdownHooks is registered as one single hook that starts |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
111 |
* all application shutdown hooks and waits until they finish. |
2 | 112 |
*/ |
113 |
private static void runHooks() { |
|
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
114 |
synchronized (lock) { |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
115 |
/* Guard against the possibility of a daemon thread invoking exit |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
116 |
* after DestroyJavaVM initiates the shutdown sequence |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
117 |
*/ |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
118 |
if (VM.isShutdown()) return; |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
119 |
} |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
120 |
|
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
121 |
for (int i=0; i < MAX_SYSTEM_HOOKS; i++) { |
2 | 122 |
try { |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
123 |
Runnable hook; |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
124 |
synchronized (lock) { |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
125 |
// acquire the lock to make sure the hook registered during |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
126 |
// shutdown is visible here. |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
127 |
currentRunningHook = i; |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
128 |
hook = hooks[i]; |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
129 |
} |
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
130 |
if (hook != null) hook.run(); |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
131 |
} catch (Throwable t) { |
2 | 132 |
if (t instanceof ThreadDeath) { |
133 |
ThreadDeath td = (ThreadDeath)t; |
|
134 |
throw td; |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
138 |
|
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
139 |
// set shutdown state |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
140 |
VM.shutdown(); |
2 | 141 |
} |
142 |
||
49176 | 143 |
/* Notify the VM that it's time to halt. */ |
144 |
static native void beforeHalt(); |
|
145 |
||
2 | 146 |
/* The halt method is synchronized on the halt lock |
147 |
* to avoid corruption of the delete-on-shutdown file list. |
|
148 |
* It invokes the true native halt method. |
|
149 |
*/ |
|
150 |
static void halt(int status) { |
|
151 |
synchronized (haltLock) { |
|
152 |
halt0(status); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
static native void halt0(int status); |
|
157 |
||
158 |
/* Invoked by Runtime.exit, which does all the security checks. |
|
159 |
* Also invoked by handlers for system-provided termination events, |
|
160 |
* which should pass a nonzero status code. |
|
161 |
*/ |
|
162 |
static void exit(int status) { |
|
163 |
synchronized (lock) { |
|
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
164 |
if (status != 0 && VM.isShutdown()) { |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
165 |
/* Halt immediately on nonzero status */ |
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
166 |
halt(status); |
2 | 167 |
} |
168 |
} |
|
169 |
synchronized (Shutdown.class) { |
|
170 |
/* Synchronize on the class object, causing any other thread |
|
171 |
* that attempts to initiate shutdown to stall indefinitely |
|
172 |
*/ |
|
49176 | 173 |
beforeHalt(); |
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
174 |
runHooks(); |
2 | 175 |
halt(status); |
176 |
} |
|
177 |
} |
|
178 |
||
179 |
||
180 |
/* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon |
|
181 |
* thread has finished. Unlike the exit method, this method does not |
|
182 |
* actually halt the VM. |
|
183 |
*/ |
|
184 |
static void shutdown() { |
|
185 |
synchronized (Shutdown.class) { |
|
48942
a6c4b85163c1
8198249: Remove deprecated Runtime::runFinalizersOnExit and System::runFinalizersOnExit
mchung
parents:
47216
diff
changeset
|
186 |
runHooks(); |
2 | 187 |
} |
188 |
} |
|
189 |
||
190 |
} |