1 /* VMThread -- VM interface for Thread of executable code
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 * VM interface for Thread of executable code. Holds VM dependent state.
42 * It is deliberately package local and final and should only be accessed
43 * by the Thread class.
45 * This is the GNU Classpath reference implementation, it should be adapted
48 * The following methods must be implemented:
50 * <li>native void start(long stacksize);
51 * <li>native void interrupt();
52 * <li>native boolean isInterrupted();
53 * <li>native void suspend();
54 * <li>native void resume();
55 * <li>native void nativeSetPriority(int priority);
56 * <li>native void nativeStop(Throwable t);
57 * <li>native static Thread currentThread();
58 * <li>static native void yield();
59 * <li>static native boolean interrupted();
61 * All other methods may be implemented to make Thread handling more efficient
62 * or to implement some optional (and sometimes deprecated) behaviour. Default
63 * implementations are provided but it is highly recommended to optimize them
66 * @author Jeroen Frijters (jeroen@frijters.net)
67 * @author Dalibor Topic (robilad@kaffe.org)
72 * The Thread object that this VM state belongs to.
73 * Used in currentThread() and start().
74 * Note: when this thread dies, this reference is *not* cleared
76 volatile Thread thread;
79 * Flag that is set when the thread runs, used by stop() to protect against
80 * stop's getting lost.
82 private volatile boolean running;
87 private transient Object vmdata;
90 * Private constructor, create VMThreads with the static create method.
92 * @param thread The Thread object that was just created.
94 private VMThread(Thread thread)
100 * This method is the initial Java code that gets executed when a native
101 * thread starts. It's job is to coordinate with the rest of the VMThread
102 * logic and to start executing user code and afterwards handle clean up.
113 Throwable t = thread.stillborn;
116 thread.stillborn = null;
126 Thread.UncaughtExceptionHandler handler;
127 handler = thread.getUncaughtExceptionHandler();
128 handler.uncaughtException(thread, t);
130 catch(Throwable ignore)
137 // Setting runnable to false is partial protection against stop
138 // being called while we're cleaning up. To be safe all code in
139 // VMThread be unstoppable.
144 // release the threads waiting to join us
151 * Creates a native Thread. This is called from the start method of Thread.
152 * The Thread is started.
154 * @param thread The newly created Thread object
155 * @param stacksize Indicates the requested stacksize. Normally zero,
156 * non-zero values indicate requested stack size in bytes but it is up
157 * to the specific VM implementation to interpret them and may be ignored.
159 static void create(Thread thread, long stacksize)
161 VMThread vmThread = new VMThread(thread);
162 thread.vmThread = vmThread;
163 vmThread.start(stacksize);
167 * Gets the name of the thread. Usually this is the name field of the
168 * associated Thread object, but some implementation might choose to
169 * return the name of the underlying platform thread.
177 * Set the name of the thread. Usually this sets the name field of the
178 * associated Thread object, but some implementations might choose to
179 * set the name of the underlying platform thread.
180 * @param name The new name
182 void setName(String name)
188 * Set the thread priority field in the associated Thread object and
189 * calls the native method to set the priority of the underlying
191 * @param priority The new priority
193 void setPriority(int priority)
195 thread.priority = priority;
196 nativeSetPriority(priority);
200 * Returns the priority. Usually this is the priority field from the
201 * associated Thread object, but some implementation might choose to
202 * return the priority of the underlying platform thread.
203 * @return this Thread's priority
207 return thread.priority;
211 * Returns true if the thread is a daemon thread. Usually this is the
212 * daemon field from the associated Thread object, but some
213 * implementation might choose to return the daemon state of the underlying
215 * @return whether this is a daemon Thread or not
219 return thread.daemon;
223 * Returns the number of stack frames in this Thread.
224 * Will only be called when when a previous call to suspend() returned true.
226 * @deprecated unsafe operation
228 native int countStackFrames();
231 * Wait the specified amount of time for the Thread in question to die.
233 * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
234 * not offer that fine a grain of timing resolution. Besides, there is
235 * no guarantee that this thread can start up immediately when time expires,
236 * because some other thread may be active. So don't expect real-time
239 * @param ms the number of milliseconds to wait, or 0 for forever
240 * @param ns the number of extra nanoseconds to sleep (0-999999)
241 * @throws InterruptedException if the Thread is interrupted; it's
242 * <i>interrupted status</i> will be cleared
244 synchronized void join(long ms, int ns) throws InterruptedException
247 ms += (ns != 0) ? 1 : 0;
249 // Compute end time, but don't overflow
250 long now = System.currentTimeMillis();
253 end = Long.MAX_VALUE;
255 // A VM is allowed to return from wait() without notify() having been
256 // called, so we loop to handle possible spurious wakeups.
257 while(thread.vmThread != null)
259 // We use the VMThread object to wait on, because this is a private
260 // object, so client code cannot call notify on us.
264 now = System.currentTimeMillis();
275 * Cause this Thread to stop abnormally and throw the specified exception.
276 * If you stop a Thread that has not yet started, the stop is ignored
277 * (contrary to what the JDK documentation says).
278 * <b>WARNING</b>This bypasses Java security, and can throw a checked
279 * exception which the call stack is unprepared to handle. Do not abuse
282 * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
283 * leave data in bad states.
285 * <p><b>NOTE</b> stop() should take care not to stop a thread if it is
286 * executing code in this class.
288 * @param t the Throwable to throw when the Thread dies
289 * @deprecated unsafe operation, try not to use
291 void stop(Throwable t)
293 // Note: we assume that we own the lock on thread
294 // (i.e. that Thread.stop() is synchronized)
298 thread.stillborn = t;
302 * Create a native thread on the underlying platform and start it executing
303 * on the run method of this object.
304 * @param stacksize the requested size of the native thread stack
306 native void start(long stacksize);
309 * Interrupt this thread.
311 native void interrupt();
314 * Determine whether this Thread has been interrupted, but leave
315 * the <i>interrupted status</i> alone in the process.
317 * @return whether the Thread has been interrupted
319 native boolean isInterrupted();
322 * Suspend this Thread. It will not come back, ever, unless it is resumed.
324 native void suspend();
327 * Resume this Thread. If the thread is not suspended, this method does
330 native void resume();
333 * Set the priority of the underlying platform thread.
335 * @param priority the new priority
337 native void nativeSetPriority(int priority);
340 * Asynchronously throw the specified throwable in this Thread.
342 * @param t the exception to throw
344 native void nativeStop(Throwable t);
347 * Return the Thread object associated with the currently executing
350 * @return the currently executing Thread
352 static native Thread currentThread();
355 * Yield to another thread. The Thread will not lose any locks it holds
356 * during this time. There are no guarantees which thread will be
357 * next to run, and it could even be this one, but most VMs will choose
358 * the highest priority thread that has been waiting longest.
360 static native void yield();
363 * Suspend the current Thread's execution for the specified amount of
364 * time. The Thread will not lose any locks it has during this time. There
365 * are no guarantees which thread will be next to run, but most VMs will
366 * choose the highest priority thread that has been waiting longest.
368 * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
369 * not offer that fine a grain of timing resolution. Besides, there is
370 * no guarantee that this thread can start up immediately when time expires,
371 * because some other thread may be active. So don't expect real-time
374 * @param ms the number of milliseconds to sleep.
375 * @param ns the number of extra nanoseconds to sleep (0-999999)
376 * @throws InterruptedException if the Thread is (or was) interrupted;
377 * it's <i>interrupted status</i> will be cleared
379 static void sleep(long ms, int ns) throws InterruptedException
381 // Note: JDK treats a zero length sleep is like Thread.yield(),
382 // without checking the interrupted status of the thread.
383 // It's unclear if this is a bug in the implementation or the spec.
384 // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203
385 if (ms == 0 && ns == 0)
387 if (Thread.interrupted())
388 throw new InterruptedException();
392 // Compute end time, but don't overflow
393 long now = System.currentTimeMillis();
396 end = Long.MAX_VALUE;
398 // A VM is allowed to return from wait() without notify() having been
399 // called, so we loop to handle possible spurious wakeups.
400 VMThread vt = Thread.currentThread().vmThread;
406 now = System.currentTimeMillis();
416 * Determine whether the current Thread has been interrupted, and clear
417 * the <i>interrupted status</i> in the process.
419 * @return whether the current Thread has been interrupted
421 static native boolean interrupted();
424 * Checks whether the current thread holds the monitor on a given object.
425 * This allows you to do <code>assert Thread.holdsLock(obj)</code>.
427 * @param obj the object to check
428 * @return true if the current thread is currently synchronized on obj
429 * @throws NullPointerException if obj is null
431 // static boolean holdsLock(Object obj)
433 // /* Use obj.notify to check if the current thread holds
434 // * the monitor of the object.
435 // * If it doesn't, notify will throw an exception.
440 // // okay, current thread holds lock
443 // catch (IllegalMonitorStateException e)
445 // // it doesn't hold the lock
449 static native boolean holdsLock(Object obj);
452 * Returns the current state of the thread.
453 * The value must be one of "BLOCKED", "NEW",
454 * "RUNNABLE", "TERMINATED", "TIMED_WAITING" or
457 * @return a string corresponding to one of the
458 * thread enumeration states specified above.
460 native String getState();