summaryrefslogtreecommitdiff
path: root/include/mbgl/util/run_loop.hpp
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2015-09-01 11:09:19 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2015-12-01 00:00:09 +0200
commitd422c8cac4e436fb2fb1eeafbae5ed5d847e0b00 (patch)
treedd8efd2cd842820f3f13fddf40cc0039fb261363 /include/mbgl/util/run_loop.hpp
parent1d623322044c4650bb39c9224f596341a23f490f (diff)
downloadqtlocation-mapboxgl-d422c8cac4e436fb2fb1eeafbae5ed5d847e0b00.tar.gz
[core] Abstract main loop inside RunLoop class
Diffstat (limited to 'include/mbgl/util/run_loop.hpp')
-rw-r--r--include/mbgl/util/run_loop.hpp45
1 files changed, 33 insertions, 12 deletions
diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp
index 6113ac2215..4b98653556 100644
--- a/include/mbgl/util/run_loop.hpp
+++ b/include/mbgl/util/run_loop.hpp
@@ -1,6 +1,7 @@
#ifndef MBGL_UTIL_RUN_LOOP
#define MBGL_UTIL_RUN_LOOP
+#include <mbgl/util/async_task.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/work_task.hpp>
#include <mbgl/util/work_request.hpp>
@@ -15,19 +16,26 @@
namespace mbgl {
namespace util {
+typedef void * LOOP_HANDLE;
+
class RunLoop : private util::noncopyable {
public:
- RunLoop(uv_loop_t*);
+ enum class Type : uint8_t {
+ Default,
+ New,
+ };
+
+ RunLoop(Type type = Type::Default);
~RunLoop();
static RunLoop* Get() {
return current.get();
}
- static uv_loop_t* getLoop() {
- return current.get()->get();
- }
+ static LOOP_HANDLE getLoopHandle();
+ void run();
+ void runOnce();
void stop();
// Invoke fn(args...) on this RunLoop.
@@ -39,7 +47,7 @@ public:
std::move(tuple));
withMutex([&] { queue.push(task); });
- async.send();
+ async->send();
}
// Post the cancellable work fn(args...) to this RunLoop.
@@ -56,7 +64,7 @@ public:
flag);
withMutex([&] { queue.push(task); });
- async.send();
+ async->send();
return std::make_unique<WorkRequest>(task);
}
@@ -90,13 +98,11 @@ public:
flag);
withMutex([&] { queue.push(task); });
- async.send();
+ async->send();
return std::make_unique<WorkRequest>(task);
}
- uv_loop_t* get() { return async.get()->loop; }
-
private:
template <class F, class P>
class Invoker : public WorkTask {
@@ -143,12 +149,27 @@ private:
using Queue = std::queue<std::shared_ptr<WorkTask>>;
- void withMutex(std::function<void()>&&);
- void process();
+ void withMutex(std::function<void()>&& fn) {
+ std::lock_guard<std::mutex> lock(mutex);
+ fn();
+ }
+
+ void process() {
+ Queue queue_;
+ withMutex([&] { queue_.swap(queue); });
+
+ while (!queue_.empty()) {
+ (*(queue_.front()))();
+ queue_.pop();
+ }
+ }
Queue queue;
std::mutex mutex;
- uv::async async;
+ std::unique_ptr<AsyncTask> async;
+
+ class Impl;
+ std::unique_ptr<Impl> impl;
static uv::tls<RunLoop> current;
};