OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [vm/] [reference/] [java/] [lang/] [VMThread.java] - Blame information for rev 780

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 780 jeremybenn
/* VMThread -- VM interface for Thread of executable code
2
   Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation
3
 
4
This file is part of GNU Classpath.
5
 
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)
9
any later version.
10
 
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.
15
 
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
19
02110-1301 USA.
20
 
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
24
combination.
25
 
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. */
37
 
38
package java.lang;
39
 
40
/**
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.
44
 * <p>
45
 * This is the GNU Classpath reference implementation, it should be adapted
46
 * for a specific VM.
47
 * <p>
48
 * The following methods must be implemented:
49
 * <ul>
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();
60
 * </ul>
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
64
 * for a specific VM.
65
 *
66
 * @author Jeroen Frijters (jeroen@frijters.net)
67
 * @author Dalibor Topic (robilad@kaffe.org)
68
 */
69
final class VMThread
70
{
71
    /**
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
75
     */
76
    volatile Thread thread;
77
 
78
    /**
79
     * Flag that is set when the thread runs, used by stop() to protect against
80
     * stop's getting lost.
81
     */
82
    private volatile boolean running;
83
 
84
    /**
85
     * VM private data.
86
     */
87
    private transient Object vmdata;
88
 
89
    /**
90
     * Private constructor, create VMThreads with the static create method.
91
     *
92
     * @param thread The Thread object that was just created.
93
     */
94
    private VMThread(Thread thread)
95
    {
96
        this.thread = thread;
97
    }
98
 
99
    /**
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.
103
     */
104
    private void run()
105
    {
106
        try
107
        {
108
            try
109
            {
110
                running = true;
111
                synchronized(thread)
112
                {
113
                    Throwable t = thread.stillborn;
114
                    if(t != null)
115
                    {
116
                        thread.stillborn = null;
117
                        throw t;
118
                    }
119
                }
120
                thread.run();
121
            }
122
            catch(Throwable t)
123
            {
124
                try
125
                {
126
                  Thread.UncaughtExceptionHandler handler;
127
                  handler = thread.getUncaughtExceptionHandler();
128
                  handler.uncaughtException(thread, t);
129
                }
130
                catch(Throwable ignore)
131
                {
132
                }
133
            }
134
        }
135
        finally
136
        {
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.
140
            running = false;
141
            thread.die();
142
            synchronized(this)
143
            {
144
                // release the threads waiting to join us
145
                notifyAll();
146
            }
147
        }
148
    }
149
 
150
    /**
151
     * Creates a native Thread. This is called from the start method of Thread.
152
     * The Thread is started.
153
     *
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.
158
     */
159
    static void create(Thread thread, long stacksize)
160
    {
161
        VMThread vmThread = new VMThread(thread);
162
        vmThread.start(stacksize);
163
        thread.vmThread = vmThread;
164
    }
165
 
166
    /**
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.
170
     */
171
    String getName()
172
    {
173
        return thread.name;
174
    }
175
 
176
    /**
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
181
     */
182
    void setName(String name)
183
    {
184
        thread.name = name;
185
    }
186
 
187
    /**
188
     * Set the thread priority field in the associated Thread object and
189
     * calls the native method to set the priority of the underlying
190
     * platform thread.
191
     * @param priority The new priority
192
     */
193
    void setPriority(int priority)
194
    {
195
        thread.priority = priority;
196
        nativeSetPriority(priority);
197
    }
198
 
199
    /**
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
204
     */
205
    int getPriority()
206
    {
207
        return thread.priority;
208
    }
209
 
210
    /**
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
214
     * platform thread.
215
     * @return whether this is a daemon Thread or not
216
     */
217
    boolean isDaemon()
218
    {
219
        return thread.daemon;
220
    }
221
 
222
    /**
223
     * Returns the number of stack frames in this Thread.
224
     * Will only be called when when a previous call to suspend() returned true.
225
     *
226
     * @deprecated unsafe operation
227
     */
228
    native int countStackFrames();
229
 
230
    /**
231
     * Wait the specified amount of time for the Thread in question to die.
232
     *
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
237
     * performance.
238
     *
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
243
     */
244
    synchronized void join(long ms, int ns) throws InterruptedException
245
    {
246
        // Round up
247
        ms += (ns != 0) ? 1 : 0;
248
 
249
        // Compute end time, but don't overflow
250
        long now = System.currentTimeMillis();
251
        long end = now + ms;
252
        if (end < now)
253
            end = Long.MAX_VALUE;
254
 
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)
258
        {
259
            // We use the VMThread object to wait on, because this is a private
260
            // object, so client code cannot call notify on us.
261
            wait(ms);
262
            if(ms != 0)
263
            {
264
                now = System.currentTimeMillis();
265
                ms = end - now;
266
                if(ms <= 0)
267
                {
268
                    break;
269
                }
270
            }
271
        }
272
    }
