[core] Introduce the AsyncTask
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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': {
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
53
platform/default/async_task.cpp
Normal file
53
platform/default/async_task.cpp
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
28
src/mbgl/util/async_task.hpp
Normal file
28
src/mbgl/util/async_task.hpp
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user