MeVisLab Toolbox Reference
mlBackgroundTaskManager.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2009, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_BACKGROUND_TASK_MANAGER_H
14 #define ML_BACKGROUND_TASK_MANAGER_H
15 
16 // Local includes
18 
19 // ML includes
20 #include <mlModuleIncludes.h>
21 
22 #include "mlBackgroundTask.h"
24 #include "mlBackgroundTaskHandle.h"
25 
26 #include <ThirdPartyWarningsDisable.h>
27 #include <vector>
28 #include <deque>
29 
30 #include <ThirdPartyWarningsRestore.h>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <cassert>
35 
36 #ifdef _DEBUG
37 #define ML_ENSURE_GUITHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureGUIThread());
38 #define ML_ENSURE_WORKERTHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureWorkerThread());
39 #else
40 #define ML_ENSURE_GUITHREAD
41 #define ML_ENSURE_WORKERTHREAD
42 #endif
43 
44 ML_START_NAMESPACE
45 
46 class BackgroundTaskManagerPrivate;
47 class BackgroundTaskMessage;
48 class PageRequestProcessor;
49 
51 
54 {
56 
57 public:
59  static BackgroundTaskManager& self();
60 
62  static void init();
63 
65  static void cleanup();
66 
69 
72 
74  void getStatusInformation(std::vector<BackgroundTaskStatusInformation>& infos);
75 
78 
81 
83  static bool ensureGUIThread();
84 
88  static bool ensureWorkerThread();
89 
91  bool hasSynchronousTask() const { return _synchronousTaskRunning; }
92 
96 
99 
102 
104 
106  class Observer
107  {
108  public:
110  virtual ~Observer() { }
111 
113  virtual void taskAdded(BackgroundTask* task) = 0;
115  virtual void taskRemoved(BackgroundTask* task) = 0;
116 
118 
122  virtual void taskStatusUpdated() = 0;
123  };
124 
126  void addObserver(Observer* observer);
127 
129  void removeObserver(Observer* observer);
130 
133 
143  void* userData);
144 
148  void** userData);
149 
153 
154 protected:
155  friend struct BackgroundTaskManagerWorkerFunc;
157  friend class BackgroundTask;
158 
161 
164 
167 
170 
173 
176 
179 
180 private:
182  void createWorkerThreads();
183 
185  void taskFinished(BackgroundTask* task);
186 
188  bool removeTaskFromQueue(BackgroundTask* task);
189 
192  friend class BackgroundTaskManagerPrivate;
193 
194  static BackgroundTaskManager* _manager;
195 
196  bool _synchronousTaskRunning;
197 
198  std::vector<boost::thread*> _threads;
199 
200  std::vector<BackgroundTask*> _tasks;
201 
202  std::deque<BackgroundTask*> _taskQueue;
203  boost::condition _taskAvailable;
204  boost::mutex _taskQueueMonitor;
205 
206  MLuint32 _uniqueTaskId;
207  boost::mutex _uniqueTaskIdMonitor;
208 
209  std::vector<Observer*> _observers;
210 
211  BackgroundTaskScheduleProcessPendingMessagesCB* _scheduleProcessPendingMessagesCB;
212  void* _scheduleProcessPendingMessagesCBUserData;
213 
214  BackgroundTaskManagerPrivate* _private;
215 
216 };
217 
218 
219 ML_END_NAMESPACE
220 
221 #endif
222 
223 
#define MLBACKGROUNDTASKS_EXPORT
Project global and OS specific declarations.
A handle to a task that has been created by the background task manager.
abstract interface to observe the BackgroundTaskManager
virtual void taskRemoved(BackgroundTask *task)=0
called when a task was removed (either finished or canceled or not even started)
virtual void taskStatusUpdated()=0
called in regular intervals if there are running tasks and if the tasks change status
virtual void taskAdded(BackgroundTask *task)=0
called when a task was added (but is not yet started)
the BackgroundTaskManager is the central singleton that manages running background tasks
void removeObserver(Observer *observer)
remove an observer (the ownership stays with the caller)
static void cleanup()
cleanup the background task manager
void sendTaskStatusUpdated()
send task status updated to observers
void sendTaskAdded(BackgroundTask *task)
send task added to observers
void sendMessageToGUI(BackgroundTaskMessage *message)
sends the given message asynchronously to the to the GUI thread, the message ownership is passed to t...
BackgroundTaskHandle runTask(BackgroundTask *task)
run the given task, the ownership of the task is transfered to the task manager (do NOT delete the ta...
static bool ensureWorkerThread()
returns true if the current thread is not the GUI thread (and thus a worker thread) NOTE: in synchron...
void cancelPendingRequestsForTask(BackgroundTask *task)
cancel pending requests for a given task
bool processPendingMessages()
processes all pending messages (will not block and may only be called from the GUI thread,...
void sendMessageToTask(BackgroundTask *task, BackgroundTaskMessage *message)
sends the given message asynchronously to the worker thread of the task, the message ownership is pas...
bool supportsAsynchronousTasks()
Returns if asynchronous tasks are supported (which is the case if setBackgroundTaskScheduleProcessPen...
int totalTaskCount()
returns the number of queued and running tasks (only valid in GUI thread)
static bool ensureGUIThread()
returns true if the current thread is the GUI thread
static void init()
initialize the background task manager
void getStatusInformation(std::vector< BackgroundTaskStatusInformation > &infos)
get a snapshot of the state of all background tasks (which will represent that consistent status at t...
void removeMessagesForReceiver(BackgroundTaskMessageReceiver *receiver)
remove all messages for receiver from the GUI message queue.
BackgroundTask * waitForTask()
waits for a new task, returns the new task from the queue
void getBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB **cb, void **userData)
Get the currently set callback.
PageRequestProcessor & getPageRequestProcessor()
get instance of the page request processor (TODO: this will probably move into the ML host!...
void addObserver(Observer *observer)
add an observer (the ownership stays with the caller)
void sendTaskRemoved(BackgroundTask *task)
send task removed to observers
void removeTaskFromQueueAndSendTaskFinished(BackgroundTask *task)
remove a task if it was queued
void setBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB *cb, void *userData)
Set the callback that will be called from any worker thread when a call of processPendingMessages() o...
void sendTaskFinishedMessage(BackgroundTask *task)
called by worker threads when a task is finished
bool hasSynchronousTask() const
returns true if the manager is currently processing a synchronous task
MLuint32 getUniqueTaskId()
get new unique id of a task (threadsafe)
The background task receiver is a light-weight base class which can be inherited (e....
The base class of all background messages.
Base class for a task that is started in an extra thread.
Abstract base class for page request processors.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Definition: mlMacros.h:23
unsigned int MLuint32
Definition: mlTypeDefs.h:191
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)