diff options
author | Dwight <dmerriman@gmail.com> | 2009-10-09 11:24:37 -0400 |
---|---|---|
committer | Dwight <dmerriman@gmail.com> | 2009-10-09 11:24:37 -0400 |
commit | 75cc68851febcec4a77905433a1656b7c3c5d67a (patch) | |
tree | c3f298ab73f40623b3aba5b9f8d8e9cbc7cdbbba /db/clientcursor.h | |
parent | 7425aa3d706f628ca50f6aa10c15520dee334a7a (diff) | |
download | mongo-75cc68851febcec4a77905433a1656b7c3c5d67a.tar.gz |
make ClientCursor methods threadsafe
Diffstat (limited to 'db/clientcursor.h')
-rw-r--r-- | db/clientcursor.h | 66 |
1 files changed, 42 insertions, 24 deletions
diff --git a/db/clientcursor.h b/db/clientcursor.h index b207ebc06be..5d5724f5f16 100644 --- a/db/clientcursor.h +++ b/db/clientcursor.h @@ -44,26 +44,37 @@ namespace mongo { class ClientCursor { friend class CmdCursorInfo; + DiskLoc _lastLoc; // use getter and setter not this (important) + unsigned _idleAgeMillis; // how long has the cursor been around, relative to server idle time + bool _liveForever; // if true, never time out cursor + static CCById clientCursorsById; static CCByLoc byLoc; - DiskLoc _lastLoc; // use getter and setter not this. - unsigned _idleAgeMillis; // how long has the cursor been around, relative to server idle time - bool _liveForever; // if true, never time out cursor - static CursorId allocCursorId(); + static boost::recursive_mutex ccmutex; // must use this for all statics above! + + static CursorId allocCursorId_inlock(); + public: - ClientCursor() : _idleAgeMillis(0), _liveForever(false), cursorid( allocCursorId() ), pos(0) { - clientCursorsById.insert( make_pair(cursorid, this) ); - } - ~ClientCursor(); - const CursorId cursorid; + /*const*/ CursorId cursorid; string ns; auto_ptr<KeyValJSMatcher> matcher; auto_ptr<Cursor> c; - int pos; /* # objects into the cursor so far */ + int pos; // # objects into the cursor so far + + ClientCursor() : _idleAgeMillis(0), _liveForever(false), pos(0) { + recursive_boostlock lock(ccmutex); + cursorid = allocCursorId_inlock(); + clientCursorsById.insert( make_pair(cursorid, this) ); + } + ~ClientCursor(); + DiskLoc lastLoc() const { return _lastLoc; } - void setLastLoc(DiskLoc); + private: + void setLastLoc_inlock(DiskLoc); + public: + auto_ptr< FieldMatcher > filter; // which fields query wants returned Message originalMessage; // this is effectively an auto ptr for data the matcher points to @@ -72,16 +83,8 @@ namespace mongo { */ static void invalidate(const char *nsPrefix); - static bool erase(CursorId id) { - ClientCursor *cc = find(id); - if ( cc ) { - delete cc; - return true; - } - return false; - } - - static ClientCursor* find(CursorId id, bool warn = true) { + private: + static ClientCursor* find_inlock(CursorId id, bool warn = true) { CCById::iterator it = clientCursorsById.find(id); if ( it == clientCursorsById.end() ) { if ( warn ) @@ -90,6 +93,21 @@ namespace mongo { } return it->second; } + public: + static ClientCursor* find(CursorId id, bool warn = true) { + recursive_boostlock lock(ccmutex); + return find_inlock(id, warn); + } + + static bool erase(CursorId id) { + recursive_boostlock lock(ccmutex); + ClientCursor *cc = find_inlock(id); + if ( cc ) { + delete cc; + return true; + } + return false; + } /* call when cursor's location changes so that we can update the cursorsbylocation map. if you are locked and internally iterating, only @@ -100,7 +118,7 @@ namespace mongo { void cleanupByLocation(DiskLoc loc); void mayUpgradeStorage() { -/* if ( !ids_.get() ) + /* if ( !ids_.get() ) return; stringstream ss; ss << ns << "." << cursorid; @@ -119,14 +137,14 @@ namespace mongo { return _idleAgeMillis; } - void liveForever(){ + void liveForever() { _liveForever = true; } static unsigned byLocSize(); // just for diagnostics static void idleTimeReport(unsigned millis); - static void aboutToDeleteBucket(const DiskLoc& b); + static void informAboutToDeleteBucket(const DiskLoc& b); static void aboutToDelete(const DiskLoc& dl); }; |