summaryrefslogtreecommitdiff
path: root/src/mbgl/util/work_queue.cpp
blob: 80c4af8778d269c063b49d9ec7a120cb63ab8f4d (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
38
#include <mbgl/util/work_queue.hpp>
#include <mbgl/util/run_loop.hpp>

#include <cassert>

namespace mbgl {
namespace util {

WorkQueue::WorkQueue() : runLoop(RunLoop::Get()) {
}

WorkQueue::~WorkQueue() {
    assert(runLoop == RunLoop::Get());

    // Cancel all pending WorkRequests.
    while (!queue.empty()) {
        queue.pop();
    }
}

void WorkQueue::push(std::function<void()>&& fn) {
    std::lock_guard<std::mutex> lock(queueMutex);

    auto workRequest = runLoop->invokeCancellable(std::bind(&WorkQueue::pop, this, std::move(fn)));
    queue.push(std::move(workRequest));
}

void WorkQueue::pop(const std::function<void()>& fn) {
    assert(runLoop == RunLoop::Get());

    fn();

    std::lock_guard<std::mutex> lock(queueMutex);
    queue.pop();
}

} // namespace util
} // namespace mbgl