Commit ef52bd9a authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Fix builds on windows

Summary:
Move ContextPrimaryPtr definition outside of the function.
Rename MasterPtr to PrimaryPtr.

Reviewed By: Orvid

Differential Revision: D23836006

fbshipit-source-id: d2ad229809fb260c485a7349eca7378e22dc66c4
parent 39e09616
...@@ -94,40 +94,33 @@ class ObserverCreatorContext { ...@@ -94,40 +94,33 @@ class ObserverCreatorContext {
} // namespace detail } // namespace detail
// This master shared_ptr allows grabbing derived weak_ptrs, pointing to the
// the same Context object, but using a separate reference count. Primary
// shared_ptr destructor then blocks until all shared_ptrs obtained from
// derived weak_ptrs are released.
template <typename Observable, typename Traits> template <typename Observable, typename Traits>
template <typename... Args> class ObserverCreator<Observable, Traits>::ContextPrimaryPtr {
ObserverCreator<Observable, Traits>::ObserverCreator(Args&&... args)
: context_(std::make_shared<Context>(std::forward<Args>(args)...)) {}
template <typename Observable, typename Traits>
Observer<typename ObserverCreator<Observable, Traits>::T>
ObserverCreator<Observable, Traits>::getObserver() && {
// This master shared_ptr allows grabbing derived weak_ptrs, pointing to the
// the same Context object, but using a separate reference count. Master
// shared_ptr destructor then blocks until all shared_ptrs obtained from
// derived weak_ptrs are released.
class ContextMasterPointer {
public: public:
explicit ContextMasterPointer(std::shared_ptr<Context> context) explicit ContextPrimaryPtr(std::shared_ptr<Context> context)
: contextMaster_(std::move(context)), : contextPrimary_(std::move(context)),
context_( context_(
contextMaster_.get(), contextPrimary_.get(),
[destroyBaton = destroyBaton_](Context*) { [destroyBaton = destroyBaton_](Context*) {
destroyBaton->post(); destroyBaton->post();
}) {} }) {}
~ContextMasterPointer() { ~ContextPrimaryPtr() {
if (context_) { if (context_) {
context_.reset(); context_.reset();
destroyBaton_->wait(); destroyBaton_->wait();
} }
} }
ContextMasterPointer(const ContextMasterPointer&) = delete; ContextPrimaryPtr(const ContextPrimaryPtr&) = delete;
ContextMasterPointer(ContextMasterPointer&&) = default; ContextPrimaryPtr(ContextPrimaryPtr&&) = default;
ContextMasterPointer& operator=(const ContextMasterPointer&) = delete; ContextPrimaryPtr& operator=(const ContextPrimaryPtr&) = delete;
ContextMasterPointer& operator=(ContextMasterPointer&&) = default; ContextPrimaryPtr& operator=(ContextPrimaryPtr&&) = default;
Context* operator->() const { Context* operator->() const {
return contextMaster_.get(); return contextPrimary_.get();
} }
std::weak_ptr<Context> get_weak() { std::weak_ptr<Context> get_weak() {
...@@ -137,18 +130,27 @@ ObserverCreator<Observable, Traits>::getObserver() && { ...@@ -137,18 +130,27 @@ ObserverCreator<Observable, Traits>::getObserver() && {
private: private:
std::shared_ptr<folly::Baton<>> destroyBaton_{ std::shared_ptr<folly::Baton<>> destroyBaton_{
std::make_shared<folly::Baton<>>()}; std::make_shared<folly::Baton<>>()};
std::shared_ptr<Context> contextMaster_; std::shared_ptr<Context> contextPrimary_;
std::shared_ptr<Context> context_; std::shared_ptr<Context> context_;
}; };
template <typename Observable, typename Traits>
template <typename... Args>
ObserverCreator<Observable, Traits>::ObserverCreator(Args&&... args)
: context_(std::make_shared<Context>(std::forward<Args>(args)...)) {}
template <typename Observable, typename Traits>
Observer<typename ObserverCreator<Observable, Traits>::T>
ObserverCreator<Observable, Traits>::getObserver() && {
// We want to make sure that Context can only be destroyed when Core is // We want to make sure that Context can only be destroyed when Core is
// destroyed. So we have to avoid the situation when subscribe callback is // destroyed. So we have to avoid the situation when subscribe callback is
// locking Context shared_ptr and remains the last to release it. // locking Context shared_ptr and remains the last to release it.
// We solve this by having Core hold the master shared_ptr and subscription // We solve this by having Core hold the master shared_ptr and subscription
// callback gets derived weak_ptr. // callback gets derived weak_ptr.
ContextMasterPointer contextMaster(context_); ContextPrimaryPtr contextPrimary(context_);
auto contextWeak = contextMaster.get_weak(); auto contextWeak = contextPrimary.get_weak();
auto observer = makeObserver( auto observer = makeObserver(
[context = std::move(contextMaster)]() { return context->get(); }); [context = std::move(contextPrimary)]() { return context->get(); });
context_->setCore(observer.core_); context_->setCore(observer.core_);
context_->subscribe([contextWeak = std::move(contextWeak)] { context_->subscribe([contextWeak = std::move(contextWeak)] {
......
...@@ -58,6 +58,7 @@ class ObserverCreator { ...@@ -58,6 +58,7 @@ class ObserverCreator {
private: private:
using Context = detail::ObserverCreatorContext<Observable, Traits>; using Context = detail::ObserverCreatorContext<Observable, Traits>;
class ContextPrimaryPtr;
std::shared_ptr<Context> context_; std::shared_ptr<Context> context_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment