diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-08-11 12:38:57 +0200 |
commit | 7645618fd3914cb8a20561625913c20d49504a49 (patch) | |
tree | 8370f846f58f6d71165b7a0e2eda04648584ec76 /util/background.cpp | |
parent | 68c73c3c7608b4c87f07440dc3232801720b1168 (diff) | |
download | mongodb-7645618fd3914cb8a20561625913c20d49504a49.tar.gz |
Imported Upstream version 1.6.0
Diffstat (limited to 'util/background.cpp')
-rw-r--r-- | util/background.cpp | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/util/background.cpp b/util/background.cpp index 4125315..a6d8290 100644 --- a/util/background.cpp +++ b/util/background.cpp @@ -15,14 +15,15 @@ * limitations under the License. */ -#include "stdafx.h" +#include "pch.h" #include "goodies.h" #include "background.h" +#include <list> namespace mongo { BackgroundJob *BackgroundJob::grab = 0; - mongo::mutex BackgroundJob::mutex; + mongo::mutex BackgroundJob::mutex("BackgroundJob"); /* static */ void BackgroundJob::thr() { @@ -31,9 +32,25 @@ namespace mongo { assert( us->state == NotStarted ); us->state = Running; grab = 0; - us->run(); + + { + string nm = us->name(); + setThreadName(nm.c_str()); + } + + try { + us->run(); + } + catch ( std::exception& e ){ + log( LL_ERROR ) << "backgroundjob error: " << e.what() << endl; + } + catch(...) { + log( LL_ERROR ) << "uncaught exception in BackgroundJob" << endl; + } us->state = Done; - if ( us->deleteSelf ) + bool delSelf = us->deleteSelf; + us->ending(); + if( delSelf ) delete us; } @@ -47,18 +64,56 @@ namespace mongo { return *this; } - bool BackgroundJob::wait(int msMax) { + bool BackgroundJob::wait(int msMax, unsigned maxsleep) { assert( state != NotStarted ); - int ms = 1; + unsigned ms = 0; Date_t start = jsTime(); while ( state != Done ) { sleepmillis(ms); - if ( ms < 1000 ) - ms = ms * 2; + if( ms*2<maxsleep ) ms*=2; if ( msMax && ( int( jsTime() - start ) > msMax) ) return false; } return true; } + void BackgroundJob::go(list<BackgroundJob*>& L) { + for( list<BackgroundJob*>::iterator i = L.begin(); i != L.end(); i++ ) + (*i)->go(); + } + + /* wait for several jobs to finish. */ + void BackgroundJob::wait(list<BackgroundJob*>& L, unsigned maxsleep) { + unsigned ms = 0; + { + x: + sleepmillis(ms); + if( ms*2<maxsleep ) ms*=2; + for( list<BackgroundJob*>::iterator i = L.begin(); i != L.end(); i++ ) { + assert( (*i)->state != NotStarted ); + if( (*i)->state != Done ) + goto x; + } + } + } + + void PeriodicBackgroundJob::run(){ + // want to handle first one differently so inShutdown is obeyed nicely + sleepmillis( _millis ); + + while ( ! inShutdown() ){ + try { + runLoop(); + } + catch ( std::exception& e ){ + log( LL_ERROR ) << "PeriodicBackgroundJob [" << name() << "] error: " << e.what() << endl; + } + catch ( ... ){ + log( LL_ERROR ) << "PeriodicBackgroundJob [" << name() << "] unknown error" << endl; + } + + sleepmillis( _millis ); + } + } + } // namespace mongo |