/* VMThread -- VM interface for Thread of executable code Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.lang; /** * VM interface for Thread of executable code. Holds VM dependent state. * It is deliberately package local and final and should only be accessed * by the Thread class. *
* This is the GNU Classpath reference implementation, it should be adapted * for a specific VM. *
* The following methods must be implemented: *
Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do * not offer that fine a grain of timing resolution. Besides, there is * no guarantee that this thread can start up immediately when time expires, * because some other thread may be active. So don't expect real-time * performance. * * @param ms the number of milliseconds to wait, or 0 for forever * @param ns the number of extra nanoseconds to sleep (0-999999) * @throws InterruptedException if the Thread is interrupted; it's * interrupted status will be cleared */ synchronized void join(long ms, int ns) throws InterruptedException { // Round up ms += (ns != 0) ? 1 : 0; // Compute end time, but don't overflow long now = System.currentTimeMillis(); long end = now + ms; if (end < now) end = Long.MAX_VALUE; // A VM is allowed to return from wait() without notify() having been // called, so we loop to handle possible spurious wakeups. while(thread.vmThread != null) { // We use the VMThread object to wait on, because this is a private // object, so client code cannot call notify on us. wait(ms); if(ms != 0) { now = System.currentTimeMillis(); ms = end - now; if(ms <= 0) { break; } } } } /** * Cause this Thread to stop abnormally and throw the specified exception. * If you stop a Thread that has not yet started, the stop is ignored * (contrary to what the JDK documentation says). * WARNINGThis bypasses Java security, and can throw a checked * exception which the call stack is unprepared to handle. Do not abuse * this power. * *
This is inherently unsafe, as it can interrupt synchronized blocks and * leave data in bad states. * *
NOTE stop() should take care not to stop a thread if it is * executing code in this class. * * @param t the Throwable to throw when the Thread dies * @deprecated unsafe operation, try not to use */ void stop(Throwable t) { // Note: we assume that we own the lock on thread // (i.e. that Thread.stop() is synchronized) if(running) nativeStop(t); else thread.stillborn = t; } /** * Create a native thread on the underlying platform and start it executing * on the run method of this object. * @param stacksize the requested size of the native thread stack */ native void start(long stacksize); /** * Interrupt this thread. */ native void interrupt(); /** * Determine whether this Thread has been interrupted, but leave * the interrupted status alone in the process. * * @return whether the Thread has been interrupted */ native boolean isInterrupted(); /** * Suspend this Thread. It will not come back, ever, unless it is resumed. */ native void suspend(); /** * Resume this Thread. If the thread is not suspended, this method does * nothing. */ native void resume(); /** * Set the priority of the underlying platform thread. * * @param priority the new priority */ native void nativeSetPriority(int priority); /** * Asynchronously throw the specified throwable in this Thread. * * @param t the exception to throw */ native void nativeStop(Throwable t); /** * Return the Thread object associated with the currently executing * thread. * * @return the currently executing Thread */ static native Thread currentThread(); /** * Yield to another thread. The Thread will not lose any locks it holds * during this time. There are no guarantees which thread will be * next to run, and it could even be this one, but most VMs will choose * the highest priority thread that has been waiting longest. */ static native void yield(); /** * Suspend the current Thread's execution for the specified amount of * time. The Thread will not lose any locks it has during this time. There * are no guarantees which thread will be next to run, but most VMs will * choose the highest priority thread that has been waiting longest. * *
Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
* not offer that fine a grain of timing resolution. Besides, there is
* no guarantee that this thread can start up immediately when time expires,
* because some other thread may be active. So don't expect real-time
* performance.
*
* @param ms the number of milliseconds to sleep.
* @param ns the number of extra nanoseconds to sleep (0-999999)
* @throws InterruptedException if the Thread is (or was) interrupted;
* it's interrupted status will be cleared
*/
static void sleep(long ms, int ns) throws InterruptedException
{
// Note: JDK treats a zero length sleep is like Thread.yield(),
// without checking the interrupted status of the thread.
// It's unclear if this is a bug in the implementation or the spec.
// See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203
if (ms == 0 && ns == 0)
{
if (Thread.interrupted())
throw new InterruptedException();
return;
}
// Compute end time, but don't overflow
long now = System.currentTimeMillis();
long end = now + ms;
if (end < now)
end = Long.MAX_VALUE;
// A VM is allowed to return from wait() without notify() having been
// called, so we loop to handle possible spurious wakeups.
VMThread vt = Thread.currentThread().vmThread;
synchronized (vt)
{
while (true)
{
vt.wait(ms, ns);
now = System.currentTimeMillis();
if (now >= end)
break;
ms = end - now;
ns = 0;
}
}
}
/**
* Determine whether the current Thread has been interrupted, and clear
* the interrupted status in the process.
*
* @return whether the current Thread has been interrupted
*/
static native boolean interrupted();
/**
* Checks whether the current thread holds the monitor on a given object.
* This allows you to do assert Thread.holdsLock(obj)
.
*
* @param obj the object to check
* @return true if the current thread is currently synchronized on obj
* @throws NullPointerException if obj is null
*/
static boolean holdsLock(Object obj)
{
/* Use obj.notify to check if the current thread holds
* the monitor of the object.
* If it doesn't, notify will throw an exception.
*/
try
{
obj.notify();
// okay, current thread holds lock
return true;
}
catch (IllegalMonitorStateException e)
{
// it doesn't hold the lock
return false;
}
}
/**
* Returns the current state of the thread.
* The value must be one of "BLOCKED", "NEW",
* "RUNNABLE", "TERMINATED", "TIMED_WAITING" or
* "WAITING".
*
* @return a string corresponding to one of the
* thread enumeration states specified above.
*/
native String getState();
}