1 | // FinalizerThread.java -- Thread in which finalizers are run.
|
---|
2 |
|
---|
3 | /* Copyright (C) 2001 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | package gnu.gcj.runtime;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @author Tom Tromey <tromey@redhat.com>
|
---|
15 | * @date October 3, 2001
|
---|
16 | */
|
---|
17 | public final class FinalizerThread extends Thread
|
---|
18 | {
|
---|
19 | // Finalizers must be run in a thread with no Java-visible locks
|
---|
20 | // held. This qualifies because we don't make the lock visible.
|
---|
21 | private static final Object lock = new Object ();
|
---|
22 |
|
---|
23 | // This is true if the finalizer thread started successfully. It
|
---|
24 | // might be false if, for instance, there are no threads on the
|
---|
25 | // current platform. In this situation we run finalizers in the
|
---|
26 | // caller's thread.
|
---|
27 | private static boolean thread_started = false;
|
---|
28 |
|
---|
29 | public FinalizerThread ()
|
---|
30 | {
|
---|
31 | super ("LibgcjInternalFinalizerThread");
|
---|
32 | setDaemon (true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // This is called by the runtime when a finalizer is ready to be
|
---|
36 | // run. It simply wakes up the finalizer thread.
|
---|
37 | public static void finalizerReady ()
|
---|
38 | {
|
---|
39 | synchronized (lock)
|
---|
40 | {
|
---|
41 | if (! thread_started)
|
---|
42 | runFinalizers ();
|
---|
43 | else
|
---|
44 | lock.notify ();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | // Actually run the finalizers.
|
---|
49 | private static native void runFinalizers ();
|
---|
50 |
|
---|
51 | public void run ()
|
---|
52 | {
|
---|
53 | // Wait on a lock. Whenever we wake up, try to invoke the
|
---|
54 | // finalizers.
|
---|
55 | synchronized (lock)
|
---|
56 | {
|
---|
57 | thread_started = true;
|
---|
58 | while (true)
|
---|
59 | {
|
---|
60 | try
|
---|
61 | {
|
---|
62 | lock.wait ();
|
---|
63 | }
|
---|
64 | catch (InterruptedException _)
|
---|
65 | {
|
---|
66 | // Just ignore it. It doesn't hurt to run finalizers
|
---|
67 | // when none are pending.
|
---|
68 | }
|
---|
69 | runFinalizers ();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|