get rid of per-cref mutex: it's redundant now that access is

synchronized.  Add method to check if new players are still welcome to
allow fix for race.
This commit is contained in:
Andy2 2011-06-23 18:57:48 -07:00
parent a5bab1232f
commit 63a4e6c953
2 changed files with 16 additions and 12 deletions

View file

@ -114,7 +114,6 @@ CookieRef::ReInit( const char* cookie, const char* connName, CookieID id,
CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id, CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id,
int langCode, int nPlayersT, int nAlreadyHere ) int langCode, int nPlayersT, int nAlreadyHere )
{ {
pthread_mutex_init( &m_mutex, NULL );
ReInit( cookie, connName, id, langCode, nPlayersT, nAlreadyHere ); ReInit( cookie, connName, id, langCode, nPlayersT, nAlreadyHere );
} }
@ -152,8 +151,6 @@ CookieRef::Lock( void )
{ {
bool success = true; bool success = true;
pthread_mutex_lock( &m_mutex );
/* We get here possibly after having been blocked on the mutex for a /* We get here possibly after having been blocked on the mutex for a
while. This cref may no longer be live. If it's not, unlock and while. This cref may no longer be live. If it's not, unlock and
return. */ return. */
@ -166,7 +163,6 @@ CookieRef::Lock( void )
this ); this );
success = false; success = false;
m_locking_thread = 0; m_locking_thread = 0;
pthread_mutex_unlock( &m_mutex );
} }
return success; return success;
@ -176,7 +172,6 @@ void
CookieRef::Unlock() { CookieRef::Unlock() {
assert( m_locking_thread == pthread_self() ); assert( m_locking_thread == pthread_self() );
m_locking_thread = 0; m_locking_thread = 0;
pthread_mutex_unlock( &m_mutex );
} }
bool bool
@ -313,7 +308,7 @@ CookieRef::SocketForHost( HostID dest )
bool bool
CookieRef::AlreadyHere( unsigned short seed, int socket, HostID* prevHostID ) CookieRef::AlreadyHere( unsigned short seed, int socket, HostID* prevHostID )
{ {
logf( XW_LOGINFO, "%s(seed=%x,socket=%d)", __func__, seed, socket ); logf( XW_LOGINFO, "%s(seed=%x(%d),socket=%d)", __func__, seed, seed, socket );
bool here = false; bool here = false;
vector<HostRec>::iterator iter; vector<HostRec>::iterator iter;
@ -337,8 +332,8 @@ bool
CookieRef::AlreadyHere( HostID hid, unsigned short seed, int socket, CookieRef::AlreadyHere( HostID hid, unsigned short seed, int socket,
bool* spotTaken ) bool* spotTaken )
{ {
logf( XW_LOGINFO, "%s(hid=%d,seed=%x,socket=%d)", __func__, logf( XW_LOGINFO, "%s(hid=%d,seed=%x(%d),socket=%d)", __func__,
hid, seed, socket ); hid, seed, seed, socket );
bool here = false; bool here = false;
vector<HostRec>::iterator iter; vector<HostRec>::iterator iter;
@ -419,6 +414,17 @@ CookieRef::removeSocket( int socket )
} }
} /* removeSocket */ } /* removeSocket */
bool
CookieRef::HaveRoom( int nPlayers )
{
int total = m_nPlayersSought;
int soFar = m_nPlayersHere;
bool haveRoom = nPlayers <= total - soFar;
logf( XW_LOGINFO, "%s(%d): total %d - soFar %d >= new %d => %d", __func__,
nPlayers, total, soFar, nPlayers, haveRoom );
return haveRoom;
}
bool bool
CookieRef::HasSocket( int socket ) CookieRef::HasSocket( int socket )
{ {

View file

@ -90,6 +90,8 @@ class CookieRef {
int GetPlayersSought() { return m_nPlayersSought; } int GetPlayersSought() { return m_nPlayersSought; }
int GetPlayersHere() { return m_nPlayersHere; } int GetPlayersHere() { return m_nPlayersHere; }
bool HaveRoom( int nPlayers );
int CountSockets() { return m_sockets.size(); } int CountSockets() { return m_sockets.size(); }
bool HasSocket( int socket ); bool HasSocket( int socket );
bool HasSocket_locked( int socket ); bool HasSocket_locked( int socket );
@ -216,8 +218,6 @@ class CookieRef {
void postCheckAllHere(); void postCheckAllHere();
void postDropDevice( HostID hostID ); void postDropDevice( HostID hostID );
bool hostAlreadyHere( int seed, int socket );
void reducePlayerCounts( int socket ); void reducePlayerCounts( int socket );
void setAllConnectedTimer(); void setAllConnectedTimer();
@ -284,8 +284,6 @@ class CookieRef {
AckTimer m_timers[4]; AckTimer m_timers[4];
pthread_mutex_t m_mutex;
pthread_t m_locking_thread; /* for debugging only */ pthread_t m_locking_thread; /* for debugging only */
bool m_in_handleEvents; /* for debugging only */ bool m_in_handleEvents; /* for debugging only */
int m_delayMicros; int m_delayMicros;