DroidFish: Do GTB initialization in a background thread.

This commit is contained in:
Peter Osterlund 2012-05-12 16:16:38 +00:00
parent d442bb002e
commit 94f9994fe6
2 changed files with 31 additions and 7 deletions

View File

@ -18,6 +18,8 @@
package org.petero.droidfish.gtb;
import java.util.concurrent.ConcurrentLinkedQueue;
/** Interface to native gtb probing code. */
class GtbProbe {
static {
@ -25,14 +27,32 @@ class GtbProbe {
}
private String currTbPath = "";
private ConcurrentLinkedQueue<String> tbPathQueue = new ConcurrentLinkedQueue<String>();
GtbProbe() {
}
final synchronized void setPath(String tbPath, boolean forceReload) {
if (forceReload || !currTbPath.equals(tbPath)) {
currTbPath = tbPath;
init(tbPath);
public final void setPath(String tbPath, boolean forceReload) {
if (forceReload || !tbPathQueue.isEmpty() || !currTbPath.equals(tbPath)) {
tbPathQueue.add(tbPath);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
initIfNeeded();
}
});
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}
}
public final synchronized void initIfNeeded() {
String path = tbPathQueue.poll();
while (!tbPathQueue.isEmpty())
path = tbPathQueue.poll();
if (path != null) {
currTbPath = path;
init(currTbPath);
}
}

View File

@ -143,9 +143,13 @@ public class Probe {
epSquare = GtbProbe.NOSQUARE;
int[] result = new int[2];
boolean res = gtb.probeHard(pos.whiteMove, epSquare, castleMask,
whiteSquares, blackSquares, whitePieces, blackPieces,
result);
boolean res = false;
if (nWhite + nBlack <= 5) {
gtb.initIfNeeded();
res = gtb.probeHard(pos.whiteMove, epSquare, castleMask,
whiteSquares, blackSquares, whitePieces, blackPieces,
result);
}
ProbeResult ret = new ProbeResult();
if (res) {
switch (result[0]) {