13 #ifndef ML_BACKGROUND_TASK_MANAGER_H
14 #define ML_BACKGROUND_TASK_MANAGER_H
26 #include <ThirdPartyWarningsDisable.h>
30 #include <ThirdPartyWarningsRestore.h>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
33 #include <boost/thread/mutex.hpp>
37 #define ML_ENSURE_GUITHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureGUIThread());
38 #define ML_ENSURE_WORKERTHREAD assert(ML_NAMESPACE::BackgroundTaskManager::ensureWorkerThread());
40 #define ML_ENSURE_GUITHREAD
41 #define ML_ENSURE_WORKERTHREAD
46 class BackgroundTaskManagerPrivate;
47 class BackgroundTaskMessage;
48 class PageRequestProcessor;
160 friend struct BackgroundTaskManagerWorkerFunc;
188 void createWorkerThreads();
199 friend class BackgroundTaskManagerPrivate;
203 bool _synchronousTaskRunning;
205 std::vector<boost::thread*> _threads;
207 std::vector<BackgroundTask*> _tasks;
209 std::deque<BackgroundTask*> _taskQueue;
210 boost::condition _taskAvailable;
211 boost::mutex _taskQueueMonitor;
214 boost::mutex _uniqueTaskIdMonitor;
216 std::vector<Observer*> _observers;
219 void* _scheduleProcessPendingMessagesCBUserData;
221 BackgroundTaskManagerPrivate* _private;
#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 ~Observer()
Virtual destructor.
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...
int totalTaskCount()
Returns the number of queued and running tasks.
static bool ensureGUIThread()
Returns true if the current thread is the GUI thread.
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.
PageRequestProcessor & getPageRequestProcessor()
Returns instance of the page request processor (TODO: this will probably move into the ML host!...
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.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)