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
46class BackgroundTaskManagerPrivate;
47class BackgroundTaskMessage;
48class PageRequestProcessor;
59 static BackgroundTaskManager&
self();
188 void createWorkerThreads();
197 BackgroundTaskManager();
198 ~BackgroundTaskManager();
201 static BackgroundTaskManager* _manager;
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;
#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).
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)
BackgroundTaskHandle runTask(BackgroundTask *task)
static bool ensureWorkerThread()
void cancelPendingRequestsForTask(BackgroundTask *task)
Cancels pending requests for a given task.
bool processPendingMessages()
void sendMessageToTask(BackgroundTask *task, BackgroundTaskMessage *message)
bool supportsAsynchronousTasks()
friend struct BackgroundTaskManagerWorkerFunc
PageRequestProcessor & getPageRequestProcessor()
Returns instance of the page request processor (TODO: this will probably move into the ML host!...
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.
friend class BackgroundTaskFinishedMessage
void setBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB *cb, void *userData)
void sendTaskFinishedMessage(BackgroundTask *task)
Called by worker threads when a task is finished.
friend class BackgroundTask
bool hasSynchronousTask() const
Returns true if the manager is currently processing a synchronous task.
MLuint32 getUniqueTaskId()
friend class BackgroundTaskManagerPrivate
The base class of all background messages.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)