mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-15 04:55:22 +02:00
[170910] Fix AbstractTerminalShell#waitFor() implementation
This commit is contained in:
parent
fe84b78f7d
commit
33f62079ea
2 changed files with 38 additions and 5 deletions
|
@ -58,18 +58,49 @@ public abstract class AbstractTerminalShell extends PlatformObject implements IT
|
||||||
public int exitValue() {
|
public int exitValue() {
|
||||||
// exit values are not supported by default, but we need to observe the
|
// exit values are not supported by default, but we need to observe the
|
||||||
// API by throwing IllegalThreadStateException
|
// API by throwing IllegalThreadStateException
|
||||||
if (isActive())
|
if (isActive()) {
|
||||||
throw new IllegalThreadStateException();
|
throw new IllegalThreadStateException();
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the interval (in milliseconds) for polling the {@ink #isActive()}
|
||||||
|
* method during the {@link #waitFor(long)} method. Subclasses may override
|
||||||
|
* to return different poll intervals.
|
||||||
|
*
|
||||||
|
* The interval may be changed dynamically as appropriate for the current
|
||||||
|
* state of this shell. That way, wait polling mechanisms such as
|
||||||
|
* exponential backoff can be implemented.
|
||||||
|
*
|
||||||
|
* Or, a concrete implementation that supports a notification mechanism for
|
||||||
|
* knowing when the shell terminates, can use this to tweak the waitFor()
|
||||||
|
* method by returning Long.MAX_VALUE here (wait forever), but calling
|
||||||
|
* {@link #notifyAll()} when the shell is dead.
|
||||||
|
*
|
||||||
|
* @return interval (in milliseconds) for polling active state
|
||||||
|
*/
|
||||||
|
protected long getWaitForPollInterval() {
|
||||||
|
return 500L;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for the shell to terminate. This uses a polling mechanism by
|
||||||
|
* default, which can be tweaked by overriding
|
||||||
|
* {@link #getWaitForPollInterval()}.
|
||||||
|
*
|
||||||
|
* @see IBaseShell#waitFor(long)
|
||||||
|
*/
|
||||||
public boolean waitFor(long timeout) throws InterruptedException {
|
public boolean waitFor(long timeout) throws InterruptedException {
|
||||||
boolean active = isActive();
|
boolean active = isActive();
|
||||||
if (active) {
|
if (active) {
|
||||||
synchronized (this) {
|
long endTime = (timeout <= 0) ? Long.MAX_VALUE : System.currentTimeMillis() + timeout - getWaitForPollInterval();
|
||||||
wait(timeout);
|
do {
|
||||||
}
|
synchronized (this) {
|
||||||
active = isActive();
|
wait(getWaitForPollInterval());
|
||||||
|
}
|
||||||
|
active = isActive();
|
||||||
|
} while (active && (timeout <= 0 || System.currentTimeMillis() < endTime));
|
||||||
}
|
}
|
||||||
return active;
|
return active;
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,8 @@ public class ProcessBaseShell extends PlatformObject implements IBaseShell {
|
||||||
if (active) {
|
if (active) {
|
||||||
Thread watchdog = null;
|
Thread watchdog = null;
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
|
// TODO Check if using java.util.Timer would be more efficient
|
||||||
|
// than our own Watchdog
|
||||||
watchdog = new Watchdog(Thread.currentThread(), timeout) {
|
watchdog = new Watchdog(Thread.currentThread(), timeout) {
|
||||||
protected boolean conditionDone() {
|
protected boolean conditionDone() {
|
||||||
return !isActive();
|
return !isActive();
|
||||||
|
|
Loading…
Add table
Reference in a new issue