Commit ebb18e1e authored by Pranav Thulasiram Bhat's avatar Pranav Thulasiram Bhat Committed by Facebook GitHub Bot

Simplify getFiberManager

Summary: The call-once logic in getFiberManager was unnecessarily complex. getFiberManager should only be called on main context, so use a static var initialization to implement the call-once logic (using executor.cpp as an example).

Reviewed By: yfeldblum

Differential Revision: D25467655

fbshipit-source-id: c880b1740edf28944e772c9310f3e45f5612396d
parent 33a42353
......@@ -15,27 +15,28 @@
*/
#include <folly/python/fibers.h>
#include <folly/fibers/CallOnce.h>
#include <stdexcept>
#include <folly/CppAttributes.h>
#include <folly/python/fiber_manager_api.h>
namespace folly {
namespace python {
namespace {
void do_import() {
if (0 != import_folly__fiber_manager()) {
throw std::runtime_error("import_folly__fiber_manager failed");
}
}
} // namespace
folly::fibers::FiberManager* getFiberManager(
const folly::fibers::FiberManager::Options& opts) {
static folly::fibers::once_flag flag;
// Use call_once because Python performance is really poor,
// just to check if a module was already imported
folly::call_once(flag, [&]() {
// Use main context, because import can load arbitrary number of files,
// which is incompatible with fiber stack size restrictions
folly::fibers::runInMainContext([&]() {
import_folly__fiber_manager();
if (PyErr_Occurred() != nullptr) {
throw std::logic_error("Fail to import cython fiber_manager");
}
});
});
DCHECK(!folly::fibers::onFiber());
FOLLY_MAYBE_UNUSED static bool done = (do_import(), false);
return get_fiber_manager(opts);
}
......
......@@ -28,6 +28,7 @@
namespace folly {
namespace python {
// Must be called from main context
folly::fibers::FiberManager* getFiberManager(
const folly::fibers::FiberManager::Options& opts = {});
......
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