@NotNull private InspectionTreeNode getToolParentNode( @NotNull String groupName, HighlightDisplayLevel errorLevel, boolean groupedBySeverity) { if (groupName.isEmpty()) { return getRelativeRootNode(groupedBySeverity, errorLevel); } ConcurrentMap<String, InspectionGroupNode> map = myGroups.get(errorLevel); if (map == null) { map = ConcurrencyUtil.cacheOrGet( myGroups, errorLevel, ContainerUtil.<String, InspectionGroupNode>newConcurrentMap()); } InspectionGroupNode group; if (groupedBySeverity) { group = map.get(groupName); } else { group = null; for (Map<String, InspectionGroupNode> groupMap : myGroups.values()) { if ((group = groupMap.get(groupName)) != null) break; } } if (group == null) { group = ConcurrencyUtil.cacheOrGet(map, groupName, new InspectionGroupNode(groupName)); addChildNodeInEDT(getRelativeRootNode(groupedBySeverity, errorLevel), group); } return group; }
public PsiJavaPackage findPackage(@NotNull String qualifiedName, GlobalSearchScope searchScope) { PackageCache cache = SoftReference.dereference(packageCache); if (cache == null) { packageCache = new SoftReference<PackageCache>(cache = new PackageCache()); } Pair<String, GlobalSearchScope> key = new Pair<String, GlobalSearchScope>(qualifiedName, searchScope); PsiJavaPackage aPackage = cache.packageInScopeCache.get(key); if (aPackage != null) { return aPackage; } KotlinPsiElementFinderWrapper[] finders = filteredFinders(); Boolean packageFoundInAllScope = cache.hasPackageInAllScopeCache.get(qualifiedName); if (packageFoundInAllScope != null) { if (!packageFoundInAllScope.booleanValue()) return null; // Package was found in AllScope with some of finders but is absent in packageCache for // current scope. // We check only finders that depend on scope. for (KotlinPsiElementFinderWrapper finder : finders) { if (!finder.isSameResultForAnyScope()) { aPackage = finder.findPackage(qualifiedName, searchScope); if (aPackage != null) { return ConcurrencyUtil.cacheOrGet(cache.packageInScopeCache, key, aPackage); } } } } else { for (KotlinPsiElementFinderWrapper finder : finders) { aPackage = finder.findPackage(qualifiedName, searchScope); if (aPackage != null) { return ConcurrencyUtil.cacheOrGet(cache.packageInScopeCache, key, aPackage); } } boolean found = false; for (KotlinPsiElementFinderWrapper finder : finders) { if (!finder.isSameResultForAnyScope()) { aPackage = finder.findPackage(qualifiedName, GlobalSearchScope.allScope(project)); if (aPackage != null) { found = true; break; } } } cache.hasPackageInAllScopeCache.put(qualifiedName, found); } return null; }
@Override protected PsiFile getPsiInner(@NotNull final Language target) { PsiFileImpl file = myRoots.get(target); if (file == null) { if (isPhysical()) { VirtualFile virtualFile = getVirtualFile(); if (isIgnored()) return null; VirtualFile parent = virtualFile.getParent(); if (parent != null) { getManager().findDirectory(parent); } } if (target != getBaseLanguage() && !getLanguages().contains(target)) { return null; } file = (PsiFileImpl) createFile(target); if (file == null) return null; if (myOriginal != null) { final PsiFile originalFile = myOriginal.getPsi(target); if (originalFile != null) { file.setOriginalFile(originalFile); } } file = ConcurrencyUtil.cacheOrGet(myRoots, target, file); } return file; }
@Override public RefModule getRefModule(Module module) { if (module == null) { return null; } RefModule refModule = myModules.get(module); if (refModule == null) { refModule = ConcurrencyUtil.cacheOrGet(myModules, module, new RefModuleImpl(module, this)); } return refModule; }
public Server(File systemDir, boolean cachesInMemory) { Utils.setSystemRoot(systemDir); final ExecutorService threadPool = Executors.newCachedThreadPool(); myScheduler = ConcurrencyUtil.newSingleScheduledThreadExecutor( "Client activity checker", Thread.MIN_PRIORITY); myBuildsExecutor = Executors.newFixedThreadPool(MAX_SIMULTANEOUS_BUILD_SESSIONS); myChannelFactory = new NioServerSocketChannelFactory(threadPool, threadPool, 1); final ChannelRegistrar channelRegistrar = new ChannelRegistrar(); myMessageHandler = new ServerMessageHandler( this, new AsyncTaskExecutor() { @Override public void submit(final Runnable runnable) { myBuildsExecutor.submit( new Runnable() { @Override public void run() { try { runnable.run(); } finally { Thread.interrupted(); // clear interrupted status before returning to pull } } }); } }); myPipelineFactory = new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( channelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(JpsRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), myMessageHandler); } }; ServerState.getInstance().setKeepTempCachesInMemory(cachesInMemory); Runtime.getRuntime() .addShutdownHook( new Thread("Shutdown hook thread") { public void run() { try { myMessageHandler.cancelAllBuildsAndClearState(); } finally { Server.this.stop(); } } }); }
@Nullable public static Icon findIcon(URL url, boolean useCache) { if (url == null) { return null; } CachedImageIcon icon = ourIconsCache.get(url); if (icon == null) { icon = new CachedImageIcon(url); if (useCache) { icon = ConcurrencyUtil.cacheOrGet(ourIconsCache, url, icon); } } return icon; }
static void registerDisposable( @NotNull Disposable parentDisposable, @NotNull VirtualFilePointerImpl pointer) { DelegatingDisposable result = ourInstances.get(parentDisposable); if (result == null) { DelegatingDisposable newDisposable = new DelegatingDisposable(parentDisposable); result = ConcurrencyUtil.cacheOrGet(ourInstances, parentDisposable, newDisposable); if (result == newDisposable) { Disposer.register(parentDisposable, result); } } synchronized (result) { result.myCounts.put(pointer, result.myCounts.get(pointer) + 1); } }
@NotNull private InspectionTreeNode getRelativeRootNode( boolean isGroupedBySeverity, HighlightDisplayLevel level) { if (isGroupedBySeverity) { InspectionSeverityGroupNode severityGroupNode = mySeverityGroupNodes.get(level); if (severityGroupNode == null) { InspectionSeverityGroupNode newNode = new InspectionSeverityGroupNode(myProject, level); severityGroupNode = ConcurrencyUtil.cacheOrGet(mySeverityGroupNodes, level, newNode); if (severityGroupNode == newNode) { InspectionTreeNode root = myTree.getRoot(); addChildNodeInEDT(root, severityGroupNode); } } return severityGroupNode; } return myTree.getRoot(); }
protected PsiFile getPsiInner(final Language target) { PsiFile file = myRoots.get(target); if (file == null) { if (isPhysical()) { VirtualFile virtualFile = getVirtualFile(); VirtualFile parent = virtualFile.getParent(); if (parent != null) { getManager().findDirectory(parent); } } file = createFile(target); if (file == null) return null; if (myOriginal != null) { final PsiFile originalFile = myOriginal.getPsi(target); if (originalFile != null) { ((PsiFileImpl) file).setOriginalFile(originalFile); } } file = ConcurrencyUtil.cacheOrGet(myRoots, target, file); } return file; }
@NotNull @Override public PsiClassType createTypeByFQClassName( @NotNull final String qName, @NotNull final GlobalSearchScope resolveScope) { if (CommonClassNames.JAVA_LANG_OBJECT.equals(qName)) { PsiClassType cachedObjectType = myCachedObjectType.get(resolveScope); if (cachedObjectType != null) { return cachedObjectType; } PsiClass aClass = JavaPsiFacade.getInstance(myManager.getProject()) .findClass(CommonClassNames.JAVA_LANG_OBJECT, resolveScope); if (aClass != null) { cachedObjectType = new PsiImmediateClassType(aClass, PsiSubstitutor.EMPTY); cachedObjectType = ConcurrencyUtil.cacheOrGet(myCachedObjectType, resolveScope, cachedObjectType); return cachedObjectType; } } return new PsiClassReferenceType( createReferenceElementByFQClassName(qName, resolveScope), null); }
/** * Base class for the actions, which update() method might be potentially slow to be executed * synchronously in Swing UI thread. * * @author max */ public abstract class AsyncUpdateAction<T> extends AnAction { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.actionSystem.AsyncUpdateAction"); private static final ExecutorService ourUpdaterService = ConcurrencyUtil.newSingleThreadExecutor("Action Updater"); // Async update @Override public final void update(AnActionEvent e) { final T data = prepareDataFromContext(e); final Presentation originalPresentation = e.getPresentation(); if (!forceSyncUpdate(e) && isDumbAware()) { final Presentation realPresentation = (Presentation) originalPresentation.clone(); ourUpdaterService.submit( new Runnable() { @Override public void run() { performUpdate(realPresentation, data); SwingUtilities.invokeLater( new Runnable() { @Override public void run() { if (originalPresentation.isVisible() != realPresentation.isVisible()) { LOG.error( "Async update is not supported for actions that change their visibility." + "Either stop extending AsyncUpdateAction or override forceSyncUpdate() to return true." + "Action class is: " + AsyncUpdateAction.this.getClass().getName()); } originalPresentation.copyFrom(realPresentation); } }); } }); originalPresentation.setVisible(true); originalPresentation.setEnabled(false); } else { performUpdate(originalPresentation, data); } } // Sync update @Override public final void beforeActionPerformedUpdate(@NotNull AnActionEvent e) { performUpdate(e.getPresentation(), prepareDataFromContext(e)); } /** * Get all necessary data from event's DataContext to be used in <code>performUpdate()</code>, * which is called asynchronously. * * @param e action event original update() method have been called with. * @return prepared data for {@link #performUpdate} method. */ protected abstract T prepareDataFromContext(final AnActionEvent e); /** * Perform real presentation tweaking here. Be aware of the fact this method may be called in * thread other than Swing UI thread thus probable restrictions like necessity to call * ApplcationManager.getApplication().runReadAction() apply. * * @param presentation Presentation object to be tweaked. * @param data necessary data calculated by {@link #prepareDataFromContext(AnActionEvent)}. */ protected abstract void performUpdate(Presentation presentation, T data); /** * Override this method to return <code>true</code> value if update method cannot be called * asynchronously for whatever reason. * * @param e action event original update() method have been called with. * @return <code>false</code> if async update is possible and <code>false</code> otherwise. */ protected boolean forceSyncUpdate(AnActionEvent e) { return false; } }
private static int getThreadNum() { return ConcurrencyUtil.cacheOrGet(threads, Thread.currentThread(), threads.size()); }