Remove VanillaChronicleHash from OnExitHook on close()

This commit is contained in:
leventov
2016-11-05 17:19:07 -06:00
parent 86dd624ed3
commit 406bd59848
3 changed files with 21 additions and 14 deletions
@@ -21,8 +21,6 @@ import net.openhft.chronicle.algo.MemoryUnit;
import net.openhft.chronicle.algo.hashing.LongHashFunction;
import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
@@ -1524,7 +1522,7 @@ public final class ChronicleMapBuilder<K, V> implements
map.registerCleaner();
// Ensure safe publication of a ChronicleMap
OS.memory().storeFence();
ChronicleMapCloseOnExitHook.add(map);
map.addToOnExitHook();
}
/**
@@ -1,73 +0,0 @@
/*
* Copyright (C) 2016 Roman Leventov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.impl.VanillaChronicleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.TreeMap;
import java.util.WeakHashMap;
final class ChronicleMapCloseOnExitHook {
private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapCloseOnExitHook.class);
private static WeakHashMap<VanillaChronicleHash.Identity, Long> maps = new WeakHashMap<>();
private static long order = 0;
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
closeAll();
}
});
}
static synchronized void add(VanillaChronicleHash hash) {
if (maps == null)
throw new IllegalStateException("Shutdown in progress");
maps.put(hash.identity, order++);
}
private static void closeAll() {
try {
WeakHashMap<VanillaChronicleHash.Identity, Long> maps;
synchronized (ChronicleMapCloseOnExitHook.class) {
maps = ChronicleMapCloseOnExitHook.maps;
ChronicleMapCloseOnExitHook.maps = null;
}
TreeMap<Long, VanillaChronicleHash> orderedMaps = new TreeMap<>();
maps.forEach((identity, order) -> orderedMaps.put(order, identity.hash()));
// close later added maps first
orderedMaps.descendingMap().values().forEach(h -> {
try {
h.close();
} catch (RuntimeException | AssertionError e) {
LOG.error("", e);
}
});
} catch (Throwable t) {
LOG.error("", t);
throw t;
}
}
private ChronicleMapCloseOnExitHook() {}
}