summaryrefslogtreecommitdiff
path: root/src/mbgl/util/run_loop.cpp
blob: 7f277c988587ec6b9769f37135c3dc110f7a9d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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(); });
}

}
}