[darwin] move RunLoop initialization to static variable

This commit is contained in:
Konstantin Käfer
2017-02-16 19:15:30 +01:00
parent e2d8a83664
commit 03a14ff000
7 changed files with 41 additions and 20 deletions

View File

@@ -7,7 +7,11 @@
namespace mbgl {
namespace util {
static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;
// Use a static function to avoid the static initialization order fiasco.
static auto& current() {
static ThreadLocal<RunLoop> tl;
return tl;
};
class RunLoop::Impl {
public:
@@ -15,19 +19,20 @@ public:
};
RunLoop* RunLoop::Get() {
assert(current.get());
return current.get();
assert(current().get());
return current().get();
}
RunLoop::RunLoop(Type)
: impl(std::make_unique<Impl>()) {
assert(!current.get());
current.set(this);
assert(!current().get());
current().set(this);
impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}
RunLoop::~RunLoop() {
current.set(nullptr);
assert(current().get());
current().set(nullptr);
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {