Commit 1bc16f19 authored by Lee Howes's avatar Lee Howes Committed by Facebook GitHub Bot

Add Unsafe names for set and get executor calls

Summary:
To encourage migration to safer executors, add transitional *Unsafe names for the old, mutable, global executors. This is consistent with the use of *Unsafe elsewhere in the async framework.

(Note: this ignores all push blocking failures!)

Reviewed By: yfeldblum

Differential Revision: D26418111

fbshipit-source-id: 414ee238ed4ae43619ce8d671fa5725f0679f67e
parent 6696dbb0
...@@ -22,7 +22,8 @@ namespace folly { ...@@ -22,7 +22,8 @@ namespace folly {
template <class F> template <class F>
auto async(F&& fn) { auto async(F&& fn) {
return folly::via<F>(getCPUExecutor().get(), std::forward<F>(fn)); return folly::via<F>(
getUnsafeMutableGlobalCPUExecutor().get(), std::forward<F>(fn));
} }
} // namespace folly } // namespace folly
...@@ -159,37 +159,57 @@ Executor::KeepAlive<IOExecutor> getGlobalIOExecutor() { ...@@ -159,37 +159,57 @@ Executor::KeepAlive<IOExecutor> getGlobalIOExecutor() {
return folly::getKeepAliveToken(executorPtrPtr->get()); return folly::getKeepAliveToken(executorPtrPtr->get());
} }
std::shared_ptr<Executor> getCPUExecutor() { std::shared_ptr<Executor> getUnsafeMutableGlobalCPUExecutor() {
auto& singleton = gGlobalCPUExecutor.get(); auto& singleton = gGlobalCPUExecutor.get();
auto executor = singleton.get(); auto executor = singleton.get();
async_tracing::logGetGlobalCPUExecutor(executor.get()); async_tracing::logGetGlobalCPUExecutor(executor.get());
return executor; return executor;
} }
void setCPUExecutorToGlobalCPUExecutor() { std::shared_ptr<Executor> getCPUExecutor() {
return getUnsafeMutableGlobalCPUExecutor();
}
void setUnsafeMutableGlobalCPUExecutorToGlobalCPUExecutor() {
async_tracing::logSetGlobalCPUExecutorToImmutable(); async_tracing::logSetGlobalCPUExecutorToImmutable();
gGlobalCPUExecutor.get().setFromImmutable(); gGlobalCPUExecutor.get().setFromImmutable();
} }
void setCPUExecutor(std::weak_ptr<Executor> executor) { void setCPUExecutorToGlobalCPUExecutor() {
setUnsafeMutableGlobalCPUExecutorToGlobalCPUExecutor();
}
void setUnsafeMutableGlobalCPUExecutor(std::weak_ptr<Executor> executor) {
async_tracing::logSetGlobalCPUExecutor(executor.lock().get()); async_tracing::logSetGlobalCPUExecutor(executor.lock().get());
gGlobalCPUExecutor.get().set(std::move(executor)); gGlobalCPUExecutor.get().set(std::move(executor));
} }
std::shared_ptr<IOExecutor> getIOExecutor() { void setCPUExecutor(std::weak_ptr<Executor> executor) {
setUnsafeMutableGlobalCPUExecutor(std::move(executor));
}
std::shared_ptr<IOExecutor> getUnsafeMutableGlobalIOExecutor() {
auto& singleton = gGlobalIOExecutor.get(); auto& singleton = gGlobalIOExecutor.get();
auto executor = singleton.get(); auto executor = singleton.get();
async_tracing::logGetGlobalIOExecutor(executor.get()); async_tracing::logGetGlobalIOExecutor(executor.get());
return executor; return executor;
} }
void setIOExecutor(std::weak_ptr<IOExecutor> executor) { std::shared_ptr<IOExecutor> getIOExecutor() {
return getUnsafeMutableGlobalIOExecutor();
}
void setUnsafeMutableGlobalIOExecutor(std::weak_ptr<IOExecutor> executor) {
async_tracing::logSetGlobalIOExecutor(executor.lock().get()); async_tracing::logSetGlobalIOExecutor(executor.lock().get());
gGlobalIOExecutor.get().set(std::move(executor)); gGlobalIOExecutor.get().set(std::move(executor));
} }
EventBase* getEventBase() { void setIOExecutor(std::weak_ptr<IOExecutor> executor) {
auto executor = getIOExecutor(); setUnsafeMutableGlobalIOExecutor(std::move(executor));
}
EventBase* getUnsafeMutableGlobalEventBase() {
auto executor = getUnsafeMutableGlobalIOExecutor();
if (FOLLY_LIKELY(!!executor)) { if (FOLLY_LIKELY(!!executor)) {
return executor->getEventBase(); return executor->getEventBase();
} }
...@@ -197,4 +217,8 @@ EventBase* getEventBase() { ...@@ -197,4 +217,8 @@ EventBase* getEventBase() {
return nullptr; return nullptr;
} }
EventBase* getEventBase() {
return getUnsafeMutableGlobalEventBase();
}
} // namespace folly } // namespace folly
...@@ -50,19 +50,34 @@ folly::Executor::KeepAlive<IOExecutor> getGlobalIOExecutor(); ...@@ -50,19 +50,34 @@ folly::Executor::KeepAlive<IOExecutor> getGlobalIOExecutor();
* *
* Can return nullptr on shutdown. * Can return nullptr on shutdown.
*/ */
std::shared_ptr<folly::Executor> getCPUExecutor(); [[deprecated(
"getCPUExecutor is deprecated. "
"To use the global mutable executor use getUnsafeMutableGlobalCPUExecutor. "
"For a better solution use getGlobalCPUExecutor.")]] std::
shared_ptr<folly::Executor>
getCPUExecutor();
std::shared_ptr<folly::Executor> getUnsafeMutableGlobalCPUExecutor();
/** /**
* Set an Executor to be the global Executor which will be returned by * Set an Executor to be the global Executor which will be returned by
* subsequent calls to getCPUExecutor(). * subsequent calls to getCPUExecutor().
*/ */
void setCPUExecutor(std::weak_ptr<folly::Executor> executor); [[deprecated(
"setCPUExecutor is deprecated. "
"To use the global mutable executor use setUnsafeMutableGlobalCPUExecutor. "
"For a better solution use getGlobalCPUExecutor and avoid calling set.")]] void
setCPUExecutor(std::weak_ptr<folly::Executor> executor);
void setUnsafeMutableGlobalCPUExecutor(std::weak_ptr<folly::Executor> executor);
/** /**
* Set the CPU executor to the immutable default returned by * Set the CPU executor to the immutable default returned by
* getGlobalCPUExecutor. * getGlobalCPUExecutor.
*/ */
void setCPUExecutorToGlobalCPUExecutor(); [[deprecated(
"setCPUExecutorToGlobalCPUExecutor is deprecated. "
"Switch to setUnsafeMutableGlobalCPUExecutorToGlobalCPUExecutor. ")]] void
setCPUExecutorToGlobalCPUExecutor();
void setUnsafeMutableGlobalCPUExecutorToGlobalCPUExecutor();
/** /**
* Retrieve the global IOExecutor. If there is none, a default * Retrieve the global IOExecutor. If there is none, a default
...@@ -73,13 +88,24 @@ void setCPUExecutorToGlobalCPUExecutor(); ...@@ -73,13 +88,24 @@ void setCPUExecutorToGlobalCPUExecutor();
* *
* Can return nullptr on shutdown. * Can return nullptr on shutdown.
*/ */
std::shared_ptr<IOExecutor> getIOExecutor(); [[deprecated(
"getIOExecutor is deprecated. "
"To use the global mutable executor use getUnsafeMutableGlobalIOExecutor. "
"For a better solution use getGlobalIOExecutor.")]] std::
shared_ptr<IOExecutor>
getIOExecutor();
std::shared_ptr<IOExecutor> getUnsafeMutableGlobalIOExecutor();
/** /**
* Set an IOExecutor to be the global IOExecutor which will be returned by * Set an IOExecutor to be the global IOExecutor which will be returned by
* subsequent calls to getIOExecutor(). * subsequent calls to getIOExecutor().
*/ */
void setIOExecutor(std::weak_ptr<IOExecutor> executor); [[deprecated(
"setIOExecutor is deprecated. "
"To use the global mutable executor use setUnsafeMutableGlobalIOExecutor. "
"For a better solution use getGlobalIOExecutor and avoid calling set.")]] void
setIOExecutor(std::weak_ptr<IOExecutor> executor);
void setUnsafeMutableGlobalIOExecutor(std::weak_ptr<IOExecutor> executor);
/** /**
* Retrieve an event base from the global IOExecutor * Retrieve an event base from the global IOExecutor
...@@ -87,6 +113,12 @@ void setIOExecutor(std::weak_ptr<IOExecutor> executor); ...@@ -87,6 +113,12 @@ void setIOExecutor(std::weak_ptr<IOExecutor> executor);
* NOTE: This is not shutdown-safe, the returned pointer may be * NOTE: This is not shutdown-safe, the returned pointer may be
* invalid during shutdown. * invalid during shutdown.
*/ */
folly::EventBase* getEventBase(); [[deprecated(
"getEventBase is deprecated. "
"To use the global mutable executor use getUnsafeMutableGlobalEventBase. "
"For a better solution use getGlobalIOExecutor and request the EventBase "
"from there.")]] folly::EventBase*
getEventBase();
folly::EventBase* getUnsafeMutableGlobalEventBase();
} // namespace folly } // namespace folly
...@@ -58,7 +58,8 @@ class SerialExecutor : public SequencedExecutor { ...@@ -58,7 +58,8 @@ class SerialExecutor : public SequencedExecutor {
SerialExecutor& operator=(SerialExecutor&&) = delete; SerialExecutor& operator=(SerialExecutor&&) = delete;
static KeepAlive<SerialExecutor> create( static KeepAlive<SerialExecutor> create(
KeepAlive<Executor> parent = getKeepAliveToken(getCPUExecutor().get())); KeepAlive<Executor> parent =
getKeepAliveToken(getUnsafeMutableGlobalCPUExecutor().get()));
class Deleter { class Deleter {
public: public:
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
using namespace folly; using namespace folly;
TEST(AsyncFunc, manual_executor) { TEST(AsyncFunc, manual_executor) {
FOLLY_PUSH_WARNING
FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
auto x = std::make_shared<ManualExecutor>(); auto x = std::make_shared<ManualExecutor>();
auto oldX = getCPUExecutor(); auto oldX = getCPUExecutor();
setCPUExecutor(x); setCPUExecutor(x);
...@@ -32,6 +34,7 @@ TEST(AsyncFunc, manual_executor) { ...@@ -32,6 +34,7 @@ TEST(AsyncFunc, manual_executor) {
x->run(); x->run();
EXPECT_EQ(42, f.value()); EXPECT_EQ(42, f.value());
setCPUExecutor(oldX); setCPUExecutor(oldX);
FOLLY_POP_WARNING
} }
TEST(AsyncFunc, value_lambda) { TEST(AsyncFunc, value_lambda) {
......
...@@ -34,7 +34,8 @@ TEST(GlobalExecutorAssignmentTest, GlobalCPUExecutorAsImmutable) { ...@@ -34,7 +34,8 @@ TEST(GlobalExecutorAssignmentTest, GlobalCPUExecutorAsImmutable) {
b.post(); b.post();
}; };
{ FOLLY_PUSH_WARNING
FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations") {
auto inlineExec = getCPUExecutor(); auto inlineExec = getCPUExecutor();
EXPECT_EQ( EXPECT_EQ(
dynamic_cast<folly::CPUThreadPoolExecutor*>(inlineExec.get()), nullptr); dynamic_cast<folly::CPUThreadPoolExecutor*>(inlineExec.get()), nullptr);
...@@ -50,4 +51,5 @@ TEST(GlobalExecutorAssignmentTest, GlobalCPUExecutorAsImmutable) { ...@@ -50,4 +51,5 @@ TEST(GlobalExecutorAssignmentTest, GlobalCPUExecutorAsImmutable) {
b.wait(); b.wait();
EXPECT_EQ(1, count); EXPECT_EQ(1, count);
} }
FOLLY_POP_WARNING
} }
...@@ -67,6 +67,8 @@ TEST(GlobalExecutorTest, GlobalCPUExecutor) { ...@@ -67,6 +67,8 @@ TEST(GlobalExecutorTest, GlobalCPUExecutor) {
auto f = [&]() {}; auto f = [&]() {};
// Don't explode, we should create the default global CPUExecutor lazily here. // Don't explode, we should create the default global CPUExecutor lazily here.
FOLLY_PUSH_WARNING
FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
getCPUExecutor()->add(f); getCPUExecutor()->add(f);
{ {
...@@ -80,6 +82,7 @@ TEST(GlobalExecutorTest, GlobalCPUExecutor) { ...@@ -80,6 +82,7 @@ TEST(GlobalExecutorTest, GlobalCPUExecutor) {
// Don't explode, we should restore the default global CPUExecutor because our // Don't explode, we should restore the default global CPUExecutor because our
// weak reference to dummy has expired // weak reference to dummy has expired
getCPUExecutor()->add(f); getCPUExecutor()->add(f);
FOLLY_POP_WARNING
} }
TEST(GlobalExecutorTest, GlobalIOExecutor) { TEST(GlobalExecutorTest, GlobalIOExecutor) {
...@@ -92,6 +95,8 @@ TEST(GlobalExecutorTest, GlobalIOExecutor) { ...@@ -92,6 +95,8 @@ TEST(GlobalExecutorTest, GlobalIOExecutor) {
auto f = []() {}; auto f = []() {};
FOLLY_PUSH_WARNING
FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
// Don't explode, we should create the default global IOExecutor lazily here. // Don't explode, we should create the default global IOExecutor lazily here.
getIOExecutor()->add(f); getIOExecutor()->add(f);
...@@ -106,4 +111,5 @@ TEST(GlobalExecutorTest, GlobalIOExecutor) { ...@@ -106,4 +111,5 @@ TEST(GlobalExecutorTest, GlobalIOExecutor) {
// Don't explode, we should restore the default global IOExecutor because our // Don't explode, we should restore the default global IOExecutor because our
// weak reference to dummy has expired // weak reference to dummy has expired
getIOExecutor()->add(f); getIOExecutor()->add(f);
FOLLY_POP_WARNING
} }
...@@ -74,7 +74,8 @@ class SimpleAsyncIO : public EventHandler { ...@@ -74,7 +74,8 @@ class SimpleAsyncIO : public EventHandler {
struct Config { struct Config {
Config() Config()
: maxRequests_(1000), : maxRequests_(1000),
completionExecutor_(getKeepAliveToken(getCPUExecutor().get())), completionExecutor_(
getKeepAliveToken(getUnsafeMutableGlobalCPUExecutor().get())),
mode_(AIO), mode_(AIO),
evb_(nullptr) {} evb_(nullptr) {}
Config& setMaxRequests(size_t maxRequests) { Config& setMaxRequests(size_t maxRequests) {
......
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