diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/actor/mailbox.cpp | 12 | ||||
-rw-r--r-- | src/mbgl/util/thread_pool.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/util/thread_pool.hpp | 4 |
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 }; |