Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Use weak scheduler inside mailbox
Browse files Browse the repository at this point in the history
There is no guarantee that scheduler is alive when message
is pushed to the mailbox.
  • Loading branch information
alexshalamov committed Jan 22, 2020
1 parent 1765a64 commit 40d42f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
4 changes: 3 additions & 1 deletion include/mbgl/actor/mailbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <mutex>
#include <queue>

#include <mapbox/weak.hpp>

namespace mbgl {

class Scheduler;
Expand Down Expand Up @@ -38,7 +40,7 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
static std::function<void()> makeClosure(std::weak_ptr<Mailbox>);

private:
optional<Scheduler*> scheduler;
mapbox::base::WeakPtr<Scheduler> weakScheduler;

std::recursive_mutex receivingMutex;
std::mutex pushingMutex;
Expand Down
30 changes: 16 additions & 14 deletions src/mbgl/actor/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ namespace mbgl {

Mailbox::Mailbox() = default;

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(makeClosure(shared_from_this()));
auto guard = weakScheduler.lock();
if (weakScheduler) weakScheduler->schedule(makeClosure(shared_from_this()));
}
}

Expand All @@ -43,8 +42,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);
Expand All @@ -56,15 +56,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(makeClosure(shared_from_this()));
auto guard = weakScheduler.lock();
if (wasEmpty && weakScheduler) {
weakScheduler->schedule(makeClosure(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;
Expand All @@ -84,7 +86,7 @@ void Mailbox::receive() {
(*message)();

if (!wasEmpty) {
(*scheduler)->schedule(makeClosure(shared_from_this()));
weakScheduler->schedule(makeClosure(shared_from_this()));
}
}

Expand Down

0 comments on commit 40d42f3

Please sign in to comment.