tmf: make latches synchronized in abstract analysis module [bug 485793]
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / TmfAbstractAnalysisModule.java
index 6d0cc8346d03b12e81511d97ac3bf03d9d28dff5..a1c65f2371c5dc96c7b91f7b4e17448ca47200bc 100644 (file)
@@ -29,15 +29,17 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.internal.tmf.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.core.TmfCoreTracer;
-import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement.ValuePriorityLevel;
+import org.eclipse.tracecompass.tmf.core.analysis.requirements.TmfAbstractAnalysisRequirement;
 import org.eclipse.tracecompass.tmf.core.component.TmfComponent;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
+import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
 import org.eclipse.tracecompass.tmf.core.signal.TmfStartAnalysisSignal;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
@@ -52,7 +54,8 @@ import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
  * @author Geneviève Bastien
  */
 @NonNullByDefault
-public abstract class TmfAbstractAnalysisModule extends TmfComponent implements IAnalysisModule {
+public abstract class TmfAbstractAnalysisModule extends TmfComponent
+        implements IAnalysisModule, ITmfPropertiesProvider {
 
     private @Nullable String fId;
     private boolean fAutomatic = false, fStarted = false;
@@ -62,6 +65,7 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
     private final List<IAnalysisOutput> fOutputs = new ArrayList<>();
     private Set<IAnalysisParameterProvider> fParameterProviders = new HashSet<>();
     private @Nullable Job fJob = null;
+    private int fDependencyLevel = 0;
 
     private final Object syncObj = new Object();
 
@@ -206,8 +210,8 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
 
     @Override
     public boolean canExecute(ITmfTrace trace) {
-        for (TmfAnalysisRequirement requirement : getAnalysisRequirements()) {
-            if (!requirement.isFulfilled(trace)) {
+        for (TmfAbstractAnalysisRequirement requirement : getAnalysisRequirements()) {
+            if (!requirement.test(trace)) {
                 return false;
             }
         }
@@ -219,8 +223,10 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
      */
     protected void resetAnalysis() {
         TmfCoreTracer.traceAnalysis(getId(), getTrace(), "reset: ready for execution"); //$NON-NLS-1$
-        fFinishedLatch.countDown();
-        fFinishedLatch = new CountDownLatch(1);
+        synchronized (syncObj) {
+            fFinishedLatch.countDown();
+            fFinishedLatch = new CountDownLatch(1);
+        }
     }
 
     /**
@@ -262,7 +268,9 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
     public final void cancel() {
         synchronized (syncObj) {
             TmfCoreTracer.traceAnalysis(getId(), getTrace(), "cancelled by application"); //$NON-NLS-1$
-            if (fJob != null && fJob.cancel()) {
+            Job job = fJob;
+            if (job != null) {
+                job.cancel();
                 fAnalysisCancelled = true;
                 setAnalysisCompleted();
             }
@@ -288,6 +296,14 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
         return Collections.EMPTY_LIST;
     }
 
+    /**
+     * @since 2.0
+     */
+    @Override
+    public int getDependencyLevel() {
+        return fDependencyLevel;
+    }
+
     private void execute(final ITmfTrace trace) {
         /*
          * TODO: The analysis in a job should be done at the analysis manager
@@ -312,9 +328,14 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
 
         /* Execute dependent analyses before creating the job for this one */
         final Iterable<IAnalysisModule> dependentAnalyses = getDependentAnalyses();
+        int depLevel = 0;
         for (IAnalysisModule module : dependentAnalyses) {
             module.schedule();
+            // Add the dependency level of the analysis + 1 to make sure that if
+            // an analysis already depends on another, it is taken into account
+            depLevel += module.getDependencyLevel() + 1;
         }
+        fDependencyLevel = depLevel;
 
         /*
          * Actual analysis will be run on a separate thread
@@ -391,8 +412,16 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
 
     @Override
     public boolean waitForCompletion() {
+        CountDownLatch finishedLatch;
+        boolean started;
+        synchronized (syncObj) {
+            finishedLatch = fFinishedLatch;
+            started = fStarted;
+        }
         try {
-            fFinishedLatch.await();
+            if (started) {
+                finishedLatch.await();
+            }
         } catch (InterruptedException e) {
             Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$
         }
@@ -487,12 +516,12 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
     protected String getTraceCannotExecuteHelpText(ITmfTrace trace) {
         StringBuilder builder = new StringBuilder();
         builder.append(NLS.bind(Messages.TmfAbstractAnalysisModule_AnalysisCannotExecute, getName()));
-        for (TmfAnalysisRequirement requirement : getAnalysisRequirements()) {
-            if (!requirement.isFulfilled(trace)) {
+        for (TmfAbstractAnalysisRequirement requirement : getAnalysisRequirements()) {
+            if (!requirement.test(trace)) {
                 builder.append("\n\n"); //$NON-NLS-1$
-                builder.append(NLS.bind(Messages.TmfAnalysis_RequirementNotFulfilled, requirement.getType()));
+                builder.append(NLS.bind(Messages.TmfAnalysis_RequirementNotFulfilled, requirement.getPriorityLevel()));
                 builder.append("\n"); //$NON-NLS-1$
-                builder.append(NLS.bind(Messages.TmfAnalysis_RequirementMandatoryValues, requirement.getValues(ValuePriorityLevel.MANDATORY)));
+                builder.append(NLS.bind(Messages.TmfAnalysis_RequirementMandatoryValues, requirement.getValues()));
                 Set<String> information = requirement.getInformation();
                 if (!information.isEmpty()) {
                     builder.append("\n"); //$NON-NLS-1$
@@ -518,7 +547,23 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
     }
 
     @Override
-    public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
+    public Iterable<@NonNull TmfAbstractAnalysisRequirement> getAnalysisRequirements() {
         return Collections.EMPTY_SET;
     }
+
+    // ------------------------------------------------------------------------
+    // ITmfPropertiesProvider
+    // ------------------------------------------------------------------------
+
+    /**
+     * @since 2.0
+     */
+    @Override
+    public Map<@NonNull String, @NonNull String> getProperties() {
+        Map<@NonNull String, @NonNull String> properties = new HashMap<>();
+
+        properties.put(NonNullUtils.checkNotNull(Messages.TmfAbstractAnalysisModule_LabelId), getId());
+
+        return properties;
+    }
 }
This page took 0.028776 seconds and 5 git commands to generate.