X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fmodule%2Frepository%2FModuleRepository.java;h=23491235ce3289c46fd254009cde5963774374d6;hp=0f0b8542049a53c7f8eea2667ba15c40789f0f7b;hb=3a31aa451eae6bc9fa359ada9df47a354605f4b6;hpb=7444d4b3a2f3c25fac462d8a168898656dafd52e diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java index 0f0b85420..23491235c 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/ModuleRepository.java @@ -37,6 +37,8 @@ import org.simantics.scl.compiler.top.ModuleInitializer; import org.simantics.scl.compiler.top.SCLCompilerConfiguration; import org.simantics.scl.compiler.top.ValueNotFound; import org.simantics.scl.compiler.types.Types; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import gnu.trove.map.hash.THashMap; import gnu.trove.map.hash.TObjectLongHashMap; @@ -49,6 +51,9 @@ import gnu.trove.set.hash.THashSet; * @author Hannu Niemistö */ public class ModuleRepository { + + private static final Logger LOGGER = LoggerFactory.getLogger(ModuleRepository.class); + private final ModuleRepository parentRepository; private final ModuleSourceRepository sourceRepository; private ConcurrentHashMap moduleCache = new ConcurrentHashMap(); @@ -70,10 +75,11 @@ public class ModuleRepository { private static void finishModuleCompilation(String moduleName) { PENDING_MODULES.get().remove(moduleName); } - + private class ModuleEntry extends UpdateListener implements Observable { final String moduleName; - THashSet listeners = new THashSet(); + THashSet listeners = new THashSet(); // listeners == null is used as a marker that this entry is disposed + // should be handled only inside synchronized code ModuleSource source; Failable compilationResult; @@ -98,33 +104,38 @@ public class ModuleRepository { @Override public void notifyAboutUpdate() { + // There is a chance that another observable calls notifyAboutUpdate() before stopListening has been completed, + // but notifyAboutUpdate(ArrayList) lets only one thread to do the notification of dependencies + // by clearing listeners field. + stopListening(); ArrayList externalListeners = new ArrayList(); notifyAboutUpdate(externalListeners); for(UpdateListener listener : externalListeners) listener.notifyAboutUpdate(); } - synchronized void notifyAboutUpdate(ArrayList externalListeners) { - stopListening(); - if (listeners == null) - return; + void notifyAboutUpdate(ArrayList externalListeners) { + THashSet listenersCopy; + synchronized(this) { + listenersCopy = listeners; + if (listenersCopy == null) + return; + listeners = null; + } if(moduleCache.get(moduleName) == this) { moduleCache.remove(moduleName); if(SCLCompilerConfiguration.TRACE_MODULE_UPDATE) { - System.out.println("Invalidate " + moduleName); - for(UpdateListener l : listeners) - System.out.println(" " + l); + LOGGER.info("Invalidate " + moduleName); + for(UpdateListener l : listenersCopy) + LOGGER.info(" " + l); } - THashSet listenersCopy = listeners; - listeners = null; for(UpdateListener l : listenersCopy) - l.stopListening(); - for(UpdateListener l : listenersCopy) - if(l instanceof ModuleEntry) + if(!l.stopListening()) + ; + else if(l instanceof ModuleEntry) ((ModuleEntry)l).notifyAboutUpdate(externalListeners); - else { + else externalListeners.add(l); - } } } @@ -135,7 +146,7 @@ public class ModuleRepository { compilationResult = DoesNotExist.getInstance(); else { if(SCLCompilerConfiguration.TRACE_MODULE_UPDATE) - System.out.println("Compile " + source); + LOGGER.info("Compile " + source); beginModuleCompilation(moduleName); compilationResult = source.compileModule(ModuleRepository.this, this, advisor == null ? null : advisor.getOptions(moduleName)); finishModuleCompilation(moduleName); @@ -195,8 +206,6 @@ public class ModuleRepository { } public synchronized void dispose() { - if (listeners != null) - listeners.clear(); listeners = null; stopListening(); source = null; @@ -231,6 +240,10 @@ public class ModuleRepository { return getModule(moduleName, null); } + public void update(String moduleName) { + getModuleEntry(moduleName, null).notifyAboutUpdate(); + } + public Failable getRuntimeModule(String moduleName, UpdateListener listener) { return getModuleEntry(moduleName, listener).getRuntimeModule(); } @@ -490,18 +503,24 @@ public class ModuleRepository { return documentation; } + /** + * Flush clears module repository cache completely. It should not be called in + * normal operation, but may be useful during testing for clearing repositories + * that are statically defined. + */ public void flush() { if (parentRepository != null) parentRepository.flush(); - if (moduleCache != null) { - for (ModuleEntry entry : moduleCache.values()) { + if (moduleCache != null) + for (ModuleEntry entry : moduleCache.values()) entry.dispose(); - } - moduleCache.clear(); - } - moduleCache = null; + moduleCache = new ConcurrentHashMap(); } + /** + * Gets the map of all modules that have been currently compiled successfully. + * Not that the method does not return all possible modules in the source repository. + */ public Map getModules() { Map result = new HashMap<>(moduleCache.size()); for (Map.Entry entry : moduleCache.entrySet()) {