summaryrefslogtreecommitdiff
path: root/src/mbgl/actor/scheduler.cpp
blob: fd4845488024fe3b375c40d75583004f28efee13 (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/actor/scheduler.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/util/thread_pool.hpp>
#include <iostream>

namespace mbgl {

util::ThreadLocal<Scheduler> g_currentScheduler;

static auto& current() {
    static util::ThreadLocal<Scheduler> scheduler;
    return scheduler;
};

void Scheduler::SetCurrent(Scheduler* scheduler) {
    current().set(scheduler);
}

Scheduler* Scheduler::GetCurrent() {
    return current().get();
}

// static
std::shared_ptr<Scheduler> Scheduler::GetBackground() {
    static std::weak_ptr<Scheduler> weak;
    static std::mutex mtx;

    std::lock_guard<std::mutex> lock(mtx);
    std::shared_ptr<Scheduler> scheduler = weak.lock();

    if (!scheduler) {
        //std::clog << "creating scheduler\n";
        weak = scheduler = std::make_shared<ThreadPool>(4);
    }
    return scheduler;
}

} //namespace mbgl