Skip to content

Commit

Permalink
spinlock in DiagStr. implement getHostNameCached with cloud cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
dwight committed Oct 3, 2011
1 parent 3f05f1d commit fa1ec1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
23 changes: 14 additions & 9 deletions util/concurrency/value.h
Expand Up @@ -20,7 +20,7 @@

#pragma once

#include "mutex.h"
#include "spin_lock.h"

namespace mongo {

Expand All @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions util/concurrency/vars.cpp
Expand Up @@ -22,8 +22,6 @@

namespace mongo {

SimpleMutex DiagStr::m("diags");

// intentional leak. otherwise destructor orders can be problematic at termination.
MutexDebugger &mutexDebugger = *(new MutexDebugger());

Expand Down
7 changes: 3 additions & 4 deletions util/net/sock.cpp
Expand Up @@ -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();
Expand All @@ -311,7 +310,7 @@ namespace mongo {
return buf;
}

string _hostNameCached;
DiagStr _hostNameCached;
static void _hostNameCachedInit() {
_hostNameCached = getHostName();
}
Expand Down

0 comments on commit fa1ec1c

Please sign in to comment.