diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-08-08 15:33:33 +0300 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-08-08 15:33:33 +0300 |
commit | 8d509b12611f0c3d57d03e92852376334e4781af (patch) | |
tree | e90ef126998953c16c8f11c4cb7237a4c3760acf | |
parent | d8d685de35d407a72a057256a6498eddcb8effb7 (diff) | |
download | qtlocation-mapboxgl-upstream/alexshalamov_cancellable_async_task.tar.gz |
WIP: Cancellable async tasksupstream/alexshalamov_cancellable_async_task
-rw-r--r-- | include/mbgl/util/async_task.hpp | 1 | ||||
-rw-r--r-- | platform/android/src/async_task.cpp | 23 |
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 |