This commit is contained in:
Ilya.Kazakevich
2018-03-26 18:31:08 +03:00
2 changed files with 8 additions and 17 deletions
@@ -47,18 +47,8 @@ public class TypeEvalContext {
private final Map<PyTypedElement, PyType> myEvaluated = new HashMap<>();
private final Map<PyCallable, PyType> myEvaluatedReturn = new HashMap<>();
private final ThreadLocal<Set<PyTypedElement>> myEvaluating = new ThreadLocal<Set<PyTypedElement>>() {
@Override
protected Set<PyTypedElement> initialValue() {
return new HashSet<>();
}
};
private final ThreadLocal<Set<PyCallable>> myEvaluatingReturn = new ThreadLocal<Set<PyCallable>>() {
@Override
protected Set<PyCallable> initialValue() {
return new HashSet<>();
}
};
private final ThreadLocal<Set<PyTypedElement>> myEvaluating = ThreadLocal.withInitial(HashSet::new);
private final ThreadLocal<Set<PyCallable>> myEvaluatingReturn = ThreadLocal.withInitial(HashSet::new);
private TypeEvalContext(boolean allowDataFlow, boolean allowStubToAST, boolean allowCallContext, @Nullable PsiFile origin) {
myConstraints = new TypeEvalConstraints(allowDataFlow, allowStubToAST, allowCallContext, origin);
@@ -254,7 +244,7 @@ public class TypeEvalContext {
}
/**
* @return context constraints (see {@link com.jetbrains.python.psi.types.TypeEvalConstraints}
* @return context constraints (see {@link TypeEvalConstraints}
*/
@NotNull
TypeEvalConstraints getConstraints() {
@@ -20,9 +20,9 @@ import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
@@ -68,8 +68,9 @@ public final class TypeEvalContextBasedCache<T> {
return value;
}
final T newValue = myProvider.fun(context);
map.put(key, newValue); // ConcurrentMap guarantees happens-before so from this moment get() should work in other threads
return newValue;
T oldValue =
map.putIfAbsent(key, newValue);// ConcurrentMap guarantees happens-before so from this moment get() should work in other threads
return oldValue == null ? newValue : oldValue;
}
/**
@@ -81,7 +82,7 @@ public final class TypeEvalContextBasedCache<T> {
public Result<ConcurrentMap<TypeEvalConstraints, T>> compute() {
// This method is called if cache is empty. Create new map for it.
// Concurrent map allows several threads to call get and put, so it is thread safe but not atomic
final ConcurrentHashMap<TypeEvalConstraints, T> map = new ConcurrentHashMap<>();
final ConcurrentMap<TypeEvalConstraints, T> map = ContainerUtil.createConcurrentSoftValueMap();
return new Result<>(map, PsiModificationTracker.MODIFICATION_COUNT);
}
}