From e579199ac12b58e1099bba5f880a19588c867429 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 4 Jul 2016 19:22:21 +0300 Subject: [android] Fix a bug when canceling tasks If the next task in the queue gets canceled, we need to adjust the iterator to the next task, otherwise a canceled task gets executed. --- platform/android/src/run_loop.cpp | 16 +++++++++++----- platform/android/src/run_loop_impl.hpp | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'platform') diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 591cb31fd3..6c6d3555e9 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -123,10 +123,16 @@ void RunLoop::Impl::addRunnable(Runnable* runnable) { void RunLoop::Impl::removeRunnable(Runnable* runnable) { std::lock_guard lock(mtx); - if (runnable->iter != runnables.end()) { - runnables.erase(runnable->iter); - runnable->iter = runnables.end(); + if (runnable->iter == runnables.end()) { + return; + } + + if (nextRunnable == runnable->iter) { + ++nextRunnable; } + + runnables.erase(runnable->iter); + runnable->iter = runnables.end(); } void RunLoop::Impl::initRunnable(Runnable* runnable) { @@ -141,8 +147,8 @@ Milliseconds RunLoop::Impl::processRunnables() { // O(N) but in the render thread where we get tons // of messages, the size of the list is usually 1~2. - for (auto iter = runnables.begin(); iter != runnables.end();) { - Runnable* runnable = *(iter++); + for (nextRunnable = runnables.begin(); nextRunnable != runnables.end();) { + Runnable* runnable = *(nextRunnable++); auto const dueTime = runnable->dueTime(); if (dueTime <= now) { diff --git a/platform/android/src/run_loop_impl.hpp b/platform/android/src/run_loop_impl.hpp index 1ca3dbc61a..cb553d1f8a 100644 --- a/platform/android/src/run_loop_impl.hpp +++ b/platform/android/src/run_loop_impl.hpp @@ -52,6 +52,7 @@ private: std::recursive_mutex mtx; std::list runnables; + std::list::iterator nextRunnable; }; } // namespace util -- cgit v1.2.1