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"
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
44ML_START_NAMESPACE
45
46class BackgroundTaskManagerPrivate;
47class BackgroundTaskMessage;
48class PageRequestProcessor;
49
51
53class MLBACKGROUNDTASKS_EXPORT BackgroundTaskManager
54{
55 ML_DISALLOW_COPY_AND_ASSIGN(BackgroundTaskManager)
56
57public:
59 static BackgroundTaskManager& self();
60
62 static void init();
63
65 static void cleanup();
66
71
75
77 void getStatusInformation(std::vector<BackgroundTaskStatusInformation>& infos);
78
82
86
88 static bool ensureGUIThread();
89
93 static bool ensureWorkerThread();
94
96 bool hasSynchronousTask() const { return _synchronousTaskRunning; }
97
101
104
107
109
112 {
113 public:
115 virtual ~Observer() { }
116
118 virtual void taskAdded(BackgroundTask* task) = 0;
120 virtual void taskRemoved(BackgroundTask* task) = 0;
121
123
127 virtual void taskStatusUpdated() = 0;
128 };
129
131 void addObserver(Observer* observer);
132
134 void removeObserver(Observer* observer);
135
138
148 void* userData);
149
153 void** userData);
154
158
159protected:
162 friend class BackgroundTask;
163
167
170
173
176
179
182
185
186private:
188 void createWorkerThreads();
189
192 void taskFinished(BackgroundTask* task);
193
195 bool removeTaskFromQueue(BackgroundTask* task);
196
197 BackgroundTaskManager();
198 ~BackgroundTaskManager();
200
201 static BackgroundTaskManager* _manager;
202
203 bool _synchronousTaskRunning;
204
205 std::vector<boost::thread*> _threads;
206
207 std::vector<BackgroundTask*> _tasks;
208
209 std::deque<BackgroundTask*> _taskQueue;
210 boost::condition _taskAvailable;
211 boost::mutex _taskQueueMonitor;
212
213 MLuint32 _uniqueTaskId;
214 boost::mutex _uniqueTaskIdMonitor;
215
216 std::vector<Observer*> _observers;
217
218 BackgroundTaskScheduleProcessPendingMessagesCB* _scheduleProcessPendingMessagesCB;
219 void* _scheduleProcessPendingMessagesCBUserData;
220
222
223};
224
225
226ML_END_NAMESPACE
227
228#endif
229
230
#define MLBACKGROUNDTASKS_EXPORT
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).
void removeObserver(Observer *observer)
Removes an observer (the ownership stays with the caller).
static void cleanup()
Cleans up the background task manager.
void sendTaskStatusUpdated()
Sends that a task's status is updated to the observers.
void sendTaskAdded(BackgroundTask *task)
Sends a task that is added to the observers.
void sendMessageToGUI(BackgroundTaskMessage *message)
BackgroundTaskHandle runTask(BackgroundTask *task)
static bool ensureWorkerThread()
void cancelPendingRequestsForTask(BackgroundTask *task)
Cancels pending requests for a given task.
void sendMessageToTask(BackgroundTask *task, BackgroundTaskMessage *message)
PageRequestProcessor & getPageRequestProcessor()
Returns instance of the page request processor (TODO: this will probably move into the ML host!...
static bool ensureGUIThread()
Returns true if the current thread is the GUI thread.
static BackgroundTaskManager & self()
Returns the global singleton.
static void init()
Initializes the background task manager.
void getStatusInformation(std::vector< BackgroundTaskStatusInformation > &infos)
Gets a snapshot of the state of all background tasks, representing the consistent status at the time ...
void removeMessagesForReceiver(BackgroundTaskMessageReceiver *receiver)
Removes all messages for receiver from the GUI message queue.
BackgroundTask * waitForTask()
Waits for a new task and returns the new task from the queue.
void getBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB **cb, void **userData)
Gets the currently set callback.
void addObserver(Observer *observer)
Adds an observer (the ownership stays with the caller).
void sendTaskRemoved(BackgroundTask *task)
Sends a task that is removed from the observers.
void removeTaskFromQueueAndSendTaskFinished(BackgroundTask *task)
Removes a task if it was queued.
void setBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB *cb, void *userData)
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.
The base class of all background messages.
Abstract base class for page request processors.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Definition mlMacros.h:21
unsigned int MLuint32
Definition mlTypeDefs.h:184
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)