273
 
274
    /**
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
280
     * this power.
281
     *
282
     * <p>This is inherently unsafe, as it can interrupt synchronized blocks and
283
     * leave data in bad states.
284
     *
285
     * <p><b>NOTE</b> stop() should take care not to stop a thread if it is
286
     * executing code in this class.
287
     *
288
     * @param t the Throwable to throw when the Thread dies
289
     * @deprecated unsafe operation, try not to use
290
     */
291
    void stop(Throwable t)
292
    {
293
        // Note: we assume that we own the lock on thread
294
        // (i.e. that Thread.stop() is synchronized)
295
        if(running)
296
            nativeStop(t);
297
        else
298
            thread.stillborn = t;
299
    }
300
 
301
    /**
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
305
     */
306
    native void start(long stacksize);
307
 
308
    /**
309
     * Interrupt this thread.
310
     */
311
    native void interrupt();
312
 
313
    /**
314
     * Determine whether this Thread has been interrupted, but leave
315
     * the <i>interrupted status</i> alone in the process.
316
     *
317
     * @return whether the Thread has been interrupted
318
     */
319
    native boolean isInterrupted();
320
 
321
    /**
322
     * Suspend this Thread.  It will not come back, ever, unless it is resumed.
323
     */
324
    native void suspend();
325
 
326
    /**
327
     * Resume this Thread.  If the thread is not suspended, this method does
328
     * nothing.
329
     */
330
    native void resume();
331
 
332
    /**
333
     * Set the priority of the underlying platform thread.
334
     *
335
     * @param priority the new priority
336
     */
337
    native void nativeSetPriority(int priority);
338
 
339
    /**
340
     * Asynchronously throw the specified throwable in this Thread.
341
     *
342
     * @param t the exception to throw
343
     */
344
    native void nativeStop(Throwable t);
345
 
346
    /**
347
     * Return the Thread object associated with the currently executing
348
     * thread.
349
     *
350
     * @return the currently executing Thread
351
     */
352
    static native Thread currentThread();
353
 
354
    /**
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.
359
     */
360
    static native void yield();
361
 
362
    /**
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.
367
     *
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
372
     * performance.
373
     *
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
378
     */
379
    static void sleep(long ms, int ns) throws InterruptedException
380
    {
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)
386
        {
387
          if (Thread.interrupted())
388
            throw new InterruptedException();
389
          return;
390
        }
391
 
392
      // Compute end time, but don't overflow
393
      long now = System.currentTimeMillis();
394
      long end = now + ms;
395
      if (end < now)
396
          end = Long.MAX_VALUE;
397
 
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;
401
      synchronized (vt)
402
        {
403
          while (true)
404
            {
405
              vt.wait(ms, ns);
406
              now = System.currentTimeMillis();
407
              if (now >= end)
408
                break;
409
              ms = end - now;
410
              ns = 0;
411
            }
412
        }
413
    }
414
 
415
    /**
416
     * Determine whether the current Thread has been interrupted, and clear
417
     * the <i>interrupted status</i> in the process.
418
     *
419
     * @return whether the current Thread has been interrupted
420
     */
421
    static native boolean interrupted();
422
 
423
    /**
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>.
426
     *
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
430
     */
431
    static boolean holdsLock(Object obj)
432
    {
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.
436
       */
437
      try
438
        {
439
          obj.notify();
440
          // okay, current thread holds lock
441
          return true;
442
        }
443
      catch (IllegalMonitorStateException e)
444
        {
445
          // it doesn't hold the lock
446
          return false;
447
        }
448
    }
449
 
450
  /**
451
   * Returns the current state of the thread.
452
   * The value must be one of "BLOCKED", "NEW",
453
   * "RUNNABLE", "TERMINATED", "TIMED_WAITING" or
454
   * "WAITING".
455
   *
456
   * @return a string corresponding to one of the
457
   *         thread enumeration states specified above.
458
   */
459
  native String getState();
460
 
461
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.