summaryrefslogtreecommitdiff
path: root/platform/qt/src/qmapboxgl_scheduler.cpp
blob: 5fc3ab13de7de7bdb5904e1ab6c7bf52b40deaa9 (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 "qmapboxgl_scheduler.hpp"

#include <mbgl/util/util.hpp>

#include <cassert>

QMapboxGLScheduler::QMapboxGLScheduler()
{
}

QMapboxGLScheduler::~QMapboxGLScheduler()
{
    MBGL_VERIFY_THREAD(tid);
}

void QMapboxGLScheduler::schedule(std::function<void()> function) {
    std::lock_guard<std::mutex> lock(m_taskQueueMutex);
    m_taskQueue.push(std::move(function));

    // Need to force the main thread to wake
    // up this thread and process the events.
    emit needsProcessing();
}

void QMapboxGLScheduler::processEvents()
{
    std::queue<std::function<void()>> taskQueue;
    {
        std::unique_lock<std::mutex> lock(m_taskQueueMutex);
        std::swap(taskQueue, m_taskQueue);
    }

    while (!taskQueue.empty()) {
        auto& function = taskQueue.front();
        if (function) function();
        taskQueue.pop();
    }
}