00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include "concurrency/spin_lock.h"
00021
00022 namespace mongo {
00023
00043 class BackgroundJob : boost::noncopyable {
00044 protected:
00051 explicit BackgroundJob(bool selfDelete = false);
00052
00053 virtual string name() const = 0;
00054
00065 virtual void run() = 0;
00066
00067 public:
00068 enum State {
00069 NotStarted,
00070 Running,
00071 Done
00072 };
00073
00074 virtual ~BackgroundJob() { }
00075
00083 BackgroundJob& go();
00084
00094 bool wait( unsigned msTimeOut = 0 );
00095
00096
00097 State getState() const;
00098 bool running() const;
00099
00100 private:
00101 struct JobStatus;
00102 boost::shared_ptr<JobStatus> _status;
00103
00104 void jobBody( boost::shared_ptr<JobStatus> status );
00105
00106 };
00107
00117 class PeriodicTask {
00118 public:
00119 PeriodicTask();
00120 virtual ~PeriodicTask();
00121
00122 virtual void taskDoWork() = 0;
00123 virtual string taskName() const = 0;
00124
00125 class Runner : public BackgroundJob {
00126 public:
00127 virtual ~Runner(){}
00128
00129 virtual string name() const { return "PeriodicTask::Runner"; }
00130
00131 virtual void run();
00132
00133 void add( PeriodicTask* task );
00134 void remove( PeriodicTask* task );
00135
00136 private:
00137
00138 SpinLock _lock;
00139
00140
00141
00142
00143
00144 vector<PeriodicTask*> _tasks;
00145
00146 };
00147
00148 static Runner* theRunner;
00149
00150 };
00151
00152
00153
00154
00155 }