Experimental: Add support for running states without using threads

This commit is contained in:
2023-12-17 10:46:03 -06:00
parent fe99b9f6fd
commit a2d1416632
2 changed files with 33 additions and 4 deletions

View File

@@ -113,6 +113,14 @@ public abstract class CyberarmEngine extends OpMode {
return;
}
if (!useThreads) {
threadlessExecState(state);
for (CyberarmState task : backgroundTasks) {
threadlessExecState(task);
}
}
// Add telemetry to show currently running state
telemetry.addLine(
"Running state: " +state.getClass().getSimpleName() + ". State: " +
@@ -213,18 +221,36 @@ public abstract class CyberarmEngine extends OpMode {
final CyberarmState finalState = state;
// if (state.isRunning()) { return; } // Assume that we have already started running this state
new Thread(() -> {
if (useThreads) {
new Thread(() -> {
finalState.prestart();
finalState.start();
finalState.startTime = System.currentTimeMillis();
finalState.run();
}).start();
} else {
finalState.prestart();
finalState.start();
finalState.startTime = System.currentTimeMillis();
finalState.run();
}).start();
}
for (CyberarmState kid : state.children) {
runState(kid);
}
}
/**
* Recursively exec states
* @param state State to exec
*/
private void threadlessExecState(final CyberarmState state) {
state.exec();
for (CyberarmState kid : state.children) {
threadlessExecState(kid);
}
}
/**
* Add state to queue, will call init() on state if engine is running
* @param state State to add to queue
@@ -485,7 +511,7 @@ public abstract class CyberarmEngine extends OpMode {
}
/**
* NO OP
* Disable running states in their own threads which might help with random crashes.
*/
public void threadless() {
useThreads = false;

View File

@@ -45,6 +45,7 @@ public abstract class CyberarmState implements Runnable {
/**
* State's main loop, calls exec() until hasFinished is true
* DO NO OVERRIDE
* blocking
*/
@Override
public void run() {
@@ -162,6 +163,7 @@ public abstract class CyberarmState implements Runnable {
*/
public void setHasFinished(boolean value) {
hasFinished = value;
isRunning = value; // Dubious?
}
/**
@@ -177,6 +179,7 @@ public abstract class CyberarmState implements Runnable {
*/
public void finished() {
hasFinished = true;
isRunning = false;
}
/**