diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java b/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java index ab760dc76..6d9415308 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/entry/MapEntryStages.java @@ -188,6 +188,7 @@ public abstract class MapEntryStages extends HashEntryStages } protected void relocation(Data newValue, long newSizeOfEverythingBeforeValue) { + s.innerWriteLock.lock(); s.free(pos, entrySizeInChunks); long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); allocatedChunks.initEntryAndKeyCopying(entrySize, valueSizeOffset - keySizeOffset); diff --git a/src/test/java/net/openhft/chronicle/map/TrickyContextCasesTest.java b/src/test/java/net/openhft/chronicle/map/TrickyContextCasesTest.java new file mode 100644 index 000000000..cdccd8171 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/TrickyContextCasesTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * 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 . + */ + +package net.openhft.chronicle.map; + +import net.openhft.chronicle.core.values.IntValue; +import net.openhft.lang.model.DataValueClasses; +import org.junit.Test; + +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; + +import static org.junit.Assert.assertNotNull; + +public class TrickyContextCasesTest { + + @Test(expected = IllegalStateException.class) + public void nestedContextsSameKeyTest() { + ChronicleMap map = ChronicleMapBuilder + .of(Integer.class, IntValue.class) + .entries(1).create(); + + IntValue v = DataValueClasses.newInstance(IntValue.class); + v.setValue(2); + map.put(1, v); + try (ExternalMapQueryContext q = map.queryContext(1)) { + q.writeLock().lock(); + // assume the value is 2 + IntValue v2 = q.entry().value().get(); + // this call should throw ISE, as accessing the key 1 in a nested context, but if not... + map.remove(1); + v.setValue(3); + map.put(2, v); + // prints 3 + System.out.println(v2.getValue()); + } + } + + @Test(expected = Exception.class) + public void testPutShouldBeWriteLocked() throws ExecutionException, InterruptedException { + ChronicleMap map = ChronicleMapBuilder + .of(Integer.class, byte[].class) + .averageValue(new byte[1]) + .entries(100).actualSegments(1).create(); + map.put(1, new byte[] {1}); + map.put(2, new byte[] {2}); + try (ExternalMapQueryContext q = map.queryContext(1)) { + MapEntry entry = q.entry(); // acquires read lock implicitly + assertNotNull(entry); + Executors.newFixedThreadPool(1).submit(() -> { + // this call should try to acquire write lock, that should lead to dead lock + // but if not... + // relocates the entry for the key 1 after the entry for 2, under update lock + map.put(1, new byte[] {1, 2, 3, 4, 5}); + // puts the entry for 3 at the place of the entry for the key 1, under update lock + map.put(3, new byte[] {3}); + }).get(); + // prints [3] + System.out.println(Arrays.toString(entry.value().get())); + } + } + +}