Debian Codegraph @

src/main/org/h2/util/AbbaDetector.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.util.ArrayDeque;
9 import java.util.Deque;
10 import java.util.HashSet;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.WeakHashMap;
14
15 /**
16 * Utility to detect AB-BA deadlocks.
17 */
18 public class AbbaDetector {
19
20 private static final boolean TRACE = false;
21
22 private static final ThreadLocal<Deque<Object>> STACK = ThreadLocal.withInitial(ArrayDeque::new);
23
24 /**
25 * Map of (object A) -> (
26 * map of (object locked before object A) ->
27 * (stack trace where locked) )
28 */
29 private static final Map<Object, Map<Object, Exception>> LOCK_ORDERING =
30 new WeakHashMap<>();
31
32 private static final Set<String> KNOWN_DEADLOCKS = new HashSet<>();
33
34 /**
35 * This method is called just before or just after an object is
36 * synchronized.
37 *
38 * @param o the object, or null for the current class
39 * @return the object that was passed
40 */
41 public static Object begin(Object o) {
42 if (o == null) {
43 o = new SecurityManager() {
44 Class<?> clazz = getClassContext()[2];
45 }.clazz;
46 }
47 Deque<Object> stack = STACK.get();
48 if (!stack.isEmpty()) {
49 // Ignore locks which are locked multiple times in succession -
50 // Java locks are recursive
51 if (stack.contains(o)) {
52 // already synchronized on this
53 return o;
54 }
55 while (!stack.isEmpty()) {
56 Object last = stack.peek();
57 if (Thread.holdsLock(last)) {
58 break;
59 }
60 stack.pop();
61 }
62 }
63 if (TRACE) {
64 String thread = "[thread " + Thread.currentThread().getId() + "]";
65 String indent = new String(new char[stack.size() * 2]).replace((char) 0, ' ');
66 System.out.println(thread + " " + indent +
67 "sync " + getObjectName(o));
68 }
69 if (!stack.isEmpty()) {
70 markHigher(o, stack);
71 }
72 stack.push(o);
73 return o;
74 }
75
76 private static Object getTest(Object o) {
77 // return o.getClass();
78 return o;
79 }
80
81 private static String getObjectName(Object o) {
82 return o.getClass().getSimpleName() + "@" + System.identityHashCode(o);
83 }
84
85 private static synchronized void markHigher(Object o, Deque<Object> older) {
86 Object test = getTest(o);
87 Map<Object, Exception> map = LOCK_ORDERING.get(test);
88 if (map == null) {
89 map = new WeakHashMap<>();
90 LOCK_ORDERING.put(test, map);
91 }
92 Exception oldException = null;
93 for (Object old : older) {
94 Object oldTest = getTest(old);
95 if (oldTest == test) {
96 continue;
97 }
98 Map<Object, Exception> oldMap = LOCK_ORDERING.get(oldTest);
99 if (oldMap != null) {
100 Exception e = oldMap.get(test);
101 if (e != null) {
102 String deadlockType = test.getClass() + " " + oldTest.getClass();
103 if (!KNOWN_DEADLOCKS.contains(deadlockType)) {
104 String message = getObjectName(test) +
105 " synchronized after \n " + getObjectName(oldTest) +
106 ", but in the past before";
107 RuntimeException ex = new RuntimeException(message);
108 ex.initCause(e);
109 ex.printStackTrace(System.out);
110 // throw ex;
111 KNOWN_DEADLOCKS.add(deadlockType);
112 }
113 }
114 }
115 if (!map.containsKey(oldTest)) {
116 if (oldException == null) {
117 oldException = new Exception("Before");
118 }
119 map.put(oldTest, oldException);
120 }
121 }
122 }
123
124 }