From: Hannu Niemistö Date: Sun, 18 Mar 2018 20:45:52 +0000 (+0200) Subject: Fixed race condition in getValues/Relations/EntityTypes X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F96%2F1596%2F1;p=simantics%2Fplatform.git Fixed race condition in getValues/Relations/EntityTypes refs #7826 Change-Id: I30842b6fe067b58c6916679804f56ed20b821ada (cherry picked from commit 9ea5cf59a4d87c3db3a486e86d7b54efffd5516d) --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java index 6daef2b17..ce3b7cc5f 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java @@ -3,6 +3,8 @@ package org.simantics.scl.compiler.module; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; import org.simantics.scl.compiler.constants.Constant; import org.simantics.scl.compiler.elaboration.chr.CHRRuleset; @@ -25,10 +27,10 @@ import gnu.trove.map.hash.THashMap; public abstract class LazyModule implements Module { - String moduleName; - private THashMap values = new THashMap(); - private THashMap relations = new THashMap(); - private THashMap entityTypes = new THashMap(); + private final String moduleName; + private final ConcurrentHashMap values = new ConcurrentHashMap(); + private final ConcurrentHashMap relations = new ConcurrentHashMap(); + private final ConcurrentHashMap entityTypes = new ConcurrentHashMap(); protected abstract SCLValue createValue(String name); @@ -62,15 +64,11 @@ public abstract class LazyModule implements Module { public void findValuesForPrefix(final Collection values, final String prefix) { } + private final Function createValue = this::createValue; + @Override public SCLValue getValue(String name) { - if(values.containsKey(name)) - return values.get(name); - else { - SCLValue value = createValue(name); - values.put(name, value); - return value; - } + return values.computeIfAbsent(name, createValue); } @Override @@ -78,24 +76,16 @@ public abstract class LazyModule implements Module { return null; } + private final Function createRelation = this::createRelation; + public SCLRelation getRelation(String name) { - if(relations.containsKey(name)) - return relations.get(name); - else { - SCLRelation relation = createRelation(name); - relations.put(name, relation); - return relation; - } + return relations.computeIfAbsent(name, createRelation); } + private final Function createEntityType = this::createEntityType; + public SCLEntityType getEntityType(String name) { - if(entityTypes.containsKey(name)) - return entityTypes.get(name); - else { - SCLEntityType entityType = createEntityType(name); - entityTypes.put(name, entityType); - return entityType; - } + return entityTypes.computeIfAbsent(name, createEntityType); } @Override