MeVisLabToolboxReference
MeVis/Foundation/Sources/MLBackgroundTasks/mlBackgroundTaskManager.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00009 //----------------------------------------------------------------------------------
00010 
00011 
00012 #ifndef __mlBackgroundTaskManager_H
00013 #define __mlBackgroundTaskManager_H
00014 
00015 
00016 // Local includes
00017 #include "MLBackgroundTasksSystem.h"
00018 
00019 // ML includes
00020 #include "mlModuleIncludes.h"
00021 
00022 #include "mlBackgroundTask.h"
00023 #include "mlBackgroundTaskMessageQueue.h"
00024 #include "mlBackgroundTaskHandle.h"
00025 
00026 #include <vector>
00027 #include <deque>
00028 
00029 #include <boost/thread.hpp>
00030 #include <boost/thread/condition.hpp>
00031 #include <boost/thread/mutex.hpp>
00032 #include <assert.h>
00033 
00034 #ifdef _DEBUG
00035 #define ML_ENSURE_GUITHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureGUIThread());
00036 #define ML_ENSURE_WORKERTHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureWorkerThread());
00037 #else
00038 #define ML_ENSURE_GUITHREAD
00039 #define ML_ENSURE_WORKERTHREAD
00040 #endif
00041 
00042 ML_START_NAMESPACE
00043 
00044 class BackgroundTaskManagerPrivate;
00045 class BackgroundTaskMessage;
00046 class PageRequestProcessor;
00047 
00048 typedef void BackgroundTaskScheduleProcessPendingMessagesCB(void *data);
00049 
00051 class MLBACKGROUNDTASKS_EXPORT BackgroundTaskManager
00052 {
00053   ML_DISALLOW_COPY_AND_ASSIGN(BackgroundTaskManager)
00054 
00055 public:
00057   static BackgroundTaskManager& self();
00058 
00060   static void init();
00061 
00063   static void cleanup();
00064 
00066   BackgroundTaskHandle runTask(BackgroundTask* task);
00067 
00069   int totalTaskCount();
00070 
00072   void getStatusInformation(std::vector<BackgroundTaskStatusInformation>& infos);
00073 
00075   void sendMessageToGUI(BackgroundTaskMessage* message);
00076 
00078   void sendMessageToTask(BackgroundTask* task, BackgroundTaskMessage* message);
00079 
00081   static bool ensureGUIThread();
00082 
00086   static bool ensureWorkerThread();
00087 
00089   bool hasSynchronousTask() const { return _synchronousTaskRunning; }
00090 
00093   bool processPendingMessages();
00094 
00096   void cancelPendingRequestsForTask(BackgroundTask* task);
00097 
00099 
00101   class Observer
00102   {
00103   public:
00105     virtual ~Observer() { }
00106 
00108     virtual void taskAdded(BackgroundTask* task) = 0;
00110     virtual void taskRemoved(BackgroundTask* task) = 0;
00111 
00113 
00117     virtual void taskStatusUpdated() = 0;
00118   };
00119 
00121   void addObserver(Observer* observer);
00122   
00124   void removeObserver(Observer* observer);
00125 
00127   PageRequestProcessor& getPageRequestProcessor();
00128 
00136   void setBackgroundTaskScheduleProcessPendingMessagesCB(
00137     BackgroundTaskScheduleProcessPendingMessagesCB* cb, 
00138     void* userData);
00139 
00141   void getBackgroundTaskScheduleProcessPendingMessagesCB(
00142     BackgroundTaskScheduleProcessPendingMessagesCB** cb, 
00143     void** userData);
00144 
00147   bool supportsAsynchronousTasks();
00148 
00149 protected:
00150   friend struct BackgroundTaskManagerWorkerFunc;
00151   friend class  BackgroundTaskFinishedMessage;
00152   friend class  BackgroundTask;
00153 
00155   MLuint32 getUniqueTaskId();
00156 
00158   void sendTaskAdded(BackgroundTask* task);
00159 
00161   void sendTaskRemoved(BackgroundTask* task);
00162 
00164   void sendTaskStatusUpdated();
00165 
00167   void removeTaskFromQueueAndSendTaskFinished(BackgroundTask* task);
00168 
00170   BackgroundTask* waitForTask();
00171 
00173   void sendTaskFinishedMessage(BackgroundTask* task);
00174 
00175 private:
00177   void createWorkerThreads();
00178 
00180   void taskFinished(BackgroundTask* task);
00181 
00183   bool removeTaskFromQueue(BackgroundTask* task);
00184 
00185   BackgroundTaskManager();
00186   ~BackgroundTaskManager();
00187   friend class BackgroundTaskManagerPrivate;
00188 
00189   static BackgroundTaskManager* _manager;
00190 
00191   bool                          _synchronousTaskRunning;
00192 
00193   std::vector<boost::thread*>   _threads;
00194 
00195   std::vector<BackgroundTask*>  _tasks;
00196 
00197   std::deque<BackgroundTask*>   _taskQueue;
00198   boost::condition              _taskAvailable;
00199   boost::mutex                  _taskQueueMonitor;
00200   
00201   MLuint32                      _uniqueTaskId;
00202   boost::mutex                  _uniqueTaskIdMonitor;
00203 
00204   std::vector<Observer*> _observers;
00205 
00206   BackgroundTaskScheduleProcessPendingMessagesCB* _scheduleProcessPendingMessagesCB;
00207   void*                                       _scheduleProcessPendingMessagesCBUserData;
00208 
00209   BackgroundTaskManagerPrivate* _private;
00210 
00211 };
00212 
00213 
00214 ML_END_NAMESPACE
00215 
00216 #endif
00217 
00218