summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-10-04 12:30:41 +0300
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-10-04 17:42:21 +0300
commitbef635765a1bbec14d7732856c38c037ea8add6a (patch)
tree47443222a5982031b4c9ecd531bea14edc61a8c8 /src
parent198e3453394ccb2b1f7db72d1858cfd18e302a1e (diff)
downloadqtlocation-mapboxgl-bef635765a1bbec14d7732856c38c037ea8add6a.tar.gz
[core] Decouple Scheduler interface from actor model
So that it is possible to schedule normal `std::function` and use `mapbox::base::WeakPtr`.
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/actor/mailbox.cpp12
-rw-r--r--src/mbgl/util/thread_pool.cpp9
-rw-r--r--src/mbgl/util/thread_pool.hpp4
3 files changed, 15 insertions, 10 deletions
diff --git a/src/mbgl/actor/mailbox.cpp b/src/mbgl/actor/mailbox.cpp
index dfe0520790..070e14bdb0 100644
--- a/src/mbgl/actor/mailbox.cpp
+++ b/src/mbgl/actor/mailbox.cpp
@@ -27,7 +27,7 @@ void Mailbox::open(Scheduler& scheduler_) {
}
if (!queue.empty()) {
- (*scheduler)->schedule(shared_from_this());
+ (*scheduler)->schedule(makeClosure(shared_from_this()));
}
}
@@ -57,7 +57,7 @@ void Mailbox::push(std::unique_ptr<Message> message) {
bool wasEmpty = queue.empty();
queue.push(std::move(message));
if (wasEmpty && scheduler) {
- (*scheduler)->schedule(shared_from_this());
+ (*scheduler)->schedule(makeClosure(shared_from_this()));
}
}
@@ -84,14 +84,20 @@ void Mailbox::receive() {
(*message)();
if (!wasEmpty) {
- (*scheduler)->schedule(shared_from_this());
+ (*scheduler)->schedule(makeClosure(shared_from_this()));
}
}
+// static
void Mailbox::maybeReceive(std::weak_ptr<Mailbox> mailbox) {
if (auto locked = mailbox.lock()) {
locked->receive();
}
}
+// static
+std::function<void()> Mailbox::makeClosure(std::weak_ptr<Mailbox> mailbox) {
+ return [mailbox]() { maybeReceive(mailbox); };
+}
+
} // namespace mbgl
diff --git a/src/mbgl/util/thread_pool.cpp b/src/mbgl/util/thread_pool.cpp
index e839d1b4be..d8df0cd575 100644
--- a/src/mbgl/util/thread_pool.cpp
+++ b/src/mbgl/util/thread_pool.cpp
@@ -26,11 +26,10 @@ ThreadPool::ThreadPool(std::size_t count) {
return;
}
- auto mailbox = queue.front();
+ auto function = std::move(queue.front());
queue.pop();
lock.unlock();
-
- Mailbox::maybeReceive(mailbox);
+ if (function) function();
}
});
}
@@ -49,10 +48,10 @@ ThreadPool::~ThreadPool() {
}
}
-void ThreadPool::schedule(std::weak_ptr<Mailbox> mailbox) {
+void ThreadPool::schedule(std::function<void()> fn) {
{
std::lock_guard<std::mutex> lock(mutex);
- queue.push(mailbox);
+ queue.push(std::move(fn));
}
cv.notify_one();
diff --git a/src/mbgl/util/thread_pool.hpp b/src/mbgl/util/thread_pool.hpp
index 509fd06061..96fc13bda5 100644
--- a/src/mbgl/util/thread_pool.hpp
+++ b/src/mbgl/util/thread_pool.hpp
@@ -15,11 +15,11 @@ public:
explicit ThreadPool(std::size_t count);
~ThreadPool() override;
- void schedule(std::weak_ptr<Mailbox>) override;
+ void schedule(std::function<void()>) override;
private:
std::vector<std::thread> threads;
- std::queue<std::weak_ptr<Mailbox>> queue;
+ std::queue<std::function<void()>> queue;
std::mutex mutex;
std::condition_variable cv;
bool terminate{ false };