1. /*
  2. * @(#)Shutdown.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. /**
  11. * Package-private utility class containing data structures and logic
  12. * governing the virtual-machine shutdown sequence.
  13. *
  14. * @author Mark Reinhold
  15. * @version 1.9, 03/01/23
  16. * @since 1.3
  17. */
  18. class Shutdown {
  19. /* Wrapper class for registered hooks, to ensure that hook identity is
  20. * object identity rather than .equals identity
  21. */
  22. private static class WrappedHook {
  23. private Thread hook;
  24. WrappedHook(Thread t) {
  25. hook = t;
  26. }
  27. public int hashCode() {
  28. return System.identityHashCode(hook);
  29. }
  30. public boolean equals(Object o) {
  31. if (!(o instanceof WrappedHook)) return false;
  32. return (((WrappedHook)o).hook == hook);
  33. }
  34. }
  35. /* Shutdown state */
  36. private static final int RUNNING = 0;
  37. private static final int HOOKS = 1;
  38. private static final int FINALIZERS = 2;
  39. private static int state = RUNNING;
  40. /* Should we run all finalizers upon exit? */
  41. private static boolean runFinalizersOnExit = false;
  42. /* The set of registered, wrapped hooks, or null if there aren't any */
  43. private static HashSet hooks = null;
  44. /* The preceding static fields are protected by this lock */
  45. private static class Lock { };
  46. private static Object lock = new Lock();
  47. /* Invoked by Runtime.runFinalizersOnExit */
  48. static void setRunFinalizersOnExit(boolean run) {
  49. synchronized (lock) {
  50. runFinalizersOnExit = run;
  51. }
  52. }
  53. /* Add a new shutdown hook. Checks the shutdown state and the hook itself,
  54. * but does not do any security checks.
  55. */
  56. static void add(Thread hook) {
  57. synchronized (lock) {
  58. if (state > RUNNING)
  59. throw new IllegalStateException("Shutdown in progress");
  60. if (hook.isAlive())
  61. throw new IllegalArgumentException("Hook already running");
  62. if (hooks == null) {
  63. hooks = new HashSet(11);
  64. hooks.add(new WrappedHook(hook));
  65. Terminator.setup();
  66. } else {
  67. WrappedHook wh = new WrappedHook(hook);
  68. if (hooks.contains(wh))
  69. throw new IllegalArgumentException("Hook previously registered");
  70. hooks.add(wh);
  71. }
  72. }
  73. }
  74. /* Remove a previously-registered hook. Like the add method, this method
  75. * does not do any security checks.
  76. */
  77. static boolean remove(Thread hook) {
  78. synchronized (lock) {
  79. if (state > RUNNING)
  80. throw new IllegalStateException("Shutdown in progress");
  81. if (hook == null) throw new NullPointerException();
  82. if (hooks == null) {
  83. return false;
  84. } else {
  85. boolean rv = hooks.remove(new WrappedHook(hook));
  86. if (rv && hooks.isEmpty()) {
  87. hooks = null;
  88. Terminator.teardown();
  89. }
  90. return rv;
  91. }
  92. }
  93. }
  94. /* Run all registered shutdown hooks
  95. */
  96. private static void runHooks() {
  97. /* We needn't bother acquiring the lock just to read the hooks field,
  98. * since the hooks can't be modified once shutdown is in progress
  99. */
  100. if (hooks == null) return;
  101. for (Iterator i = hooks.iterator(); i.hasNext();) {
  102. ((WrappedHook)(i.next())).hook.start();
  103. }
  104. for (Iterator i = hooks.iterator(); i.hasNext();) {
  105. try {
  106. ((WrappedHook)(i.next())).hook.join();
  107. } catch (InterruptedException x) {
  108. continue;
  109. }
  110. }
  111. }
  112. /* The true native halt method; also invoked by Runtime.halt
  113. * after doing the necessary security checks
  114. */
  115. static native void halt(int status);
  116. /* Wormhole for invoking java.lang.ref.Finalizer.runAllFinalizers */
  117. private static native void runAllFinalizers();
  118. /* The actual shutdown sequence is defined here.
  119. *
  120. * If it weren't for runFinalizersOnExit, this would be simple -- we'd just
  121. * run the hooks and then halt. Instead we need to keep track of whether
  122. * we're running hooks or finalizers. In the latter case a finalizer could
  123. * invoke exit(1) to cause immediate termination, while in the former case
  124. * any further invocations of exit(n), for any n, simply stall. Note that
  125. * if on-exit finalizers are enabled they're run iff the shutdown is
  126. * initiated by an exit(0); they're never run on exit(n) for n != 0 or in
  127. * response to SIGINT, SIGTERM, etc.
  128. */
  129. private static void sequence() {
  130. synchronized (lock) {
  131. /* Guard against the possibility of a daemon thread invoking exit
  132. * after DestroyJavaVM initiates the shutdown sequence
  133. */
  134. if (state != HOOKS) return;
  135. }
  136. runHooks();
  137. boolean rfoe;
  138. synchronized (lock) {
  139. state = FINALIZERS;
  140. rfoe = runFinalizersOnExit;
  141. }
  142. if (rfoe) runAllFinalizers();
  143. }
  144. /* Invoked by Runtime.exit, which does all the security checks.
  145. * Also invoked by handlers for system-provided termination events,
  146. * which should pass a nonzero status code.
  147. */
  148. static void exit(int status) {
  149. boolean runMoreFinalizers = false;
  150. synchronized (lock) {
  151. if (status != 0) runFinalizersOnExit = false;
  152. switch (state) {
  153. case RUNNING: /* Initiate shutdown */
  154. state = HOOKS;
  155. break;
  156. case HOOKS: /* Stall and halt */
  157. break;
  158. case FINALIZERS:
  159. if (status != 0) {
  160. /* Halt immediately on nonzero status */
  161. halt(status);
  162. } else {
  163. /* Compatibility with old behavior:
  164. * Run more finalizers and then halt
  165. */
  166. runMoreFinalizers = runFinalizersOnExit;
  167. }
  168. break;
  169. }
  170. }
  171. if (runMoreFinalizers) {
  172. runAllFinalizers();
  173. halt(status);
  174. }
  175. synchronized (Shutdown.class) {
  176. /* Synchronize on the class object, causing any other thread
  177. * that attempts to initiate shutdown to stall indefinitely
  178. */
  179. sequence();
  180. halt(status);
  181. }
  182. }
  183. /* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
  184. * thread has finished. Unlike the exit method, this method does not
  185. * actually halt the VM.
  186. */
  187. static void shutdown() {
  188. synchronized (lock) {
  189. switch (state) {
  190. case RUNNING: /* Initiate shutdown */
  191. state = HOOKS;
  192. break;
  193. case HOOKS: /* Stall and then return */
  194. case FINALIZERS:
  195. break;
  196. }
  197. }
  198. synchronized (Shutdown.class) {
  199. sequence();
  200. }
  201. }
  202. }