use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.
the class ResolverService method resolve.
@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
if (!result.valid() || !result.hasAst()) {
return null;
}
final FileObject source = result.source();
final IContext context = result.context();
final IProject project = context.project();
final ILanguageImpl language = context.language();
final FacetContribution<ResolverFacet> facetContrib = facet(language);
final ResolverFacet facet = facetContrib.facet;
final String strategy = facet.strategyName;
try {
final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple;
try (IClosableLock lock = context.read()) {
tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
}
return resolve(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Reference resolution failed", e);
}
}
use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.
the class Builder method transform.
private Collection<T> transform(BuildInput input, ILanguageImpl langImpl, FileObject location, Multimap<IContext, A> allAnalysisUnits, Set<FileName> includeFiles, boolean pardoned, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
final int size = allAnalysisUnits.size();
progress.setWorkRemaining(size);
final Collection<T> allTransformUnits = Lists.newArrayListWithCapacity(size);
if (size == 0) {
return allTransformUnits;
}
progress.setDescription("Compiling " + size + " file(s) of " + langImpl.belongsTo().name());
logger.debug("Compiling {} analysis results", size);
for (Entry<IContext, Collection<A>> entry : allAnalysisUnits.asMap().entrySet()) {
cancel.throwIfCancelled();
final IContext context = entry.getKey();
final Iterable<A> analysisResults = entry.getValue();
try (IClosableLock lock = context.read()) {
for (A analysisResult : analysisResults) {
cancel.throwIfCancelled();
final FileObject source = analysisResult.source();
final FileName name = source.getName();
if (removedResources.contains(name) || includeFiles.contains(name)) {
// Don't compile removed resources, which the analysis results contain for legacy reasons.
// Don't transform included resources, they should just be parsed and analyzed.
progress.work(1);
continue;
}
if (!analysisResult.valid()) {
logger.warn("Input result for {} is invalid, cannot transform it", source != null ? source.getName().getPath() : "detached source");
progress.work(1);
continue;
}
for (ITransformGoal goal : input.transformGoals) {
cancel.throwIfCancelled();
if (!transformService.available(context, goal)) {
logger.trace("No {} transformation required for {}", goal, context.language());
progress.work(1);
continue;
}
try {
final Collection<TA> results = transformService.transform(analysisResult, context, goal);
for (TA result : results) {
final boolean noErrors = printMessages(result.messages(), goal + " transformation", input, pardoned);
success.and(noErrors);
@SuppressWarnings("unchecked") final T genericResult = (T) result;
allTransformUnits.add(genericResult);
}
progress.work(1);
} catch (TransformException e) {
final String message = String.format("Transformation failed unexpectedly for %s", name);
logger.error(message, e);
final boolean noErrors = printMessage(source, message, e, input, pardoned);
success.and(noErrors);
extraMessages.add(MessageFactory.newBuilderErrorAtTop(location, "Transformation failed unexpectedly", e));
}
}
}
// GTODO: also compile any affected sources
}
}
return allTransformUnits;
}
use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.
the class AnalysisResultProcessor method getUpdates.
private BehaviorSubject<AnalysisChange<A>> getUpdates(I input, IContext context) {
if (input.detached()) {
throw new MetaborgRuntimeException("Cannot get updates for detached (no source) units");
}
final FileObject source = input.source();
final FileName name = source.getName();
// THREADING: it is possible that two different threads asking for a subject may do the parsing twice here, as
// this is not an atomic operation. However, the chance is very low and it does not break anything (only
// duplicates some work), so it is acceptable.
BehaviorSubject<AnalysisChange<A>> updates = updatesPerResource.get(name);
if (updates == null) {
updates = BehaviorSubject.create();
updatesPerResource.put(name, updates);
try {
logger.trace("Requesting parse result for {}", source);
final P parseResult = parseResultRequester.request(input).toBlocking().single();
if (!parseResult.valid()) {
updates.onNext(AnalysisChange.<A>error(source, new AnalysisException(context, "Parsing failed")));
return updates;
}
logger.trace("Analysing for {}", source);
final IAnalyzeResult<A, AU> result;
try (IClosableLock lock = context.write()) {
result = analysisService.analyze(parseResult, context);
}
updates.onNext(AnalysisChange.<A>update(source, result.result()));
// HACK: ignore analyze unit updates from result.updates(), may cause incrementality problems.
} catch (AnalysisException e) {
final String message = logger.format("Analysis for {} failed", name);
logger.error(message, e);
updates.onNext(AnalysisChange.<A>error(source, e));
} catch (Exception e) {
final String message = logger.format("Analysis for {} failed", name);
logger.error(message, e);
updates.onNext(AnalysisChange.<A>error(source, new AnalysisException(context, message, e)));
}
}
return updates;
}
use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.
the class IndexTaskContext method readLock.
private IClosableLock readLock() {
final Lock readLock = lock.readLock();
final IClosableLock lock = new ClosableLock(readLock);
return lock;
}
use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.
the class IndexTaskContext method writeLock.
private IClosableLock writeLock() {
final Lock writeLock = lock.writeLock();
final IClosableLock lock = new ClosableLock(writeLock);
return lock;
}
Aggregations