001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgueni Brevnov, Roman S. Bushmanov
019: * @version $Revision: 1.1.2.1.4.4 $
020: */package java.lang;
021:
022: import org.apache.harmony.drlvm.VMHelper;
023: import org.apache.harmony.drlvm.thread.ThreadHelper;
024:
025: /**
026: * Provides the methods to interact with VM Thread Manager that are used by
027: * {@link java.lang.Thread Thread} class and {@link java.lang.Object Object}
028: * synchronization implementation.
029: * <p>
030: * A public constructor of the <code>java.lang.Thread</code> class should be
031: * called by the VM within startup phase to initialize the main thread.
032: * Note that the <code>VMThread.currentThread()</code> method should return a
033: * reference to the Thread object even if it hasn't been initialized yet.
034: * <p>
035: * This class must be implemented according to the common policy for porting
036: * interfaces - see the porting interface overview for more details.
037: *
038: * @api2vm
039: */
040: final class VMThreadManager {
041:
042: /**
043: * Thread Manager functions error codes.
044: */
045: public static final int TM_ERROR_NONE = 0;
046: public static final int TM_ERROR_INTERRUPT = 52;
047: public static final int TM_ERROR_ILLEGAL_STATE = 51;
048: public static final int TM_ERROR_EBUSY = 70025;
049:
050: /**
051: * JVMTI constants
052: */
053: public static final int TM_THREAD_STATE_ALIVE = 0x0001;
054: public static final int TM_THREAD_STATE_TERMINATED = 0x0002;
055: public static final int TM_THREAD_STATE_RUNNABLE = 0x0004;
056: public static final int TM_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER = 0x0400;
057: public static final int TM_THREAD_STATE_WAITING = 0x0080;
058: public static final int TM_THREAD_STATE_WAITING_INDEFINITELY = 0x0010;
059: public static final int TM_THREAD_STATE_WAITING_WITH_TIMEOUT = 0x0020;
060: public static final int TM_THREAD_STATE_SLEEPING = 0x0040;
061: public static final int TM_THREAD_STATE_IN_OBJECT_WAIT = 0x0100;
062: public static final int TM_THREAD_STATE_PARKED = 0x0200;
063: public static final int TM_THREAD_STATE_SUSPENDED = 0x100000;
064: public static final int TM_THREAD_STATE_INTERRUPTED = 0x200000;
065: public static final int TM_THREAD_STATE_IN_NATIVE = 0x400000;
066: public static final int TM_THREAD_STATE_VENDOR_1 = 0x10000000;
067: public static final int TM_THREAD_STATE_VENDOR_2 = 0x20000000;
068: public static final int TM_THREAD_STATE_VENDOR_3 = 0x40000000;
069:
070: /**
071: * This class is not supposed to be instantiated.
072: */
073: private VMThreadManager() {
074: }
075:
076: /**
077: * This method satisfies the requirements of the specification for the
078: * {@link Thread#currentThread() Thread.currentThread()} method.
079: * <p>
080: * But there is one special case. When this method is called while
081: * initializing the main thread it should return a reference to the main
082: * thread object even if it hasn't been initialized yet.
083: * <p>
084: * In other words the condition (VMThreadManager.currentThread() == this)
085: * should be satisfied for <code>java.lang.Thread</code> object in the only
086: * case: this object was created by VM to represent the application's main
087: * thread and hasn't been initialized yet.
088: * @api2vm
089: */
090: public static native Thread currentThreadNative();
091:
092: static Thread currentThread() {
093: if (VMHelper.isVMMagicPackageSupported()) {
094: return ThreadHelper.getCurrentThread();
095: }
096: return currentThreadNative();
097: }
098:
099: /**
100: * This method satisfies the requirements of the specification for the
101: * {@link Thread#holdsLock(java.lang.Object) Thread.holdsLock(Object obj)}
102: * method. But it doesn't throw an <code>NullPointerException</code>
103: * exception. The <code>object</code> argument must not be null.
104: * @api2vm
105: */
106: static native boolean holdsLock(Object object);
107:
108: /**
109: * This method satisfies the requirements of the specification for the
110: * {@link Thread#interrupt() Thread.interrupt()} method.
111: * @api2vm
112: */
113: static native int interrupt(Thread thread);
114:
115: /**
116: * Checks if the specified thread is dead.
117: * <p>
118: * <b>Note:</b> This method is used for the {@link Thread#isAlive()
119: * Thread.isAlive()} method implementation.
120: *
121: * @param thread the thread to check the status for.
122: * @return true if the thread has died already, false otherwise.
123: * @api2vm
124: */
125: static native boolean isAlive(Thread thread);
126:
127: /**
128: * This method satisfies the requirements of the specification for the
129: * {@link Thread#interrupted() Thread.interrupted()} method.
130: * @api2vm
131: */
132: static native boolean isInterrupted();
133:
134: /**
135: * This method satisfies the requirements of the specification for the
136: * {@link Thread#isInterrupted() Thread.isInterrupted()} method.
137: * @api2vm
138: */
139: static native boolean isInterrupted(Thread thread);
140:
141: /**
142: * This method satisfies the requirements of the specification for the
143: * {@link Object#notify() Object.notify()} method.
144: * @api2vm
145: */
146: static native int notify(Object object);
147:
148: /**
149: * This method satisfies the requirements of the specification for the
150: * {@link Object#notifyAll() Object.notifyAll()} method.
151: * @api2vm
152: */
153: static native int notifyAll(Object object);
154:
155: /**
156: * This method satisfies the requirements of the specification for the
157: * {@link Thread#resume() Thread.resume()} method.
158: * @api2vm
159: */
160: static native int resume(Thread thread);
161:
162: /**
163: * Changes the priority of the specified thread.
164: * <p>
165: * <b>Note:</b> This method is used for the {@link Thread#setPriority(int)
166: * Thread.setPriority(int newPriority)} method implementation.
167: *
168: * @param thread the thread to change the priority for
169: * @param priority new priority value
170: * @api2vm
171: */
172: static native int setPriority(Thread thread, int priority);
173:
174: /**
175: * This method satisfies the requirements of the specification for the
176: * {@link Thread#sleep(long, int) Thread.sleep(long timeout, int nanos)}
177: * method. But it doesn't throw an <code>IllegalArgumentException</code>
178: * exception. The <code>millis</code> and <code>nanos</code> arguments
179: * must be valid.
180: * @api2vm
181: */
182: static native int sleep(long millis, int nanos);
183:
184: /**
185: * Starts the specified thread. Causes JVM to start executing
186: * <code>run()</code> method of the specified thread.
187: * <p>
188: * The method allows to set the stack size for the thread specified in its
189: * {@link Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable,
190: * java.lang.String, long) constructor}.
191: * <p>
192: * <b>Note:</b> This method is used for the {@link Thread#start()
193: * Thread.start()} method implementation.
194: *
195: * @param thread the thread to be started
196: * @param size the desired stack size in bytes or zero to be ignored
197: * @api2vm
198: */
199: static native int start(Thread thread, long stackSize,
200: boolean daemon, int priority);
201:
202: /**
203: * This method satisfies the requirements of the specification for the
204: * {@link Thread#stop(java.lang.Throwable) Thread.stop(Throwable obj)}
205: * method. But it doesn't throw an <code>NullPointerException</code>
206: * exception. The <code>throwable</code> argument must not be null.
207: * @api2vm
208: */
209: static native int stop(Thread thread, Throwable throwable);
210:
211: /**
212: * This method satisfies the requirements of the specification for the
213: * {@link Thread#suspend() Thread.suspend()} method.
214: * @api2vm
215: */
216: static native int suspend(Thread thread);
217:
218: /**
219: * This method satisfies the requirements of the specification for the
220: * {@link Object#wait(long, int) Object.wait(long timeout, int nanos)}
221: * method. But it doesn't throw an <code>IllegalArgumentException</code>
222: * exception. The <code>millis</code> and <code>nanos</code> arguments
223: * must be valid.
224: * @api2vm
225: */
226: static native int wait(Object object, long millis, int nanos)
227: throws InterruptedException;
228:
229: /**
230: * This method satisfies the requirements of the specification for the
231: * {@link Thread#yield() Thread.yield()} method.
232: * @api2vm
233: */
234: static native int yield();
235:
236: /**
237: * This method initialize native thread structure as well as inter dependencies
238: * between java thread and native thread.
239: * @api2vm
240: */
241: static native long init(Thread thread, ThreadWeakRef ref,
242: long oldAddr);
243:
244: /**
245: * Returns the state of the given thread as described by JVMTI spec
246: * @api2vm
247: */
248: static native int getState(java.lang.Thread thread);
249: }
|