diff --git a/DroidFish/jni/jni.cpp b/DroidFish/jni/jni.cpp index 36a985f..3f7a869 100644 --- a/DroidFish/jni/jni.cpp +++ b/DroidFish/jni/jni.cpp @@ -31,6 +31,8 @@ int main(int argc, char* argv[]); static int fdFromChild = -1; static int fdToChild = -1; +static int childpid = -1; + /* * Class: org_petero_droidfish_engine_NativePipedProcess * Method: startProcess @@ -39,13 +41,16 @@ static int fdToChild = -1; extern "C" JNIEXPORT void JNICALL Java_org_petero_droidfish_engine_NativePipedProcess_startProcess (JNIEnv* env, jobject obj) { + if (childpid != -1) + kill(childpid, SIGKILL); + int fd1[2]; /* parent -> child */ int fd2[2]; /* child -> parent */ if (pipe(fd1) < 0) exit(1); if (pipe(fd2) < 0) exit(1); - int childpid = fork(); + childpid = fork(); if (childpid == -1) { exit(1); } diff --git a/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java b/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java index b25efa6..c8d9aca 100644 --- a/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java +++ b/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java @@ -302,6 +302,7 @@ public class DroidComputerPlayer { /** Start an engine, if not already started. * Will shut down old engine first, if needed. */ public final synchronized void queueStartEngine(int id, String engine) { + killOldEngine(engine); stopSearch(); searchRequest = SearchRequest.startRequest(id, engine); handleQueue(); @@ -314,6 +315,7 @@ public class DroidComputerPlayer { * command, such as "draw" and "resign". */ public final synchronized void queueSearchRequest(SearchRequest sr) { + killOldEngine(sr.engine); stopSearch(); if (sr.ponderMove != null) @@ -364,6 +366,7 @@ public class DroidComputerPlayer { /** Start analyzing a position. */ public final synchronized void queueAnalyzeRequest(SearchRequest sr) { + killOldEngine(sr.engine); stopSearch(); // If no legal moves, there is nothing to analyze @@ -385,6 +388,12 @@ public class DroidComputerPlayer { handleIdleState(); } + private void killOldEngine(String engine) { + if (engine.equals(engineState.engine)) + return; + shutdownEngine(); + } + /** Tell engine to stop searching. */ public final synchronized boolean stopSearch() { searchRequest = null; @@ -562,7 +571,10 @@ public class DroidComputerPlayer { private final void monitorLoop() { while (true) { int timeout = getReadTimeout(); - String s = uciEngine.readLineFromEngine(timeout); + UCIEngine uci = uciEngine; + if (uci == null) + return; + String s = uci.readLineFromEngine(timeout); if (Thread.currentThread().isInterrupted()) return; processEngineOutput(s);