diff options
author | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-07-27 14:20:08 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-07-27 14:20:08 +0300 |
commit | a9d581b50145d5693c153c38628030a65dd75a49 (patch) | |
tree | 8136995159f6c6ddb5b1a0dc88faa1141aa2d7f8 | |
parent | a74e5b0d43a844be9844dd605f704634bc6114a0 (diff) | |
download | qtlocation-mapboxgl-upstream/tmpsantos-alarm.tar.gz |
[Android] Use Alarm for timers in the main threadupstream/tmpsantos-alarm
Needed because of the restrictions of using util::Timer in the main
thread on Android, i.e. we don't have control over the ALooper timeout.
-rw-r--r-- | platform/android/src/run_loop.cpp | 29 | ||||
-rw-r--r-- | platform/android/src/run_loop_impl.hpp | 3 |
2 files changed, 4 insertions, 28 deletions
diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 3c605b70e8..77f9896e05 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -1,9 +1,8 @@ #include "run_loop_impl.hpp" +#include <mbgl/util/alarm.hpp> #include <mbgl/util/platform.hpp> #include <mbgl/util/thread_local.hpp> -#include <mbgl/util/thread.hpp> -#include <mbgl/util/timer.hpp> #include <android/looper.h> @@ -56,28 +55,6 @@ int looperCallbackDefault(int fd, int, void* data) { namespace mbgl { namespace util { -// This is needed only for the RunLoop living on the main thread because of -// how we implement timers. We sleep on `ALooper_pollAll` until the next -// timeout, but on the main thread `ALooper_pollAll` is called by the activity -// automatically, thus we cannot set the timeout. Instead we wake the loop -// with an external file descriptor event coming from this thread. -// -// Usually an actor should not carry pointers to other threads, but in -// this case the RunLoop itself owns the Alarm and calling wake() is the most -// efficient way of waking up the RunLoop and it is also thread-safe. -class Alarm { -public: - Alarm(ActorRef<Alarm>, RunLoop::Impl* loop_) : loop(loop_) {} - - void set(const Milliseconds& timeout) { - alarm.start(timeout, mbgl::Duration::zero(), [this]() { loop->wake(); }); - } - -private: - Timer alarm; - RunLoop::Impl* loop; -}; - RunLoop::Impl::Impl(RunLoop* runLoop_, RunLoop::Type type) : runLoop(runLoop_) { using namespace mbgl::android; detach = attach_jni_thread(theJVM, &env, platform::getCurrentThreadName()); @@ -105,7 +82,7 @@ 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); - alarm = std::make_unique<Thread<Alarm>>("Alarm", this); + alarm = std::make_unique<Alarm>(); running = true; break; } @@ -193,7 +170,7 @@ Milliseconds RunLoop::Impl::processRunnables() { auto timeout = std::chrono::duration_cast<Milliseconds>(nextDue - now); if (alarm) { - alarm->actor().invoke(&Alarm::set, timeout); + alarm->start(timeout, [this] { wake(); }); } return timeout; diff --git a/platform/android/src/run_loop_impl.hpp b/platform/android/src/run_loop_impl.hpp index 15cbfa14ae..856a3daba6 100644 --- a/platform/android/src/run_loop_impl.hpp +++ b/platform/android/src/run_loop_impl.hpp @@ -15,7 +15,6 @@ struct ALooper; namespace mbgl { namespace util { -template <typename T> class Thread; class Alarm; class RunLoop::Impl { @@ -53,7 +52,7 @@ private: JNIEnv *env = nullptr; bool detach = false; - std::unique_ptr<Thread<Alarm>> alarm; + std::unique_ptr<Alarm> alarm; std::recursive_mutex mtx; std::list<Runnable*> runnables; |