return !instance_weak_.expired();
}
+template <typename T>
+void SingletonHolder<T>::preDestroyInstance(
+ ReadMostlyMainPtrDeleter<>& deleter) {
+ instance_copy_ = instance_;
+ deleter.add(std::move(instance_));
+}
+
template <typename T>
void SingletonHolder<T>::destroyInstance() {
state_ = SingletonHolderState::Dead;
instance_.reset();
+ instance_copy_.reset();
if (destroy_baton_) {
constexpr std::chrono::seconds kDestroyWaitTime{5};
auto wait_result = destroy_baton_->timed_wait(
CHECK_GE(singletons_.size(), creation_order_.size());
+ // Release all ReadMostlyMainPtrs at once
+ {
+ ReadMostlyMainPtrDeleter<> deleter;
+ for (auto& singleton_type : creation_order_) {
+ singletons_[singleton_type]->preDestroyInstance(deleter);
+ }
+ }
+
for (auto type_iter = creation_order_.rbegin();
type_iter != creation_order_.rend();
++type_iter) {
virtual bool hasLiveInstance() = 0;
virtual void createInstance() = 0;
virtual bool creationStarted() = 0;
+ virtual void preDestroyInstance(ReadMostlyMainPtrDeleter<>&) = 0;
virtual void destroyInstance() = 0;
private:
virtual bool hasLiveInstance() override;
virtual void createInstance() override;
virtual bool creationStarted() override;
+ virtual void preDestroyInstance(ReadMostlyMainPtrDeleter<>&) override;
virtual void destroyInstance() override;
private:
// holds a ReadMostlyMainPtr to singleton instance, set when state is changed
// from Dead to Living. Reset when state is changed from Living to Dead.
folly::ReadMostlyMainPtr<T> instance_;
+ // used to release all ReadMostlyMainPtrs at once
+ folly::ReadMostlySharedPtr<T> instance_copy_;
// weak_ptr to the singleton instance, set when state is changed from Dead
// to Living. We never write to this object after initialization, so it is
// safe to read it from different threads w/o synchronization if we know