Commit 9c19d342 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Viswanath Sivakumar

Use Fiber locals for TAO Fiber-locals

Summary:
This simplifies TAO fibers locals, using FiberManager based fiber-local storage.
This depends on D1958135.

Test Plan: integration tests

Reviewed By: bwatling@fb.com

Subscribers: alikhtarov

FB internal diff: D1962214

Signature: t1:1962214:1427997755:b546b9039382a7600f234b0a2a60cc96da34e662
parent 626aa2dd
...@@ -39,12 +39,6 @@ inline void* Fiber::getUserBuffer() { ...@@ -39,12 +39,6 @@ inline void* Fiber::getUserBuffer() {
return &userBuffer_; return &userBuffer_;
} }
template <typename G>
void Fiber::setReadyFunction(G&& func) {
assert(state_ == INVALID || state_ == NOT_STARTED);
readyFunc_ = std::move(func);
}
template <typename T> template <typename T>
T& Fiber::LocalData::get() { T& Fiber::LocalData::get() {
if (data_) { if (data_) {
......
...@@ -75,9 +75,6 @@ class Fiber { ...@@ -75,9 +75,6 @@ class Fiber {
template <typename F, typename G> template <typename F, typename G>
void setFunctionFinally(F&& func, G&& finally); void setFunctionFinally(F&& func, G&& finally);
template <typename G>
void setReadyFunction(G&& func);
static void fiberFuncHelper(intptr_t fiber); static void fiberFuncHelper(intptr_t fiber);
void fiberFunc(); void fiberFunc();
...@@ -101,8 +98,6 @@ class Fiber { ...@@ -101,8 +98,6 @@ class Fiber {
FContext fcontext_; /**< current task execution context */ FContext fcontext_; /**< current task execution context */
intptr_t data_; /**< Used to keep some data with the Fiber */ intptr_t data_; /**< Used to keep some data with the Fiber */
std::function<void()> func_; /**< task function */ std::function<void()> func_; /**< task function */
std::function<void()> readyFunc_; /**< function to be executed before jumping
to this fiber */
/** /**
* Points to next fiber in remote ready list * Points to next fiber in remote ready list
......
...@@ -46,9 +46,6 @@ inline void FiberManager::runReadyFiber(Fiber* fiber) { ...@@ -46,9 +46,6 @@ inline void FiberManager::runReadyFiber(Fiber* fiber) {
while (fiber->state_ == Fiber::NOT_STARTED || while (fiber->state_ == Fiber::NOT_STARTED ||
fiber->state_ == Fiber::READY_TO_RUN) { fiber->state_ == Fiber::READY_TO_RUN) {
activeFiber_ = fiber; activeFiber_ = fiber;
if (fiber->readyFunc_) {
fiber->readyFunc_();
}
jumpContext(&mainContext_, &fiber->fcontext_, fiber->data_); jumpContext(&mainContext_, &fiber->fcontext_, fiber->data_);
if (fiber->state_ == Fiber::AWAITING_IMMEDIATE) { if (fiber->state_ == Fiber::AWAITING_IMMEDIATE) {
try { try {
...@@ -198,22 +195,6 @@ void FiberManager::addTask(F&& func) { ...@@ -198,22 +195,6 @@ void FiberManager::addTask(F&& func) {
ensureLoopScheduled(); ensureLoopScheduled();
} }
template <typename F, typename G>
void FiberManager::addTaskReadyFunc(F&& func, G&& readyFunc) {
auto fiber = getFiber();
if (currentFiber_) {
fiber->localData_ = currentFiber_->localData_;
}
fiber->setFunction(std::forward<F>(func));
fiber->setReadyFunction(std::forward<G>(readyFunc));
fiber->data_ = reinterpret_cast<intptr_t>(fiber);
readyFibers_.push_back(*fiber);
ensureLoopScheduled();
}
template <typename F> template <typename F>
void FiberManager::addTaskRemote(F&& func) { void FiberManager::addTaskRemote(F&& func) {
auto task = [&]() { auto task = [&]() {
...@@ -402,8 +383,16 @@ inline bool FiberManager::hasActiveFiber() { ...@@ -402,8 +383,16 @@ inline bool FiberManager::hasActiveFiber() {
template <typename T> template <typename T>
T& FiberManager::local() { T& FiberManager::local() {
assert(getFiberManager().currentFiber_ != nullptr); if (currentFiber_) {
return currentFiber_->localData_.get<T>(); return currentFiber_->localData_.get<T>();
}
return localThread<T>();
}
template <typename T>
T& FiberManager::localThread() {
static thread_local T t;
return t;
} }
template <typename F> template <typename F>
......
...@@ -135,18 +135,6 @@ class FiberManager { ...@@ -135,18 +135,6 @@ class FiberManager {
template <typename F> template <typename F>
void addTask(F&& func); void addTask(F&& func);
/**
* Add a new task to be executed, along with a function readyFunc_ which needs
* to be executed just before jumping to the ready fiber
*
* @param func Task functor; must have a signature of `T func()` for some T.
* @param readyFunc functor that needs to be executed just before jumping to
* ready fiber on the main context. This can for example be
* used to set up state before starting or resuming a fiber.
*/
template <typename F, typename G>
void addTaskReadyFunc(F&& func, G&& readyFunc);
/** /**
* Add a new task to be executed. Safe to call from other threads. * Add a new task to be executed. Safe to call from other threads.
* *
...@@ -189,6 +177,9 @@ class FiberManager { ...@@ -189,6 +177,9 @@ class FiberManager {
template <typename T> template <typename T>
T& local(); T& local();
template <typename T>
static T& localThread();
/** /**
* @return How many fiber objects (and stacks) has this manager allocated. * @return How many fiber objects (and stacks) has this manager allocated.
*/ */
...@@ -403,7 +394,11 @@ inline runInMainContext(F&& func) { ...@@ -403,7 +394,11 @@ inline runInMainContext(F&& func) {
*/ */
template <typename T> template <typename T>
T& local() { T& local() {
return FiberManager::getFiberManager().local<T>(); auto fm = FiberManager::getFiberManagerUnsafe();
if (fm) {
return fm->local<T>();
}
return FiberManager::localThread<T>();
} }
}} }}
......
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