MeVisLab Toolbox Reference
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 { return _synchronous; }
79 void setSynchronous(bool flag) {
80 _synchronous = flag;
81 }
82
84 void setSynchronousCancelField(NotifyField* field) { _synchronousCancelField = field; }
85
87
88 //------------------------------------------------------
91 //------------------------------------------------------
92
95 Status status() const;
96
99 void cancel();
100
103 bool isCanceled() const;
104
112
116 void resume();
117
121 void suspend();
122
125 std::string statusString() const;
126
129 virtual void setStatusString(const std::string& status);
130
133 float progress() const;
134
137 virtual void setProgress(float progress);
138
140
141 //------------------------------------------------------
144 //------------------------------------------------------
145
149
152 bool hasOwner() const;
153
156 void* owner() const;
157
159
160 //------------------------------------------------------
163 //------------------------------------------------------
164
168
173
178
180
181 //------------------------------------------------------
184 //------------------------------------------------------
185
188 void logMessage(const std::string& message);
189
191 bool isVerbose() const { return _verbose; }
194 void setVerbose(bool flag) {
195 _verbose = flag;
196 }
197
199
200 //------------------------------------------------------
203 //------------------------------------------------------
206 double runningTime() const { return _runningTime; }
207
210 double idleTime() const { return _idleTime; }
211
215
217 MLuint32 taskId() const { return _taskId; }
218
220
221 //------------------------------------------------------
224 //------------------------------------------------------
225
228 void setTaskFinishedCB(BackgroundTaskFinishedCB* callback, void* userdata);
229
231
237 {
238 public:
240 virtual ~Observer() { }
241
244
246 virtual void statusStringChanged(BackgroundTask* task, double elapsedSeconds, const std::string& status) = 0;
247
249 virtual void logMessage(BackgroundTask* task, double elapsedSeconds, const std::string& message) = 0;
250 };
251
258
260
261protected:
262
268 void sendLogMessageToObservers(const std::string& msg);
269
271 void addIdleTime(double timeInSecs) { _idleTime += timeInSecs; }
272
273private:
275 friend struct BackgroundTaskManagerWorkerFunc;
276
278 void runTask();
279
281 void aboutToRun();
282
284 void aboutToFinish();
285
289 BackgroundTaskMessage* waitForMessage();
290
293 void putMessageIntoQueue(BackgroundTaskMessage* message);
294
296 void setStatus(Status status);
297
299 void setStatusIfNotCanceledOrSuspended(Status status);
300
302 void setStatusNoLock(Status status);
303
305 void runTaskFinishedCallback();
306
307 Status _status;
308 std::string _statusString;
309 float _progress;
310 mutable boost::mutex _statusMonitor;
311 boost::condition _statusNoLongerSuspended;
312
313 void* _owner;
314
316 TimeCounter _performanceTimer;
317 double _runningTime;
318 double _idleTime;
319
321 BackgroundTaskFinishedCB* _taskFinishedCB;
322 void* _taskFinishedCBUserData;
323
325 BackgroundTaskMessageQueue _messageQueue;
326
328 MLuint32 _taskId;
329
331 std::vector<Observer*> _observers;
332
334 bool _verbose;
335
337 bool _synchronous;
338
340 NotifyField* _synchronousCancelField;
341
342};
343
344
346
347#endif
348
349
#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. Call this only in the setup of the task,...
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:598
Class to measure precise time intervals.
#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 BackgroundTaskFinishedCB(void *data, BackgroundTask *task)