From c51e5e41f41cdba3bdbf0a96bd739a820e651703 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Thu, 23 Jun 2011 18:58:50 -0700 Subject: [PATCH] fix ClaimSocket: it was neither marking claims nor checking for marks. --- xwords4/relay/cidlock.cpp | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/xwords4/relay/cidlock.cpp b/xwords4/relay/cidlock.cpp index 3e8b1cb3f..3a522079e 100644 --- a/xwords4/relay/cidlock.cpp +++ b/xwords4/relay/cidlock.cpp @@ -96,18 +96,31 @@ CidInfo* CidLock::ClaimSocket( int sock ) { CidInfo* info = NULL; - logf( XW_LOGINFO, "%s(%d)", __func__, sock ); + logf( XW_LOGINFO, "%s(sock=%d)", __func__, sock ); - MutexLock ml( &m_infos_mutex ); - map< CookieID, CidInfo*>::iterator iter = m_infos.begin(); - while ( iter != m_infos.end() ) { - if ( sock == iter->second->GetSocket() ) { - info = iter->second; + for ( ; ; ) { + MutexLock ml( &m_infos_mutex ); + + map< CookieID, CidInfo*>::iterator iter = m_infos.begin(); + while ( iter != m_infos.end() ) { + if ( sock == iter->second->GetSocket() ) { + if ( 0 == iter->second->GetOwner() ) { + info = iter->second; + info->SetOwner( pthread_self() ); + } + break; + } + ++iter; + } + + /* break if socket isn't here or if it's not claimed */ + if ( iter == m_infos.end() || NULL != info ) { break; } - ++iter; + logf( XW_LOGINFO, "%s(sock=%d): waiting....", __func__, sock ); + pthread_cond_wait( &m_infos_condvar, &m_infos_mutex ); } - + print_claimed(); logf( XW_LOGINFO, "%s(%d): DONE", __func__, info? info->GetCid():0 ); return info; } @@ -149,12 +162,14 @@ void CidLock::Relinquish( CidInfo* claim, bool drop ) { CookieID cid = claim->GetCid(); - logf( XW_LOGINFO, "%s(%d)", __func__, cid ); + logf( XW_LOGINFO, "%s(%d,drop=%d)", __func__, cid, drop ); MutexLock ml( &m_infos_mutex ); map< CookieID, CidInfo*>::iterator iter = m_infos.find( cid ); assert( iter != m_infos.end() ); + assert( iter->second->GetOwner() == pthread_self() ); if ( drop ) { + logf( XW_LOGINFO, "%s: deleting %p", __func__, iter->second ); delete iter->second; m_infos.erase( iter ); } else { @@ -162,5 +177,5 @@ CidLock::Relinquish( CidInfo* claim, bool drop ) } print_claimed(); pthread_cond_signal( &m_infos_condvar ); - logf( XW_LOGINFO, "%s(%d): DONE", __func__, cid ); + logf( XW_LOGINFO, "%s(%d,drop=%d): DONE", __func__, cid, drop ); }