[core] Abstract main loop inside RunLoop class
This commit is contained in:
@@ -78,7 +78,7 @@ void DefaultFileSource::cancel(const Resource& res, FileRequest* req) {
|
||||
// ----- Impl -----
|
||||
|
||||
DefaultFileSource::Impl::Impl(FileCache* cache_, const std::string& root)
|
||||
: loop(util::RunLoop::getLoop()),
|
||||
: loop(reinterpret_cast<uv_loop_t*>(util::RunLoop::getLoopHandle())),
|
||||
cache(cache_),
|
||||
assetRoot(root.empty() ? platform::assetRoot() : root),
|
||||
assetContext(AssetContextBase::createContext(loop)),
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
uv::tls<RunLoop> RunLoop::current;
|
||||
|
||||
RunLoop::RunLoop(uv_loop_t* loop)
|
||||
: async(loop, std::bind(&RunLoop::process, this)) {
|
||||
current.set(this);
|
||||
}
|
||||
|
||||
RunLoop::~RunLoop() {
|
||||
current.set(nullptr);
|
||||
}
|
||||
|
||||
void RunLoop::withMutex(std::function<void()>&& fn) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
fn();
|
||||
}
|
||||
|
||||
void RunLoop::process() {
|
||||
Queue queue_;
|
||||
withMutex([&] { queue_.swap(queue); });
|
||||
|
||||
while (!queue_.empty()) {
|
||||
(*(queue_.front()))();
|
||||
queue_.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void RunLoop::stop() {
|
||||
invoke([&] { async.unref(); });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -112,26 +112,19 @@ Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
|
||||
template <class Object>
|
||||
template <typename P, std::size_t... I>
|
||||
void Thread<Object>::run(ThreadContext context, P&& params, std::index_sequence<I...>) {
|
||||
uv::loop l;
|
||||
|
||||
ThreadContext::current.set(&context);
|
||||
|
||||
{
|
||||
RunLoop loop_(l.get());
|
||||
loop = &loop_;
|
||||
RunLoop loop_(RunLoop::Type::New);
|
||||
loop = &loop_;
|
||||
|
||||
Object object_(std::get<I>(std::forward<P>(params))...);
|
||||
object = &object_;
|
||||
Object object_(std::get<I>(std::forward<P>(params))...);
|
||||
object = &object_;
|
||||
|
||||
running.set_value();
|
||||
l.run();
|
||||
running.set_value();
|
||||
loop_.run();
|
||||
|
||||
loop = nullptr;
|
||||
object = nullptr;
|
||||
}
|
||||
|
||||
// Run the loop again to ensure that async close callbacks have been called.
|
||||
l.run();
|
||||
loop = nullptr;
|
||||
object = nullptr;
|
||||
|
||||
ThreadContext::current.set(nullptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user