diff --git a/util/concurrency/value.h b/util/concurrency/value.h index d75a0e58ce6e6..8c6820a7628ec 100644 --- a/util/concurrency/value.h +++ b/util/concurrency/value.h @@ -20,7 +20,7 @@ #pragma once -#include "mutex.h" +#include "spin_lock.h" namespace mongo { @@ -45,35 +45,40 @@ namespace mongo { } }; + /** there is now one mutex per DiagStr. If you have hundreds or millions of + DiagStrs you'll need to do something different. + */ class DiagStr { + mutable SpinLock m; string _s; - static SimpleMutex m; public: DiagStr(const DiagStr& r) : _s(r.get()) { } + DiagStr(const string& r) : _s(r) { } DiagStr() { } bool empty() const { - SimpleMutex::scoped_lock lk(m); + scoped_spinlock lk(m); return _s.empty(); } string get() const { - SimpleMutex::scoped_lock lk(m); + scoped_spinlock lk(m); return _s; } - void set(const char *s) { - SimpleMutex::scoped_lock lk(m); + scoped_spinlock lk(m); _s = s; } void set(const string& s) { - SimpleMutex::scoped_lock lk(m); + scoped_spinlock lk(m); _s = s; } operator string() const { return get(); } void operator=(const string& s) { set(s); } void operator=(const DiagStr& rhs) { - SimpleMutex::scoped_lock lk(m); - _s = rhs.get(); + set( rhs.get() ); } + + // == is not defined. use get() == ... instead. done this way so one thinks about if composing multiple operations + bool operator==(const string& s) const; }; /** Thread safe map. diff --git a/util/concurrency/vars.cpp b/util/concurrency/vars.cpp index 669725a149200..b561ccce00343 100644 --- a/util/concurrency/vars.cpp +++ b/util/concurrency/vars.cpp @@ -22,8 +22,6 @@ namespace mongo { - SimpleMutex DiagStr::m("diags"); - // intentional leak. otherwise destructor orders can be problematic at termination. MutexDebugger &mutexDebugger = *(new MutexDebugger()); diff --git a/util/net/sock.cpp b/util/net/sock.cpp index 1b5828c24ea38..b4bbf524c6e14 100644 --- a/util/net/sock.cpp +++ b/util/net/sock.cpp @@ -286,10 +286,9 @@ namespace mongo { // ------ hostname ------------------- string hostbyname(const char *hostname) { - { + if( *hostname == '#' ) { string s = dynHostNames.get(hostname); - if( !s.empty() ) - return s; + return s; } string addr = SockAddr(hostname, 0).getAddr(); @@ -311,7 +310,7 @@ namespace mongo { return buf; } - string _hostNameCached; + DiagStr _hostNameCached; static void _hostNameCachedInit() { _hostNameCached = getHostName(); }