MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlBackgroundTask.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_H
14#define ML_BACKGROUND_TASK_H
15
16// Local includes
18
20#include <mlTimeCounter.h>
21
22#include <ThirdPartyWarningsDisable.h>
23#include <boost/thread/mutex.hpp>
24#include <boost/thread/condition.hpp>
25#include <ThirdPartyWarningsRestore.h>
26
28
29//-----------------------------------------------------------------------
30class NotifyField;
31class BackgroundTask;
32class BackgroundTaskMessage;
33class BackgroundTaskStatusInformation;
34
35typedef void BackgroundTaskFinishedCB(void* data, BackgroundTask* task);
36
37//-----------------------------------------------------------------------
42{
44
45public:
55
58 BackgroundTask(void* owner);
59 virtual ~BackgroundTask();
60
61 //------------------------------------------------------
64 //------------------------------------------------------
65
67 virtual void run() = 0;
68
70
71 //------------------------------------------------------
74 //------------------------------------------------------
75
77 bool isSynchronous() const
78 {
79 return _synchronous;
80 }
84 {
85 _synchronous = flag;
86 }
87
90 {
91 _synchronousCancelField = field;
92 }
93
95
96 //------------------------------------------------------
99 //------------------------------------------------------
100
103 Status status() const;
104
107 void cancel();
108
111 bool isCanceled() const;
112
120
124 void resume();
125
129 void suspend();
130
133 std::string statusString() const;
134
137 virtual void setStatusString(const std::string& status);
138
141 float progress() const;
142
145 virtual void setProgress(float progress);
146
148
149 //------------------------------------------------------
152 //------------------------------------------------------
153
157
160 bool hasOwner() const;
161
164 void* owner() const;
165
167
168 //------------------------------------------------------
171 //------------------------------------------------------
172
176
181
186
188
189 //------------------------------------------------------
192 //------------------------------------------------------
193
196 void logMessage(const std::string& message);
197
199 bool isVerbose() const
200 {
201 return _verbose;
202 }
205 void setVerbose(bool flag)
206 {
207 _verbose = flag;
208 }
209
211
212 //------------------------------------------------------
215 //------------------------------------------------------
218 double runningTime() const
219 {
220 return _runningTime;
221 }
222
225 double idleTime() const
226 {
227 return _idleTime;
228 }
229
233
236 {
237 return _taskId;
238 }
239
241
242 //------------------------------------------------------
245 //------------------------------------------------------
246
249 void setTaskFinishedCB(BackgroundTaskFinishedCB* callback, void* userdata);
250
252
258 {
259 public:
261 virtual ~Observer()
262 {}
263
266
268 virtual void statusStringChanged(BackgroundTask* task, double elapsedSeconds, const std::string& status) = 0;
269
271 virtual void logMessage(BackgroundTask* task, double elapsedSeconds, const std::string& message) = 0;
272 };
273
280
282
283protected:
289 void sendLogMessageToObservers(const std::string& msg);
290
293 {
294 _idleTime += timeInSecs;
295 }
296
297private:
299 friend struct BackgroundTaskManagerWorkerFunc;
300
302 void runTask();
303
305 void aboutToRun();
306
308 void aboutToFinish();
309
313 BackgroundTaskMessage* waitForMessage();
314
317 void putMessageIntoQueue(BackgroundTaskMessage* message);
318
320 void setStatus(Status status);
321
323 void setStatusIfNotCanceledOrSuspended(Status status);
324
326 void setStatusNoLock(Status status);
327
329 void runTaskFinishedCallback();
330
331 Status _status;
332 std::string _statusString;
333 float _progress;
334 mutable boost::mutex _statusMonitor;
335 boost::condition _statusNoLongerSuspended;
336
337 void* _owner;
338
340 TimeCounter _performanceTimer;
341 double _runningTime;
342 double _idleTime;
343
345 BackgroundTaskFinishedCB* _taskFinishedCB;
346 void* _taskFinishedCBUserData;
347
349 BackgroundTaskMessageQueue _messageQueue;
350
352 MLuint32 _taskId;
353
355 std::vector<Observer*> _observers;
356
358 bool _verbose;
359
361 bool _synchronous;
362
364 NotifyField* _synchronousCancelField;
365};
366
367
369
370#endif
#define MLBACKGROUNDTASKS_EXPORT
The BackgroundTaskManager is the central singleton that manages running background tasks.
The base class of all background messages.
BackgroundTaskStatusInformation creates a snapshot of the status of a given BackgroundTask to allow f...
Abstract interface to observe the BackgroundTask.
virtual void logMessage(BackgroundTask *task, double elapsedSeconds, const std::string &message)=0
Called when a task logs additional messages for verbose logging.
virtual ~Observer()
Virtual destructor.
virtual void statusStringChanged(BackgroundTask *task, double elapsedSeconds, const std::string &status)=0
Called when the status string has changed.
virtual void statusChanged(BackgroundTask *task, double elapsedSeconds, BackgroundTask::Status status)=0
Called when the status has changed.
Base class for a task that is started in an extra thread.
void setTaskFinishedCB(BackgroundTaskFinishedCB *callback, void *userdata)
Set this directly after the initialization of the task from the GUI thread.
void suspend()
Suspends the task.
void sendStatusStringChangedToObservers()
Sends the status string change to all observers.
void cancel()
Cancels the task (i.e., it sets status to canceled).
void setSynchronousCancelField(NotifyField *field)
Sets a cancel field that is used in synchronous mode to check whether cancel was pressed.
void removeObserver(Observer *observer)
Removes the given observer.
void resume()
Resumes the task after it was suspended.
virtual ~BackgroundTask()
void sendMessageToGUI(BackgroundTaskMessage *message)
Sends the message to the GUI.
BackgroundTask(void *owner)
Creates a new task with given owner.
void waitAndExecuteNextMessage()
Executes the next message.
void * owner() const
Returns the owner of the task Call this from GUI thread only!
void addObserver(Observer *observer)
Adds the given observer.
void setVerbose(bool flag)
Sets whether task uses verbose logging.
void getStatusInformation(BackgroundTaskStatusInformation &info)
Returns the complete status information.
bool shouldStop()
Returns whether the task should stop its work because it has been canceled.
std::string statusString() const
Returns current status string.
MLuint32 taskId() const
Returns the unique id of this task. The id of a task never changes.
void setSynchronous(bool flag)
Sets whether the task is handled synchronously.
void sendStatusChangedToObservers()
Sends the status change to all observers.
bool hasOwner() const
Returns whether the owner was deleted.
virtual void setProgress(float progress)
Updates the progress while processing.
void sendLogMessageToObservers(const std::string &msg)
Sends the status string change to all observers.
bool isVerbose() const
Checks whether task uses verbose logging.
bool isCanceled() const
Checks whether the task was canceled.
float progress() const
Returns current progress This is thread-safe.
Status status() const
Returns the status of the task.
double idleTime() const
Returns the idle time in seconds of the task (e.g., the time it waited for getTile to complete).
bool isSynchronous() const
Checks whether the task is synchronous, which means it is not executed in a different thread.
void setOwnerWasDeleted()
Tells the task that its owner was deleted and cancels the thread.
Status
Defines current status of the task.
void logMessage(const std::string &message)
Logs a message.
virtual void setStatusString(const std::string &status)
Updates the status string while processing.
virtual void run()=0
Method that needs to be reimplemented. This is called from the worker thread.
void addIdleTime(double timeInSecs)
Adds given timeInSecs as spent idle time.
bool executeNextMessage()
Executes the next message.
double runningTime() const
Returns the running time in seconds of the task.
Field without value for notifications.
Definition mlFields.h:687
Class to measure precise time intervals.
#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 BackgroundTaskFinishedCB(void *data, BackgroundTask *task)