allow a thread to get a cidlock it already has.

This commit is contained in:
Eric House 2013-06-17 07:26:54 -07:00
parent 950e754ca2
commit 3e9dfb9a9d
2 changed files with 27 additions and 4 deletions

View file

@ -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<CookieID, CidInfo*>( 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();

View file

@ -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<AddrInfo> 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<AddrInfo> m_addrs;
};