Make nested contexts acquisition ordered

This commit is contained in:
leventov
2015-07-23 00:45:25 +03:00
parent d5d5c2e48e
commit 6e269323f8
2 changed files with 27 additions and 0 deletions
+14
View File
@@ -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<Integer, Set<Integer>, ?> sc = graph.queryContext(source)) {
sc.updateLock().lock();
try (ExternalMapQueryContext<Integer, Set<Integer>, ?> tc =
@@ -2000,6 +2007,13 @@ Using nested contexts:
public static boolean removeEdge(
ChronicleMap<Integer, Set<Integer>> 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<Integer, Set<Integer>, ?> sc = graph.queryContext(source)) {
sc.updateLock().lock();
MapEntry<Integer, Set<Integer>> sEntry = sc.entry();
@@ -81,6 +81,12 @@ public class NestedContextsTest {
ChronicleMap<Integer, Set<Integer>> 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<Integer, Set<Integer>, ?> sc = graph.queryContext(source)) {
sc.updateLock().lock();
@@ -155,6 +161,13 @@ public class NestedContextsTest {
public static boolean removeEdge(
ChronicleMap<Integer, Set<Integer>> 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<Integer, Set<Integer>, ?> sc = graph.queryContext(source)) {
sc.updateLock().lock();
MapEntry<Integer, Set<Integer>> sEntry = sc.entry();