Files
mongo/util/background.cpp

116 lines
3.5 KiB
C++
Raw Normal View History

2010-10-13 17:38:09 -04:00
// @file background.cpp
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2010-10-13 17:38:09 -04:00
#include "concurrency/mutex.h"
#include "background.h"
2009-01-14 17:09:51 -05:00
namespace mongo {
2010-10-13 17:38:09 -04:00
// both the BackgroundJob and the internal thread point to JobStatus
struct BackgroundJob::JobStatus {
JobStatus( bool delFlag )
: deleteSelf(delFlag), m("backgroundJob"), state(NotStarted) { }
2010-10-13 17:38:09 -04:00
const bool deleteSelf;
2010-10-13 17:38:09 -04:00
mongo::mutex m; // protects state below
boost::condition finished; // means _state == Done
State state;
};
2010-05-18 12:17:43 -04:00
2010-10-13 17:38:09 -04:00
BackgroundJob::BackgroundJob( bool selfDelete ) {
_status.reset( new JobStatus( selfDelete ) );
2010-10-13 17:38:09 -04:00
}
2010-10-13 17:43:57 -04:00
// Background object can be only be destroyed after jobBody() ran
2010-10-13 17:38:09 -04:00
void BackgroundJob::jobBody( boost::shared_ptr<JobStatus> status ){
{
scoped_lock l( status->m );
assert( status->state == NotStarted );
status->state = Running;
2010-05-18 12:17:43 -04:00
}
const string threadName = name();
if( ! threadName.empty() )
setThreadName( threadName.c_str() );
2010-10-13 17:38:09 -04:00
2010-05-06 17:25:50 -04:00
try {
run();
2010-05-06 17:25:50 -04:00
}
2010-07-28 16:03:32 -04:00
catch ( std::exception& e ){
log( LL_ERROR ) << "backgroundjob " << name() << "error: " << e.what() << endl;
2010-07-28 16:03:32 -04:00
}
2010-05-06 17:25:50 -04:00
catch(...) {
log( LL_ERROR ) << "uncaught exception in BackgroundJob " << name() << endl;
2010-05-06 17:25:50 -04:00
}
2010-10-13 17:38:09 -04:00
{
scoped_lock l( status->m );
status->state = Done;
status->finished.notify_all();
}
if( status->deleteSelf )
delete this;
}
BackgroundJob& BackgroundJob::go() {
2010-10-13 17:38:09 -04:00
boost::thread t( boost::bind( &BackgroundJob::jobBody , this, _status ) );
return *this;
}
2010-10-13 17:38:09 -04:00
bool BackgroundJob::wait( unsigned msTimeOut ) {
scoped_lock l( _status->m );
while ( _status->state != Done ) {
if ( msTimeOut ) {
// add msTimeOut millisecond to current time
boost::xtime xt;
boost::xtime_get( &xt, boost::TIME_UTC );
unsigned long long ns = msTimeOut * 1000000ULL; // milli to nano
2010-10-13 17:38:09 -04:00
if ( xt.nsec + ns < 1000000000 ) {
2010-10-15 15:50:41 -04:00
xt.nsec = (xtime::xtime_nsec_t) (xt.nsec + ns);
2010-10-13 17:38:09 -04:00
} else {
xt.sec += 1 + ns / 1000000000;
xt.nsec = ( ns + xt.nsec ) % 1000000000;
}
if ( ! _status->finished.timed_wait( l.boost() , xt ) )
return false;
} else {
_status->finished.wait( l.boost() );
}
}
return true;
}
2009-01-14 17:09:51 -05:00
2010-10-13 17:38:09 -04:00
BackgroundJob::State BackgroundJob::getState() const {
scoped_lock l( _status->m);
return _status->state;
2010-05-07 15:35:16 -04:00
}
2010-10-13 17:38:09 -04:00
bool BackgroundJob::running() const {
scoped_lock l( _status->m);
return _status->state == Running;
2010-05-06 17:25:50 -04:00
}
2010-10-13 17:38:09 -04:00
2009-01-14 17:09:51 -05:00
} // namespace mongo