Commit df2469a3 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Viswanath Sivakumar

Replace singleton names with type tags

Summary: This change simplifies Singleton API (methods don't need to accept name) and the actual implementation. It also makes it similar to folly::ThreadLocalPtr. Additionally misspelled singleton name becomes compilation error, not runtime error. Some users were actually naming singletons, when that was neccessary, this should also be fixed.

Test Plan: unit tests for all touched projects

Reviewed By: chip@fb.com

Subscribers: trunkagent, fugalh, jsedgwick, fbcode-common-diffs@, mcduff, hitesh, mshneer, folly-diffs@

FB internal diff: D1744978

Signature: t1:1744978:1419282587:bd29dd8a70d7572530ac371a96a21764229bc397
parent 760b4c4f
This diff is collapsed.
...@@ -126,7 +126,8 @@ TEST(Singleton, DirectUsage) { ...@@ -126,7 +126,8 @@ TEST(Singleton, DirectUsage) {
// Verify we can get to the underlying singletons via directly using // Verify we can get to the underlying singletons via directly using
// the singleton definition. // the singleton definition.
Singleton<Watchdog> watchdog(nullptr, nullptr, &vault); Singleton<Watchdog> watchdog(nullptr, nullptr, &vault);
Singleton<Watchdog> named_watchdog("named", nullptr, nullptr, &vault); struct TestTag {};
Singleton<Watchdog, TestTag> named_watchdog(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
vault.registrationComplete(); vault.registrationComplete();
...@@ -143,31 +144,32 @@ TEST(Singleton, NamedUsage) { ...@@ -143,31 +144,32 @@ TEST(Singleton, NamedUsage) {
EXPECT_EQ(vault.registeredSingletonCount(), 0); EXPECT_EQ(vault.registeredSingletonCount(), 0);
// Define two named Watchdog singletons and one unnamed singleton. // Define two named Watchdog singletons and one unnamed singleton.
Singleton<Watchdog> watchdog1_singleton( struct Watchdog1 {};
"watchdog1", nullptr, nullptr, &vault); struct Watchdog2 {};
typedef detail::DefaultTag Watchdog3;
Singleton<Watchdog, Watchdog1> watchdog1_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 1); EXPECT_EQ(vault.registeredSingletonCount(), 1);
Singleton<Watchdog> watchdog2_singleton( Singleton<Watchdog, Watchdog2> watchdog2_singleton(nullptr, nullptr, &vault);
"watchdog2", nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
Singleton<Watchdog> watchdog3_singleton(nullptr, nullptr, &vault); Singleton<Watchdog, Watchdog3> watchdog3_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 3); EXPECT_EQ(vault.registeredSingletonCount(), 3);
vault.registrationComplete(); vault.registrationComplete();
// Verify our three singletons are distinct and non-nullptr. // Verify our three singletons are distinct and non-nullptr.
Watchdog* s1 = Singleton<Watchdog>::get("watchdog1", &vault); Watchdog* s1 = Singleton<Watchdog, Watchdog1>::get(&vault);
EXPECT_EQ(s1, watchdog1_singleton.ptr()); EXPECT_EQ(s1, watchdog1_singleton.ptr());
Watchdog* s2 = Singleton<Watchdog>::get("watchdog2", &vault); Watchdog* s2 = Singleton<Watchdog, Watchdog2>::get(&vault);
EXPECT_EQ(s2, watchdog2_singleton.ptr()); EXPECT_EQ(s2, watchdog2_singleton.ptr());
EXPECT_NE(s1, s2); EXPECT_NE(s1, s2);
Watchdog* s3 = Singleton<Watchdog>::get(&vault); Watchdog* s3 = Singleton<Watchdog, Watchdog3>::get(&vault);
EXPECT_EQ(s3, watchdog3_singleton.ptr()); EXPECT_EQ(s3, watchdog3_singleton.ptr());
EXPECT_NE(s3, s1); EXPECT_NE(s3, s1);
EXPECT_NE(s3, s2); EXPECT_NE(s3, s2);
// Verify the "default" singleton is the same as the empty string // Verify the "default" singleton is the same as the DefaultTag-tagged
// singleton. // singleton.
Watchdog* s4 = Singleton<Watchdog>::get("", &vault); Watchdog* s4 = Singleton<Watchdog>::get(&vault);
EXPECT_EQ(s4, watchdog3_singleton.ptr()); EXPECT_EQ(s4, watchdog3_singleton.ptr());
} }
...@@ -216,8 +218,8 @@ TEST(Singleton, SharedPtrUsage) { ...@@ -216,8 +218,8 @@ TEST(Singleton, SharedPtrUsage) {
Singleton<ChildWatchdog> child_watchdog_singleton(nullptr, nullptr, &vault); Singleton<ChildWatchdog> child_watchdog_singleton(nullptr, nullptr, &vault);
EXPECT_EQ(vault.registeredSingletonCount(), 2); EXPECT_EQ(vault.registeredSingletonCount(), 2);
Singleton<Watchdog> named_watchdog_singleton( struct ATag {};
"a_name", nullptr, nullptr, &vault); Singleton<Watchdog, ATag> named_watchdog_singleton(nullptr, nullptr, &vault);
vault.registrationComplete(); vault.registrationComplete();
Watchdog* s1 = Singleton<Watchdog>::get(&vault); Watchdog* s1 = Singleton<Watchdog>::get(&vault);
...@@ -234,7 +236,7 @@ TEST(Singleton, SharedPtrUsage) { ...@@ -234,7 +236,7 @@ TEST(Singleton, SharedPtrUsage) {
EXPECT_EQ(shared_s1.use_count(), 2); EXPECT_EQ(shared_s1.use_count(), 2);
{ {
auto named_weak_s1 = Singleton<Watchdog>::get_weak("a_name", &vault); auto named_weak_s1 = Singleton<Watchdog, ATag>::get_weak(&vault);
auto locked = named_weak_s1.lock(); auto locked = named_weak_s1.lock();
EXPECT_NE(locked.get(), shared_s1.get()); EXPECT_NE(locked.get(), shared_s1.get());
} }
......
...@@ -24,7 +24,6 @@ using namespace folly::wangle; ...@@ -24,7 +24,6 @@ using namespace folly::wangle;
namespace { namespace {
Singleton<IOThreadPoolExecutor> globalIOThreadPoolSingleton( Singleton<IOThreadPoolExecutor> globalIOThreadPoolSingleton(
"GlobalIOThreadPool",
[](){ [](){
return new IOThreadPoolExecutor( return new IOThreadPoolExecutor(
sysconf(_SC_NPROCESSORS_ONLN), sysconf(_SC_NPROCESSORS_ONLN),
...@@ -42,7 +41,7 @@ IOExecutor* getIOExecutor() { ...@@ -42,7 +41,7 @@ IOExecutor* getIOExecutor() {
IOExecutor* nullIOExecutor = nullptr; IOExecutor* nullIOExecutor = nullptr;
singleton->compare_exchange_strong( singleton->compare_exchange_strong(
nullIOExecutor, nullIOExecutor,
Singleton<IOThreadPoolExecutor>::get("GlobalIOThreadPool")); globalIOThreadPoolSingleton.get_fast());
executor = singleton->load(); executor = singleton->load();
} }
return executor; return executor;
......
...@@ -24,7 +24,6 @@ using folly::wangle::IOExecutor; ...@@ -24,7 +24,6 @@ using folly::wangle::IOExecutor;
namespace { namespace {
Singleton<std::atomic<IOExecutor*>> globalIOExecutorSingleton( Singleton<std::atomic<IOExecutor*>> globalIOExecutorSingleton(
"GlobalIOExecutor",
[](){ [](){
return new std::atomic<IOExecutor*>(nullptr); return new std::atomic<IOExecutor*>(nullptr);
}); });
...@@ -44,7 +43,7 @@ IOExecutor::~IOExecutor() { ...@@ -44,7 +43,7 @@ IOExecutor::~IOExecutor() {
} }
std::atomic<IOExecutor*>* IOExecutor::getSingleton() { std::atomic<IOExecutor*>* IOExecutor::getSingleton() {
return Singleton<std::atomic<IOExecutor*>>::get("GlobalIOExecutor"); return globalIOExecutorSingleton.get_fast();
} }
}} // folly::wangle }} // folly::wangle
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