From 9db74bca1d855230edbc5c3b6616e56c5de44127 Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 1 May 2018 15:06:27 +0200 Subject: [PATCH] task: use consistent ordering for tasks with same sort value We don't guarantee a specific order in this case, especially because they can be run in either order if there is more than one thread, but it is still useful to have a defined order for single-threaded task chains. To that end, tasks are now run in the order in which they were added to taskMgr.add (in absence of any other ordering constraints). Fixes #309 --- panda/src/event/asyncTask.h | 1 + panda/src/event/asyncTaskChain.cxx | 6 +++++- panda/src/event/asyncTaskChain.h | 9 ++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/panda/src/event/asyncTask.h b/panda/src/event/asyncTask.h index d514b2f5a3..9712daa0f5 100644 --- a/panda/src/event/asyncTask.h +++ b/panda/src/event/asyncTask.h @@ -119,6 +119,7 @@ protected: double _wake_time; int _sort; int _priority; + unsigned int _implicit_sort; State _state; Thread *_servicing_thread; diff --git a/panda/src/event/asyncTaskChain.cxx b/panda/src/event/asyncTaskChain.cxx index c7d732eca9..bb82e00f92 100644 --- a/panda/src/event/asyncTaskChain.cxx +++ b/panda/src/event/asyncTaskChain.cxx @@ -51,7 +51,8 @@ AsyncTaskChain(AsyncTaskManager *manager, const string &name) : _needs_cleanup(false), _current_frame(0), _time_in_frame(0.0), - _block_till_next_frame(false) + _block_till_next_frame(false), + _next_implicit_sort(0) { } @@ -418,6 +419,9 @@ do_add(AsyncTask *task) { task->_start_time = now; task->_start_frame = _manager->_clock->get_frame_count(); + // Remember the order in which tasks were added to the chain. + task->_implicit_sort = _next_implicit_sort++; + _manager->add_task_by_name(task); if (task->has_delay()) { diff --git a/panda/src/event/asyncTaskChain.h b/panda/src/event/asyncTaskChain.h index 512a7b9fa0..5f81f6c3dd 100644 --- a/panda/src/event/asyncTaskChain.h +++ b/panda/src/event/asyncTaskChain.h @@ -146,7 +146,12 @@ protected: if (a->get_priority() != b->get_priority()) { return a->get_priority() < b->get_priority(); } - return a->get_start_time() > b->get_start_time(); + if (a->get_start_time() != b->get_start_time()) { + return a->get_start_time() > b->get_start_time(); + } + // Failing any other ordering criteria, we sort the tasks based on the + // order in which they were added to the task chain. + return a->_implicit_sort > b->_implicit_sort; } }; @@ -186,6 +191,8 @@ protected: double _time_in_frame; bool _block_till_next_frame; + unsigned int _next_implicit_sort; + static PStatCollector _task_pcollector; static PStatCollector _wait_pcollector;