From 03a14ff0003e976a4ded70d284bc80adf54bc6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 16 Feb 2017 19:15:30 +0100 Subject: [darwin] move RunLoop initialization to static variable --- platform/darwin/src/MGLFoundation.mm | 4 ++++ platform/darwin/src/MGLFoundation_Private.h | 5 +++++ platform/darwin/src/run_loop.cpp | 17 +++++++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 platform/darwin/src/MGLFoundation.mm create mode 100644 platform/darwin/src/MGLFoundation_Private.h (limited to 'platform/darwin') diff --git a/platform/darwin/src/MGLFoundation.mm b/platform/darwin/src/MGLFoundation.mm new file mode 100644 index 0000000000..1cc56de298 --- /dev/null +++ b/platform/darwin/src/MGLFoundation.mm @@ -0,0 +1,4 @@ +#import "MGLFoundation_Private.h" + +/// Initializes the run loop shim that lives on the main thread. +mbgl::util::RunLoop mgl_runLoop; diff --git a/platform/darwin/src/MGLFoundation_Private.h b/platform/darwin/src/MGLFoundation_Private.h new file mode 100644 index 0000000000..940bb1df69 --- /dev/null +++ b/platform/darwin/src/MGLFoundation_Private.h @@ -0,0 +1,5 @@ +#import "MGLFoundation.h" + +#include + +extern mbgl::util::RunLoop mgl_runLoop; diff --git a/platform/darwin/src/run_loop.cpp b/platform/darwin/src/run_loop.cpp index 63bd8d2f53..bae8164ab6 100644 --- a/platform/darwin/src/run_loop.cpp +++ b/platform/darwin/src/run_loop.cpp @@ -7,7 +7,11 @@ namespace mbgl { namespace util { -static ThreadLocal& current = *new ThreadLocal; +// Use a static function to avoid the static initialization order fiasco. +static auto& current() { + static ThreadLocal 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()) { - assert(!current.get()); - current.set(this); + assert(!current().get()); + current().set(this); impl->async = std::make_unique(std::bind(&RunLoop::process, this)); } RunLoop::~RunLoop() { - current.set(nullptr); + assert(current().get()); + current().set(nullptr); } void RunLoop::push(std::shared_ptr task) { -- cgit v1.2.1