From f9afcc0e6fb395061b2e3eeb376b962abdcd76ce Mon Sep 17 00:00:00 2001 From: Andy2 Date: Fri, 30 Jul 2010 06:43:56 -0700 Subject: [PATCH] add ability to tag events as UI or not. CMD_DO is not. Only UI events in queue prevent engine from continuing. This fixes bug where server running engine on behalf of robot would starve the UI thread by looping forever seeing the engine bail because a CMD_DO was in the queue and then adding a CMD_DO to try running the engine yet again. --- .../eehouse/android/xw4/BoardActivity.java | 2 +- .../eehouse/android/xw4/jni/JNIThread.java | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 99abc2ff2..eadca9ba6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -592,7 +592,7 @@ public class BoardActivity extends Activity implements UtilCtxt { m_handler.post( new Runnable() { public void run() { if ( null != m_jniThread ) { - m_jniThread.handle( JNIThread.JNICmd.CMD_DO ); + m_jniThread.handle( JNIThread.JNICmd.CMD_DO, false ); } } } ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java index 9eaf3a5e3..e1862b294 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java @@ -25,6 +25,7 @@ import org.eehouse.android.xw4.Utils; import android.content.Context; import java.lang.InterruptedException; import java.util.concurrent.LinkedBlockingQueue; +import java.util.Iterator; import android.os.Handler; import android.os.Message; import android.graphics.Paint; @@ -99,10 +100,11 @@ public class JNIThread extends Thread { LinkedBlockingQueue m_queue; private class QueueElem { - protected QueueElem( JNICmd cmd, Object[] args ) + protected QueueElem( JNICmd cmd, boolean isUI, Object[] args ) { - m_cmd = cmd; m_args = args; + m_cmd = cmd; m_isUIEvent = isUI; m_args = args; } + boolean m_isUIEvent; JNICmd m_cmd; Object[] m_args; } @@ -132,8 +134,15 @@ public class JNIThread extends Thread { public boolean busy() { // synchronize this!!! - int siz = m_queue.size(); - return siz > 0; + boolean result = false; + Iterator iter = m_queue.iterator(); + while ( iter.hasNext() ) { + if ( iter.next().m_isUIEvent ) { + result = true; + break; + } + } + return result; } public void setInBackground( boolean inBack ) @@ -488,11 +497,16 @@ public class JNIThread extends Thread { Utils.logf( "run exiting" ); } // run - public void handle( JNICmd cmd, Object... args ) + public void handle( JNICmd cmd, boolean isUI, Object... args ) { - QueueElem elem = new QueueElem( cmd, args ); + QueueElem elem = new QueueElem( cmd, isUI, args ); // Utils.logf( "adding: " + cmd.toString() ); m_queue.add( elem ); } + public void handle( JNICmd cmd, Object... args ) + { + handle( cmd, true, args ); + } + }