blob: d84bad909109cd18daa78f63f5e50349796a4a79 (
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/util/thread_pool.hpp>
namespace mbgl {
namespace {
class BindContext {
public:
BindContext(std::function<void()> scheduled_, mapbox::base::WeakPtr<Scheduler> scheduler_)
: scheduled(std::move(scheduled_)), scheduler(std::move(scheduler_)) {}
std::function<void()> scheduled;
mapbox::base::WeakPtr<Scheduler> scheduler;
void scheduleOnce() {
if (!scheduled) return; // Repeated call.
auto schedulerGuard = scheduler.lock();
if (scheduler) scheduler->schedule(std::move(scheduled));
assert(!scheduled);
}
};
} // namespace
std::function<void()> Scheduler::bindOnce(std::function<void()> fn) {
assert(fn);
auto ctx = new BindContext(std::move(fn), makeWeakPtr());
return [ctx]() { ctx->scheduleOnce(); };
}
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) {
weak = scheduler = std::make_shared<ThreadPool>();
}
return scheduler;
}
} //namespace mbgl
|