Permalink
Browse files

made PeriodicTask more explicity

  • Loading branch information...
1 parent fc2f623 commit ec722585a9e6817a99b7929cc66b2815fa7d8349 @erh erh committed May 17, 2011
Showing with 48 additions and 15 deletions.
  1. +1 −1 s/d_writeback.cpp
  2. +2 −2 s/d_writeback.h
  3. +33 −9 util/background.cpp
  4. +12 −3 util/background.h
View
@@ -109,7 +109,7 @@ namespace mongo {
return false;
}
- void WriteBackManager::Cleaner::doWork() {
+ void WriteBackManager::Cleaner::taskDoWork() {
for ( int i=0; i<1000; i++ ) {
if ( ! writeBackManager.cleanupOldQueues() )
break;
View
@@ -93,8 +93,8 @@ namespace mongo {
class Cleaner : public PeriodicTask {
public:
- virtual string name() const { return "WriteBackManager::cleaner"; }
- virtual void doWork();
+ virtual string taskName() const { return "WriteBackManager::cleaner"; }
+ virtual void taskDoWork();
};
Cleaner _cleaner;
View
@@ -18,6 +18,7 @@
#include "pch.h"
#include "concurrency/mutex.h"
+#include "concurrency/spin_lock.h"
#include "background.h"
#include "time_support.h"
@@ -125,38 +126,61 @@ namespace mongo {
PeriodicTask::PeriodicTask() {
if ( ! theRunner )
theRunner = new Runner();
- theRunner->_tasks.push_back( this );
+ theRunner->add( this );
}
PeriodicTask::~PeriodicTask() {
+ 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;
+ }
+ }
}
void PeriodicTask::Runner::run() {
+ int sleeptime = 60;
+ DEV sleeptime = 5; // to catch race conditions
+
while ( ! inShutdown() ) {
- sleepsecs( 60 );
+ sleepsecs( sleeptime );
+
+ scoped_spinlock lk( _lock );
size_t size = _tasks.size();
for ( size_t i=0; i<size; i++ ) {
- if ( inShutdown() )
- break;
-
PeriodicTask * t = _tasks[i];
+ if ( ! t )
+ continue;
+ if ( inShutdown() )
+ break;
+
Timer timer;
try {
- t->doWork();
+ t->taskDoWork();
}
catch ( std::exception& e ) {
- error() << "task: " << t->name() << " failed: " << e.what() << endl;
+ error() << "task: " << t->taskName() << " failed: " << e.what() << endl;
}
catch ( ... ) {
- error() << "task: " << t->name() << " failed with unknown error" << endl;
+ error() << "task: " << t->taskName() << " failed with unknown error" << endl;
}
int ms = timer.millis();
- LOG( ms <= 3 ) << "task: " << t->name() << " took: " << ms << "ms" << endl;
+ LOG( ms <= 3 ) << "task: " << t->taskName() << " took: " << ms << "ms" << endl;
}
}
}
View
@@ -17,6 +17,8 @@
#pragma once
+#include "concurrency/spin_lock.h"
+
namespace mongo {
/**
@@ -117,8 +119,8 @@ namespace mongo {
PeriodicTask();
virtual ~PeriodicTask();
- virtual void doWork() = 0;
- virtual string name() const = 0;
+ virtual void taskDoWork() = 0;
+ virtual string taskName() const = 0;
class Runner : public BackgroundJob {
public:
@@ -127,13 +129,20 @@ namespace mongo {
virtual string name() const { return "PeriodicTask::Runner"; }
virtual void run();
+
+ void add( PeriodicTask* task );
+ void remove( PeriodicTask* task );
private:
+
+ SpinLock _lock;
+
// these are NOT owned by Runner
// Runner will not delete these
+ // this never gets smaller
+ // only fields replaced with nulls
vector<PeriodicTask*> _tasks;
- friend class PeriodicTask;
};
static Runner* theRunner;

0 comments on commit ec72258

Please sign in to comment.