typo in io/Cursor.h
[folly.git] / folly / Singleton-inl.h
index e6675165a8d8b15b1ef3d593d801d1df8a6e2b03..762a9ec57376089e6804e0f543f8e195f2a7f125 100644 (file)
@@ -69,7 +69,9 @@ void SingletonHolder<T>::registerSingletonMock(CreateFunc c, TeardownFunc t) {
     LOG(FATAL) << "Registering mock before singleton was registered: "
                << type().name();
   }
-  destroyInstance();
+  if (state_ == SingletonHolderState::Living) {
+    destroyInstance();
+  }
 
   {
     auto creationOrder = vault_.creationOrder_.wlock();
@@ -153,8 +155,8 @@ void SingletonHolder<T>::destroyInstance() {
   instance_copy_.reset();
   if (destroy_baton_) {
     constexpr std::chrono::seconds kDestroyWaitTime{5};
-    auto last_reference_released = destroy_baton_->timed_wait(
-        std::chrono::steady_clock::now() + kDestroyWaitTime);
+    auto last_reference_released =
+        destroy_baton_->try_wait_for(kDestroyWaitTime);
     if (last_reference_released) {
       teardown_(instance_ptr_);
     } else {
@@ -228,6 +230,21 @@ void SingletonHolder<T>::createInstance() {
   creating_thread_.store(std::this_thread::get_id(), std::memory_order_release);
 
   auto state = vault_.state_.rlock();
+  if (vault_.type_ != SingletonVault::Type::Relaxed &&
+      !state->registrationComplete) {
+    auto stack_trace_getter = SingletonVault::stackTraceGetter().load();
+    auto stack_trace = stack_trace_getter ? stack_trace_getter() : "";
+    if (!stack_trace.empty()) {
+      stack_trace = "Stack trace:\n" + stack_trace;
+    }
+
+    LOG(FATAL) << "Singleton " << type().name() << " requested before "
+               << "registrationComplete() call.\n"
+               << "This usually means that either main() never called "
+               << "folly::init, or singleton was requested before main() "
+               << "(which is not allowed).\n"
+               << stack_trace;
+  }
   if (state->state == SingletonVault::SingletonVaultState::Quiescing) {
     return;
   }
@@ -279,6 +296,6 @@ void SingletonHolder<T>::createInstance() {
   vault_.creationOrder_.wlock()->push_back(type());
 }
 
-}
+} // namespace detail
 
-}
+} // namespace folly