[ios, osx] Replace libuv with native implementations

This commit is contained in:
John Firebaugh
2016-02-16 12:52:25 -08:00
parent 7e9a4f15c5
commit 879382f724
13 changed files with 215 additions and 28 deletions

View File

@@ -0,0 +1,71 @@
#include <mbgl/util/async_task.hpp>
#include <CoreFoundation/CoreFoundation.h>
#include <atomic>
namespace mbgl {
namespace util {
class AsyncTask::Impl {
public:
Impl(std::function<void()>&& fn)
: task(std::move(fn)),
loop(CFRunLoopGetCurrent()) {
CFRunLoopSourceContext context = {
0,
this,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
perform
};
source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
}
~Impl() {
CFRunLoopRemoveSource(loop, source, kCFRunLoopDefaultMode);
CFRelease(source);
}
void maySend() {
if (!queued.test_and_set()) {
CFRunLoopSourceSignal(source);
CFRunLoopWakeUp(loop);
}
}
void runTask() {
queued.clear();
task();
}
private:
static void perform(void* info) {
reinterpret_cast<Impl*>(info)->runTask();
}
std::function<void()> task;
std::atomic_flag queued = ATOMIC_FLAG_INIT;
CFRunLoopRef loop;
CFRunLoopSourceRef source;
};
AsyncTask::AsyncTask(std::function<void()>&& fn)
: impl(std::make_unique<Impl>(std::move(fn))) {
}
AsyncTask::~AsyncTask() = default;
void AsyncTask::send() {
impl->maySend();
}
} // namespace util
} // namespace mbgl