From 881ebccdc4a0995732aea2052f85015aebe6f9a2 Mon Sep 17 00:00:00 2001 From: Naios Date: Sun, 21 Jun 2015 18:04:47 +0200 Subject: Core/Utilities: TaskScheduler: add a task validator and on update success callback. * makes it possible to block tasks if there is an active spellcast. * requested by @joschiwald (cherry picked from commit 3d95aba2b3d58057f3eeed6d4dd6e51ea49b77f6) --- src/server/shared/Utilities/TaskScheduler.cpp | 34 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/server/shared/Utilities/TaskScheduler.cpp') diff --git a/src/server/shared/Utilities/TaskScheduler.cpp b/src/server/shared/Utilities/TaskScheduler.cpp index 4b261413fd9..04a4071d1f5 100644 --- a/src/server/shared/Utilities/TaskScheduler.cpp +++ b/src/server/shared/Utilities/TaskScheduler.cpp @@ -17,16 +17,22 @@ #include "TaskScheduler.h" -TaskScheduler& TaskScheduler::Update() +TaskScheduler& TaskScheduler::ClearValidator() +{ + _predicate = EmptyValidator; + return *this; +} + +TaskScheduler& TaskScheduler::Update(success_t const& callback) { _now = clock_t::now(); - Dispatch(); + Dispatch(callback); return *this; } -TaskScheduler& TaskScheduler::Update(size_t const milliseconds) +TaskScheduler& TaskScheduler::Update(size_t const milliseconds, success_t const& callback) { - return Update(std::chrono::milliseconds(milliseconds)); + return Update(std::chrono::milliseconds(milliseconds), callback); } TaskScheduler& TaskScheduler::Async(std::function const& callable) @@ -66,15 +72,26 @@ TaskScheduler& TaskScheduler::InsertTask(TaskContainer task) return *this; } -void TaskScheduler::Dispatch() +void TaskScheduler::Dispatch(success_t const& callback) { + // If the validation failed abort the dispatching here. + if (!_predicate()) + return; + // Process all asyncs while (!_asyncHolder.empty()) { _asyncHolder.front()(); _asyncHolder.pop(); + + // If the validation failed abort the dispatching here. + if (!_predicate()) + return; } + if (_task_holder.IsEmpty()) + return; + while (!_task_holder.IsEmpty()) { if (_task_holder.First()->_end > _now) @@ -86,7 +103,14 @@ void TaskScheduler::Dispatch() // Invoke the context context.Invoke(); + + // If the validation failed abort the dispatching here. + if (!_predicate()) + return; } + + // On finish call the final callback + callback(); } void TaskScheduler::TaskQueue::Push(TaskContainer&& task) -- cgit v1.2.3