1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 /**
18 * @author Evgueni Brevnov, Roman S. Bushmanov
19 */
20 package java.lang;
21
22 import java.util.Properties;
23 import java.util.Vector;
24
25 /**
26 * Provides the methods to interact with VM Execution Engine that are used by
27 * different classes from the <code>java.lang</code> package, such as System,
28 * Runtime.
29 * <p>
30 * This class must be implemented according to the common policy for porting
31 * interfaces - see the porting interface overview for more detailes.
32 *
33 * @api2vm
34 */
35 final class VMExecutionEngine {
36
37 /**
38 * keeps Runnable objects of the shutdown sequence
39 */
40 private static Vector<Runnable> shutdownActions = new Vector<Runnable>();
41
42 /**
43 * This class is not supposed to be instantiated.
44 */
45 private VMExecutionEngine() {
46 }
47
48 /**
49 * Terminates a Virtual Machine with possible invocation of finilization
50 * methods. This method is used by the {@link Runtime#exit(int)
51 * Runtime.exit(int status)} and {@link Runtime#halt(int)
52 * Runtime.halt(int satus)} methods implementation. When it is called by the
53 * <code>Runtime.exit(int status)</code> method all shutdown hook threads
54 * should have been already finished. The needFinilization argument must
55 * be true if uninvoked finilizers should be called on VM exit. The
56 * implementation simply calls the
57 * {@link VMExecutionEngine#exit(int, boolean, Runnable[])
58 * VMExecutionEngine.exit(int status, boolean needFinalization,
59 * Runnable[] shutdownSequence)} method with an array of
60 * <code>Runnable</code> objects which were registered before by means of
61 * the {@link VMExecutionEngine#registerShutdownAction(Runnable)
62 * VMExecutionEngine.registerShutdownAction(Runnable action)} method.
63 *
64 * @param status exit status
65 * @param needFinalize specifies that finalization must be performed. If
66 * true then it perfoms finalization of all not finalized objects
67 * that have finalizers
68 * @api2vm
69 */
70 static void exit(int status, boolean needFinalization) {
71 exit(status, needFinalization, shutdownActions.toArray(new Runnable[0]));
72 }
73
74
75 /**
76 * Call to this method forces VM to
77 * <ol>
78 * <li> Execute uninvoked finilizers if needFinalization is true </li>
79 * <li> Forcibly stop all running non-system threads
80 * <li> Sequentially execute the <code>run</code> method for each object
81 * of the shutdownSequence array. The execution starts from the last
82 * element of the array. No threads will be created to perform
83 * execution. If uncatched exception occurs it's stack trace is
84 * printed to the error stream and the next element of the array if
85 * any will be executed
86 * <li> Exit
87 * </ol>
88 *
89 * @param status exit status
90 * @param needFinalization indicates that finilization should be performed
91 * @param shutdownSequence array of shutdown actions
92 * @api2vm
93 */
94 private static native void exit(int status, boolean needFinalization,
95 Runnable[] shutdownSequence);
96
97 /**
98 * This method provides an information about the assertion status specified
99 * via command line options.
100 * <p>
101 *
102 * @see java.lang.Class#desiredAssertionStatus()
103 *
104 * @param clss the class to be initialized with the assertion status. Note,
105 * assertion status is applicable to top-level classes only, therefore
106 * any member/local class passed is a subject to conversion to corresponding
107 * top-level declaring class. Also, <code>null</code> argument can be used to
108 * check if any assertion was specified through command line options.
109 * @param recursive controls whether this method should check exact match
110 * with name of the class, or check (super)packages recursively
111 * (most specific one has precedence).
112 * @param defaultStatus if no specific package setting found,
113 * this value may override command-line defaults. This parameter is
114 * actual only when <code>recursive == true</code>.
115 * @see java.lang.ClassLoader#setDefaultAssertionStatus(boolean)
116 * @return 0 - unspecified, < 0 - false, > 0 - true
117 * @api2vm
118 */
119 static native int getAssertionStatus(Class clss, boolean recursive,
120 int defaultStatus);
121
122 /**
123 * This method satisfies the requirements of the specification for the
124 * {@link Runtime#availableProcessors() Runtime.availableProcessors()}
125 * method.
126 * @api2vm
127 */
128 static native int getAvailableProcessors();
129
130 /**
131 * This method satisfies the requirements of the specification for the
132 * {@link System#getProperties() System.getProperties()} method.
133 * <p>
134 * Additionally a class library implementation may relay on existance of
135 * the following properties "vm.boot.class.path" & "vm.boot.library.path".
136 * The "vm.boot.class.path" property can be used to load classes and
137 * resources which reside in the bootstrap sequence of the VM.
138 * The "vm.boot.library.path" property can be used to find libraries that
139 * should be obtatined by classes which were loaded by bootstrap class loader.
140 * @api2vm
141 */
142 static native Properties getProperties();
143
144 /**
145 * Adds the specified action to the list of shutdown actions. The
146 * {@link Runnable#run() Runnable.run()} method of the specified action
147 * object is executed after all non-system threads have been stopped. Last
148 * registered action is executed before previously registered actions. Each
149 * action should not create threads inside. It is expected that registered
150 * actions doesn't requre a lot of time to complete.
151 * <p>
152 * Typicily one may use this method to close open files, connections etc. on
153 * Virtual Machine exit
154 * @param action action which should be performed on VM exit
155 * @api2vm
156 */
157 public static void registerShutdownAction(Runnable action) {
158 shutdownActions.add(action);
159 }
160
161 /**
162 * This method satisfies the requirements of the specification for the
163 * {@link Runtime#traceInstructions(boolean)
164 * Runtime.traceInstructions(boolean on)} method.
165 * @api2vm
166 */
167 static native void traceInstructions(boolean enable);
168
169 /**
170 * This method satisfies the requirements of the specification for the
171 * {@link Runtime#traceMethodCalls(boolean)
172 * Runtime.traceMethodCalls(boolean on)} method.
173 * @api2vm
174 */
175 static native void traceMethodCalls(boolean enable);
176
177 /**
178 * Returns the current system time in milliseconds since
179 * the Unix epoch (midnight, 1 Jan, 1970).
180 * @api2vm
181 */
182 static native long currentTimeMillis();
183
184 /**
185 * Returns the current value of a system timer with the best accuracy
186 * the OS can provide, in nanoseconds.
187 * @api2vm
188 */
189 static native long nanoTime();
190
191 /**
192 * Returns platform-specific name of the specified library.
193 * @api2vm
194 */
195 static native String mapLibraryName(String libname);
196 }