summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <tmpsantos@gmail.com>2018-10-25 18:47:24 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2018-11-02 15:46:17 +0200
commit7f7f74871a3107e7ec667d26a1176f8a93e3f597 (patch)
tree5bb54b64228769a63ed7944ff1afd47ace640ba6
parent9618b8cf92a037aaced382892106350ddf67ffa3 (diff)
downloadqtlocation-mapboxgl-upstream/tmpsantos-platform.tar.gz
-rw-r--r--CMakeLists.txt2
-rw-r--r--include/mbgl/platform/factory.hpp6
-rw-r--r--include/mbgl/platform/platform_run_loop.hpp17
-rw-r--r--include/mbgl/util/run_loop.hpp7
-rw-r--r--platform/default/run_loop.cpp97
5 files changed, 64 insertions, 65 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a48e2c2b01..5dbf320c65 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,7 +24,7 @@ option(WITH_COVERAGE "Enable coverage reports" OFF)
option(WITH_OSMESA "Use OSMesa headless backend" OFF)
option(WITH_EGL "Use EGL backend" OFF)
option(WITH_NODEJS "Download test dependencies like NPM and Node.js" ON)
-option(WITH_ERROR "Add -Werror flag to build (turns warnings into errors)" ON)
+option(WITH_ERROR "Add -Werror flag to build (turns warnings into errors)" OFF)
if (WITH_ERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
diff --git a/include/mbgl/platform/factory.hpp b/include/mbgl/platform/factory.hpp
index d8615b424f..b9274457c0 100644
--- a/include/mbgl/platform/factory.hpp
+++ b/include/mbgl/platform/factory.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <mbgl/platform/platform_run_loop.hpp>
+
#include <memory>
namespace mbgl {
@@ -21,9 +23,9 @@ public:
*
* @return the platform run loop.
*/
- static std::unique_ptr<PlatformRunLoop> runLoop();
+ static std::unique_ptr<PlatformRunLoop> createRunLoop();
PlatformFactory() = delete;
-}
+};
} // namespace mbgl
diff --git a/include/mbgl/platform/platform_run_loop.hpp b/include/mbgl/platform/platform_run_loop.hpp
index c3b3d4c198..dce328f5d5 100644
--- a/include/mbgl/platform/platform_run_loop.hpp
+++ b/include/mbgl/platform/platform_run_loop.hpp
@@ -1,16 +1,27 @@
#pragma once
-#include <mbgl/util/run_loop.hpp>
+#include <functional>
namespace mbgl {
-class PlatformRunLoop
+class PlatformRunLoop {
public:
+ virtual ~PlatformRunLoop() = default;
+
+ virtual void *handle() = 0;
+
virtual void run() = 0;
virtual void runOnce() = 0;
virtual void stop() = 0;
- virtual void addWatch(int fd, Event, std::function<void(int, util::RunLoop::Event)>&& callback) = 0;
+ enum class Event : uint8_t {
+ None = 0,
+ Read = 1,
+ Write = 2,
+ ReadWrite = Read | Write,
+ };
+
+ virtual void addWatch(int fd, Event, std::function<void(int, Event)>&& callback) = 0;
virtual void removeWatch(int fd) = 0;
};
diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp
index c719f978ed..f1049594f1 100644
--- a/include/mbgl/util/run_loop.hpp
+++ b/include/mbgl/util/run_loop.hpp
@@ -2,6 +2,7 @@
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/actor/mailbox.hpp>
+#include <mbgl/platform/factory.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/work_task.hpp>
@@ -14,9 +15,6 @@
#include <mutex>
namespace mbgl {
-
-class PlatformRunLoop;
-
namespace util {
class RunLoop : public Scheduler,
@@ -43,6 +41,7 @@ public:
~RunLoop() override;
static RunLoop* Get();
+ static void* getLoopHandle();
void run();
void runOnce();
@@ -122,7 +121,7 @@ private:
Queue highPriorityQueue;
std::mutex mutex;
- std::unique_ptr<PlatformRunLoop> impl;
+ std::unique_ptr<mbgl::PlatformRunLoop> impl;
};
} // namespace util
diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp
index 868ee72114..1ead203af2 100644
--- a/platform/default/run_loop.cpp
+++ b/platform/default/run_loop.cpp
@@ -1,7 +1,4 @@
-#include <mbgl/util/run_loop.hpp>
-#include <mbgl/util/async_task.hpp>
-#include <mbgl/util/thread_local.hpp>
-#include <mbgl/actor/scheduler.hpp>
+#include <mbgl/platform/platform_run_loop.hpp>
#include <uv.h>
@@ -16,22 +13,21 @@ void dummyCallback(uv_async_t*) {}
} // namespace
namespace mbgl {
-namespace util {
struct Watch {
static void onEvent(uv_poll_t* poll, int, int event) {
auto watch = reinterpret_cast<Watch*>(poll->data);
- RunLoop::Event watchEvent = RunLoop::Event::None;
+ PlatformRunLoop::Event watchEvent = PlatformRunLoop::Event::None;
switch (event) {
case UV_READABLE:
- watchEvent = RunLoop::Event::Read;
+ watchEvent = PlatformRunLoop::Event::Read;
break;
case UV_WRITABLE:
- watchEvent = RunLoop::Event::Write;
+ watchEvent = PlatformRunLoop::Event::Write;
break;
case UV_READABLE | UV_WRITABLE:
- watchEvent = RunLoop::Event::ReadWrite;
+ watchEvent = PlatformRunLoop::Event::ReadWrite;
break;
}
@@ -46,16 +42,16 @@ struct Watch {
uv_poll_t poll;
int fd;
- std::function<void(int, RunLoop::Event)> eventCallback;
+ std::function<void(int, PlatformRunLoop::Event)> eventCallback;
std::function<void()> closeCallback;
};
-RunLoop* RunLoop::Get() {
+RunLoop* PlatformRunLoop::Get() {
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}
-class RunLoop::Impl {
+class DefaultRunLoop : public PlatformRunLoop {
public:
void closeHolder() {
uv_close(holderHandle(), [](uv_handle_t* h) {
@@ -70,45 +66,45 @@ public:
uv_loop_t *loop = nullptr;
uv_async_t* holder = new uv_async_t;
- RunLoop::Type type;
+ PlatformRunLoop::Type type;
std::unique_ptr<AsyncTask> async;
std::unordered_map<int, std::unique_ptr<Watch>> watchPoll;
};
-RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {
+PlatformRunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {
switch (type) {
case Type::New:
- impl->loop = new uv_loop_t;
- if (uv_loop_init(impl->loop) != 0) {
+ loop = new uv_loop_t;
+ if (uv_loop_init(loop) != 0) {
throw std::runtime_error("Failed to initialize loop.");
}
break;
case Type::Default:
- impl->loop = uv_default_loop();
+ loop = uv_default_loop();
break;
}
// Just for holding a ref to the main loop and keep
// it alive as required by libuv.
- if (uv_async_init(impl->loop, impl->holder, dummyCallback) != 0) {
+ if (uv_async_init(loop, holder, dummyCallback) != 0) {
throw std::runtime_error("Failed to initialize async.");
}
- impl->type = type;
+ type = type;
Scheduler::SetCurrent(this);
- impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
+ async = std::make_unique<AsyncTask>(std::bind(&PlatformRunLoop::process, this));
}
-RunLoop::~RunLoop() {
+PlatformRunLoop::~RunLoop() {
Scheduler::SetCurrent(nullptr);
// Close the dummy handle that we have
// just to keep the main loop alive.
- impl->closeHolder();
+ closeHolder();
- if (impl->type == Type::Default) {
+ if (type == Type::Default) {
return;
}
@@ -116,53 +112,47 @@ RunLoop::~RunLoop() {
// close callbacks have been called. Not needed
// for the default main loop because it is only
// closed when the application exits.
- impl->async.reset();
+ async.reset();
runOnce();
- if (uv_loop_close(impl->loop) == UV_EBUSY) {
+ if (uv_loop_close(loop) == UV_EBUSY) {
assert(false && "Failed to close loop.");
}
- delete impl->loop;
+ delete loop;
}
-LOOP_HANDLE RunLoop::getLoopHandle() {
- return Get()->impl->loop;
+LOOP_HANDLE PlatformRunLoop::getLoopHandle() {
+ return Get()->loop;
}
-void RunLoop::wake() {
- impl->async->send();
+void PlatformRunLoop::wake() {
+ async->send();
}
-void RunLoop::run() {
- MBGL_VERIFY_THREAD(tid);
-
- uv_ref(impl->holderHandle());
- uv_run(impl->loop, UV_RUN_DEFAULT);
+void PlatformRunLoop::run() {
+ uv_ref(holderHandle());
+ uv_run(loop, UV_RUN_DEFAULT);
}
-void RunLoop::runOnce() {
- MBGL_VERIFY_THREAD(tid);
-
- uv_run(impl->loop, UV_RUN_NOWAIT);
+void PlatformRunLoop::runOnce() {
+ uv_run(loop, UV_RUN_NOWAIT);
}
-void RunLoop::stop() {
- invoke([&] { uv_unref(impl->holderHandle()); });
+void PlatformRunLoop::stop() {
+ invoke([&] { uv_unref(holderHandle()); });
}
-void RunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& callback) {
- MBGL_VERIFY_THREAD(tid);
-
+void PlatformRunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& callback) {
Watch *watch = nullptr;
- auto watchPollIter = impl->watchPoll.find(fd);
+ auto watchPollIter = watchPoll.find(fd);
- if (watchPollIter == impl->watchPoll.end()) {
+ if (watchPollIter == watchPoll.end()) {
std::unique_ptr<Watch> watchPtr = std::make_unique<Watch>();
watch = watchPtr.get();
- impl->watchPoll[fd] = std::move(watchPtr);
+ watchPoll[fd] = std::move(watchPtr);
- if (uv_poll_init(impl->loop, &watch->poll, fd)) {
+ if (uv_poll_init(loop, &watch->poll, fd)) {
throw std::runtime_error("Failed to init poll on file descriptor.");
}
} else {
@@ -193,16 +183,14 @@ void RunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& ca
}
}
-void RunLoop::removeWatch(int fd) {
- MBGL_VERIFY_THREAD(tid);
-
- auto watchPollIter = impl->watchPoll.find(fd);
- if (watchPollIter == impl->watchPoll.end()) {
+void PlatformRunLoop::removeWatch(int fd) {
+ auto watchPollIter = watchPoll.find(fd);
+ if (watchPollIter == watchPoll.end()) {
return;
}
Watch* watch = watchPollIter->second.release();
- impl->watchPoll.erase(watchPollIter);
+ watchPoll.erase(watchPollIter);
watch->closeCallback = [watch] {
delete watch;
@@ -215,5 +203,4 @@ void RunLoop::removeWatch(int fd) {
uv_close(reinterpret_cast<uv_handle_t*>(&watch->poll), &Watch::onClose);
}
-} // namespace util
} // namespace mbgl