jdk/src/share/classes/sun/misc/VM.java
author mchung
Mon, 05 Oct 2009 18:15:32 -0700
changeset 3958 b8acd5ee4f4f
parent 3111 fefdeafb7ab9
child 5506 202f599c92aa
permissions -rw-r--r--
6612680: Remove classloader dependency on jkernel Summary: Add a new sun.misc.BootClassLoaderHook that DownloadManager will implement Reviewed-by: alanb, forax, igor
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 1996-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 sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class VM {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    /* The following methods used to be native methods that instruct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * the VM to selectively suspend certain threads in low-memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * situations. They are inherently dangerous and not implementable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * on native threads. We removed them in JDK 1.2. The skeletons
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * remain so that existing applications that use these methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * will still work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static boolean suspended = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static boolean threadsSuspended() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return suspended;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static boolean allowThreadSuspension(ThreadGroup g, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return g.allowThreadSuspension(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static boolean suspendThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        suspended = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Causes any suspended threadgroups to be resumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void unsuspendThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        suspended = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Causes threadgroups no longer marked suspendable to be resumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static void unsuspendSomeThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* Deprecated fields and methods -- Memory advice not supported in 1.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int STATE_GREEN = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int STATE_YELLOW = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int STATE_RED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return STATE_GREEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void registerVMNotification(VMNotification n) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void asChange(int as_old, int as_new) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /** @deprecated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void asChange_otherthread(int as_old, int as_new) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Not supported in 1.2 because these will have to be exported as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * JVM functions, and we are not sure we want do that. Leaving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * here so it can be easily resurrected -- just remove the //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * comments.
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
     * Resume Java profiling.  All profiling data is added to any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * earlier profiling, unless <code>resetJavaProfiler</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * called in between.  If profiling was not started from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * command line, <code>resumeJavaProfiler</code> will start it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * NOTE: Profiling must be enabled from the command line for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * java.prof report to be automatically generated on exit; if not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * writeJavaProfilerReport must be invoked to write a report.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see     resetJavaProfiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see     writeJavaProfilerReport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // public native static void resumeJavaProfiler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Suspend Java profiling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // public native static void suspendJavaProfiler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Initialize Java profiling.  Any accumulated profiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * information is discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // public native static void resetJavaProfiler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Write the current profiling contents to the file "java.prof".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * If the file already exists, it will be overwritten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // public native static void writeJavaProfilerReport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static volatile boolean booted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Invoked by by System.initializeSystemClass just before returning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // Subsystems that are invoked during initialization can check this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // property in order to avoid doing things that should wait until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // application class loader has been set up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static void booted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        booted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public static boolean isBooted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return booted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // A user-settable upper limit on the maximum amount of allocatable direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // buffer memory.  This value may be changed during VM initialization if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // "java" is launched with "-XX:MaxDirectMemorySize=<size>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // The initial value of this field is arbitrary; during JRE initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // it will be reset to the value specified on the command line, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // otherwise to Runtime.getRuntime.maxDirectMemory().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private static long directMemory = 64 * 1024 * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // If this method is invoked during VM initialization, it initializes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // maximum amount of allocatable direct buffer memory (in bytes) from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // system property sun.nio.MaxDirectMemorySize.  The system property will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // be removed when it is accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // If this method is invoked after the VM is booted, it returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // maximum amount of allocatable direct buffer memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static long maxDirectMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (booted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return directMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Properties p = System.getProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String s = (String)p.remove("sun.nio.MaxDirectMemorySize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.setProperties(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (s.equals("-1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // -XX:MaxDirectMemorySize not given, take default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                directMemory = Runtime.getRuntime().maxMemory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                long l = Long.parseLong(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (l > -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    directMemory = l;
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
        return directMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // A user-settable boolean to determine whether ClassLoader.loadClass should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    // accept array syntax.  This value may be changed during VM initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // via the system property "sun.lang.ClassLoader.allowArraySyntax".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    // The default for 1.5 is "true", array syntax is allowed.  In 1.6, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // default will be "false".  The presence of this system property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // control array syntax allows applications the ability to preview this new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static boolean defaultAllowArraySyntax = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private static boolean allowArraySyntax = defaultAllowArraySyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // If this method is invoked during VM initialization, it initializes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // allowArraySyntax boolean based on the value of the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // "sun.lang.ClassLoader.allowArraySyntax".  If the system property is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // provided, the default for 1.5 is "true".  In 1.6, the default will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // "false".  If the system property is provided, then the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // allowArraySyntax will be equal to "true" if Boolean.parseBoolean()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // returns "true".   Otherwise, the field will be set to "false".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // If this method is invoked after the VM is booted, it returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // allowArraySyntax boolean set during initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static boolean allowArraySyntax() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (!booted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            String s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                = System.getProperty("sun.lang.ClassLoader.allowArraySyntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            allowArraySyntax = (s == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                ? defaultAllowArraySyntax
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                : Boolean.parseBoolean(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return allowArraySyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // Initialize any miscellenous operating system settings that need to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // set for the class libraries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static void initializeOSEnvironment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (!booted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            OSEnvironment.initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /* Current count of objects pending for finalization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private static volatile int finalRefCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /* Peak count of objects pending for finalization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static volatile int peakFinalRefCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Gets the number of objects pending for finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @return the number of objects pending for finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public static int getFinalRefCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return finalRefCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Gets the peak number of objects pending for finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the peak number of objects pending for finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static int getPeakFinalRefCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return peakFinalRefCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Add <tt>n</tt> to the objects pending for finalization count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param n an integer value to be added to the objects pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * for finalization count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static void addFinalRefCount(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // The caller must hold lock to synchronize the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        finalRefCount += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (finalRefCount > peakFinalRefCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            peakFinalRefCount = finalRefCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static Thread.State toThreadState(int threadStatus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // Initialize the threadStateMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        initThreadStateMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Thread.State s = threadStateMap.get(threadStatus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // default to RUNNABLE if the threadStatus value is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            s = Thread.State.RUNNABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // a map of threadStatus values to the corresponding Thread.State
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static Map<Integer, Thread.State> threadStateMap = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static Map<Integer, String> threadStateNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private synchronized static void initThreadStateMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (threadStateMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        final Thread.State[] ts = Thread.State.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        final int[][] vmThreadStateValues = new int[ts.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        final String[][] vmThreadStateNames = new String[ts.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        getThreadStateValues(vmThreadStateValues, vmThreadStateNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        threadStateMap = new HashMap<Integer, Thread.State>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        threadStateNames = new HashMap<Integer, String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        for (int i = 0; i < ts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            String state = ts[i].name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            int[] values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            String[] names = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            for (int j = 0; j < ts.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (vmThreadStateNames[j][0].startsWith(state)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    values = vmThreadStateValues[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    names = vmThreadStateNames[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                throw new InternalError("No VM thread state mapped to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (values.length != names.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                throw new InternalError("VM thread state values and names " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    " mapped to " + state + ": length not matched" );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (int k = 0; k < values.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                threadStateMap.put(values[k], ts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                threadStateNames.put(values[k], names[k]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // Fill in vmThreadStateValues with int arrays, each of which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // the threadStatus values mapping to the Thread.State enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    // Fill in vmThreadStateNames with String arrays, each of which contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    // the name of each threadStatus value of the format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    //    <Thread.State.name()>[.<Substate name>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    // e.g. WAITING.OBJECT_WAIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private native static void getThreadStateValues(int[][] vmThreadStateValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                                    String[][] vmThreadStateNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private native static void initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}