Task is an interface for tasks that can be executed within BGThread. More...
#include <Radiant/Task.hpp>
Public Types | |
| enum | { PRIORITY_LOW = 1, PRIORITY_NORMAL = 500, PRIORITY_HIGH = 1000, PRIORITY_URGENT = 1500 } |
| Standard priorities for tasks. More... | |
| enum | State { WAITING, RUNNING, DONE } |
| State of the task. More... | |
Public Member Functions | |
| virtual void | doTask ()=0 |
| The actual work the task does should be implemented in here. More... | |
| bool | isCanceled () const |
| Returns whether a task has been canceled. More... | |
| Priority | priority () const |
| Get the current priority of the task. More... | |
| void | runNow (bool finish) |
| If the task isn't already finished, runs the task immediately in the calling thread. More... | |
| void | schedule (Radiant::TimeStamp ts) |
| Schedule the next execution time for this task. More... | |
| Radiant::TimeStamp | scheduled () const |
| Return a timestamp for the next execution of the task. More... | |
| void | scheduleFromNow (Radiant::TimeStamp wait) |
| Schedule the next execution time for this task. More... | |
| void | scheduleFromNowSecs (double seconds) |
| Schedule the next execution time for this task. More... | |
| void | setCanceled () |
| Marks the task as canceled, so it will be removed. | |
| void | setFinished () |
| Marks the task as finished, so it will be removed. | |
| void | setPriority (Priority priority) |
| Sets priority for task. More... | |
| State | state () const |
| Get the current state of the task. More... | |
| Task (Priority p=PRIORITY_NORMAL) | |
| Constructs a task with the given priority. More... | |
| virtual | ~Task () |
| Destructor. | |
Protected Member Functions | |
| virtual void | canceled () |
| Cancel the task. More... | |
| virtual void | finished () |
| Finished the task. More... | |
| virtual void | initialize () |
| Initialize the task. Called by BGThread before the task is processed. | |
| void | setState (State s) |
| Sets the task state. More... | |
Friends | |
| class | BGThread |
Task is an interface for tasks that can be executed within BGThread.
The purpose of Task is to make it easy to move time-consuing operations away from the main thread of the application. Tasks are placed in the Radiant::BGThread, that schedules and runs them as specified.
Typical operations that can be implemented with tasks are:
Please note that Tasks are expected to execute fast. In other words, a task should not perform long, blocking operations. Such operations (for example database queries, or network file transfers), are best handled by launching a separate thread for them. For this purpose, see Radiant::Thread.
If you implement tasks that take a long time to execute, you should check the task state periodically inside you Task::doTask function and return from the method if the task is set to Task::DONE state. Otherwise your application may stall for a while when closing down because the application will wait for any tasks to finish before it stops the BGThread running them.
| anonymous enum |
| enum Radiant::Task::State |
| Radiant::Task::Task | ( | Priority | p = PRIORITY_NORMAL | ) |
Constructs a task with the given priority.
| p | Priority for task |
|
protectedvirtual |
Cancel the task.
Called by BGThread after the task has been canceled
|
pure virtual |
The actual work the task does should be implemented in here.
Override in the derived class. When the task is finished it is important to set the State of the task to State::DONE (f.ex. calling setFinished) so that BGThread can properly release the task. If the state of the task is not set to State::DONE after call to this function this task is scheduled to be executed later.
Implemented in Radiant::SingleShotTask, Radiant::FunctionTask, and Luminous::MipMapGenerator.
|
protectedvirtual |
Finished the task.
Called by BGThread after the task has been processed
|
inline |
Returns whether a task has been canceled.
|
inline |
Get the current priority of the task.
| void Radiant::Task::runNow | ( | bool | finish | ) |
If the task isn't already finished, runs the task immediately in the calling thread.
If the task is running in a background thread, waits until the task is released. Will call initialize() and finished() -functions if necessary. It's fine to call this function either before or after the task is added to BGThread, but this shouldn't be called at the same time as BGThread::addTask.
| finish | run doTask until the task is in DONE state, otherwise just run the task once. |
|
inline |
Schedule the next execution time for this task.
| ts | Time when the task is next executed |
|
inline |
Return a timestamp for the next execution of the task.
|
inline |
Schedule the next execution time for this task.
| wait | How long to wait before the execution of the task |
|
inline |
Schedule the next execution time for this task.
| seconds | number of seconds before next execution |
|
inline |
Sets priority for task.
| priority | Priority of the task |
|
inlineprotected |
Sets the task state.
| s | State to set |
|
inline |
Get the current state of the task.