summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2020-04-23 16:04:37 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-04-23 16:04:37 +0300
commitcd966c2ba6c3b3dbecd5c81627aa5a4ca59a40c9 (patch)
tree590d71c21601b81ac78813ea7b3d6a124db78cdf
parent4d01b54408be00f7a41608b07a949d5555e26994 (diff)
downloadqtlocation-mapboxgl-upstream/alexshalamov_backport_mailbox_fix.tar.gz
Backport '[core] Use weak scheduler inside mailbox'upstream/alexshalamov_backport_mailbox_fix
Backport: 40d42f339216e21e04b0ad2509de2fb4f6483103
-rw-r--r--include/mbgl/actor/mailbox.hpp4
-rw-r--r--src/mbgl/actor/mailbox.cpp28
2 files changed, 17 insertions, 15 deletions
diff --git a/include/mbgl/actor/mailbox.hpp b/include/mbgl/actor/mailbox.hpp
index 23c579917a..d2510e9270 100644
--- a/include/mbgl/actor/mailbox.hpp
+++ b/include/mbgl/actor/mailbox.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include <mbgl/util/optional.hpp>
+#include <mapbox/std/weak.hpp>
#include <memory>
#include <mutex>
@@ -36,7 +36,7 @@ public:
static void maybeReceive(std::weak_ptr<Mailbox>);
private:
- optional<Scheduler*> scheduler;
+ mapbox::base::WeakPtr<Scheduler> weakScheduler;
std::recursive_mutex receivingMutex;
std::mutex pushingMutex;
diff --git a/src/mbgl/actor/mailbox.cpp b/src/mbgl/actor/mailbox.cpp
index 8ee8dca114..e4e59efd77 100644
--- a/src/mbgl/actor/mailbox.cpp
+++ b/src/mbgl/actor/mailbox.cpp
@@ -9,26 +9,25 @@ namespace mbgl {
Mailbox::Mailbox() {
}
-Mailbox::Mailbox(Scheduler& scheduler_)
- : scheduler(&scheduler_) {
-}
+Mailbox::Mailbox(Scheduler& scheduler_) : weakScheduler(scheduler_.makeWeakPtr()) {}
void Mailbox::open(Scheduler& scheduler_) {
- assert(!scheduler);
+ assert(!weakScheduler);
// As with close(), block until neither receive() nor push() are in progress, and acquire the two
// mutexes in the same order.
std::lock_guard<std::recursive_mutex> receivingLock(receivingMutex);
std::lock_guard<std::mutex> pushingLock(pushingMutex);
- scheduler = &scheduler_;
+ weakScheduler = scheduler_.makeWeakPtr();
if (closed) {
return;
}
if (!queue.empty()) {
- (*scheduler)->schedule(shared_from_this());
+ auto guard = weakScheduler.lock();
+ if (weakScheduler) weakScheduler->schedule(shared_from_this());
}
}
@@ -44,8 +43,9 @@ void Mailbox::close() {
closed = true;
}
-bool Mailbox::isOpen() const { return bool(scheduler); }
-
+bool Mailbox::isOpen() const {
+ return bool(weakScheduler);
+}
void Mailbox::push(std::unique_ptr<Message> message) {
std::lock_guard<std::mutex> pushingLock(pushingMutex);
@@ -57,15 +57,17 @@ void Mailbox::push(std::unique_ptr<Message> message) {
std::lock_guard<std::mutex> queueLock(queueMutex);
bool wasEmpty = queue.empty();
queue.push(std::move(message));
- if (wasEmpty && scheduler) {
- (*scheduler)->schedule(shared_from_this());
+ auto guard = weakScheduler.lock();
+ if (wasEmpty && weakScheduler) {
+ weakScheduler->schedule(shared_from_this());
}
}
void Mailbox::receive() {
std::lock_guard<std::recursive_mutex> receivingLock(receivingMutex);
-
- assert(scheduler);
+
+ auto guard = weakScheduler.lock();
+ assert(weakScheduler);
if (closed) {
return;
@@ -85,7 +87,7 @@ void Mailbox::receive() {
(*message)();
if (!wasEmpty) {
- (*scheduler)->schedule(shared_from_this());
+ weakScheduler->schedule(shared_from_this());
}
}