| 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.scheduler.Monitor; |
| 24 |
|
| 25 |
/** |
| 26 |
* @author Loic Rouchon (loic.rouchon@insa-lyon.fr) |
| 27 |
*/ |
| 28 |
final class GenGCSweepThread extends Thread { |
| 29 |
|
| 30 |
/** |
| 31 |
* The queue that contains references to objects to free |
| 32 |
*/ |
| 33 |
private final GenGCManager gcManager; |
| 34 |
|
| 35 |
private final Monitor heapMonitor; |
| 36 |
|
| 37 |
private Monitor monitor = new Monitor(); |
| 38 |
|
| 39 |
private boolean runNeeded = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize this instance. |
| 43 |
* |
| 44 |
* @param manager |
| 45 |
* The queue that contains references to objects to free |
| 46 |
* @param defaultHeap |
| 47 |
* The default heap that contains objects to free |
| 48 |
*/ |
| 49 |
public GenGCSweepThread(GenGCManager gcManager, Monitor heapMonitor) { |
| 50 |
super("gc-sweep-thread"); |
| 51 |
this.gcManager = gcManager; |
| 52 |
this.heapMonitor = heapMonitor; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* |
| 57 |
* @param waitToFinish |
| 58 |
*/ |
| 59 |
public final void trigger(boolean waitToFinish) { |
| 60 |
if (runNeeded && !waitToFinish) { |
| 61 |
return; |
| 62 |
} |
| 63 |
monitor.enter(); |
| 64 |
if (!runNeeded) { |
| 65 |
startThread(); |
| 66 |
} |
| 67 |
if (waitToFinish) { |
| 68 |
waitForEnd(); |
| 69 |
} |
| 70 |
monitor.exit(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Wait for objects to delete and when available, free them. |
| 75 |
* |
| 76 |
* @see java.lang.Runnable#run() |
| 77 |
*/ |
| 78 |
@Override |
| 79 |
public final void run() { |
| 80 |
// Unsafe.debug("GenGCSweepThread.run()\n"); |
| 81 |
while (true) { |
| 82 |
waitForTrigger(); |
| 83 |
// Unsafe.debug("GenGCSweepThread.run(sweep)\n"); |
| 84 |
gcManager.sweep(); |
| 85 |
heapMonitor.enter(); |
| 86 |
gcManager.cleanup(); |
| 87 |
heapMonitor.exit(); |
| 88 |
notifyEnds(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private void startThread() { |
| 93 |
runNeeded = true; |
| 94 |
monitor.NotifyAll(); |
| 95 |
// Unsafe.debug("GenGCSweepThread.startThread()\n"); |
| 96 |
} |
| 97 |
|
| 98 |
private void waitForEnd() { |
| 99 |
// Unsafe.debug("GenGCSweepThread.waitForEnd()\n"); |
| 100 |
while (runNeeded) { |
| 101 |
waitForMonitor(); |
| 102 |
} |
| 103 |
// Unsafe.debug("GenGCSweepThread.waitForEnd(done)\n"); |
| 104 |
} |
| 105 |
|
| 106 |
private void waitForTrigger() { |
| 107 |
// Unsafe.debug("GenGCSweepThread.run(wait)\n"); |
| 108 |
monitor.enter(); |
| 109 |
while (!runNeeded) { |
| 110 |
waitForMonitor(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private void notifyEnds() { |
| 115 |
// Unsafe.debug("GenGCSweepThread.run(ended)\n"); |
| 116 |
runNeeded = false; |
| 117 |
monitor.NotifyAll(); |
| 118 |
monitor.exit(); |
| 119 |
} |
| 120 |
|
| 121 |
private void waitForMonitor() { |
| 122 |
try { |
| 123 |
monitor.Wait(0L); |
| 124 |
} catch (InterruptedException e) { |
| 125 |
} |
| 126 |
} |
| 127 |
} |