|
27 | 27 | import java.util.Random;
|
28 | 28 |
|
29 | 29 | import com.oracle.svm.core.annotate.Alias;
|
| 30 | +import com.oracle.svm.core.annotate.Inject; |
30 | 31 | import com.oracle.svm.core.annotate.InjectAccessors;
|
| 32 | +import com.oracle.svm.core.annotate.RecomputeFieldValue; |
31 | 33 | import com.oracle.svm.core.annotate.TargetClass;
|
32 | 34 |
|
| 35 | +import jdk.graal.compiler.nodes.extended.MembarNode; |
| 36 | + |
33 | 37 | @TargetClass(className = "jdk.jfr.internal.settings.Throttler")
|
34 | 38 | public final class Target_jdk_jfr_internal_settings_Throttler {
|
35 | 39 | @Alias //
|
36 | 40 | @InjectAccessors(ThrottlerRandomAccessor.class) //
|
37 | 41 | private Random randomGenerator;
|
| 42 | + |
| 43 | + @Inject // |
| 44 | + @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) // |
| 45 | + Random injectedRandomGenerator; |
38 | 46 | }
|
39 | 47 |
|
40 | 48 | final class ThrottlerRandomAccessor {
|
41 |
| - private static Random cachedRandom; |
42 |
| - |
43 | 49 | @SuppressWarnings("unused")
|
44 | 50 | static Random get(Target_jdk_jfr_internal_settings_Throttler receiver) {
|
45 |
| - if (cachedRandom != null) { |
46 |
| - return cachedRandom; |
| 51 | + Random value = receiver.injectedRandomGenerator; |
| 52 | + if (value != null) { |
| 53 | + return value; |
47 | 54 | }
|
48 |
| - return initializeRandom(); |
| 55 | + return initializeRandom(receiver); |
49 | 56 | }
|
50 | 57 |
|
51 |
| - private static synchronized Random initializeRandom() { |
52 |
| - if (cachedRandom != null) { |
53 |
| - return cachedRandom; |
| 58 | + private static synchronized Random initializeRandom(Target_jdk_jfr_internal_settings_Throttler receiver) { |
| 59 | + Random value = receiver.injectedRandomGenerator; |
| 60 | + if (value != null) { |
| 61 | + return value; |
54 | 62 | }
|
55 | 63 |
|
56 |
| - cachedRandom = new Random(); |
57 |
| - return cachedRandom; |
| 64 | + value = new Random(); |
| 65 | + /* Ensure that other threads see a fully initialized Random object once published below. */ |
| 66 | + MembarNode.memoryBarrier(MembarNode.FenceKind.STORE_STORE); |
| 67 | + |
| 68 | + receiver.injectedRandomGenerator = value; |
| 69 | + return value; |
58 | 70 | }
|
59 | 71 |
|
60 | 72 | @SuppressWarnings("unused")
|
61 | 73 | static synchronized void set(Target_jdk_jfr_internal_settings_Throttler receiver, Random value) {
|
62 |
| - throw new RuntimeException("The field jdk.jfr.internal.settings.Throttler.randomGenerator cannot be set"); |
| 74 | + receiver.injectedRandomGenerator = value; |
63 | 75 | }
|
64 | 76 | }
|
0 commit comments