blob: e1c18a45b583797c960a9acdbed958ddca7b279c (
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
|
#include <mbgl/util/thread_pool.hpp>
#include <mbgl/platform/settings.hpp>
#include <mbgl/platform/thread.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
namespace mbgl {
ThreadedSchedulerBase::~ThreadedSchedulerBase() = default;
void ThreadedSchedulerBase::terminate() {
{
std::lock_guard<std::mutex> lock(mutex);
terminated = true;
}
cv.notify_all();
}
std::thread ThreadedSchedulerBase::makeSchedulerThread(size_t index) {
return std::thread([this, index] {
auto& settings = platform::Settings::getInstance();
auto value = settings.get(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER);
if (auto* priority = value.getDouble()) {
platform::setCurrentThreadPriority(*priority);
}
platform::setCurrentThreadName(std::string{"Worker "} + util::toString(index + 1));
platform::attachThread();
while (true) {
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this] { return !queue.empty() || terminated; });
if (terminated) {
platform::detachThread();
return;
}
auto function = std::move(queue.front());
queue.pop();
lock.unlock();
if (function) function();
}
});
}
void ThreadedSchedulerBase::schedule(std::function<void()> fn) {
assert(fn);
{
std::lock_guard<std::mutex> lock(mutex);
queue.push(std::move(fn));
}
cv.notify_one();
}
} // namespace mbgl
|