#pragma once
#include <folly/Baton.h>
+#include <folly/Demangle.h>
#include <folly/Exception.h>
+#include <folly/Executor.h>
#include <folly/Hash.h>
#include <folly/Memory.h>
#include <folly/RWSpinLock.h>
-#include <folly/Demangle.h>
-#include <folly/Executor.h>
-#include <folly/experimental/ReadMostlySharedPtr.h>
+#include <folly/Synchronized.h>
#include <folly/detail/StaticSingletonManager.h>
+#include <folly/experimental/ReadMostlySharedPtr.h>
#include <algorithm>
#include <atomic>
// For testing; how many registered and living singletons we have.
size_t registeredSingletonCount() const {
- RWSpinLock::ReadHolder rh(&mutex_);
-
- return singletons_.size();
+ return singletons_.rlock()->size();
}
/**
bool eagerInitComplete() const;
size_t livingSingletonCount() const {
- RWSpinLock::ReadHolder rh(&mutex_);
+ auto singletons = singletons_.rlock();
size_t ret = 0;
- for (const auto& p : singletons_) {
+ for (const auto& p : *singletons) {
if (p.second->hasLiveInstance()) {
++ret;
}
Quiescing,
};
+ struct State {
+ SingletonVaultState state{SingletonVaultState::Running};
+ bool registrationComplete{false};
+ };
+
// Each singleton in the vault can be in two states: dead
// (registered but never created), living (CreateFunc returned an instance).
- void stateCheck(SingletonVaultState expected,
- const char* msg="Unexpected singleton state change") {
- if (expected != state_) {
- throw std::logic_error(msg);
+ static void stateCheck(
+ SingletonVaultState expected,
+ const State& state,
+ const char* msg = "Unexpected singleton state change") {
+ if (expected != state.state) {
+ throw std::logic_error(msg);
}
}
typedef std::unordered_map<detail::TypeDescriptor,
detail::SingletonHolderBase*,
detail::TypeDescriptorHasher> SingletonMap;
+ folly::Synchronized<SingletonMap> singletons_;
+ folly::Synchronized<std::unordered_set<detail::SingletonHolderBase*>>
+ eagerInitSingletons_;
+ folly::Synchronized<std::vector<detail::TypeDescriptor>> creationOrder_;
+
+ folly::Synchronized<State> state_;
- mutable folly::RWSpinLock mutex_;
- SingletonMap singletons_;
- std::unordered_set<detail::SingletonHolderBase*> eagerInitSingletons_;
- std::vector<detail::TypeDescriptor> creation_order_;
- SingletonVaultState state_{SingletonVaultState::Running};
- bool registrationComplete_{false};
- folly::RWSpinLock stateMutex_;
Type type_{Type::Relaxed};
};