summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2015-08-31 10:41:52 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2015-12-01 00:00:09 +0200
commit123c5df92219bde3dda39d3ba942b996e765e8e9 (patch)
tree2d2087794966201dc79d15f7acd62c45dfa181c8 /platform
parent704ab891a77e352226872fd54d0ceea947a45579 (diff)
downloadqtlocation-mapboxgl-123c5df92219bde3dda39d3ba942b996e765e8e9.tar.gz
[core] Introduce the AsyncTask
Diffstat (limited to 'platform')
-rw-r--r--platform/default/async_task.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/platform/default/async_task.cpp b/platform/default/async_task.cpp
new file mode 100644
index 0000000000..4b4372451c
--- /dev/null
+++ b/platform/default/async_task.cpp
@@ -0,0 +1,53 @@
+#include <mbgl/util/async_task.hpp>
+
+#include <mbgl/util/run_loop.hpp>
+#include <mbgl/util/uv_detail.hpp>
+
+#include <atomic>
+#include <functional>
+
+namespace mbgl {
+namespace util {
+
+class AsyncTask::Impl {
+public:
+ Impl(std::function<void()>&& fn)
+ : async(RunLoop::getLoop(), [this] { runTask(); })
+ , task(std::move(fn)) {
+ }
+
+ void maySend() {
+ async.send();
+ }
+
+ void runTask() {
+ task();
+ }
+
+ void unref() {
+ async.unref();
+ }
+
+private:
+ uv::async async;
+
+ std::function<void()> task;
+};
+
+AsyncTask::AsyncTask(std::function<void()>&& fn)
+ : impl(std::make_unique<Impl>(std::move(fn))) {
+}
+
+AsyncTask::~AsyncTask() {
+}
+
+void AsyncTask::send() {
+ impl->maySend();
+}
+
+void AsyncTask::unref() {
+ impl->unref();
+}
+
+}
+}