summaryrefslogtreecommitdiff
path: root/src/mbgl/util/run_loop.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/util/run_loop.cpp')
-rw-r--r--src/mbgl/util/run_loop.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/mbgl/util/run_loop.cpp b/src/mbgl/util/run_loop.cpp
new file mode 100644
index 0000000000..fd9ad43060
--- /dev/null
+++ b/src/mbgl/util/run_loop.cpp
@@ -0,0 +1,39 @@
+#include <mbgl/util/run_loop.hpp>
+
+namespace mbgl {
+namespace util {
+
+uv::tls<RunLoop> RunLoop::current;
+
+RunLoop::RunLoop()
+ : async(*loop, std::bind(&RunLoop::process, this)) {
+}
+
+void RunLoop::withMutex(std::function<void()>&& fn) {
+ std::lock_guard<std::mutex> lock(mutex);
+ fn();
+}
+
+void RunLoop::process() {
+ Queue queue_;
+ withMutex([&] { queue_.swap(queue); });
+
+ while (!queue_.empty()) {
+ (*(queue_.front()))();
+ queue_.pop();
+ }
+}
+
+void RunLoop::run() {
+ assert(!current.get());
+ current.set(this);
+ loop.run();
+ current.set(nullptr);
+}
+
+void RunLoop::stop() {
+ invoke([&] { async.unref(); });
+}
+
+}
+}