replace mutex with rwlock; move creation inside class so can be

controlled by same rwlock.
This commit is contained in:
ehouse 2005-04-08 14:15:23 +00:00
parent a9c3bac6df
commit d783a5ba2e

View file

@ -30,37 +30,43 @@
using namespace std; using namespace std;
static CookieMap gCookieMap; static CookieMap gCookieMap;
pthread_mutex_t gCookieMapMutex = PTHREAD_MUTEX_INITIALIZER; pthread_rwlock_t gCookieMapRWLock = PTHREAD_RWLOCK_INITIALIZER;
CookieID CookieRef::ms_nextConnectionID = 1000; CookieID CookieRef::ms_nextConnectionID = 1000;
static pthread_mutex_t gNextConnIDMutex = PTHREAD_MUTEX_INITIALIZER;
/* static */ CookieRef*
CookieRef::AddNew( string s )
{
RWWriteLock rwl( &gCookieMapRWLock );
CookieRef* ref = new CookieRef( s );
gCookieMap.insert( pair<CookieID, CookieRef*>(ref->GetConnID(), ref ) );
logf( "paired cookie %s with id %d", s.c_str(), ref->GetConnID() );
return ref;
}
CookieRef* CookieRef*
get_make_cookieRef( char* cookie ) get_make_cookieRef( char* cookie, CookieID connID ) /* connID ignored for now */
{ {
CookieRef* ref; CookieRef* ref = NULL;
MutexLock ml( &gCookieMapMutex );
string s(cookie); string s(cookie);
CookieMap::iterator iter = gCookieMap.begin();
while ( iter != gCookieMap.end() ) { {
ref = iter->second; RWReadLock rwl( &gCookieMapRWLock );
if ( ref->Name() == s ) {
break; CookieMap::iterator iter = gCookieMap.begin();
while ( iter != gCookieMap.end() ) {
ref = iter->second;
if ( ref->Name() == s ) {
ref = iter->second;
break;
}
++iter;
} }
++iter;
} }
if ( iter != gCookieMap.end() ) { if ( !ref ) {
logf( "ref found for cookie %s", cookie ); ref = CookieRef::AddNew(s);
ref = iter->second;
} else {
ref = new CookieRef(s);
gCookieMap.insert( pair<CookieID, CookieRef*>(ref->GetConnID(), ref ) );
} }
return ref; return ref;
} }
@ -68,7 +74,7 @@ CookieRef*
get_cookieRef( CookieID cookieID ) get_cookieRef( CookieID cookieID )
{ {
CookieRef* ref = NULL; CookieRef* ref = NULL;
MutexLock ml( &gCookieMapMutex ); RWReadLock rwl( &gCookieMapRWLock );
CookieMap::iterator iter = gCookieMap.find( cookieID); CookieMap::iterator iter = gCookieMap.find( cookieID);
while ( iter != gCookieMap.end() ) { while ( iter != gCookieMap.end() ) {
@ -85,7 +91,7 @@ get_cookieRef( CookieID cookieID )
static void static void
ForgetCref( CookieRef* cref ) ForgetCref( CookieRef* cref )
{ {
MutexLock ml( &gCookieMapMutex ); RWWriteLock ml( &gCookieMapRWLock );
CookieMap::iterator iter = gCookieMap.begin(); CookieMap::iterator iter = gCookieMap.begin();
while ( iter != gCookieMap.end() ) { while ( iter != gCookieMap.end() ) {
@ -185,7 +191,6 @@ CookieRef::CookieRef(string s)
: m_name(s) : m_name(s)
{ {
pthread_mutex_init( &m_mutex, NULL ); pthread_mutex_init( &m_mutex, NULL );
MutexLock ml( &gNextConnIDMutex );
m_connectionID = ms_nextConnectionID++; /* needs a mutex!!! */ m_connectionID = ms_nextConnectionID++; /* needs a mutex!!! */
} }