001: /*
002: * @(#)ThreadRegistry.java 1.19 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.misc;
029:
030: import java.util.Vector;
031: import java.security.AccessController;
032: import java.security.PrivilegedAction;
033:
034: public class ThreadRegistry {
035:
036: public static void add(Thread t) {
037: synchronized (staticLock) {
038: threadList.addElement(t);
039: dprintln("New thread " + t.getName());
040: }
041: }
042:
043: public static void remove(Thread t) {
044: synchronized (staticLock) {
045: threadList.removeElement(t);
046: // ThreadRegistry.remove() is called when the thread is about to
047: // die, at the point after which no exceptions are to be expected.
048: // Hence, we have to make sure that the following (which allocate
049: // objects) do not result in a Throwable.
050: try {
051: dprintln("Thread " + t.getName() + " removed");
052: } catch (Throwable throwable) {
053: // Just discard the exception.
054: }
055: }
056: }
057:
058: public static boolean threadCreationAllowed() {
059: return !threadCreationDisabled;
060: }
061:
062: public static void waitAllUserThreadsExit() {
063: dprintln("Waiting for all user threads to exit");
064: waitThreadsExit(false);
065: }
066:
067: public static void waitAllSystemThreadsExit() {
068: dprintln("Asking all SystemThreads to exit");
069: allExitRequestedFlag = true;
070: waitThreadsExit(true);
071: }
072:
073: private static void waitThreadsExit(boolean systemThread) {
074: Thread current = Thread.currentThread();
075: boolean tryAgain;
076:
077: do {
078: Vector snapshot;
079: tryAgain = false;
080:
081: synchronized (staticLock) {
082: snapshot = (Vector) threadList.clone();
083: }
084: int n = snapshot.size();
085: if (systemThread) {
086: // ask all the system thread to exit
087: for (int i = 0; i < n; ++i) {
088: final Thread t = (Thread) snapshot.elementAt(i);
089: if (t == current) {
090: continue;
091: }
092: // shouldn't be a user thread, but check it just in case
093: dprintln("Asking "
094: + (t.isDaemon() ? "system" : "user")
095: + " thread " + t.getName() + " to exit");
096: AccessController
097: .doPrivileged(new PrivilegedAction() {
098: public Object run() {
099: t.interrupt();
100: return null;
101: }
102: });
103: }
104: }
105: for (int i = 0; i < n; ++i) {
106: Thread t = (Thread) snapshot.elementAt(i);
107: if (t == current || (!systemThread && t.isDaemon())) {
108: continue;
109: }
110: dprintln("Waiting for "
111: + (t.isDaemon() ? "system" : "user")
112: + " thread " + t.getName() + " to exit");
113:
114: tryAgain = true;
115: while (t.isAlive()) {
116: try {
117: t.join();
118: break;
119: } catch (InterruptedException ie) {
120: }
121: }
122: }
123: } while (tryAgain);
124: }
125:
126: public static boolean exitRequested() {
127: if (allExitRequestedFlag) {
128: dprintln("System thread "
129: + Thread.currentThread().getName()
130: + " has recognized the request to exit");
131: }
132: return allExitRequestedFlag;
133: }
134:
135: public static void resetExitRequest() {
136: allExitRequestedFlag = false;
137: }
138:
139: private static synchronized void dprintln(String s) {
140: if (CVM.checkDebugFlags(CVM.DEBUGFLAG_TRACE_MISC) != 0) {
141: // Be careful of initialization order problems
142: if (System.err != null) {
143: if (debugBuffer != null) {
144: int n = debugBuffer.size();
145: for (int i = 0; i < n; ++i) {
146: String str = (String) debugBuffer.elementAt(i);
147: System.err.println(str);
148: }
149: }
150: debugBuffer = null;
151: System.err.println(s);
152: } else {
153: if (debugBuffer == null) {
154: debugBuffer = new Vector();
155: }
156: debugBuffer.addElement(s);
157: }
158: }
159: }
160:
161: private static Object staticLock = new Object();
162: private static boolean allExitRequestedFlag = false;
163: private static Vector threadList = new Vector();
164: private static Vector debugBuffer;
165: // %begin lvm
166: // The following flag gets set from native code in LVM implementation.
167: // %end lvm
168: private static boolean threadCreationDisabled = false;
169:
170: /*
171: * 4952560: These locks must always have an inflated monitor associated
172: * with them so we never have to worry about ThreadRegistry.remove
173: * failing under low memory conditions. Otherwise the VM will never
174: * shutdown.
175: */
176: static {
177: if (!sun.misc.CVM.objectInflatePermanently(staticLock)) {
178: throw new OutOfMemoryError();
179: }
180: if (!sun.misc.CVM.objectInflatePermanently(threadList)) {
181: throw new OutOfMemoryError();
182: }
183: }
184: }
|