|
| 1 | +/* |
| 2 | +* Copyright 2025 Ben Manes. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | +package com..benmanes.caffeine.cache; |
| 17 | + |
| 18 | +import java.lang.invoke.MethodHandles; |
| 19 | +import java.lang.invoke.VarHandle; |
| 20 | + |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | +import org.openjdk.jcstress.annotations.Actor; |
| 23 | +import org.openjdk.jcstress.annotations.Expect; |
| 24 | +import org.openjdk.jcstress.annotations.JCStressTest; |
| 25 | +import org.openjdk.jcstress.annotations.Outcome; |
| 26 | +import org.openjdk.jcstress.annotations.State; |
| 27 | +import org.openjdk.jcstress.infra.results.L_Result; |
| 28 | + |
| 29 | +import com.google.errorprone.annotations.Var; |
| 30 | + |
| 31 | +/** |
| 32 | +* A stress test that simulates the behavior for {@link java.lang.ref.Reference} reads and writes in |
| 33 | +* weak or soft valued caches. The {@link Node#setValue} proactively clears the underlying referent |
| 34 | +* after updating the entry's value. This is done to assist cross-generational pollution which can |
| 35 | +* cause garbage collectors to unnecessarily promote young dead objects to an old collection and |
| 36 | +* increase pause times. The {@link Node#getValue} compensates by a re-check validation to determine |
| 37 | +* if the observed null referent is due to garbage collection or a stale read. Due to referent being |
| 38 | +* read and written with plain memory semantics, an additional memory barrier is required to ensure |
| 39 | +* the correct visibility ordering. |
| 40 | +* <p> |
| 41 | +* {@snippet lang="shell" : |
| 42 | +* // JAVA_VERSION=?? for an alternative jdk |
| 43 | +* ./gradlew caffeine:jcstress --rerun |
| 44 | +* } |
| 45 | +* |
| 46 | +* @author [email protected] (Ben Manes) |
| 47 | +*/ |
| 48 | +@State |
| 49 | +@JCStressTest |
| 50 | +@Outcome(id = "ok", expect = Expect.ACCEPTABLE, desc = "Reference seen and valid") |
| 51 | +@Outcome(id = "null", expect = Expect.FORBIDDEN, desc = "Reference seen but field not visible") |
| 52 | +public class IntermittentNull { |
| 53 | +private static final VarHandle VALUE; |
| 54 | + |
| 55 | +volatile Value value = new Value("ok"); |
| 56 | + |
| 57 | +@Actor |
| 58 | +public void writer() { |
| 59 | +var oldValue = (Value) VALUE.getAcquire(this); |
| 60 | +var newValue = new Value("ok"); |
| 61 | +VALUE.setRelease(this, newValue); |
| 62 | +VarHandle.storeStoreFence(); |
| 63 | +oldValue.data = null; |
| 64 | +} |
| 65 | + |
| 66 | +@Actor |
| 67 | +public void reader(L_Result r) { |
| 68 | +@Var var value = (Value) VALUE.getAcquire(this); |
| 69 | +for (;;) { |
| 70 | +var data = value.data; |
| 71 | +if (data != null) { |
| 72 | +r.r1 = data; |
| 73 | +return; |
| 74 | +} |
| 75 | +VarHandle.loadLoadFence(); |
| 76 | +var current = (Value) VALUE.getAcquire(this); |
| 77 | +if (value == current) { |
| 78 | +r.r1 = null; |
| 79 | +return; |
| 80 | +} |
| 81 | +value = current; |
| 82 | +} |
| 83 | +} |
| 84 | + |
| 85 | +static { |
| 86 | +try { |
| 87 | +VALUE = MethodHandles.lookup().findVarHandle(IntermittentNull.class, "value", Value.class); |
| 88 | +} catch (ReflectiveOperationException e) { |
| 89 | +throw new ExceptionInInitializerError(e); |
| 90 | +} |
| 91 | +} |
| 92 | + |
| 93 | +static final class Value { |
| 94 | +@Nullable String data; |
| 95 | + |
| 96 | +Value(String data) { |
| 97 | +this.data = data; |
| 98 | +} |
| 99 | +} |
| 100 | +} |
0 commit comments