aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Utilities/TaskScheduler.cpp
diff options
context:
space:
mode:
authorNaios <naios-dev@live.de>2015-06-21 18:04:47 +0200
committerNaios <naios-dev@live.de>2015-06-21 18:19:17 +0200
commit3d95aba2b3d58057f3eeed6d4dd6e51ea49b77f6 (patch)
tree029607567faa34caf217f7b60bf9a4877c3ddfeb /src/server/shared/Utilities/TaskScheduler.cpp
parentbd661b58fd2c37850a719cfffe135e89043c112d (diff)
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
Diffstat (limited to 'src/server/shared/Utilities/TaskScheduler.cpp')
-rw-r--r--src/server/shared/Utilities/TaskScheduler.cpp34
1 files changed, 29 insertions, 5 deletions
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<void()> 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)