mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-09 05:24:44 +01:00
ensure writer gets run every time OS schedules via onHandleWork()
This commit is contained in:
parent
bf2ab49c20
commit
ab0469fe39
1 changed files with 72 additions and 42 deletions
|
@ -389,6 +389,13 @@ public class RelayService extends JobIntentService
|
||||||
DbgUtils.assertOnUIThread( false );
|
DbgUtils.assertOnUIThread( false );
|
||||||
Log.d( TAG, "%s.onHandleWork(cmd=%s)", this, cmdFrom( intent ) );
|
Log.d( TAG, "%s.onHandleWork(cmd=%s)", this, cmdFrom( intent ) );
|
||||||
handleCommand( intent );
|
handleCommand( intent );
|
||||||
|
|
||||||
|
boolean goOn = mThreads.serviceQueue();
|
||||||
|
if ( !goOn ) {
|
||||||
|
Log.e( TAG, "onHandleWork(): need to exit... HELP!!!!" );
|
||||||
|
mThreads.killThreads();
|
||||||
|
}
|
||||||
|
|
||||||
resetExitTimer();
|
resetExitTimer();
|
||||||
Log.d( TAG, "%s.onHandleWork(cmd=%s) DONE", this, cmdFrom( intent ) );
|
Log.d( TAG, "%s.onHandleWork(cmd=%s) DONE", this, cmdFrom( intent ) );
|
||||||
}
|
}
|
||||||
|
@ -405,6 +412,8 @@ public class RelayService extends JobIntentService
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( startImmediately ) {
|
if ( startImmediately ) {
|
||||||
|
// Log.d( TAG, "onDestroy(): restarting: %d in queue",
|
||||||
|
// mThreads.m_queue.size() );
|
||||||
timerFired( this );
|
timerFired( this );
|
||||||
} else if ( shouldMaintainConnection() ) {
|
} else if ( shouldMaintainConnection() ) {
|
||||||
long interval_millis = getMaxIntervalSeconds() * 1000;
|
long interval_millis = getMaxIntervalSeconds() * 1000;
|
||||||
|
@ -1068,9 +1077,12 @@ public class RelayService extends JobIntentService
|
||||||
// continue to use the instance for a short while after returning.
|
// continue to use the instance for a short while after returning.
|
||||||
void unsetService()
|
void unsetService()
|
||||||
{
|
{
|
||||||
|
// RelayService oldService;
|
||||||
synchronized ( mServiceHolder ) {
|
synchronized ( mServiceHolder ) {
|
||||||
|
// oldService = mServiceHolder[0];
|
||||||
mServiceHolder[0] = null;
|
mServiceHolder[0] = null;
|
||||||
}
|
}
|
||||||
|
// Log.d( TAG, "unsetService() DONE (was %s)", oldService );
|
||||||
}
|
}
|
||||||
|
|
||||||
private RelayService getService() throws InterruptedException
|
private RelayService getService() throws InterruptedException
|
||||||
|
@ -1163,26 +1175,25 @@ public class RelayService extends JobIntentService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startWriteThread()
|
// Break out the work of sending queued packets so it can be called
|
||||||
|
// from several threads. Fixes starvation problems on some devices.
|
||||||
|
private synchronized boolean serviceQueue()
|
||||||
{
|
{
|
||||||
Assert.assertNull( m_UDPWriteThread );
|
Log.d( TAG, "serviceQueue()" );
|
||||||
|
boolean shouldGoOn = true;
|
||||||
m_UDPWriteThread = new Thread( null, new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
Log.i( TAG, "write thread starting" );
|
|
||||||
for ( boolean gotEOQ = false; !gotEOQ; ) {
|
|
||||||
List<PacketData> dataListUDP = new ArrayList<>();
|
List<PacketData> dataListUDP = new ArrayList<>();
|
||||||
List<PacketData> dataListWeb = new ArrayList<>();
|
List<PacketData> dataListWeb = new ArrayList<>();
|
||||||
PacketData outData;
|
PacketData outData;
|
||||||
try {
|
try {
|
||||||
long ts = s_packetsSentUDP.size() > 0 ? 10 : 3600;
|
long ts = s_packetsSentUDP.size() > 0 ? 10 : 1000;
|
||||||
Log.d( TAG, "blocking %d sec on poll()", ts );
|
Log.d( TAG, "blocking %dms on poll()", ts );
|
||||||
for ( outData = m_queue.poll(ts, TimeUnit.SECONDS);
|
for ( outData = m_queue.poll( ts, TimeUnit.MILLISECONDS );
|
||||||
null != outData;
|
null != outData;
|
||||||
outData = m_queue.poll() ) { // doesn't block
|
outData = m_queue.poll() ) { // doesn't block
|
||||||
Log.d( TAG, "removed packet from queue: %s", outData );
|
Log.d( TAG, "removed packet from queue (%d left): %s",
|
||||||
|
m_queue.size(), outData );
|
||||||
if ( outData == sEOQPacket ) {
|
if ( outData == sEOQPacket ) {
|
||||||
gotEOQ = true;
|
shouldGoOn = false;
|
||||||
break;
|
break;
|
||||||
} else if ( skipNativeSend() || outData.getForWeb() ) {
|
} else if ( skipNativeSend() || outData.getForWeb() ) {
|
||||||
dataListWeb.add( outData );
|
dataListWeb.add( outData );
|
||||||
|
@ -1201,13 +1212,35 @@ public class RelayService extends JobIntentService
|
||||||
ConnStatusHandler.showSuccessOut();
|
ConnStatusHandler.showSuccessOut();
|
||||||
} catch ( InterruptedException ie ) {
|
} catch ( InterruptedException ie ) {
|
||||||
Log.w( TAG, "write thread killed" );
|
Log.w( TAG, "write thread killed" );
|
||||||
|
shouldGoOn = false;
|
||||||
|
}
|
||||||
|
Log.d( TAG, "serviceQueue() => %b", shouldGoOn );
|
||||||
|
return shouldGoOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startWriteThread()
|
||||||
|
{
|
||||||
|
Assert.assertNull( m_UDPWriteThread );
|
||||||
|
|
||||||
|
m_UDPWriteThread = new Thread( null, new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
Log.i( TAG, "write thread starting" );
|
||||||
|
for ( ; ; ) {
|
||||||
|
if ( !serviceQueue() ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i( TAG, "write thread killing read thread" );
|
Log.i( TAG, "write thread killing read thread" );
|
||||||
|
killThreads();
|
||||||
// now kill the read thread
|
// now kill the read thread
|
||||||
|
}
|
||||||
|
}, getClass().getName() );
|
||||||
|
m_UDPWriteThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void killThreads()
|
||||||
|
{
|
||||||
m_UDPSocket.close();
|
m_UDPSocket.close();
|
||||||
try {
|
try {
|
||||||
m_UDPReadThread.join();
|
m_UDPReadThread.join();
|
||||||
|
@ -1219,9 +1252,6 @@ public class RelayService extends JobIntentService
|
||||||
Log.i( TAG, "write thread exiting (with %d in queue)",
|
Log.i( TAG, "write thread exiting (with %d in queue)",
|
||||||
m_queue.size() );
|
m_queue.size() );
|
||||||
}
|
}
|
||||||
}, getClass().getName() );
|
|
||||||
m_UDPWriteThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int sendViaWeb( List<PacketData> packets ) throws InterruptedException
|
private int sendViaWeb( List<PacketData> packets ) throws InterruptedException
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue