-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathActiveBitmaps.java
More file actions
80 lines (67 loc) · 1.96 KB
/
Copy pathActiveBitmaps.java
File metadata and controls
80 lines (67 loc) · 1.96 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.datadoghq.profiler;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
class ActiveBitmaps {
private static final Unsafe UNSAFE;
static {
Unsafe unsafe = null;
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
} catch (Exception ignore) { }
UNSAFE = unsafe;
}
private static long activeCountAddr = 0;
private static final ThreadLocal<Long> Address = new ThreadLocal<Long>() {
@Override protected Long initialValue() {
return -1L;
}
};
public static void initialize() {
activeCountAddr = getActiveCountAddr0();
}
// Set bitmap to native code
static native long bitmapAddressFor0(int tid);
static long getBitmask(int tid) {
int tmp = (tid >> 8) & 0xff ;
int bits = 0;
for (int index = 0; index < 7 ; index++) {
if ((tmp & 0x01) == 0x01) {
bits |= 0x01;
}
tmp >>= 1;
bits <<= 1;
}
return 1L << (bits & 0x3f);
}
static void setActive(int tid, boolean active) {
long addr = Address.get();
if (addr == -1) {
addr = bitmapAddressFor0(tid);
Address.set(addr);
}
long bitmask = getBitmask(tid);
long value = UNSAFE.getLong(addr);
long newVal;
if (active) {
newVal = value | bitmask;
} else {
newVal = value & ~bitmask;
}
while (!UNSAFE.compareAndSwapLong(null, addr, value, newVal)) {
value = UNSAFE.getLong(addr);
newVal = active ? (value | bitmask) : (value & ~bitmask);
}
int delta = active ? 1 : -1;
assert activeCountAddr != 0;
UNSAFE.getAndAddInt(null, activeCountAddr, delta);
// if (isActive(tid) != active) {
// throw new RuntimeException("SetActive failed");
// }
assert isActive(tid) == active;
}
// For verification
static native boolean isActive(int tid);
static native long getActiveCountAddr0();
}