summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2011-02-13 19:12:54 -0500
committerEliot Horowitz <eliot@10gen.com>2011-02-13 19:13:21 -0500
commit7e5a60ba66225d261463cf1c7151acf5cb3331ba (patch)
treeb9786da629235eb71d7d08a6244072f6ddb80608
parented360a84f92d3c234ca9a4c2256de95be5562abe (diff)
downloadmongo-7e5a60ba66225d261463cf1c7151acf5cb3331ba.tar.gz
protect ReplicaSet watcher with a lock
-rw-r--r--client/dbclient_rs.cpp20
1 files changed, 17 insertions, 3 deletions
diff --git a/client/dbclient_rs.cpp b/client/dbclient_rs.cpp
index f844cd0d3b4..76ed1992f41 100644
--- a/client/dbclient_rs.cpp
+++ b/client/dbclient_rs.cpp
@@ -34,8 +34,21 @@ namespace mongo {
// global background job responsible for checking every X amount of time
class ReplicaSetMonitorWatcher : public BackgroundJob {
public:
- virtual string name() const { return "ReplicaSetMonitorWatcher"; }
+ ReplicaSetMonitorWatcher() : _safego("ReplicaSetMonitorWatcher::_safego") {}
+ virtual string name() const { return "ReplicaSetMonitorWatcher"; }
+
+ void safeGo() {
+ // check outside of lock for speed
+ if ( getState() == BackgroundJob::Running )
+ return;
+
+ scoped_lock lk( _safego );
+ if ( getState() == BackgroundJob::Running )
+ return;
+
+ go();
+ }
protected:
void run() {
while ( ! inShutdown() ) {
@@ -49,6 +62,8 @@ namespace mongo {
}
}
+ mongo::mutex _safego;
+
} replicaSetMonitorWatcher;
@@ -93,8 +108,7 @@ namespace mongo {
if ( ! m )
m.reset( new ReplicaSetMonitor( name , servers ) );
- if ( replicaSetMonitorWatcher.getState() == BackgroundJob::NotStarted )
- replicaSetMonitorWatcher.go();
+ replicaSetMonitorWatcher.safeGo();
return m;
}