Support PicoSpinLock on MSVC
[folly.git] / folly / Singleton-inl.h
index 0071ddf5fe752e1fe9edc9366756d97bc4b444d3..386b40bd96839fb1eb045c20fb8b4b15c93a42f6 100644 (file)
@@ -54,7 +54,7 @@ void SingletonHolder<T>::registerSingleton(CreateFunc c, TeardownFunc t) {
      * Singleton<int> b([] { return new int(4); });
      *
      */
-    singletonWarnDoubleRegistrationAndAbort(type_);
+    singletonWarnDoubleRegistrationAndAbort(type());
   }
 
   create_ = std::move(c);
@@ -66,11 +66,21 @@ void SingletonHolder<T>::registerSingleton(CreateFunc c, TeardownFunc t) {
 template <typename T>
 void SingletonHolder<T>::registerSingletonMock(CreateFunc c, TeardownFunc t) {
   if (state_ == SingletonHolderState::NotRegistered) {
-    LOG(FATAL)
-        << "Registering mock before singleton was registered: " << type_.name();
+    LOG(FATAL) << "Registering mock before singleton was registered: "
+               << type().name();
   }
   destroyInstance();
 
+  {
+    RWSpinLock::WriteHolder wh(&vault_.mutex_);
+
+    auto it = std::find(
+        vault_.creation_order_.begin(), vault_.creation_order_.end(), type());
+    if (it != vault_.creation_order_.end()) {
+      vault_.creation_order_.erase(it);
+    }
+  }
+
   std::lock_guard<std::mutex> entry_lock(mutex_);
 
   create_ = std::move(c);
@@ -89,7 +99,7 @@ T* SingletonHolder<T>::get() {
     throw std::runtime_error(
         "Raw pointer to a singleton requested after its destruction."
         " Singleton type is: " +
-        type_.name());
+        type().name());
   }
 
   return instance_ptr_;
@@ -125,11 +135,6 @@ folly::ReadMostlySharedPtr<T> SingletonHolder<T>::try_get_fast() {
   return instance_weak_fast_.lock();
 }
 
-template <typename T>
-TypeDescriptor SingletonHolder<T>::type() {
-  return type_;
-}
-
 template <typename T>
 bool SingletonHolder<T>::hasLiveInstance() {
   return !instance_weak_.expired();
@@ -140,11 +145,12 @@ void SingletonHolder<T>::destroyInstance() {
   state_ = SingletonHolderState::Dead;
   instance_.reset();
   if (destroy_baton_) {
+    constexpr std::chrono::seconds kDestroyWaitTime{5};
     auto wait_result = destroy_baton_->timed_wait(
       std::chrono::steady_clock::now() + kDestroyWaitTime);
     if (!wait_result) {
       print_destructor_stack_trace_->store(true);
-      LOG(ERROR) << "Singleton of type " << type_.name() << " has a "
+      LOG(ERROR) << "Singleton of type " << type().name() << " has a "
                  << "living reference at destroyInstances time; beware! Raw "
                  << "pointer is " << instance_ptr_ << ". It is very likely "
                  << "that some other singleton is holding a shared_ptr to it. "
@@ -155,10 +161,10 @@ void SingletonHolder<T>::destroyInstance() {
 }
 
 template <typename T>
-SingletonHolder<T>::SingletonHolder(TypeDescriptor type__,
-                                    SingletonVault& vault) :
-    type_(type__), vault_(vault) {
-}
+SingletonHolder<T>::SingletonHolder(
+    TypeDescriptor typeDesc,
+    SingletonVault& vault)
+    : SingletonHolderBase(typeDesc), vault_(vault) {}
 
 template <typename T>
 bool SingletonHolder<T>::creationStarted() {
@@ -180,7 +186,7 @@ template <typename T>
 void SingletonHolder<T>::createInstance() {
   if (creating_thread_.load(std::memory_order_acquire) ==
         std::this_thread::get_id()) {
-    LOG(FATAL) << "circular singleton dependency: " << type_.name();
+    LOG(FATAL) << "circular singleton dependency: " << type().name();
   }
 
   std::lock_guard<std::mutex> entry_lock(mutex_);
@@ -191,9 +197,10 @@ void SingletonHolder<T>::createInstance() {
         SingletonHolderState::NotRegistered) {
     auto ptr = SingletonVault::stackTraceGetter().load();
     LOG(FATAL) << "Creating instance for unregistered singleton: "
-               << type_.name() << "\n"
+               << type().name() << "\n"
                << "Stacktrace:"
-               << "\n" << (ptr ? (*ptr)() : "(not available)");
+               << "\n"
+               << (ptr ? (*ptr)() : "(not available)");
   }
 
   if (state_.load(std::memory_order_acquire) == SingletonHolderState::Living) {
@@ -218,7 +225,7 @@ void SingletonHolder<T>::createInstance() {
   auto print_destructor_stack_trace =
     std::make_shared<std::atomic<bool>>(false);
   auto teardown = teardown_;
-  auto type_name = type_.name();
+  auto type_name = type().name();
 
   // Can't use make_shared -- no support for a custom deleter, sadly.
   std::shared_ptr<T> instance(
@@ -263,7 +270,7 @@ void SingletonHolder<T>::createInstance() {
 
   {
     RWSpinLock::WriteHolder wh(&vault_.mutex_);
-    vault_.creation_order_.push_back(type_);
+    vault_.creation_order_.push_back(type());
   }
 }