-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathCallDepthThreadLocalMap.java
54 lines (42 loc) · 1.22 KB
/
CallDepthThreadLocalMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package datadog.trace.bootstrap;
import datadog.trace.api.GenericClassValue;
/**
* Utility to track nested instrumentation.
*
* <p>For example, this can be used to track nested calls to super() in constructors by calling
* #incrementCallDepth at the beginning of each constructor.
*/
public class CallDepthThreadLocalMap {
private static final ClassValue<ThreadLocalDepth> TLS =
GenericClassValue.constructing(ThreadLocalDepth.class);
public static int incrementCallDepth(final Class<?> k) {
return TLS.get(k).get().increment();
}
public static int getCallDepth(final Class<?> k) {
return TLS.get(k).get().depth;
}
public static int decrementCallDepth(final Class<?> k) {
return TLS.get(k).get().decrement();
}
public static void reset(final Class<?> k) {
TLS.get(k).get().depth = 0;
}
private static final class Depth {
private int depth;
private Depth() {
this.depth = 0;
}
private int increment() {
return this.depth++;
}
private int decrement() {
return --this.depth;
}
}
public static final class ThreadLocalDepth extends ThreadLocal<Depth> {
@Override
protected Depth initialValue() {
return new Depth();
}
}
}