From c43b08af066b43d89fdbd9d7061ad0944bbcb176 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 8 Aug 2019 16:15:25 +0200 Subject: [android] Check flag before runnable task invocation --- platform/android/src/async_task.cpp | 9 ++++++--- platform/android/src/timer.cpp | 13 ++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/platform/android/src/async_task.cpp b/platform/android/src/async_task.cpp index 6c14e96fa6..0a4d90a275 100644 --- a/platform/android/src/async_task.cpp +++ b/platform/android/src/async_task.cpp @@ -16,6 +16,7 @@ public: } ~Impl() { + queued = true; loop->removeRunnable(this); } @@ -31,9 +32,11 @@ public: } void runTask() override { - loop->removeRunnable(this); - queued = true; - task(); + if (!queued) { + queued = true; + loop->removeRunnable(this); + task(); + } } private: diff --git a/platform/android/src/timer.cpp b/platform/android/src/timer.cpp index a45c48702e..2c33504dfd 100644 --- a/platform/android/src/timer.cpp +++ b/platform/android/src/timer.cpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace mbgl { @@ -10,7 +11,8 @@ namespace util { class Timer::Impl : public RunLoop::Impl::Runnable { public: - Impl() = default; + Impl() : active(false) { + } ~Impl() { stop(); @@ -25,9 +27,11 @@ public: due = (timeout == Duration::max()) ? std::chrono::time_point::max() : Clock::now() + timeout; loop->addRunnable(this); + active = true; } void stop() { + active = false; loop->removeRunnable(this); } @@ -45,8 +49,10 @@ public: } void runTask() override { - reschedule(); - task(); + if (active) { + reschedule(); + task(); + } } private: @@ -56,6 +62,7 @@ private: RunLoop::Impl* loop = reinterpret_cast(RunLoop::getLoopHandle()); std::function task; + std::atomic active; }; Timer::Timer() : impl(std::make_unique()) { -- cgit v1.2.1