]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Fixed race condition in getValues/Relations/EntityTypes 94/1594/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Sun, 18 Mar 2018 20:45:52 +0000 (22:45 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Sun, 18 Mar 2018 21:32:54 +0000 (23:32 +0200)
refs #7826

Change-Id: I30842b6fe067b58c6916679804f56ed20b821ada

bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/LazyModule.java

index 6daef2b172bd27c0c8febf7dd3359b25b4dd190e..ce3b7cc5fd22ff49fd4b2a01c5205d3647c2f82e 100644 (file)
@@ -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<String, SCLValue> values = new THashMap<String, SCLValue>();
-    private THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
-    private THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>();
+    private final String moduleName;
+    private final ConcurrentHashMap<String, SCLValue> values = new ConcurrentHashMap<String, SCLValue>();
+    private final ConcurrentHashMap<String, SCLRelation> relations = new ConcurrentHashMap<String, SCLRelation>();
+    private final ConcurrentHashMap<String, SCLEntityType> entityTypes = new ConcurrentHashMap<String, SCLEntityType>();
     
     protected abstract SCLValue createValue(String name);
     
@@ -62,15 +64,11 @@ public abstract class LazyModule implements Module {
     public void findValuesForPrefix(final Collection<SCLValue> values, final String prefix) {          
     }
 
+    private final Function<String, SCLValue> 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<String, SCLRelation> 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<String, SCLEntityType> 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