From 3e9dfb9a9db109642a1ff977f7845fa88b10152b Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 17 Jun 2013 07:26:54 -0700 Subject: [PATCH] allow a thread to get a cidlock it already has. --- xwords4/relay/cidlock.cpp | 23 +++++++++++++++++++++-- xwords4/relay/cidlock.h | 8 ++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/xwords4/relay/cidlock.cpp b/xwords4/relay/cidlock.cpp index 76c7df7bd..b9ab983a4 100644 --- a/xwords4/relay/cidlock.cpp +++ b/xwords4/relay/cidlock.cpp @@ -34,6 +34,22 @@ CidInfo::GetAddrs( void ) m_addrs : m_cref->GetAddrs(); } +void +CidInfo::SetOwner( pthread_t owner ) +{ + if ( 0 == owner ) { + if ( 0 == --m_ownerCount ) { + m_owner = 0; + } + } else { + ++m_ownerCount; + assert( 0 == m_owner || owner == m_owner ); + m_owner = owner; + } + assert( 0 <= m_ownerCount ); + logf( XW_LOGINFO, "%s(owner=%d); m_ownerCount=%d", __func__, owner, m_ownerCount ); +} + CidLock* CidLock::s_instance = NULL; CidLock::CidLock() : m_nextCID(0) @@ -79,6 +95,7 @@ CidLock::Claim( CookieID cid ) logf( XW_LOGINFO, "%s(%d)", __func__, cid ); #endif CidInfo* info = NULL; + pthread_t self = pthread_self(); for ( ; ; ) { MutexLock ml( &m_infos_mutex ); @@ -92,13 +109,14 @@ CidLock::Claim( CookieID cid ) info = new CidInfo( cid ); m_infos.insert( pair( cid, info ) ); } else { - if ( 0 == iter->second->GetOwner() ) { + pthread_t owner = iter->second->GetOwner(); + if ( 0 == owner || self == owner ) { info = iter->second; } } if ( NULL != info ) { // we're done - info->SetOwner( pthread_self() ); + info->SetOwner( self ); PRINT_CLAIMED(); break; } @@ -174,6 +192,7 @@ CidLock::Relinquish( CidInfo* claim, bool drop ) logf( XW_LOGINFO, "%s: deleting %p", __func__, iter->second ); #endif m_infos.erase( iter ); + claim->SetOwner( 0 ); delete claim; } else { CookieRef* ref = claim->GetRef(); diff --git a/xwords4/relay/cidlock.h b/xwords4/relay/cidlock.h index cb0678e08..2a2e75c1e 100644 --- a/xwords4/relay/cidlock.h +++ b/xwords4/relay/cidlock.h @@ -34,7 +34,10 @@ class CidInfo { CidInfo( CookieID cid ) :m_cid(cid), m_cref(NULL), - m_owner(0) {} + m_owner(0), + m_ownerCount(0) {} + + ~CidInfo() { assert( 0 == m_ownerCount ); } CookieID GetCid( void ) { return m_cid; } CookieRef* GetRef( void ) { return m_cref; } @@ -43,12 +46,13 @@ class CidInfo { void SetAddrs( vector addrs ) { m_addrs = addrs; }; void SetRef( CookieRef* cref ) { m_cref = cref; } - void SetOwner( pthread_t owner ) { m_owner = owner; } + void SetOwner( pthread_t owner ); private: CookieID m_cid; CookieRef* m_cref; pthread_t m_owner; + int m_ownerCount; vector m_addrs; };