MeVisLabToolboxReference
|
00001 // **InsertLicense** code 00002 //---------------------------------------------------------------------------------- 00009 //---------------------------------------------------------------------------------- 00010 00011 00012 #ifndef __mlBackgroundTask_H 00013 #define __mlBackgroundTask_H 00014 00015 00016 // Local includes 00017 #include "MLBackgroundTasksSystem.h" 00018 00019 #include "mlBackgroundTaskMessageQueue.h" 00020 #include "mlTimeCounter.h" 00021 00022 #include <boost/thread/mutex.hpp> 00023 #include <boost/thread/condition.hpp> 00024 00025 ML_START_NAMESPACE 00026 00027 //----------------------------------------------------------------------- 00028 class BackgroundTask; 00029 class BackgroundTaskMessage; 00030 class BackgroundTaskStatusInformation; 00031 00032 typedef void BackgroundTaskFinishedCB(void *data, BackgroundTask *task); 00033 00034 //----------------------------------------------------------------------- 00038 class MLBACKGROUNDTASKS_EXPORT BackgroundTask 00039 { 00040 ML_DISALLOW_COPY_AND_ASSIGN(BackgroundTask) 00041 00042 public: 00044 enum Status { 00045 NotInTaskManager, 00046 Queued, 00047 Running, 00048 Finished, 00049 Canceled, 00050 Suspended 00051 }; 00052 00055 BackgroundTask(void* owner); 00056 virtual ~BackgroundTask(); 00057 00058 //------------------------------------------------------ 00060 00061 //------------------------------------------------------ 00062 00064 virtual void run() = 0; 00065 00067 00068 //------------------------------------------------------ 00070 00071 //------------------------------------------------------ 00072 00074 bool isSynchronous() const { return _synchronous; } 00076 void setSynchronous(bool flag) { 00077 _synchronous = flag; 00078 } 00079 00081 void setSynchronousCancelField(NotifyField* field) { _synchronousCancelField = field; } 00082 00084 00085 //------------------------------------------------------ 00087 00088 //------------------------------------------------------ 00089 00091 Status status() const; 00092 00094 void cancel(); 00095 00097 bool isCanceled() const; 00098 00105 bool shouldStop(); 00106 00108 void resume(); 00109 00112 void suspend(); 00113 00115 std::string statusString() const; 00116 00118 virtual void setStatusString(const std::string& status); 00119 00121 float progress() const; 00122 00124 virtual void setProgress(float progress); 00125 00127 00128 //------------------------------------------------------ 00130 00131 //------------------------------------------------------ 00132 00134 void setOwnerWasDeleted(); 00135 00137 bool hasOwner() const; 00138 00140 void* owner() const; 00141 00143 00144 //------------------------------------------------------ 00146 00147 //------------------------------------------------------ 00148 00151 void waitAndExecuteNextMessage(); 00152 00155 void executeNextMessage(); 00156 00160 void sendMessageToGUI(BackgroundTaskMessage* message); 00161 00163 00164 //------------------------------------------------------ 00166 00167 //------------------------------------------------------ 00168 00170 void logMessage(const std::string& message); 00171 00173 bool isVerbose() const { return _verbose; } 00175 void setVerbose(bool flag) { 00176 _verbose = flag; 00177 } 00178 00180 00181 //------------------------------------------------------ 00183 00184 //------------------------------------------------------ 00186 double runningTime() const { return _runningTime; } 00187 00189 double idleTime() const { return _idleTime; } 00190 00192 void getStatusInformation(BackgroundTaskStatusInformation& info); 00193 00195 MLuint32 taskId() const { return _taskId; } 00196 00198 00199 //------------------------------------------------------ 00201 00202 //------------------------------------------------------ 00203 00206 void setTaskFinishedCB(BackgroundTaskFinishedCB* callback, void* userdata); 00207 00209 00214 class Observer 00215 { 00216 public: 00218 virtual ~Observer() { } 00219 00221 virtual void statusChanged(BackgroundTask* task, double elapsedSeconds, BackgroundTask::Status status) = 0; 00222 00224 virtual void statusStringChanged(BackgroundTask* task, double elapsedSeconds, const std::string& status) = 0; 00225 00227 virtual void logMessage(BackgroundTask* task, double elapsedSeconds, const std::string& message) = 0; 00228 }; 00229 00231 void addObserver(Observer* observer); 00233 void removeObserver(Observer* observer); 00234 00236 00237 protected: 00238 00240 void sendStatusChangedToObservers(); 00242 void sendStatusStringChangedToObservers(); 00244 void sendLogMessageToObservers(const std::string& msg); 00245 00247 void addIdleTime(double timeInSecs) { _idleTime += timeInSecs; } 00248 00249 private: 00250 friend class BackgroundTaskManager; 00251 friend struct BackgroundTaskManagerWorkerFunc; 00252 00254 void runTask(); 00255 00257 void aboutToRun(); 00258 00260 void aboutToFinish(); 00261 00264 BackgroundTaskMessage* waitForMessage(); 00265 00267 void putMessageIntoQueue(BackgroundTaskMessage* message); 00268 00270 void setStatus(Status status); 00271 00273 void setStatusIfNotCanceledOrSuspended(Status status); 00274 00276 void setStatusNoLock(Status status); 00277 00279 void runTaskFinishedCallback(); 00280 00281 Status _status; 00282 std::string _statusString; 00283 float _progress; 00284 mutable boost::mutex _statusMonitor; 00285 boost::condition _statusNoLongerSuspended; 00286 00287 void* _owner; 00288 00290 TimeCounter _performanceTimer; 00291 double _runningTime; 00292 double _idleTime; 00293 00295 BackgroundTaskFinishedCB* _taskFinishedCB; 00296 void* _taskFinishedCBUserData; 00297 00299 BackgroundTaskMessageQueue _messageQueue; 00300 00302 MLuint32 _taskId; 00303 00305 std::vector<Observer*> _observers; 00306 00308 bool _verbose; 00309 00311 bool _synchronous; 00312 00314 NotifyField* _synchronousCancelField; 00315 00316 }; 00317 00318 00319 ML_END_NAMESPACE 00320 00321 #endif 00322 00323