diff --git a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java index e6cf2a4bdad..8d939ee41e4 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java +++ b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java @@ -404,13 +404,8 @@ public class LockBasedStorageManager implements StorageManager { } private static abstract class LockBasedLazyValueWithPostCompute extends LockBasedLazyValue { - @NotNull - private final ThreadLocal valuePostCompute = new ThreadLocal() { - @Override - protected Object initialValue() { - return NotValue.NOT_COMPUTED; - } - }; + @Nullable + private volatile ThreadLocal valuePostCompute = null; public LockBasedLazyValueWithPostCompute( @NotNull LockBasedStorageManager storageManager, @@ -421,10 +416,13 @@ public class LockBasedStorageManager implements StorageManager { @Override public T invoke() { - Object _value = valuePostCompute.get(); - if (!(_value instanceof NotValue)) { - // This thread is counting the value so allow an early publication - return WrappedValues.unescapeThrowable(_value); + ThreadLocal postComputeCache = valuePostCompute; + if (postComputeCache != null) { + Object _value = postComputeCache.get(); + if (!(_value instanceof NotValue)) { + // This thread is counting the value so allow an early publication + return WrappedValues.unescapeThrowable(_value); + } } return super.invoke(); @@ -433,11 +431,21 @@ public class LockBasedStorageManager implements StorageManager { // Doing something in post-compute helps prevent infinite recursion @Override protected final void postCompute(T value) { - valuePostCompute.set(value); + ThreadLocal postComputeCache = new ThreadLocal() { + @Override + protected Object initialValue() { + return NotValue.NOT_COMPUTED; + } + }; + + postComputeCache.set(value); + try { + valuePostCompute = postComputeCache; doPostCompute(value); } finally { - valuePostCompute.remove(); + postComputeCache.remove(); + valuePostCompute = null; } }