| 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.generational; |
| 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 GenGCThread extends Thread { |
| 30 |
|
| 31 |
/** |
| 32 |
* The manager |
| 33 |
*/ |
| 34 |
private final GenGCManager manager; |
| 35 |
|
| 36 |
/** |
| 37 |
* Monitor for synchronizing access to my fields |
| 38 |
*/ |
| 39 |
private Monitor heapMonitor; |
| 40 |
|
| 41 |
/** |
| 42 |
* Is a GC run requested? |
| 43 |
*/ |
| 44 |
private boolean runMajorNeeded; |
| 45 |
private boolean runMinorNeeded; |
| 46 |
|
| 47 |
/** |
| 48 |
* Is the GC currently active |
| 49 |
*/ |
| 50 |
private boolean gcActive; |
| 51 |
|
| 52 |
/** |
| 53 |
* Initialize this instance. |
| 54 |
* |
| 55 |
* @param manager |
| 56 |
*/ |
| 57 |
public GenGCThread(GenGCManager manager, Monitor heapMonitor) { |
| 58 |
super("gc-thread"); |
| 59 |
this.manager = manager; |
| 60 |
this.heapMonitor = heapMonitor; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Trigger a major GC run. |
| 65 |
* |
| 66 |
* @param waitToFinish |
| 67 |
* If true, block until the run is ready, if false, return |
| 68 |
* immediately. |
| 69 |
*/ |
| 70 |
public final void trigger(boolean waitToFinish) { |
| 71 |
triggerMajor(waitToFinish); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Trigger a major GC run. |
| 76 |
* |
| 77 |
* @param waitToFinish |
| 78 |
* If true, block until the run is ready, if false, return |
| 79 |
* immediately. |
| 80 |
*/ |
| 81 |
public final void triggerMajor(boolean waitToFinish) { |
| 82 |
if (runMajorNeeded && !waitToFinish) { |
| 83 |
return; |
| 84 |
} |
| 85 |
heapMonitor.enter(); |
| 86 |
try { |
| 87 |
runMajorNeeded = true; |
| 88 |
heapMonitor.NotifyAll(); |
| 89 |
if (waitToFinish) { |
| 90 |
waitForMajorCollectionEnd(); |
| 91 |
} |
| 92 |
} finally { |
| 93 |
heapMonitor.exit(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Trigger a minor GC run. |
| 99 |
* |
| 100 |
* @param waitToFinish |
| 101 |
* If true, block until the run is ready, if false, return |
| 102 |
* immediately. |
| 103 |
*/ |
| 104 |
public final void triggerMinor(boolean waitToFinish) { |
| 105 |
if (runMinorNeeded && !waitToFinish) { |
| 106 |
return; |
| 107 |
} |
| 108 |
heapMonitor.enter(); |
| 109 |
try { |
| 110 |
runMinorNeeded = true; |
| 111 |
heapMonitor.NotifyAll(); |
| 112 |
if (waitToFinish) { |
| 113 |
waitForMinorCollectionEnd(); |
| 114 |
} |
| 115 |
} finally { |
| 116 |
heapMonitor.exit(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Continue to GC. |
| 122 |
* |
| 123 |
* @see java.lang.Runnable#run() |
| 124 |
*/ |
| 125 |
@Override |
| 126 |
public final void run() { |
| 127 |
while (true) { |
| 128 |
try { |
| 129 |
waitForGcStart(); |
| 130 |
callGc(); |
| 131 |
notifyGcEnd(); |
| 132 |
} catch (Throwable ex) { |
| 133 |
try { |
| 134 |
Unsafe.debug(ex.getMessage()); |
| 135 |
Unsafe.debug('\n'); |
| 136 |
Unsafe.debugStackTrace(ex); |
| 137 |
Unsafe.die("GCThread failed"); |
| 138 |
} catch (Throwable ex2) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private void waitForMajorCollectionEnd() { |
| 146 |
while (runMajorNeeded || gcActive) { |
| 147 |
try { |
| 148 |
heapMonitor.Wait(0L); |
| 149 |
} catch (InterruptedException ex) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private void waitForMinorCollectionEnd() { |
| 156 |
while (runMinorNeeded || gcActive) { |
| 157 |
try { |
| 158 |
heapMonitor.Wait(0L); |
| 159 |
} catch (InterruptedException ex) { |
| 160 |
continue; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
private final void waitForGcStart() throws InterruptedException { |
| 166 |
heapMonitor.enter(); |
| 167 |
try { |
| 168 |
while (!runMajorNeeded) { |
| 169 |
heapMonitor.Wait(0L); |
| 170 |
} |
| 171 |
gcActive = true; |
| 172 |
} finally { |
| 173 |
heapMonitor.exit(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private final void callGc() throws InterruptedException { |
| 178 |
if (runMajorNeeded) { |
| 179 |
Unsafe.debug("GenGCThread.callGc(major)\n"); |
| 180 |
manager.majorGC(); |
| 181 |
runMinorNeeded = false; |
| 182 |
} else { |
| 183 |
Unsafe.debug("GenGCThread.callGc(minor)\n"); |
| 184 |
manager.minorGC(); |
| 185 |
runMinorNeeded = false; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
private final void notifyGcEnd() { |
| 190 |
gcActive = false; |
| 191 |
heapMonitor.enter(); |
| 192 |
try { |
| 193 |
heapMonitor.NotifyAll(); |
| 194 |
} finally { |
| 195 |
heapMonitor.exit(); |
| 196 |
} |
| 197 |
} |
| 198 |
} |