From 6e269323f8e459b3dcd40aaca54f7cf7fe9d3d9b Mon Sep 17 00:00:00 2001 From: leventov Date: Thu, 23 Jul 2015 00:45:25 +0300 Subject: [PATCH] Make nested contexts acquisition ordered --- README.md | 14 ++++++++++++++ .../openhft/chronicle/map/NestedContextsTest.java | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 588d3802d..2b3a3430c 100644 --- a/README.md +++ b/README.md @@ -1927,6 +1927,13 @@ Using nested contexts: if (source == target) throw new IllegalArgumentException("loops are forbidden"); + // order for consistent lock acquisition => avoid dead lock + if (target < source) { + int t = source; + source = target; + target = t; + } + try (ExternalMapQueryContext, ?> sc = graph.queryContext(source)) { sc.updateLock().lock(); try (ExternalMapQueryContext, ?> tc = @@ -2000,6 +2007,13 @@ Using nested contexts: public static boolean removeEdge( ChronicleMap> graph, int source, int target) { + // order for consistent lock acquisition => avoid dead lock + if (target < source) { + int t = source; + source = target; + target = t; + } + try (ExternalMapQueryContext, ?> sc = graph.queryContext(source)) { sc.updateLock().lock(); MapEntry> sEntry = sc.entry(); diff --git a/src/test/java/net/openhft/chronicle/map/NestedContextsTest.java b/src/test/java/net/openhft/chronicle/map/NestedContextsTest.java index dc52d5420..88c3f574d 100644 --- a/src/test/java/net/openhft/chronicle/map/NestedContextsTest.java +++ b/src/test/java/net/openhft/chronicle/map/NestedContextsTest.java @@ -81,6 +81,12 @@ public class NestedContextsTest { ChronicleMap> graph, int source, int target) { if (source == target) throw new IllegalArgumentException("loops are forbidden"); + // order for consistent lock acquisition => avoid dead lock + if (target < source) { + int t = source; + source = target; + target = t; + } try (ExternalMapQueryContext, ?> sc = graph.queryContext(source)) { sc.updateLock().lock(); @@ -155,6 +161,13 @@ public class NestedContextsTest { public static boolean removeEdge( ChronicleMap> graph, int source, int target) { + // order for consistent lock acquisition => avoid dead lock + if (target < source) { + int t = source; + source = target; + target = t; + } + try (ExternalMapQueryContext, ?> sc = graph.queryContext(source)) { sc.updateLock().lock(); MapEntry> sEntry = sc.entry();