Selaa lähdekoodia

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
rdb 7 vuotta sitten
vanhempi
sitoutus
9db74bca1d

+ 1 - 0
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;

+ 5 - 1
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()) {

+ 8 - 1
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;