From 13cd75f099c92b6f0862db00eee8ff4a78a73316 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 4 Jul 2016 20:23:49 +0300 Subject: [PATCH] [android] Do not create Timers on Default main loops Not supported, because we don't have control over the Looper timeout like we do for Loopers we create. On the main thread, Android takes care of making the application sleep and sets an arbitrary timeout. This could be properly fixed by implementing timers in Java, but we don't really have the use case of timers in the main thread. --- platform/android/src/run_loop.cpp | 2 ++ platform/android/src/run_loop_impl.hpp | 1 + platform/android/src/timer.cpp | 4 +++- test/util/timer.cpp | 12 ++++++------ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 6c6d3555e..1656565e8 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -80,6 +80,8 @@ RunLoop::Impl::Impl(RunLoop* runLoop_, RunLoop::Type type) : runLoop(runLoop_) { case Type::Default: ret = ALooper_addFd(loop, fds[PIPE_OUT], ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, looperCallbackDefault, this); + running = true; + isDefaultLoop = true; break; } diff --git a/platform/android/src/run_loop_impl.hpp b/platform/android/src/run_loop_impl.hpp index cb553d1f8..9622efb36 100644 --- a/platform/android/src/run_loop_impl.hpp +++ b/platform/android/src/run_loop_impl.hpp @@ -41,6 +41,7 @@ public: ALooper* loop = nullptr; RunLoop* runLoop = nullptr; util::Atomic running; + bool isDefaultLoop = false; private: friend RunLoop; diff --git a/platform/android/src/timer.cpp b/platform/android/src/timer.cpp index 81cf0a68b..6c0dc81ec 100644 --- a/platform/android/src/timer.cpp +++ b/platform/android/src/timer.cpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace mbgl { @@ -11,6 +12,7 @@ namespace util { class Timer::Impl : public RunLoop::Impl::Runnable { public: Impl() { + assert(!loop->isDefaultLoop); loop->initRunnable(this); } @@ -35,7 +37,7 @@ public: void reschedule() { if (repeat != Duration::zero()) { due = Clock::now() + repeat; - loop->addRunnable(this); + loop->wake(); } else { stop(); } diff --git a/test/util/timer.cpp b/test/util/timer.cpp index f6253ab6d..c37440ed4 100644 --- a/test/util/timer.cpp +++ b/test/util/timer.cpp @@ -10,7 +10,7 @@ using namespace mbgl::util; TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Basic)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); Timer timer; @@ -34,7 +34,7 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Basic)) { } TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Repeat)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); Timer timer; @@ -60,7 +60,7 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Repeat)) { } TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Stop)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); Timer timer1; Timer timer2; @@ -96,7 +96,7 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Stop)) { } TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(DestroyShouldStop)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); auto timer1 = std::make_unique(); Timer timer2; @@ -132,7 +132,7 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(DestroyShouldStop)) { } TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StartOverrides)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); Timer timer; @@ -166,7 +166,7 @@ TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StartOverrides)) { } TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(CanStopNonStartedTimer)) { - RunLoop loop; + RunLoop loop(RunLoop::Type::New); Timer timer; timer.stop();