summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-08-08 17:15:25 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-10-15 19:11:21 +0300
commitd71350c15bbddc74901d6fe0c8587ebe1c38028f (patch)
tree0044c7ddbe2abcddf6012dd9b13e2f213a88c007
parente4e2a78033f25fd966fa9cbbd2babb7e7b499e18 (diff)
downloadqtlocation-mapboxgl-upstream/alexshalamov_check_runnable_flag.tar.gz
[android] Check flag before runnable task invocationupstream/alexshalamov_check_runnable_flag
-rw-r--r--platform/android/src/async_task.cpp9
-rw-r--r--platform/android/src/timer.cpp12
2 files changed, 15 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..1d3d05c843 100644
--- a/platform/android/src/timer.cpp
+++ b/platform/android/src/timer.cpp
@@ -3,6 +3,7 @@
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
+#include <atomic>
#include <functional>
namespace mbgl {
@@ -10,7 +11,7 @@ namespace util {
class Timer::Impl : public RunLoop::Impl::Runnable {
public:
- Impl() = default;
+ Impl() : active(false) {}
~Impl() {
stop();
@@ -25,9 +26,11 @@ public:
due = (timeout == Duration::max()) ? std::chrono::time_point<Clock>::max() : Clock::now() +
timeout;
loop->addRunnable(this);
+ active = true;
}
void stop() {
+ active = false;
loop->removeRunnable(this);
}
@@ -45,8 +48,10 @@ public:
}
void runTask() override {
- reschedule();
- task();
+ if (active) {
+ reschedule();
+ task();
+ }
}
private:
@@ -56,6 +61,7 @@ private:
RunLoop::Impl* loop = reinterpret_cast<RunLoop::Impl*>(RunLoop::getLoopHandle());
std::function<void()> task;
+ std::atomic<bool> active;
};
Timer::Timer() : impl(std::make_unique<Impl>()) {