| 1 |
/* |
| 2 |
* $Id$ |
| 3 |
* |
| 4 |
* Copyright (C) 2003-2009 JNode.org |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; either version 2.1 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 |
* License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this library; If not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
package org.jnode.vm.memmgr.def; |
| 22 |
|
| 23 |
import org.jnode.vm.Unsafe; |
| 24 |
import org.jnode.vm.scheduler.Monitor; |
| 25 |
|
| 26 |
/** |
| 27 |
* @author Ewout Prangsma (epr@users.sourceforge.net) |
| 28 |
*/ |
| 29 |
final class GCThread extends Thread { |
| 30 |
|
| 31 |
/** |
| 32 |
* The manager |
| 33 |
*/ |
| 34 |
private final GCManager manager; |
| 35 |
|
| 36 |
/** |
| 37 |
* Monitor for synchronizing access to my fields |
| 38 |
*/ |
| 39 |
private final Monitor heapMonitor; |
| 40 |
|
| 41 |
/** |
| 42 |
* Is a GC run requested? |
| 43 |
*/ |
| 44 |
private boolean runNeeded; |
| 45 |
|
| 46 |
/** |
| 47 |
* Is the GC currently active |
| 48 |
*/ |
| 49 |
private boolean gcActive; |
| 50 |
|
| 51 |
/** |
| 52 |
* Initialize this instance. |
| 53 |
* |
| 54 |
* @param manager |
| 55 |
*/ |
| 56 |
public GCThread(GCManager manager, Monitor heapMonitor) { |
| 57 |
super("gc-thread"); |
| 58 |
this.manager = manager; |
| 59 |
this.heapMonitor = heapMonitor; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Trigger a GC run. |
| 64 |
* |
| 65 |
* @param waitToFinish If true, block until the run is ready, if false, return |
| 66 |
* immediately. |
| 67 |
*/ |
| 68 |
public final void trigger(boolean waitToFinish) { |
| 69 |
if (runNeeded && !waitToFinish) { |
| 70 |
return; |
| 71 |
} |
| 72 |
heapMonitor.enter(); |
| 73 |
try { |
| 74 |
runNeeded = true; |
| 75 |
heapMonitor.NotifyAll(); |
| 76 |
if (waitToFinish) { |
| 77 |
while (runNeeded || gcActive) { |
| 78 |
try { |
| 79 |
heapMonitor.Wait(0L); |
| 80 |
} catch (InterruptedException ex) { |
| 81 |
// Ignore |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} finally { |
| 86 |
heapMonitor.exit(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Continue to GC. |
| 92 |
* |
| 93 |
* @see java.lang.Runnable#run() |
| 94 |
*/ |
| 95 |
public final void run() { |
| 96 |
while (true) { |
| 97 |
try { |
| 98 |
heapMonitor.enter(); |
| 99 |
try { |
| 100 |
while (!runNeeded) { |
| 101 |
heapMonitor.Wait(0L); |
| 102 |
} |
| 103 |
gcActive = true; |
| 104 |
runNeeded = false; |
| 105 |
} finally { |
| 106 |
heapMonitor.exit(); |
| 107 |
} |
| 108 |
|
| 109 |
// Now do the actual GC |
| 110 |
manager.gc(); |
| 111 |
|
| 112 |
// Notify that we're ready |
| 113 |
gcActive = false; |
| 114 |
heapMonitor.enter(); |
| 115 |
try { |
| 116 |
heapMonitor.NotifyAll(); |
| 117 |
} finally { |
| 118 |
heapMonitor.exit(); |
| 119 |
} |
| 120 |
} catch (Throwable ex) { |
| 121 |
try { |
| 122 |
Unsafe.debug(ex.getMessage()); |
| 123 |
Unsafe.debug('\n'); |
| 124 |
Unsafe.debugStackTrace(ex); |
| 125 |
Unsafe.die("GCThread failed"); |
| 126 |
} catch (Throwable ex2) { |
| 127 |
// Ignore |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|