1 #include "async_task_impl.hpp" 2 3 #include <mbgl/util/async_task.hpp> 4 #include <mbgl/util/run_loop.hpp> 5 6 #include <cassert> 7 8 namespace mbgl { 9 namespace util { 10 Impl(std::function<void ()> && fn)11AsyncTask::Impl::Impl(std::function<void()>&& fn) 12 : runLoop(RunLoop::Get()), 13 task(std::move(fn)) { 14 connect(this, SIGNAL(send(void)), this, SLOT(runTask(void)), Qt::QueuedConnection); 15 } 16 maySend()17void AsyncTask::Impl::maySend() { 18 if (!queued.test_and_set()) { 19 emit send(); 20 } 21 } 22 runTask()23void AsyncTask::Impl::runTask() { 24 assert(runLoop == RunLoop::Get()); 25 26 queued.clear(); 27 task(); 28 } 29 AsyncTask(std::function<void ()> && fn)30AsyncTask::AsyncTask(std::function<void()>&& fn) 31 : impl(std::make_unique<Impl>(std::move(fn))) { 32 } 33 ~AsyncTask()34AsyncTask::~AsyncTask() { 35 } 36 send()37void AsyncTask::send() { 38 impl->maySend(); 39 } 40 41 } 42 } 43