-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLockKit.java
More file actions
87 lines (73 loc) · 2.18 KB
/
LockKit.java
File metadata and controls
87 lines (73 loc) · 2.18 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
80
81
82
83
84
85
86
87
package graphql.util;
import graphql.Internal;
import java.util.concurrent.locks.Lock;
import java.util.function.Supplier;
/**
* This provides reentrant locking support for our code base. Future worlds like Loom virtual threads don't like
* synchronised calls since they pin the VT to the carrier thread. Word on the street is that locks are preferred
* to synchronised.
*/
@Internal
public class LockKit {
/**
* A class to run code inside a reentrant lock
*/
public static class ReentrantLock {
private final Lock lock = new java.util.concurrent.locks.ReentrantLock();
/**
* Sometimes you need to directly lock things like for checked exceptions
* <p>
* It's on you to unlock it!
*/
public void lock() {
lock.lock();
}
public void unlock() {
lock.unlock();
}
public void runLocked(Runnable codeToRun) {
lock.lock();
try {
codeToRun.run();
} finally {
lock.unlock();
}
}
public <E> E callLocked(Supplier<E> codeToRun) {
lock.lock();
try {
return codeToRun.get();
} finally {
lock.unlock();
}
}
}
/**
* Will allow for lazy computation of some values just once
*/
public static class ComputedOnce {
private volatile boolean beenComputed = false;
private final ReentrantLock lock = new ReentrantLock();
public boolean hasBeenComputed() {
return beenComputed;
}
public void runOnce(Runnable codeToRunOnce) {
if (beenComputed) {
return;
}
lock.runLocked(() -> {
// double lock check
if (beenComputed) {
return;
}
try {
codeToRunOnce.run();
beenComputed = true;
} finally {
// even for exceptions we will say its computed
beenComputed = true;
}
});
}
}
}