Commit f54fbf88 authored by Steve O'Brien's avatar Steve O'Brien Committed by facebook-github-bot-9

Singleton: remove dependency on Future

Summary: Singletons requires Futures, and Futures indirectly (via HHWheelTimeKeeper) require Singletons.  This breaks the dependency.  It removes a Future-using API which will be replaced with a better alternative.

Reviewed By: @luciang

Differential Revision: D2517807

fb-gh-sync-id: 93df371a74d0f80dc8c55fb0eadd0b688b27b525
parent ccbbdd36
...@@ -128,7 +128,7 @@ void SingletonVault::doEagerInit() { ...@@ -128,7 +128,7 @@ void SingletonVault::doEagerInit() {
} }
} }
Future<Unit> SingletonVault::doEagerInitVia(Executor* exe) { void SingletonVault::doEagerInitVia(Executor* exe) {
std::unordered_set<detail::SingletonHolderBase*> singletonSet; std::unordered_set<detail::SingletonHolderBase*> singletonSet;
{ {
RWSpinLock::ReadHolder rh(&stateMutex_); RWSpinLock::ReadHolder rh(&stateMutex_);
...@@ -139,16 +139,13 @@ Future<Unit> SingletonVault::doEagerInitVia(Executor* exe) { ...@@ -139,16 +139,13 @@ Future<Unit> SingletonVault::doEagerInitVia(Executor* exe) {
singletonSet = eagerInitSingletons_; // copy set of pointers singletonSet = eagerInitSingletons_; // copy set of pointers
} }
std::vector<Future<Unit>> resultFutures;
for (auto* single : singletonSet) { for (auto* single : singletonSet) {
resultFutures.emplace_back(via(exe).then([single] { exe->add([single] {
if (!single->creationStarted()) { if (!single->creationStarted()) {
single->createInstance(); single->createInstance();
} }
})); });
} }
return collectAll(resultFutures).via(exe).then();
} }
void SingletonVault::destroyInstances() { void SingletonVault::destroyInstances() {
......
...@@ -109,7 +109,6 @@ ...@@ -109,7 +109,6 @@
#include <folly/RWSpinLock.h> #include <folly/RWSpinLock.h>
#include <folly/Demangle.h> #include <folly/Demangle.h>
#include <folly/Executor.h> #include <folly/Executor.h>
#include <folly/futures/Future.h>
#include <folly/io/async/Request.h> #include <folly/io/async/Request.h>
#include <algorithm> #include <algorithm>
...@@ -341,10 +340,8 @@ class SingletonVault { ...@@ -341,10 +340,8 @@ class SingletonVault {
/** /**
* Schedule eager singletons' initializations through the given executor. * Schedule eager singletons' initializations through the given executor.
* Return a future which is fulfilled after all the initialization functions
* complete.
*/ */
Future<Unit> doEagerInitVia(Executor* exe); void doEagerInitVia(Executor* exe);
// Destroy all singletons; when complete, the vault can't create // Destroy all singletons; when complete, the vault can't create
// singletons once again until reenableInstances() is called. // singletons once again until reenableInstances() is called.
......
...@@ -459,9 +459,8 @@ TEST(Singleton, SingletonEagerInitAsync) { ...@@ -459,9 +459,8 @@ TEST(Singleton, SingletonEagerInitAsync) {
folly::EventBase eb; folly::EventBase eb;
vault.registrationComplete(); vault.registrationComplete();
EXPECT_FALSE(didEagerInit); EXPECT_FALSE(didEagerInit);
auto result = vault.doEagerInitVia(&eb); // a Future<Unit> is returned vault.doEagerInitVia(&eb);
eb.loop(); eb.loop();
result.get(); // ensure this completed successfully and didn't hang forever
EXPECT_TRUE(didEagerInit); EXPECT_TRUE(didEagerInit);
sing.get_weak(); // (avoid compile error complaining about unused var 'sing') sing.get_weak(); // (avoid compile error complaining about unused var 'sing')
} }
...@@ -538,7 +537,7 @@ TEST(Singleton, SingletonEagerInitParallel) { ...@@ -538,7 +537,7 @@ TEST(Singleton, SingletonEagerInitParallel) {
for (size_t j = 0; j < kThreads; j++) { for (size_t j = 0; j < kThreads; j++) {
threads.push_back(std::make_shared<std::thread>([&] { threads.push_back(std::make_shared<std::thread>([&] {
barrier.wait(); barrier.wait();
vault.doEagerInitVia(&exe).get(); vault.doEagerInitVia(&exe);
})); }));
} }
......
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