diff options
author | Anand Thakker <github@anandthakker.net> | 2018-06-14 15:41:53 -0400 |
---|---|---|
committer | Anand Thakker <github@anandthakker.net> | 2018-06-14 15:41:53 -0400 |
commit | 5cdacf168b43a6c4c4a8e3a502186e5c7d6523b3 (patch) | |
tree | 6c860d4e1a5aa8699f6702691a138dc482ed34cb /include/mbgl/util/thread.hpp | |
parent | 40bbc0f4a13e2560c5aaaa62646c6d23f99a5d6d (diff) | |
download | qtlocation-mapboxgl-5cdacf168b43a6c4c4a8e3a502186e5c7d6523b3.tar.gz |
Simplify design for "holding" mailbox
Diffstat (limited to 'include/mbgl/util/thread.hpp')
-rw-r--r-- | include/mbgl/util/thread.hpp | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp index b5f79aa28d..0cd12f6e95 100644 --- a/include/mbgl/util/thread.hpp +++ b/include/mbgl/util/thread.hpp @@ -19,11 +19,6 @@ namespace mbgl { namespace util { -class NoopScheduler : public Scheduler { -public: - void schedule(std::weak_ptr<Mailbox>) override {} -}; - // Manages a thread with `Object`. // Upon creation of this object, it launches a thread and creates an object of type `Object` @@ -49,14 +44,13 @@ public: std::unique_ptr<std::promise<void>> running_ = std::make_unique<std::promise<void>>(); running = running_->get_future(); - // Pre-create a mailbox for this actor, using a NoopScheduler that - // leaves the mailbox's queue unconsumed. - // Once the RunLoop on the target thread has been created, we'll replace - // the NoopScheduler with the RunLoop. Meanwhile, this allows us to - // immediately provide ActorRef using this mailbox, with any messages - // sent to them being queued in the holding mailbox until the thread is - // up and running. - std::shared_ptr<Mailbox> mailbox_ = std::make_shared<Mailbox>(noopScheduler); + // Pre-create a "holding" mailbox for this actor, whose messages are + // guaranteed not to be consumed until we explicitly call start(), which + // we'll do on the target thread, once its RunLoop and Object instance + // are ready. + // Meanwhile, this allows us to immediately provide ActorRef using this + // mailbox to queue any messages that come in before the thread is ready. + std::shared_ptr<Mailbox> mailbox_ = std::make_shared<Mailbox>(); mailbox = mailbox_; auto tuple = std::make_tuple(std::forward<Args>(args)...); @@ -81,7 +75,7 @@ public: // Replace the NoopScheduler on the mailbox with the RunLoop to // begin actually processing messages. - actor->mailbox->setScheduler(this); + actor->mailbox->start(this); runningPromise->set_value(); @@ -178,7 +172,6 @@ private: return new (&actorStorage) Actor<Object>(std::move(sharedMailbox), std::move(std::get<I>(std::forward<ArgsTuple>(args)))...); } - NoopScheduler noopScheduler; std::weak_ptr<Mailbox> mailbox; std::aligned_storage<sizeof(Actor<Object>)> actorStorage; |