|
virtual | ~TaskRunner ()=default |
|
template<typename TaskType > |
bool | post_task (TaskLocation &&location, TaskType &&task) |
|
template<typename OwnerType , typename TaskType > |
bool | post_task (TaskLocation &&location, std::weak_ptr< OwnerType > weak_owner, TaskType &&task) |
|
template<typename TaskType , typename ReplyType > |
bool | post_task_with_reply (TaskLocation &&location, TaskType &&task, ReplyType &&reply) |
|
template<typename OwnerType , typename TaskType , typename ReplyType > |
bool | post_task_with_reply (TaskLocation &&location, std::weak_ptr< OwnerType > weak_owner, TaskType &&task, ReplyType &&reply) |
|
template<typename TaskType > |
bool | post_task_with_delay (TaskLocation &&location, std::chrono::milliseconds delay, TaskType &&task) |
|
template<typename OwnerType , typename TaskType > |
bool | post_task_with_delay (TaskLocation &&location, std::weak_ptr< OwnerType > weak_owner, std::chrono::milliseconds delay, TaskType &&task) |
|
template<typename TaskType , typename ReplyType > |
bool | post_task_with_delay_and_reply (TaskLocation &&location, std::chrono::milliseconds delay, TaskType &&task, ReplyType &&reply) |
|
template<typename OwnerType , typename TaskType , typename ReplyType > |
bool | post_task_with_delay_and_reply (TaskLocation &&location, std::weak_ptr< OwnerType > weak_owner, std::chrono::milliseconds delay, TaskType &&task, ReplyType &&reply) |
|
Base class for controlling the execution of tasks. Concrete task runners control the ordering and execution of tasks.
Tasks may generally be any callable type (lambda, std::function, etc.). Specific posting methods may place restrictions on the callable type, on either the return type of the invocation or the arguments the task accepts.
Tasks whose result is a non-void type may pass their result to a reply task. For example:
auto task = []() -> int
{
// Task body here.
return 12389;
};
auto reply = [](int task_result)
{
assert(task_result == 12389);
// Reply body here.
};
task_runner->post_task_with_reply(FROM_HERE, std::move(task), std::move(reply));
Tasks whose result is void may indicate their completion to a reply task. For example:
auto task = []()
{
// Task body here.
};
auto reply = []()
{
// Reply body here.
};
task_runner->post_task_with_reply(FROM_HERE, std::move(task), std::move(reply));
Mismatching of task result types and reply parameter types is explicitly forbidden at compile time. Tasks that return a non-void result must be paired with a reply which is invocable with only that type. Tasks which return void must be paired with a reply that is invocable without arguments.
Reply tasks are not executed immediately after a task is complete. Rather, they are posted for execution on the same task runner on which that task was posted.
Once a task is posted, it may be attempted to be cancelled in a number of ways:
Use one of the posting methods which accepts a weak pointer to the owner of the task. When the task is ready to be executed, if the weak pointer cannot be promoted to a strong pointer, the task is dropped. The task must accept a single argument, the shared pointer obtained from the weak pointer. For example:
auto task = [](std::shared_ptr<MyClass> self) { // Task body here. };
std::weak_ptr<MyClass> weak_self = shared_from_this(); task_runner->post_task(FROM_HERE, weak_self, std::move(task));
Reply tasks may be cancelled in the same manner. The reply task must then accept the result of the task and the shared pointer obtained from the weak pointer. If the task was dropped due to being unable to promote the weak pointer, the reply is also dropped. If the task was executed, when the reply is ready to be executed, if the weak pointer cannot be promoted to a strong pointer, the reply is dropped. For example:
auto task = [](std::shared_ptr<MyClass> self) -> int { // Task body here. return 12389; };
auto reply = [](int task_result, std::shared_ptr<MyClass> self) { assert(task_result == 12389); // Reply body here. };
std::weak_ptr<MyClass> weak_self = shared_from_this(); task_runner->post_task_with_reply(FROM_HERE, weak_self, std::move(task), std::move(reply));
- Deleting the task runner onto which the task was posted. This will only cancel the task if the task manager has not yet instructed the task runner to execute the task.
- Author
- Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
- Version
- August 12, 2018
template<typename OwnerType , typename TaskType , typename ReplyType >
bool fly::task::TaskRunner::post_task_with_delay_and_reply |
( |
TaskLocation && |
location, |
|
|
std::weak_ptr< OwnerType > |
weak_owner, |
|
|
std::chrono::milliseconds |
delay, |
|
|
TaskType && |
task, |
|
|
ReplyType && |
reply |
|
) |
| |
Schedule a task to be posted after a delay with protection by the provided weak pointer. The task may be any callable type which accepts a single argument, a locked shared pointer obtained from the weak pointer. When the task is ready to be executed, if the weak pointer fails to be locked, the task is dropped.
When the task has been executed, the reply task is then posted for execution on this same task runner with protection by the same weak pointer. The reply task may be any callable type that is invocable with the return type of the task (if non-void) and a locked shared pointer obtained from the weak pointer, or with only the locked shared pointer. When the reply is ready to be executed, if the weak pointer fails to be locked, the reply is dropped.
- Template Parameters
-
TaskType | Callable type of the task. |
ReplyType | Callable type of the reply. |
OwnerType | Type of the owner of the task. |
- Parameters
-
location | The location from which the task was posted (use FROM_HERE). |
weak_owner | A weak pointer to the owner of the task. |
delay | Delay before posting the task. |
task | The task to be executed. |
reply | The reply to be executed with the result of the task. |
- Returns
- True if the task was posted for execution.
template<typename OwnerType , typename TaskType , typename ReplyType >
bool fly::task::TaskRunner::post_task_with_reply |
( |
TaskLocation && |
location, |
|
|
std::weak_ptr< OwnerType > |
weak_owner, |
|
|
TaskType && |
task, |
|
|
ReplyType && |
reply |
|
) |
| |
Post a task for execution with protection by the provided weak pointer. The task may be any callable type which accepts a single argument, a locked shared pointer obtained from the weak pointer. When the task is ready to be executed, if the weak pointer fails to be locked, the task is dropped.
When the task has been executed, the reply task is then posted for execution on this same task runner with protection by the same weak pointer. The reply task may be any callable type that is invocable with the return type of the task (if non-void) and a locked shared pointer obtained from the weak pointer, or with only the locked shared pointer. When the reply is ready to be executed, if the weak pointer fails to be locked, the reply is dropped.
- Template Parameters
-
TaskType | Callable type of the task. |
ReplyType | Callable type of the reply. |
OwnerType | Type of the owner of the task. |
- Parameters
-
location | The location from which the task was posted (use FROM_HERE). |
weak_owner | A weak pointer to the owner of the task. |
task | The task to be executed. |
reply | The reply to be executed with the result of the task. |
- Returns
- True if the task was posted for execution.