1 /* VMThread -- VM interface for Thread of executable code
2 Copyright (C) 2003, 2004, 2005 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
.group
.uncaughtException(thread
, t
);
128 catch(Throwable ignore
)
135 // Setting runnable to false is partial protection against stop
136 // being called while we're cleaning up. To be safe all code in
137 // VMThread be unstoppable.
142 // release the threads waiting to join us
149 * Creates a native Thread. This is called from the start method of Thread.
150 * The Thread is started.
152 * @param thread The newly created Thread object
153 * @param stacksize Indicates the requested stacksize. Normally zero,
154 * non-zero values indicate requested stack size in bytes but it is up
155 * to the specific VM implementation to interpret them and may be ignored.
157 static void create(Thread thread
, long stacksize
)
159 VMThread vmThread
= new VMThread(thread
);
160 vmThread
.start(stacksize
);
161 thread
.vmThread
= vmThread
;
165 * Gets the name of the thread. Usually this is the name field of the
166 * associated Thread object, but some implementation might choose to
167 * return the name of the underlying platform thread.
175 * Set the name of the thread. Usually this sets the name field of the
176 * associated Thread object, but some implementations might choose to
177 * set the name of the underlying platform thread.
178 * @param name The new name
180 void setName(String name
)
186 * Set the thread priority field in the associated Thread object and
187 * calls the native method to set the priority of the underlying
189 * @param priority The new priority
191 void setPriority(int priority
)
193 thread
.priority
= priority
;
194 nativeSetPriority(priority
);
198 * Returns the priority. Usually this is the priority field from the
199 * associated Thread object, but some implementation might choose to
200 * return the priority of the underlying platform thread.
201 * @return this Thread's priority
205 return thread
.priority
;
209 * Returns true if the thread is a daemon thread. Usually this is the
210 * daemon field from the associated Thread object, but some
211 * implementation might choose to return the daemon state of the underlying
213 * @return whether this is a daemon Thread or not
217 return thread
.daemon
;
221 * Returns the number of stack frames in this Thread.
222 * Will only be called when when a previous call to suspend() returned true.
224 * @deprecated unsafe operation
226 native int countStackFrames();
229 * Wait the specified amount of time for the Thread in question to die.
231 * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
232 * not offer that fine a grain of timing resolution. Besides, there is
233 * no guarantee that this thread can start up immediately when time expires,
234 * because some other thread may be active. So don't expect real-time
237 * @param ms the number of milliseconds to wait, or 0 for forever
238 * @param ns the number of extra nanoseconds to sleep (0-999999)
239 * @throws InterruptedException if the Thread is interrupted; it's
240 * <i>interrupted status</i> will be cleared
242 synchronized void join(long ms
, int ns
) throws InterruptedException
245 ms
+= (ns
!= 0) ?
1 : 0;
247 // Compute end time, but don't overflow
248 long now
= System
.currentTimeMillis();
251 end
= Long
.MAX_VALUE
;
253 // A VM is allowed to return from wait() without notify() having been
254 // called, so we loop to handle possible spurious wakeups.
255 while(thread
.vmThread
!= null)
257 // We use the VMThread object to wait on, because this is a private
258 // object, so client code cannot call notify on us.
262 now
= System
.currentTimeMillis();
273 * Cause this Thread to stop abnormally and throw the specified exception.
274 * If you stop a Thread that has not yet started, the stop is ignored
275 * (contrary to what the JDK documentation says).
276 * <b>WARNING</b>This bypasses Java security, and can throw a checked
277 * exception which the call stack is unprepared to handle. Do not abuse
280 * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
281 * leave data in bad states.
283 * <p><b>NOTE</b> stop() should take care not to stop a thread if it is
284 * executing code in this class.
286 * @param t the Throwable to throw when the Thread dies
287 * @deprecated unsafe operation, try not to use
289 void stop(Throwable t
)
291 // Note: we assume that we own the lock on thread
292 // (i.e. that Thread.stop() is synchronized)
296 thread
.stillborn
= t
;
300 * Create a native thread on the underlying platform and start it executing
301 * on the run method of this object.
302 * @param stacksize the requested size of the native thread stack
304 native void start(long stacksize
);
307 * Interrupt this thread.
309 native void interrupt();
312 * Determine whether this Thread has been interrupted, but leave
313 * the <i>interrupted status</i> alone in the process.
315 * @return whether the Thread has been interrupted
317 native boolean isInterrupted();
320 * Suspend this Thread. It will not come back, ever, unless it is resumed.
322 native void suspend();
325 * Resume this Thread. If the thread is not suspended, this method does
328 native void resume();
331 * Set the priority of the underlying platform thread.
333 * @param priority the new priority
335 native void nativeSetPriority(int priority
);
338 * Asynchronously throw the specified throwable in this Thread.
340 * @param t the exception to throw
342 native void nativeStop(Throwable t
);
345 * Return the Thread object associated with the currently executing
348 * @return the currently executing Thread
350 static native Thread
currentThread();
353 * Yield to another thread. The Thread will not lose any locks it holds
354 * during this time. There are no guarantees which thread will be
355 * next to run, and it could even be this one, but most VMs will choose
356 * the highest priority thread that has been waiting longest.
358 static native void yield();
361 * Suspend the current Thread's execution for the specified amount of
362 * time. The Thread will not lose any locks it has during this time. There
363 * are no guarantees which thread will be next to run, but most VMs will
364 * choose the highest priority thread that has been waiting longest.
366 * <p>Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do
367 * not offer that fine a grain of timing resolution. Besides, there is
368 * no guarantee that this thread can start up immediately when time expires,
369 * because some other thread may be active. So don't expect real-time
372 * @param ms the number of milliseconds to sleep.
373 * @param ns the number of extra nanoseconds to sleep (0-999999)
374 * @throws InterruptedException if the Thread is (or was) interrupted;
375 * it's <i>interrupted status</i> will be cleared
377 static void sleep(long ms
, int ns
) throws InterruptedException
379 // Note: JDK treats a zero length sleep is like Thread.yield(),
380 // without checking the interrupted status of the thread.
381 // It's unclear if this is a bug in the implementation or the spec.
382 // See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213203
383 if (ms
== 0 && ns
== 0)
385 if (Thread
.interrupted())
386 throw new InterruptedException();
390 // Compute end time, but don't overflow
391 long now
= System
.currentTimeMillis();
394 end
= Long
.MAX_VALUE
;
396 // A VM is allowed to return from wait() without notify() having been
397 // called, so we loop to handle possible spurious wakeups.
398 VMThread vt
= Thread
.currentThread().vmThread
;
404 now
= System
.currentTimeMillis();
414 * Determine whether the current Thread has been interrupted, and clear
415 * the <i>interrupted status</i> in the process.
417 * @return whether the current Thread has been interrupted
419 static native boolean interrupted();
422 * Checks whether the current thread holds the monitor on a given object.
423 * This allows you to do <code>assert Thread.holdsLock(obj)</code>.
425 * @param obj the object to check
426 * @return true if the current thread is currently synchronized on obj
427 * @throws NullPointerException if obj is null
429 static boolean holdsLock(Object obj
)
431 /* Use obj.notify to check if the current thread holds
432 * the monitor of the object.
433 * If it doesn't, notify will throw an exception.
438 // okay, current thread holds lock
441 catch (IllegalMonitorStateException e
)
443 // it doesn't hold the lock