00001 // @file background.h 00002 00003 /* Copyright 2009 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #pragma once 00019 00020 namespace mongo { 00021 00041 class BackgroundJob : boost::noncopyable { 00042 protected: 00049 explicit BackgroundJob(bool selfDelete = false); 00050 00051 virtual string name() const = 0; 00052 00063 virtual void run() = 0; 00064 00065 public: 00066 enum State { 00067 NotStarted, 00068 Running, 00069 Done 00070 }; 00071 00072 virtual ~BackgroundJob() { } 00073 00081 BackgroundJob& go(); 00082 00092 bool wait( unsigned msTimeOut = 0 ); 00093 00094 // accessors 00095 State getState() const; 00096 bool running() const; 00097 00098 private: 00099 struct JobStatus; 00100 boost::shared_ptr<JobStatus> _status; // shared between 'this' and body() thread 00101 00102 void jobBody( boost::shared_ptr<JobStatus> status ); 00103 00104 }; 00105 00106 } // namespace mongo