From 94f9994fe69983b31a644b34b776f8c7e60ad2f9 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 12 May 2012 16:16:38 +0000 Subject: [PATCH] DroidFish: Do GTB initialization in a background thread. --- .../org/petero/droidfish/gtb/GtbProbe.java | 28 ++++++++++++++++--- .../src/org/petero/droidfish/gtb/Probe.java | 10 +++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/DroidFish/src/org/petero/droidfish/gtb/GtbProbe.java b/DroidFish/src/org/petero/droidfish/gtb/GtbProbe.java index 7576db1..644b423 100644 --- a/DroidFish/src/org/petero/droidfish/gtb/GtbProbe.java +++ b/DroidFish/src/org/petero/droidfish/gtb/GtbProbe.java @@ -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 tbPathQueue = new ConcurrentLinkedQueue(); 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); } } diff --git a/DroidFish/src/org/petero/droidfish/gtb/Probe.java b/DroidFish/src/org/petero/droidfish/gtb/Probe.java index 7dd5664..2fe4772 100644 --- a/DroidFish/src/org/petero/droidfish/gtb/Probe.java +++ b/DroidFish/src/org/petero/droidfish/gtb/Probe.java @@ -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]) {