blob: 2d0c83491016a759d0c85c9aa801218d93c5778f (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.duper;
import com.yahoo.vespa.service.monitor.CriticalRegion;
import java.util.ArrayList;
import java.util.List;
/**
* To detect and throw an {@link IllegalStateException} if the execution of the current thread has
* reached code point P, some time after the start of a critical region R and before the end of it:
*
* <ol>
* <li>Declare a static final instance of {@link CriticalRegionChecker}.</li>
* <li>Invoke {@link #startCriticalRegion(String)} when entering region R, and close
* the returned {@link CriticalRegion} when leaving it.</li>
* <li>Invoke {@link #assertOutsideCriticalRegions(String)} at code point P.</li>
* </ol>
*
* @author hakonhall
*/
public class CriticalRegionChecker {
private final ThreadLocalDescriptions threadLocalDescriptions = new ThreadLocalDescriptions();
private final String name;
public CriticalRegionChecker(String name) {
this.name = name;
}
/** Start of the critical region, within which {@link #assertOutsideCriticalRegions(String)} will throw. */
public CriticalRegion startCriticalRegion(String regionDescription) {
List<String> regionDescriptions = threadLocalDescriptions.get();
regionDescriptions.add(regionDescription);
Thread threadAtStart = Thread.currentThread();
return () -> {
regionDescriptions.remove(regionDescription);
Thread treadAtClose = Thread.currentThread();
if (threadAtStart != treadAtClose) {
throw new IllegalStateException(name + ": A critical region cannot cross threads: " +
regionDescription);
}
};
}
/** @throws IllegalStateException if within one or more critical regions. */
public void assertOutsideCriticalRegions(String codePointDescription) throws IllegalStateException {
List<String> regionDescriptions = threadLocalDescriptions.get();
if (regionDescriptions.size() > 0) {
throw new IllegalStateException(name + ": Code point " + codePointDescription +
" is within these critical regions: " + regionDescriptions);
}
}
private static class ThreadLocalDescriptions extends ThreadLocal<List<String>> {
@Override
protected List<String> initialValue() {
return new ArrayList<>();
}
}
}
|