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
45
46class BackgroundTaskManagerPrivate;
47class BackgroundTaskMessage;
48class PageRequestProcessor;
49
51
54{
56
57public:
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
132
135
138
147 BackgroundTaskScheduleProcessPendingMessagesCB* cb,
148 void* userData);
149
152 BackgroundTaskScheduleProcessPendingMessagesCB** cb,
153 void** userData);
154
158
159protected:
160 friend struct BackgroundTaskManagerWorkerFunc;
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
199 friend class BackgroundTaskManagerPrivate;
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
221 BackgroundTaskManagerPrivate* _private;
222
223};
224
225
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).
The BackgroundTaskManager is the central singleton that manages running background tasks.
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)
Sends the given message asynchronously to the GUI thread.
BackgroundTaskHandle runTask(BackgroundTask *task)
Runs the given task.
static bool ensureWorkerThread()
Returns true if the current thread is not the GUI thread and is thus a worker thread.
void cancelPendingRequestsForTask(BackgroundTask *task)
Cancels 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.
bool supportsAsynchronousTasks()
Returns whether asynchronous tasks are supported, which is the case if setBackgroundTaskScheduleProce...
PageRequestProcessor & getPageRequestProcessor()
Returns instance of the page request processor (TODO: this will probably move into the ML host!...
int totalTaskCount()
Returns the number of queued and running tasks.
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)
Sets the callback that will be called from any worker thread when a call of processPendingMessages() ...
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()
Returns new unique id of a task.
The background task receiver is a light-weight base class that 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
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned int MLuint32
Definition mlTypeDefs.h:185
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)