mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-09 22:00:39 +01:00
assertion says had race condition; fix it
Use a set instead of a singleton so a late clear doesn't overwrite an early set.
This commit is contained in:
parent
a93b1da6fa
commit
4c9ded77bf
3 changed files with 28 additions and 30 deletions
|
@ -158,7 +158,7 @@ public class DelegateBase implements DlgClickNotify,
|
||||||
protected void onPause()
|
protected void onPause()
|
||||||
{
|
{
|
||||||
m_isVisible = false;
|
m_isVisible = false;
|
||||||
XWServiceHelper.setListener( null );
|
XWServiceHelper.clearListener( this );
|
||||||
m_dlgDelegate.onPausing();
|
m_dlgDelegate.onPausing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,9 @@ import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.eehouse.android.xw4.loc.LocUtils;
|
import org.eehouse.android.xw4.loc.LocUtils;
|
||||||
|
|
||||||
|
@ -58,8 +61,8 @@ public class MultiService {
|
||||||
private static final String ACTION_FETCH_DICT = "_afd";
|
private static final String ACTION_FETCH_DICT = "_afd";
|
||||||
private static final String FOR_MISSING_DICT = "_fmd";
|
private static final String FOR_MISSING_DICT = "_fmd";
|
||||||
|
|
||||||
// Shouldn't this be a Set?
|
private Set<MultiEventListener> m_lis = Collections
|
||||||
private MultiEventListener m_li;
|
.newSetFromMap(new ConcurrentHashMap<MultiEventListener, Boolean>());
|
||||||
|
|
||||||
// these do not currently pass between devices so they can change.
|
// these do not currently pass between devices so they can change.
|
||||||
public enum MultiEvent { _INVALID,
|
public enum MultiEvent { _INVALID,
|
||||||
|
@ -100,28 +103,25 @@ public class MultiService {
|
||||||
|
|
||||||
public void setListener( MultiEventListener li )
|
public void setListener( MultiEventListener li )
|
||||||
{
|
{
|
||||||
synchronized( this ) {
|
m_lis.add( li );
|
||||||
// If this is happening, the order of resume/pause isn't what we
|
|
||||||
// expect. Might need to keep a set of these instead of a
|
|
||||||
// singleton.
|
|
||||||
if ( BuildConfig.DEBUG ) {
|
|
||||||
if ( m_li == null && li == null ) {
|
|
||||||
Assert.fail();
|
|
||||||
} else if ( m_li != null && li != null ) {
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_li = li;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void postEvent( MultiEvent event, Object ... args )
|
public void clearListener( MultiEventListener li )
|
||||||
{
|
{
|
||||||
synchronized( this ) {
|
Assert.assertTrue( m_lis.contains( li ) || ! BuildConfig.DEBUG );
|
||||||
if ( null != m_li ) {
|
m_lis.remove( li );
|
||||||
m_li.eventOccurred( event, args );
|
}
|
||||||
}
|
|
||||||
|
public int postEvent( MultiEvent event, Object ... args )
|
||||||
|
{
|
||||||
|
// don't just return size(): concurrency doesn't guarantee isn't
|
||||||
|
// changed
|
||||||
|
int count = 0;
|
||||||
|
for ( MultiEventListener listener : m_lis ) {
|
||||||
|
listener.eventOccurred( event, args );
|
||||||
|
++count;
|
||||||
}
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Intent makeMissingDictIntent( Context context, NetLaunchInfo nli,
|
public static Intent makeMissingDictIntent( Context context, NetLaunchInfo nli,
|
||||||
|
|
|
@ -39,7 +39,7 @@ import java.util.Map;
|
||||||
abstract class XWServiceHelper {
|
abstract class XWServiceHelper {
|
||||||
private static final String TAG = XWServiceHelper.class.getSimpleName();
|
private static final String TAG = XWServiceHelper.class.getSimpleName();
|
||||||
private Service mService;
|
private Service mService;
|
||||||
private static MultiService s_srcMgr = null;
|
private static MultiService s_srcMgr = new MultiService();
|
||||||
|
|
||||||
public static enum ReceiveResult { OK, GAME_GONE, UNCONSUMED };
|
public static enum ReceiveResult { OK, GAME_GONE, UNCONSUMED };
|
||||||
|
|
||||||
|
@ -101,19 +101,17 @@ abstract class XWServiceHelper {
|
||||||
|
|
||||||
public final static void setListener( MultiService.MultiEventListener li )
|
public final static void setListener( MultiService.MultiEventListener li )
|
||||||
{
|
{
|
||||||
if ( null == s_srcMgr ) {
|
|
||||||
// DbgUtils.logf( "XWService.setListener: registering %s",
|
|
||||||
// li.getClass().getName() );
|
|
||||||
s_srcMgr = new MultiService();
|
|
||||||
}
|
|
||||||
s_srcMgr.setListener( li );
|
s_srcMgr.setListener( li );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final static void clearListener( MultiService.MultiEventListener li )
|
||||||
|
{
|
||||||
|
s_srcMgr.clearListener( li );
|
||||||
|
}
|
||||||
|
|
||||||
protected void postEvent( MultiEvent event, Object ... args )
|
protected void postEvent( MultiEvent event, Object ... args )
|
||||||
{
|
{
|
||||||
if ( null != s_srcMgr ) {
|
if ( 0 == s_srcMgr.postEvent( event, args ) ) {
|
||||||
s_srcMgr.postEvent( event, args );
|
|
||||||
} else {
|
|
||||||
Log.d( TAG, "postEvent(): dropping %s event",
|
Log.d( TAG, "postEvent(): dropping %s event",
|
||||||
event.toString() );
|
event.toString() );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue