diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-08-31 10:41:52 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-12-01 00:00:09 +0200 |
commit | 123c5df92219bde3dda39d3ba942b996e765e8e9 (patch) | |
tree | 2d2087794966201dc79d15f7acd62c45dfa181c8 | |
parent | 704ab891a77e352226872fd54d0ceea947a45579 (diff) | |
download | qtlocation-mapboxgl-123c5df92219bde3dda39d3ba942b996e765e8e9.tar.gz |
[core] Introduce the AsyncTask
-rw-r--r-- | gyp/platform-android.gypi | 1 | ||||
-rw-r--r-- | gyp/platform-ios.gypi | 2 | ||||
-rw-r--r-- | gyp/platform-linux.gypi | 1 | ||||
-rw-r--r-- | gyp/platform-osx.gypi | 1 | ||||
-rw-r--r-- | platform/default/async_task.cpp | 53 | ||||
-rw-r--r-- | src/mbgl/util/async_task.hpp | 28 |
6 files changed, 86 insertions, 0 deletions
diff --git a/gyp/platform-android.gypi b/gyp/platform-android.gypi index e3e1141c51..dcab0e0193 100644 --- a/gyp/platform-android.gypi +++ b/gyp/platform-android.gypi @@ -12,6 +12,7 @@ 'sources': [ '../platform/android/log_android.cpp', '../platform/android/asset_root.cpp', + '../platform/default/async_task.cpp', '../platform/default/thread.cpp', '../platform/default/string_stdlib.cpp', '../platform/default/image.cpp', diff --git a/gyp/platform-ios.gypi b/gyp/platform-ios.gypi index 205cfe22dd..0b23890c42 100644 --- a/gyp/platform-ios.gypi +++ b/gyp/platform-ios.gypi @@ -10,6 +10,7 @@ ], 'sources': [ + '../platform/default/async_task.cpp', '../platform/darwin/log_nslog.mm', '../platform/darwin/string_nsstring.mm', '../platform/darwin/application_root.mm', @@ -93,6 +94,7 @@ 'include_dirs': [ '../include', + '../src', ], 'xcode_settings': { diff --git a/gyp/platform-linux.gypi b/gyp/platform-linux.gypi index 3ee39e2b70..1ba328c5cc 100644 --- a/gyp/platform-linux.gypi +++ b/gyp/platform-linux.gypi @@ -10,6 +10,7 @@ ], 'sources': [ + '../platform/default/async_task.cpp', '../platform/default/log_stderr.cpp', '../platform/default/string_stdlib.cpp', '../platform/default/application_root.cpp', diff --git a/gyp/platform-osx.gypi b/gyp/platform-osx.gypi index af30019ee5..b3941d2e2d 100644 --- a/gyp/platform-osx.gypi +++ b/gyp/platform-osx.gypi @@ -10,6 +10,7 @@ ], 'sources': [ + '../platform/default/async_task.cpp', '../platform/darwin/log_nslog.mm', '../platform/darwin/string_nsstring.mm', '../platform/darwin/application_root.mm', 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(); +} + +} +} diff --git a/src/mbgl/util/async_task.hpp b/src/mbgl/util/async_task.hpp new file mode 100644 index 0000000000..09bdab94d7 --- /dev/null +++ b/src/mbgl/util/async_task.hpp @@ -0,0 +1,28 @@ +#ifndef MBGL_UTIL_ASYNC_TASK +#define MBGL_UTIL_ASYNC_TASK + +#include <mbgl/util/noncopyable.hpp> + +#include <memory> +#include <functional> + +namespace mbgl { +namespace util { + +class AsyncTask : private util::noncopyable { +public: + AsyncTask(std::function<void()>&&); + ~AsyncTask(); + + void send(); + void unref(); + +private: + class Impl; + std::unique_ptr<Impl> impl; +}; + +} +} + +#endif |