@@ -113,16 +113,26 @@ namespace mongo {
113113 bool atLeastReadLocked () { return _state.get () != 0 ; }
114114 void assertAtLeastReadLocked () { assert (atLeastReadLocked ()); }
115115
116- void lock () {
116+ bool _checkWriteLockAlready (){
117117 // DEV cout << "LOCK" << endl;
118118 DEV assert ( haveClient () );
119119
120120 int s = _state.get ();
121121 if ( s > 0 ) {
122122 _state.set (s+1 );
123- return ;
123+ return true ;
124124 }
125+
125126 massert ( 10293 , (string)" internal error: locks are not upgradeable: " + sayClientState () , s == 0 );
127+
128+ return false ;
129+ }
130+
131+ void lock () {
132+
133+ if ( _checkWriteLockAlready () )
134+ return ;
135+
126136 _state.set (1 );
127137
128138 curopWaitingForLock ( 1 );
@@ -131,6 +141,23 @@ namespace mongo {
131141
132142 _minfo.entered ();
133143 }
144+
145+ bool lock_try () {
146+ if ( _checkWriteLockAlready () )
147+ return true ;
148+
149+ curopWaitingForLock ( 1 );
150+ bool got = _m.try_lock ();
151+ curopGotLock ();
152+
153+ if ( got ){
154+ _minfo.entered ();
155+ _state.set (1 );
156+ }
157+
158+ return got;
159+ }
160+
134161 void unlock () {
135162 // DEV cout << "UNLOCK" << endl;
136163 int s = _state.get ();
@@ -227,12 +254,21 @@ namespace mongo {
227254 void lock () {
228255#ifdef HAVE_READLOCK
229256 m.lock ();
257+ #error this should be impossible?
230258#else
231259 boost::detail::thread::lock_ops<boost::recursive_mutex>::lock (m);
232260#endif
233261 _minfo.entered ();
234262 }
235263
264+ bool lock_try (){
265+ bool got = boost::detail::thread::lock_ops<boost::recursive_mutex>::trylock (m);
266+ if ( got ){
267+ _minfo.entered ();
268+ }
269+ return got;
270+ }
271+
236272 void releaseEarly () {
237273 assertWriteLocked (); // aso must not be recursive, although we don't verify that in the old boost version
238274 assert ( !_releasedEarly.get () );
@@ -326,6 +362,23 @@ namespace mongo {
326362 }
327363 bool _got;
328364 };
365+
366+ struct writelocktry {
367+ writelocktry ( const string&ns ){
368+ _got = dbMutex.lock_try ();
369+ }
370+ ~writelocktry () {
371+ if ( _got ){
372+ dbunlocking_write ();
373+ dbMutex.unlock ();
374+ }
375+ }
376+ bool got (){
377+ return _got;
378+ }
379+ bool _got;
380+ };
381+
329382
330383 struct atleastreadlock {
331384 atleastreadlock ( const string& ns ){
0 commit comments