Skip to content

Commit

Permalink
Allow cancellation of queing FindBugsJob
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Dec 23, 2015
1 parent fd7ec8b commit bceec81
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions eclipsePlugin/src/de/tobject/findbugs/FindBugsJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package de.tobject.findbugs;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -123,12 +124,14 @@ public IStatus run(IProgressMonitor monitor) {
if (DEBUG) {
FindbugsPlugin.log("Acquiring analysisSem");
}
analysisSem.acquire();
acquired = true;
if (DEBUG) {
FindbugsPlugin.log("Acquired analysisSem");

acquired = acquireAnalysisPermitUnlessCancelled(monitor);
if (acquired) {
if (DEBUG) {
FindbugsPlugin.log("Acquired analysisSem");
}
}
if(monitor.isCanceled()){
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
}
Expand All @@ -155,4 +158,20 @@ public IStatus run(IProgressMonitor monitor) {
}
return Status.OK_STATUS;
}

/**
* Acquires an analysis permit unless first cancelled.
*
* @return {@code true} if permit has been acquired, {@code false} if
* cancellation was observed and permit has not been acquired
*/
private static boolean acquireAnalysisPermitUnlessCancelled(IProgressMonitor monitor) throws InterruptedException {
do {
if (analysisSem.tryAcquire(1, TimeUnit.SECONDS)) {
return true;
}
} while (!monitor.isCanceled());

return false;
}
}

0 comments on commit bceec81

Please sign in to comment.