Files
mongo/util/background.cpp

191 lines
5.6 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"
2011-05-17 14:22:40 -04:00
#include "concurrency/spin_lock.h"
#include "background.h"
2011-05-17 11:58:43 -04:00
#include "time_support.h"
#include "timer.h"
2011-02-13 13:33:43 -05:00
#include "mongoutils/str.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 {
2011-01-04 00:40:41 -05:00
JobStatus( bool delFlag )
: deleteSelf(delFlag), m("backgroundJob"), state(NotStarted) { }
2011-01-04 00:40:41 -05:00
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
2011-01-04 00:40:41 -05:00
void BackgroundJob::jobBody( boost::shared_ptr<JobStatus> status ) {
2011-02-13 23:28:53 -05:00
LOG(1) << "BackgroundJob starting: " << name() << endl;
2010-10-13 17:38:09 -04:00
{
scoped_lock l( status->m );
2011-02-13 13:33:43 -05:00
massert( 13643 , mongoutils::str::stream() << "backgroundjob already started: " << name() , status->state == NotStarted );
2010-10-13 17:38:09 -04:00
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
}
2011-01-04 00:40:41 -05: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
2011-01-04 00:40:41 -05:00
{
2010-10-13 17:38:09 -04:00
scoped_lock l( status->m );
status->state = Done;
status->finished.notify_all();
}
2011-01-04 00:40:41 -05:00
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 ) {
2011-03-17 12:34:53 -04:00
assert( !_status->deleteSelf ); // you cannot call wait on a self-deleting job
2010-10-13 17:38:09 -04:00
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);
2011-01-04 00:40:41 -05:00
}
else {
2010-10-13 17:38:09 -04:00
xt.sec += 1 + ns / 1000000000;
xt.nsec = ( ns + xt.nsec ) % 1000000000;
}
if ( ! _status->finished.timed_wait( l.boost() , xt ) )
return false;
2011-01-04 00:40:41 -05:00
}
else {
2010-10-13 17:38:09 -04:00
_status->finished.wait( l.boost() );
}
}
return true;
}
2009-01-14 17:09:51 -05:00
2011-01-04 00:40:41 -05:00
BackgroundJob::State BackgroundJob::getState() const {
scoped_lock l( _status->m);
return _status->state;
2010-05-07 15:35:16 -04:00
}
2011-01-04 00:40:41 -05:00
bool BackgroundJob::running() const {
2010-10-13 17:38:09 -04:00
scoped_lock l( _status->m);
2011-01-04 00:40:41 -05:00
return _status->state == Running;
2010-05-06 17:25:50 -04:00
}
2010-10-13 17:38:09 -04:00
2011-05-17 11:58:43 -04:00
// -------------------------
PeriodicTask::PeriodicTask() {
if ( ! theRunner )
theRunner = new Runner();
2011-05-17 14:22:40 -04:00
theRunner->add( this );
2011-05-17 11:58:43 -04:00
}
PeriodicTask::~PeriodicTask() {
2011-05-17 14:22:40 -04:00
theRunner->remove( this );
}
void PeriodicTask::Runner::add( PeriodicTask* task ) {
scoped_spinlock lk( _lock );
_tasks.push_back( task );
}
void PeriodicTask::Runner::remove( PeriodicTask* task ) {
scoped_spinlock lk( _lock );
for ( size_t i=0; i<_tasks.size(); i++ ) {
if ( _tasks[i] == task ) {
_tasks[i] = 0;
break;
}
}
2011-05-17 11:58:43 -04:00
}
void PeriodicTask::Runner::run() {
2011-05-17 14:22:40 -04:00
int sleeptime = 60;
DEV sleeptime = 5; // to catch race conditions
2011-05-17 11:58:43 -04:00
while ( ! inShutdown() ) {
2011-05-17 14:22:40 -04:00
sleepsecs( sleeptime );
scoped_spinlock lk( _lock );
2011-05-17 11:58:43 -04:00
size_t size = _tasks.size();
for ( size_t i=0; i<size; i++ ) {
PeriodicTask * t = _tasks[i];
2011-05-17 14:22:40 -04:00
if ( ! t )
continue;
2011-05-17 11:58:43 -04:00
2011-05-17 14:22:40 -04:00
if ( inShutdown() )
break;
2011-05-17 11:58:43 -04:00
Timer timer;
try {
2011-05-17 14:22:40 -04:00
t->taskDoWork();
2011-05-17 11:58:43 -04:00
}
catch ( std::exception& e ) {
2011-05-17 14:22:40 -04:00
error() << "task: " << t->taskName() << " failed: " << e.what() << endl;
2011-05-17 11:58:43 -04:00
}
catch ( ... ) {
2011-05-17 14:22:40 -04:00
error() << "task: " << t->taskName() << " failed with unknown error" << endl;
2011-05-17 11:58:43 -04:00
}
int ms = timer.millis();
2011-05-23 20:09:09 -04:00
LOG( ms <= 3 ? 1 : 0 ) << "task: " << t->taskName() << " took: " << ms << "ms" << endl;
2011-05-17 11:58:43 -04:00
}
}
}
PeriodicTask::Runner* PeriodicTask::theRunner = 0;
2009-01-14 17:09:51 -05:00
} // namespace mongo