summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-02-16 12:52:25 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-04-01 09:26:54 -0700
commit879382f72405edd1840d2f37744bcba49131859a (patch)
tree4f632c8973d1c5539437e790edb471090339b533
parent7e9a4f15c5082dfe5987e1dcbb4b816f04068a60 (diff)
downloadqtlocation-mapboxgl-879382f72405edd1840d2f37744bcba49131859a.tar.gz
[ios, osx] Replace libuv with native implementations
-rw-r--r--Makefile4
-rw-r--r--gyp/mbgl.gyp44
-rw-r--r--platform/android/scripts/defaults.mk1
-rw-r--r--platform/darwin/src/async_task.cpp71
-rw-r--r--platform/darwin/src/run_loop.cpp51
-rw-r--r--platform/darwin/src/timer.cpp60
-rw-r--r--platform/ios/scripts/configure.sh1
-rw-r--r--platform/ios/scripts/defaults.mk1
-rwxr-xr-xplatform/ios/scripts/package.sh3
-rw-r--r--platform/linux/scripts/defaults.mk1
-rw-r--r--platform/osx/scripts/defaults.mk1
-rwxr-xr-xplatform/osx/scripts/package.sh1
-rw-r--r--scripts/main.mk2
13 files changed, 214 insertions, 27 deletions
diff --git a/Makefile b/Makefile
index 395ab5f78b..f24b40cdea 100644
--- a/Makefile
+++ b/Makefile
@@ -100,11 +100,11 @@ apackage: android-lib-mips
# Builds the Node.js library
.PHONY: node
-node: ; $(RUN) HTTP=none ASSET=none Makefile/node
+node: ; $(RUN) LOOP=uv HTTP=none ASSET=none Makefile/node
.PHONY: Xcode/node
-Xcode/node: ; $(RUN) HTTP=none ASSET=none Xcode/node
+Xcode/node: ; $(RUN) LOOP=uv HTTP=none ASSET=none Xcode/node
.PHONY: xnode
xnode: Xcode/node ; @open ./build/binding.xcodeproj
diff --git a/gyp/mbgl.gyp b/gyp/mbgl.gyp
index daea491365..2d541875e9 100644
--- a/gyp/mbgl.gyp
+++ b/gyp/mbgl.gyp
@@ -15,33 +15,37 @@
'type': 'static_library',
'standalone_static_library': 1,
- 'sources': [
- '../platform/default/async_task.cpp',
- '../platform/default/run_loop.cpp',
- '../platform/default/timer.cpp',
- ],
-
'include_dirs': [
'../include',
'../src',
],
- 'cflags_cc': [
- '<@(libuv_cflags)',
- ],
+ 'conditions': [
+ ['loop_lib == "darwin"', {
+ 'sources': [
+ '../platform/darwin/src/async_task.cpp',
+ '../platform/darwin/src/run_loop.cpp',
+ '../platform/darwin/src/timer.cpp',
+ ],
+ }],
- 'link_settings': {
- 'libraries': [
- '<@(libuv_static_libs)',
- '<@(libuv_ldflags)',
- ],
- },
+ ['loop_lib == "uv"', {
+ 'sources': [
+ '../platform/default/async_task.cpp',
+ '../platform/default/run_loop.cpp',
+ '../platform/default/timer.cpp',
+ ],
- 'conditions': [
- ['OS == "mac"', {
- 'xcode_settings': {
- 'OTHER_CPLUSPLUSFLAGS': [ '<@(libuv_cflags)' ],
- }
+ 'cflags_cc': [
+ '<@(libuv_cflags)',
+ ],
+
+ 'link_settings': {
+ 'libraries': [
+ '<@(libuv_static_libs)',
+ '<@(libuv_ldflags)',
+ ],
+ },
}]
],
},
diff --git a/platform/android/scripts/defaults.mk b/platform/android/scripts/defaults.mk
index 5f049aae48..78fdd6f380 100644
--- a/platform/android/scripts/defaults.mk
+++ b/platform/android/scripts/defaults.mk
@@ -2,6 +2,7 @@ HEADLESS ?= none
PLATFORM ?= android
ASSET ?= zip
HTTP ?= android
+LOOP ?= uv
GYP_FLAVOR_SUFFIX=-android
diff --git a/platform/darwin/src/async_task.cpp b/platform/darwin/src/async_task.cpp
new file mode 100644
index 0000000000..122f95f738
--- /dev/null
+++ b/platform/darwin/src/async_task.cpp
@@ -0,0 +1,71 @@
+#include <mbgl/util/async_task.hpp>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <atomic>
+
+namespace mbgl {
+namespace util {
+
+class AsyncTask::Impl {
+public:
+ Impl(std::function<void()>&& fn)
+ : task(std::move(fn)),
+ loop(CFRunLoopGetCurrent()) {
+ CFRunLoopSourceContext context = {
+ 0,
+ this,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ nullptr,
+ perform
+ };
+ source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
+ CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
+ }
+
+ ~Impl() {
+ CFRunLoopRemoveSource(loop, source, kCFRunLoopDefaultMode);
+ CFRelease(source);
+ }
+
+ void maySend() {
+ if (!queued.test_and_set()) {
+ CFRunLoopSourceSignal(source);
+ CFRunLoopWakeUp(loop);
+ }
+ }
+
+ void runTask() {
+ queued.clear();
+ task();
+ }
+
+private:
+ static void perform(void* info) {
+ reinterpret_cast<Impl*>(info)->runTask();
+ }
+
+ std::function<void()> task;
+ std::atomic_flag queued = ATOMIC_FLAG_INIT;
+
+ CFRunLoopRef loop;
+ CFRunLoopSourceRef source;
+};
+
+AsyncTask::AsyncTask(std::function<void()>&& fn)
+ : impl(std::make_unique<Impl>(std::move(fn))) {
+}
+
+AsyncTask::~AsyncTask() = default;
+
+void AsyncTask::send() {
+ impl->maySend();
+}
+
+} // namespace util
+} // namespace mbgl
diff --git a/platform/darwin/src/run_loop.cpp b/platform/darwin/src/run_loop.cpp
new file mode 100644
index 0000000000..6e9a1c50b0
--- /dev/null
+++ b/platform/darwin/src/run_loop.cpp
@@ -0,0 +1,51 @@
+#include <mbgl/util/run_loop.hpp>
+#include <mbgl/util/async_task.hpp>
+#include <mbgl/util/thread_local.hpp>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+namespace mbgl {
+namespace util {
+
+static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;
+static RunLoop mainRunLoop;
+
+class RunLoop::Impl {
+public:
+ std::unique_ptr<AsyncTask> async;
+};
+
+RunLoop* RunLoop::Get() {
+ assert(current.get());
+ return current.get();
+}
+
+RunLoop::RunLoop(Type)
+ : impl(std::make_unique<Impl>()) {
+ current.set(this);
+ impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
+}
+
+RunLoop::~RunLoop() {
+ current.set(nullptr);
+}
+
+void RunLoop::push(std::shared_ptr<WorkTask> task) {
+ withMutex([&] { queue.push(std::move(task)); });
+ impl->async->send();
+}
+
+void RunLoop::run() {
+ CFRunLoopRun();
+}
+
+void RunLoop::runOnce() {
+ CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
+}
+
+void RunLoop::stop() {
+ invoke([&] { CFRunLoopStop(CFRunLoopGetCurrent()); });
+}
+
+} // namespace util
+} // namespace mbgl
diff --git a/platform/darwin/src/timer.cpp b/platform/darwin/src/timer.cpp
new file mode 100644
index 0000000000..e9e58d86c6
--- /dev/null
+++ b/platform/darwin/src/timer.cpp
@@ -0,0 +1,60 @@
+#include <mbgl/util/timer.hpp>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+namespace mbgl {
+namespace util {
+
+CFTimeInterval toCFTimeInterval(Duration duration) {
+ return std::chrono::duration<CFTimeInterval>(duration).count();
+}
+
+CFAbsoluteTime toCFAbsoluteTime(Duration duration) {
+ return CFAbsoluteTimeGetCurrent() + toCFTimeInterval(duration);
+}
+
+class Timer::Impl {
+public:
+ Impl(Duration timeout, Duration repeat, std::function<void()>&& fn)
+ : task(std::move(fn)),
+ loop(CFRunLoopGetCurrent()) {
+ CFRunLoopTimerContext context = {
+ 0,
+ this,
+ nullptr,
+ nullptr,
+ nullptr
+ };
+ timer = CFRunLoopTimerCreate(kCFAllocatorDefault, toCFAbsoluteTime(timeout), toCFTimeInterval(repeat), 0, 0, perform, &context);
+ CFRunLoopAddTimer(loop, timer, kCFRunLoopDefaultMode);
+ }
+
+ ~Impl() {
+ CFRunLoopRemoveTimer(loop, timer, kCFRunLoopDefaultMode);
+ CFRelease(timer);
+ }
+
+private:
+ static void perform(CFRunLoopTimerRef, void* info) {
+ reinterpret_cast<Impl*>(info)->task();
+ }
+
+ std::function<void()> task;
+
+ CFRunLoopRef loop;
+ CFRunLoopTimerRef timer;
+};
+
+Timer::Timer() = default;
+Timer::~Timer() = default;
+
+void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) {
+ impl = std::make_unique<Impl>(timeout, repeat, std::move(cb));
+}
+
+void Timer::stop() {
+ impl.reset();
+}
+
+} // namespace util
+} // namespace mbgl
diff --git a/platform/ios/scripts/configure.sh b/platform/ios/scripts/configure.sh
index a62de46bef..f05d34756b 100644
--- a/platform/ios/scripts/configure.sh
+++ b/platform/ios/scripts/configure.sh
@@ -2,7 +2,6 @@
BOOST_VERSION=1.59.0
SQLITE_VERSION=system
-LIBUV_VERSION=1.7.5
ZLIB_VERSION=system
GEOJSONVT_VERSION=4.1.0
VARIANT_VERSION=1.1.0
diff --git a/platform/ios/scripts/defaults.mk b/platform/ios/scripts/defaults.mk
index ae4c815b74..9debff4ce0 100644
--- a/platform/ios/scripts/defaults.mk
+++ b/platform/ios/scripts/defaults.mk
@@ -2,6 +2,7 @@ HEADLESS = eagl
PLATFORM ?= ios
ASSET ?= fs
HTTP ?= nsurl
+LOOP ?= darwin
HOST_VERSION = all
diff --git a/platform/ios/scripts/package.sh b/platform/ios/scripts/package.sh
index 137775ef71..af03f02d80 100755
--- a/platform/ios/scripts/package.sh
+++ b/platform/ios/scripts/package.sh
@@ -6,7 +6,6 @@ set -u
NAME=Mapbox
OUTPUT=build/ios/pkg
-LIBUV_VERSION=1.7.5
BUILDTYPE=${BUILDTYPE:-Release}
BUILD_FOR_DEVICE=${BUILD_DEVICE:-true}
@@ -150,7 +149,6 @@ if [[ "${BUILD_FOR_DEVICE}" == true ]]; then
step "Assembling static framework for iOS Simulator and devices…"
mkdir -p ${OUTPUT}/static/${NAME}.framework
libtool -static -no_warning_for_no_symbols \
- `find mason_packages/ios-${IOS_SDK_VERSION} -type f -name libuv.a` \
`find mason_packages/ios-${IOS_SDK_VERSION} -type f -name libgeojsonvt.a` \
-o ${OUTPUT}/static/${NAME}.framework/${NAME} \
${LIBS[@]/#/gyp/build/${BUILDTYPE}-iphoneos/libmbgl-} \
@@ -176,7 +174,6 @@ else
step "Assembling static library for iOS Simulator…"
mkdir -p ${OUTPUT}/static/${NAME}.framework
libtool -static -no_warning_for_no_symbols \
- `find mason_packages/ios-${IOS_SDK_VERSION} -type f -name libuv.a` \
`find mason_packages/ios-${IOS_SDK_VERSION} -type f -name libgeojsonvt.a` \
-o ${OUTPUT}/static/${NAME}.framework/${NAME} \
${LIBS[@]/#/gyp/build/${BUILDTYPE}-iphonesimulator/libmbgl-}
diff --git a/platform/linux/scripts/defaults.mk b/platform/linux/scripts/defaults.mk
index c387135bb1..80f1346533 100644
--- a/platform/linux/scripts/defaults.mk
+++ b/platform/linux/scripts/defaults.mk
@@ -2,3 +2,4 @@ HEADLESS ?= glx
PLATFORM ?= linux
ASSET ?= fs
HTTP ?= curl
+LOOP ?= uv
diff --git a/platform/osx/scripts/defaults.mk b/platform/osx/scripts/defaults.mk
index 94dda854fa..be290891d6 100644
--- a/platform/osx/scripts/defaults.mk
+++ b/platform/osx/scripts/defaults.mk
@@ -2,3 +2,4 @@ HEADLESS ?= cgl
PLATFORM ?= osx
ASSET ?= fs
HTTP ?= nsurl
+LOOP ?= darwin
diff --git a/platform/osx/scripts/package.sh b/platform/osx/scripts/package.sh
index f39d730674..f4b2d0c899 100755
--- a/platform/osx/scripts/package.sh
+++ b/platform/osx/scripts/package.sh
@@ -7,7 +7,6 @@ set -u
NAME=Mapbox
OUTPUT=build/osx/pkg
OSX_SDK_VERSION=`xcrun --sdk macosx --show-sdk-version`
-LIBUV_VERSION=1.7.5
if [[ ${#} -eq 0 ]]; then # e.g. "make xpackage"
BUILDTYPE="Release"
diff --git a/scripts/main.mk b/scripts/main.mk
index 0891734543..d71bccc2e9 100644
--- a/scripts/main.mk
+++ b/scripts/main.mk
@@ -74,6 +74,7 @@ GYP_FLAGS += -Dhost=$(HOST)
GYP_FLAGS += -Iconfig/$(HOST_SLUG).gypi
GYP_FLAGS += -Dplatform_lib=$(PLATFORM)
GYP_FLAGS += -Dhttp_lib=$(HTTP)
+GYP_FLAGS += -Dloop_lib=$(LOOP)
GYP_FLAGS += -Dasset_lib=$(ASSET)
GYP_FLAGS += -Dheadless_lib=$(HEADLESS)
GYP_FLAGS += -Dtest=$(BUILD_TEST)
@@ -184,6 +185,7 @@ print-env: $(SUBMODULES)
@printf "platform=$(COLOR_CYAN)%s$(FORMAT_END) " $(PLATFORM)
@printf "asset=$(COLOR_CYAN)%s$(FORMAT_END) " $(ASSET)
@printf "http=$(COLOR_CYAN)%s$(FORMAT_END) " $(HTTP)
+ @printf "loop=$(COLOR_CYAN)%s$(FORMAT_END) " $(LOOP)
@printf "\n"
# Never remove intermediate files