diff options
Diffstat (limited to 'util/goodies.h')
-rw-r--r-- | util/goodies.h | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/util/goodies.h b/util/goodies.h index 65bfbab..735aa8b 100644 --- a/util/goodies.h +++ b/util/goodies.h @@ -324,31 +324,41 @@ namespace mongo { bool tryAcquire() { scoped_lock lk( _mutex ); - if ( _num <= 0 ) { - if ( _num < 0 ) { - cerr << "DISASTER! in TicketHolder" << endl; - } - return false; + return _tryAcquire(); + } + + void waitForTicket() { + scoped_lock lk( _mutex ); + + while( ! _tryAcquire() ) { + _newTicket.wait( lk.boost() ); } - _num--; - return true; } void release() { - scoped_lock lk( _mutex ); - _num++; + { + scoped_lock lk( _mutex ); + _num++; + } + _newTicket.notify_one(); } void resize( int newSize ) { - scoped_lock lk( _mutex ); - int used = _outof - _num; - if ( used > newSize ) { - cout << "ERROR: can't resize since we're using (" << used << ") more than newSize(" << newSize << ")" << endl; - return; + { + scoped_lock lk( _mutex ); + + int used = _outof - _num; + if ( used > newSize ) { + cout << "ERROR: can't resize since we're using (" << used << ") more than newSize(" << newSize << ")" << endl; + return; + } + + _outof = newSize; + _num = _outof - used; } - _outof = newSize; - _num = _outof - used; + // Potentially wasteful, but easier to see is correct + _newTicket.notify_all(); } int available() const { @@ -362,9 +372,22 @@ namespace mongo { int outof() const { return _outof; } private: + + bool _tryAcquire(){ + if ( _num <= 0 ) { + if ( _num < 0 ) { + cerr << "DISASTER! in TicketHolder" << endl; + } + return false; + } + _num--; + return true; + } + int _outof; int _num; mongo::mutex _mutex; + boost::condition_variable_any _newTicket; }; class TicketHolderReleaser { |