jdk/src/share/classes/java/lang/Shutdown.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 2277 445a331b4a8b
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Package-private utility class containing data structures and logic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * governing the virtual-machine shutdown sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since    1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class Shutdown {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /* Shutdown state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static final int RUNNING = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int HOOKS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int FINALIZERS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static int state = RUNNING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* Should we run all finalizers upon exit? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static boolean runFinalizersOnExit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* The set of registered, wrapped hooks, or null if there aren't any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static ArrayList<Runnable> hooks = new ArrayList<Runnable>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* The preceding static fields are protected by this lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static class Lock { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static Object lock = new Lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* Lock object for the native halt method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static Object haltLock = new Lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* Invoked by Runtime.runFinalizersOnExit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static void setRunFinalizersOnExit(boolean run) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            runFinalizersOnExit = run;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Add a new shutdown hook.  Checks the shutdown state and the hook itself,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * but does not do any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static void add(Runnable hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (state > RUNNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                throw new IllegalStateException("Shutdown in progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            hooks.add(hook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* Remove a previously-registered hook.  Like the add method, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * does not do any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static boolean remove(Runnable hook) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (state > RUNNING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new IllegalStateException("Shutdown in progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (hook == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (hooks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return hooks.remove(hook);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* Run all registered shutdown hooks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static void runHooks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        /* We needn't bother acquiring the lock just to read the hooks field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * since the hooks can't be modified once shutdown is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (Runnable hook : hooks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                hook.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } catch(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    ThreadDeath td = (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    throw td;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* The halt method is synchronized on the halt lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * to avoid corruption of the delete-on-shutdown file list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * It invokes the true native halt method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static void halt(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized (haltLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            halt0(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    static native void halt0(int status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /* Wormhole for invoking java.lang.ref.Finalizer.runAllFinalizers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static native void runAllFinalizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* The actual shutdown sequence is defined here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * If it weren't for runFinalizersOnExit, this would be simple -- we'd just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * run the hooks and then halt.  Instead we need to keep track of whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * we're running hooks or finalizers.  In the latter case a finalizer could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * invoke exit(1) to cause immediate termination, while in the former case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * any further invocations of exit(n), for any n, simply stall.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * if on-exit finalizers are enabled they're run iff the shutdown is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * initiated by an exit(0); they're never run on exit(n) for n != 0 or in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * response to SIGINT, SIGTERM, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static void sequence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            /* Guard against the possibility of a daemon thread invoking exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             * after DestroyJavaVM initiates the shutdown sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (state != HOOKS) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        runHooks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        boolean rfoe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            state = FINALIZERS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            rfoe = runFinalizersOnExit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (rfoe) runAllFinalizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /* Invoked by Runtime.exit, which does all the security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Also invoked by handlers for system-provided termination events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * which should pass a nonzero status code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static void exit(int status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        boolean runMoreFinalizers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (status != 0) runFinalizersOnExit = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            case RUNNING:       /* Initiate shutdown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                state = HOOKS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            case HOOKS:         /* Stall and halt */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            case FINALIZERS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (status != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    /* Halt immediately on nonzero status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    halt(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    /* Compatibility with old behavior:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                     * Run more finalizers and then halt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    runMoreFinalizers = runFinalizersOnExit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (runMoreFinalizers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            runAllFinalizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            halt(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        synchronized (Shutdown.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            /* Synchronize on the class object, causing any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
             * that attempts to initiate shutdown to stall indefinitely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            sequence();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            halt(status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * thread has finished.  Unlike the exit method, this method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * actually halt the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    static void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            case RUNNING:       /* Initiate shutdown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                state = HOOKS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            case HOOKS:         /* Stall and then return */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            case FINALIZERS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        synchronized (Shutdown.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            sequence();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}