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;
142 BackgroundTaskScheduleProcessPendingMessagesCB* cb,
147 BackgroundTaskScheduleProcessPendingMessagesCB** cb,
155 friend struct BackgroundTaskManagerWorkerFunc;
182 void createWorkerThreads();
192 friend class BackgroundTaskManagerPrivate;
196 bool _synchronousTaskRunning;
198 std::vector<boost::thread*> _threads;
200 std::vector<BackgroundTask*> _tasks;
202 std::deque<BackgroundTask*> _taskQueue;
203 boost::condition _taskAvailable;
204 boost::mutex _taskQueueMonitor;
207 boost::mutex _uniqueTaskIdMonitor;
209 std::vector<Observer*> _observers;
212 void* _scheduleProcessPendingMessagesCBUserData;
214 BackgroundTaskManagerPrivate* _private;
#define MLBACKGROUNDTASKS_EXPORT
Project global and OS specific declarations.
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)
remove an observer (the ownership stays with the caller)
static void cleanup()
cleanup the background task manager
void sendTaskStatusUpdated()
send task status updated to observers
void sendTaskAdded(BackgroundTask *task)
send task added to observers
void sendMessageToGUI(BackgroundTaskMessage *message)
sends the given message asynchronously to the to the GUI thread, the message ownership is passed to t...
BackgroundTaskHandle runTask(BackgroundTask *task)
run the given task, the ownership of the task is transfered to the task manager (do NOT delete the ta...
static bool ensureWorkerThread()
returns true if the current thread is not the GUI thread (and thus a worker thread) NOTE: in synchron...
void cancelPendingRequestsForTask(BackgroundTask *task)
cancel 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, the message ownership is pas...
bool supportsAsynchronousTasks()
Returns if asynchronous tasks are supported (which is the case if setBackgroundTaskScheduleProcessPen...
PageRequestProcessor & getPageRequestProcessor()
get 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 (only valid in GUI thread)
static bool ensureGUIThread()
returns true if the current thread is the GUI thread
static BackgroundTaskManager & self()
get the global singleton
static void init()
initialize the background task manager
void getStatusInformation(std::vector< BackgroundTaskStatusInformation > &infos)
get a snapshot of the state of all background tasks (which will represent that consistent status at t...
void removeMessagesForReceiver(BackgroundTaskMessageReceiver *receiver)
remove all messages for receiver from the GUI message queue.
BackgroundTask * waitForTask()
waits for a new task, returns the new task from the queue
void getBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB **cb, void **userData)
Get the currently set callback.
void addObserver(Observer *observer)
add an observer (the ownership stays with the caller)
void sendTaskRemoved(BackgroundTask *task)
send task removed to observers
void removeTaskFromQueueAndSendTaskFinished(BackgroundTask *task)
remove a task if it was queued
void setBackgroundTaskScheduleProcessPendingMessagesCB(BackgroundTaskScheduleProcessPendingMessagesCB *cb, void *userData)
Set the callback that will be called from any worker thread when a call of processPendingMessages() o...
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()
get new unique id of a task (threadsafe)
The background task receiver is a light-weight base class which 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.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void BackgroundTaskScheduleProcessPendingMessagesCB(void *data)