Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup mbgl/actor/mailbox* implementation for repetition in ensuring valid weakScheduler exists before usage #2733

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions include/mbgl/actor/mailbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>

#include <mapbox/std/weak.hpp>
Expand Down Expand Up @@ -39,10 +40,8 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
void push(std::unique_ptr<Message>);
void receive();

static void maybeReceive(const std::weak_ptr<Mailbox>&);
static std::function<void()> makeClosure(std::weak_ptr<Mailbox>);

private:
void scheduleToRecieve(const std::optional<util::SimpleIdentity>& tag = std::nullopt);
enum class State : uint32_t {
Idle = 0,
Processing,
Expand Down
42 changes: 18 additions & 24 deletions src/mbgl/actor/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ void Mailbox::open(Scheduler& scheduler_) {
weakScheduler = scheduler_.makeWeakPtr();

if (!queue.empty()) {
auto guard = weakScheduler.lock();
if (weakScheduler) {
weakScheduler->schedule(makeClosure(shared_from_this()));
}
scheduleToRecieve();
}
}

Expand Down Expand Up @@ -107,11 +104,8 @@ void Mailbox::push(std::unique_ptr<Message> message) {
}

if (wasEmpty) {
auto guard = weakScheduler.lock();
if (weakScheduler) {
MLN_TRACE_ZONE(schedule)
weakScheduler->schedule(schedulerTag, makeClosure(shared_from_this()));
}
MLN_TRACE_ZONE(schedule)
scheduleToRecieve(schedulerTag);
}
}

Expand Down Expand Up @@ -151,25 +145,25 @@ void Mailbox::receive() {
// If there are more messages in the queue and the scheduler
// is still active, create a new task to handle the next one
if (!wasEmpty) {
auto guard = weakScheduler.lock();
if (weakScheduler) {
weakScheduler->schedule(makeClosure(shared_from_this()));
}
scheduleToRecieve();
}
}

// static
void Mailbox::maybeReceive(const std::weak_ptr<Mailbox>& mailbox) {
if (auto locked = mailbox.lock()) {
locked->receive();
void Mailbox::scheduleToRecieve(const std::optional<util::SimpleIdentity>& tag) {
auto guard = weakScheduler.lock();
if (weakScheduler) {
std::weak_ptr<Mailbox> mailbox = shared_from_this();
auto setToRecieve = [mbox = std::move(mailbox)]() {
if (auto locked = mbox.lock()) {
locked->receive();
}
};
if (tag) {
weakScheduler->schedule(*tag, std::move(setToRecieve));
} else {
weakScheduler->schedule(std::move(setToRecieve));
}
}
}

// static
std::function<void()> Mailbox::makeClosure(std::weak_ptr<Mailbox> mailbox) {
return [mailbox = std::move(mailbox)]() {
maybeReceive(mailbox);
};
}

} // namespace mbgl
Loading