Commit 3b8891d6 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Make sure global CPU executor is never joined on an application thread

Reviewed By: LeeHowes

Differential Revision: D22181192

fbshipit-source-id: 279c56398cd7e4c9274bc43c3564062588c9c81a
parent 985fa6fa
...@@ -45,38 +45,38 @@ Singleton<std::shared_ptr<DefaultCPUExecutor>> gDefaultGlobalCPUExecutor([] { ...@@ -45,38 +45,38 @@ Singleton<std::shared_ptr<DefaultCPUExecutor>> gDefaultGlobalCPUExecutor([] {
return new std::shared_ptr<DefaultCPUExecutor>(new DefaultCPUExecutor{}); return new std::shared_ptr<DefaultCPUExecutor>(new DefaultCPUExecutor{});
}); });
Singleton<std::shared_ptr<CPUThreadPoolExecutor>, GlobalTag> Singleton<std::shared_ptr<Executor>, GlobalTag> gImmutableGlobalCPUExecutor([] {
gImmutableGlobalCPUExecutor([] { return new std::shared_ptr<Executor>(new CPUThreadPoolExecutor(
return new std::shared_ptr<CPUThreadPoolExecutor>(
new CPUThreadPoolExecutor(
folly::hardware_concurrency(), folly::hardware_concurrency(),
std::make_shared<NamedThreadFactory>("GlobalCPUThreadPool"))); std::make_shared<NamedThreadFactory>("GlobalCPUThreadPool")));
}); });
Singleton<std::shared_ptr<IOThreadPoolExecutor>, GlobalTag> Singleton<std::shared_ptr<IOExecutor>, GlobalTag> gImmutableGlobalIOExecutor(
gImmutableGlobalIOExecutor([] { [] {
return new std::shared_ptr<IOThreadPoolExecutor>(new IOThreadPoolExecutor( return new std::shared_ptr<IOExecutor>(new IOThreadPoolExecutor(
folly::hardware_concurrency(), folly::hardware_concurrency(),
std::make_shared<NamedThreadFactory>("GlobalIOThreadPool"))); std::make_shared<NamedThreadFactory>("GlobalIOThreadPool")));
}); });
template <class ExecutorBase> template <class ExecutorBase>
std::shared_ptr<ExecutorBase> getImmutable(); std::shared_ptr<std::shared_ptr<ExecutorBase>> getImmutablePtrPtr();
template <> template <class ExecutorBase>
std::shared_ptr<Executor> getImmutable() { std::shared_ptr<ExecutorBase> getImmutable() {
if (auto executorPtrPtr = gImmutableGlobalCPUExecutor.try_get()) { if (auto executorPtrPtr = getImmutablePtrPtr<ExecutorBase>()) {
return *executorPtrPtr; return *executorPtrPtr;
} }
return nullptr; return nullptr;
} }
template <> template <>
std::shared_ptr<IOExecutor> getImmutable() { std::shared_ptr<std::shared_ptr<Executor>> getImmutablePtrPtr() {
if (auto executorPtrPtr = gImmutableGlobalIOExecutor.try_get()) { return gImmutableGlobalCPUExecutor.try_get();
return *executorPtrPtr; }
}
return nullptr; template <>
std::shared_ptr<std::shared_ptr<IOExecutor>> getImmutablePtrPtr() {
return gImmutableGlobalIOExecutor.try_get();
} }
template <class ExecutorBase> template <class ExecutorBase>
...@@ -137,21 +137,21 @@ LeakySingleton<GlobalExecutor<IOExecutor>> gGlobalIOExecutor([] { ...@@ -137,21 +137,21 @@ LeakySingleton<GlobalExecutor<IOExecutor>> gGlobalIOExecutor([] {
namespace folly { namespace folly {
Executor::KeepAlive<> getGlobalCPUExecutor() { Executor::KeepAlive<> getGlobalCPUExecutor() {
auto executorPtr = getImmutable<Executor>(); auto executorPtrPtr = getImmutablePtrPtr<Executor>();
if (!executorPtr) { if (!executorPtrPtr) {
throw std::runtime_error("Requested global CPU executor during shutdown."); throw std::runtime_error("Requested global CPU executor during shutdown.");
} }
async_tracing::logGetImmutableCPUExecutor(executorPtr.get()); async_tracing::logGetImmutableCPUExecutor(executorPtrPtr->get());
return folly::getKeepAliveToken(executorPtr.get()); return folly::getKeepAliveToken(executorPtrPtr->get());
} }
Executor::KeepAlive<> getGlobalIOExecutor() { Executor::KeepAlive<> getGlobalIOExecutor() {
auto executorPtr = getImmutable<IOExecutor>(); auto executorPtrPtr = getImmutablePtrPtr<IOExecutor>();
if (!executorPtr) { if (!executorPtrPtr) {
throw std::runtime_error("Requested global IO executor during shutdown."); throw std::runtime_error("Requested global IO executor during shutdown.");
} }
async_tracing::logGetImmutableIOExecutor(executorPtr.get()); async_tracing::logGetImmutableIOExecutor(executorPtrPtr->get());
return folly::getKeepAliveToken(executorPtr.get()); return folly::getKeepAliveToken(executorPtrPtr->get());
} }
std::shared_ptr<Executor> getCPUExecutor() { std::shared_ptr<Executor> getCPUExecutor() {
......
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