summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-08-08 15:33:33 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-08-08 15:33:33 +0300
commit8d509b12611f0c3d57d03e92852376334e4781af (patch)
treee90ef126998953c16c8f11c4cb7237a4c3760acf
parentd8d685de35d407a72a057256a6498eddcb8effb7 (diff)
downloadqtlocation-mapboxgl-upstream/alexshalamov_cancellable_async_task.tar.gz
-rw-r--r--include/mbgl/util/async_task.hpp1
-rw-r--r--platform/android/src/async_task.cpp23
2 files changed, 20 insertions, 4 deletions
diff --git a/include/mbgl/util/async_task.hpp b/include/mbgl/util/async_task.hpp
index 69746c3eb3..1003471332 100644
--- a/include/mbgl/util/async_task.hpp
+++ b/include/mbgl/util/async_task.hpp
@@ -14,6 +14,7 @@ public:
~AsyncTask();
void send();
+ void cancel();
private:
class Impl;
diff --git a/platform/android/src/async_task.cpp b/platform/android/src/async_task.cpp
index 6c14e96fa6..9cb4d62aef 100644
--- a/platform/android/src/async_task.cpp
+++ b/platform/android/src/async_task.cpp
@@ -26,14 +26,23 @@ public:
}
}
+ void cancel() {
+ if (!queued) {
+ queued = true;
+ loop->removeRunnable(this);
+ }
+ }
+
TimePoint dueTime() const override {
return due;
}
void runTask() override {
- loop->removeRunnable(this);
- queued = true;
- task();
+ if (!queued) {
+ queued = true;
+ loop->removeRunnable(this);
+ task();
+ }
}
private:
@@ -52,11 +61,17 @@ AsyncTask::AsyncTask(std::function<void()>&& fn)
: impl(std::make_unique<Impl>(std::move(fn))) {
}
-AsyncTask::~AsyncTask() = default;
+AsyncTask::~AsyncTask() {
+ cancel();
+};
void AsyncTask::send() {
impl->maySend();
}
+void AsyncTask::cancel() {
+ impl->cancel();
+}
+
} // namespace util
} // namespace mbgl