MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
78 void getStatusInformation(std::vector<BackgroundTaskStatusInformation>& infos);
79
83
87
89 static bool ensureGUIThread();
90
94 static bool ensureWorkerThread();
95
97 bool hasSynchronousTask() const
98 {
99 return _synchronousTaskRunning;
100 }
101
105
108
111
113
116 {
117 public:
119 virtual ~Observer()
120 {}
121
123 virtual void taskAdded(BackgroundTask* task) = 0;
125 virtual void taskRemoved(BackgroundTask* task) = 0;
126
128
132 virtual void taskStatusUpdated() = 0;
133 };
134
137
140
143
151 void setBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB* cb,
152 void* userData);
153
155 void getBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB** cb,
156 void** userData);
157
161
162protected:
163 friend struct BackgroundTaskManagerWorkerFunc;
165 friend class BackgroundTask;
166
170
173
176
179
182
185
188
189private:
191 void createWorkerThreads();
192
195 void taskFinished(BackgroundTask* task);
196
198 bool removeTaskFromQueue(BackgroundTask* task);
199
202 friend class BackgroundTaskManagerPrivate;
203
204 static BackgroundTaskManager* _manager;
205
206 bool _synchronousTaskRunning;
207
208 std::vector<boost::thread*> _threads;
209
210 std::vector<BackgroundTask*> _tasks;
211
212 std::deque<BackgroundTask*> _taskQueue;
213 boost::condition _taskAvailable;
214 boost::mutex _taskQueueMonitor;
215
216 MLuint32 _uniqueTaskId;
217 boost::mutex _uniqueTaskIdMonitor;
218
219 std::vector<Observer*> _observers;
220
221 BackgroundTaskScheduleProcessPendingMessagesCB* _scheduleProcessPendingMessagesCB;
222 void* _scheduleProcessPendingMessagesCBUserData;
223
224 BackgroundTaskManagerPrivate* _private;
225};
226
227
229
230#endif
#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:21
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned int MLuint32
Definition mlTypeDefs.h:185
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)