[core] Introduce the AsyncTask

This commit is contained in:
Thiago Marcos P. Santos
2015-08-31 10:41:52 +03:00
parent 704ab891a7
commit 123c5df922
6 changed files with 86 additions and 0 deletions

View File

@@ -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();
}
}
}