fix task_scheduler_test. Fix potential reference invalidation in task_scheduler internal tasks data structure

master
Francisco Paisana 4 years ago
parent a6c3144d92
commit a9d882c1f1

@ -118,19 +118,19 @@ private:
void run_all_internal_tasks() void run_all_internal_tasks()
{ {
// Perform pending stack deferred tasks // Perform pending stack deferred tasks
// Note: Keep it indexed-based, bc a task may enqueue another task, which may cause vector reallocation, // Note: Using a deque because tasks can enqueue new tasks, which would lead to
// and iterator invalidation // reference invalidation in case of vector
for (size_t i = 0; i < internal_tasks.size(); ++i) { while (not internal_tasks.empty()) {
internal_tasks[i](); internal_tasks.front()();
internal_tasks.pop_front();
} }
internal_tasks.clear();
} }
srslte::task_thread_pool background_tasks; ///< Thread pool used for long, low-priority tasks srslte::task_thread_pool background_tasks; ///< Thread pool used for long, low-priority tasks
int background_queue_id = -1; ///< Queue for handling the outcomes of tasks run in the background int background_queue_id = -1; ///< Queue for handling the outcomes of tasks run in the background
srslte::task_multiqueue external_tasks; srslte::task_multiqueue external_tasks;
srslte::timer_handler timers; srslte::timer_handler timers;
std::vector<srslte::move_task_t> internal_tasks; ///< enqueues stack tasks from within main thread. Avoids locking std::deque<srslte::move_task_t> internal_tasks; ///< enqueues stack tasks from within main thread. Avoids locking
}; };
//! Task scheduler handle given to classes/functions running within the main control thread //! Task scheduler handle given to classes/functions running within the main control thread

@ -69,8 +69,6 @@ int test_task_scheduler_with_pool()
task_sched.notify_background_task_result([&state]() { state = task_result::external; }); task_sched.notify_background_task_result([&state]() { state = task_result::external; });
}); });
TESTASSERT(state == task_result::null); TESTASSERT(state == task_result::null);
task_sched.run_pending_tasks();
TESTASSERT(state == task_result::null);
task_sched.run_next_task(); // waits and runs notification task_sched.run_next_task(); // waits and runs notification
TESTASSERT(state == task_result::external); TESTASSERT(state == task_result::external);

Loading…
Cancel
Save