From 9b30deab7af065c8e2d33c338da8e25ab63da598 Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Mon, 31 Oct 2016 16:08:47 -0700 Subject: [PATCH] Simplify activateFiber()/deactivateFiber() Summary: A cleanup in preparation to make folly::fibers work with newer boost. Reviewed By: jsedgwick Differential Revision: D4093947 fbshipit-source-id: e9a0aaeb915c40af96282169d4758afe572f2dd8 --- folly/fibers/Baton-inl.h | 2 +- folly/fibers/Baton.cpp | 2 +- folly/fibers/Fiber.cpp | 15 ++++----------- folly/fibers/Fiber.h | 11 +++-------- folly/fibers/FiberManagerInternal-inl.h | 13 ++++++------- folly/fibers/FiberManagerInternal.h | 4 ++-- 6 files changed, 17 insertions(+), 30 deletions(-) diff --git a/folly/fibers/Baton-inl.h b/folly/fibers/Baton-inl.h index adec3c5c..eccc0bbf 100644 --- a/folly/fibers/Baton-inl.h +++ b/folly/fibers/Baton-inl.h @@ -51,7 +51,7 @@ void Baton::waitFiber(FiberManager& fm, F&& mainContextFunc) { if (LIKELY(baton_fiber == NO_WAITER)) { continue; } else if (baton_fiber == POSTED || baton_fiber == TIMEOUT) { - fiber.setData(0); + fiber.resume(); break; } else { throw std::logic_error("Some Fiber is already waiting on this Baton."); diff --git a/folly/fibers/Baton.cpp b/folly/fibers/Baton.cpp index bd685cff..caaaa7f9 100644 --- a/folly/fibers/Baton.cpp +++ b/folly/fibers/Baton.cpp @@ -151,7 +151,7 @@ void Baton::postHelper(intptr_t new_value) { } while (!waitingFiber_.compare_exchange_weak(fiber, new_value)); if (fiber != NO_WAITER) { - reinterpret_cast(fiber)->setData(0); + reinterpret_cast(fiber)->resume(); } } diff --git a/folly/fibers/Fiber.cpp b/folly/fibers/Fiber.cpp index aba1d440..2c9830d8 100644 --- a/folly/fibers/Fiber.cpp +++ b/folly/fibers/Fiber.cpp @@ -50,9 +50,8 @@ static size_t nonMagicInBytes(const FContext& context) { } // anonymous namespace -void Fiber::setData(intptr_t data) { +void Fiber::resume() { DCHECK_EQ(state_, AWAITING); - data_ = data; state_ = READY_TO_RUN; if (fiberManager_.observer_) { @@ -164,15 +163,11 @@ void Fiber::fiberFunc() { state_ = INVALID; - auto context = fiberManager_.deactivateFiber(this); - - DCHECK_EQ(reinterpret_cast(context), this); + fiberManager_.deactivateFiber(this); } } -intptr_t Fiber::preempt(State state) { - intptr_t ret; - +void Fiber::preempt(State state) { auto preemptImpl = [&]() mutable { DCHECK_EQ(fiberManager_.activeFiber_, this); DCHECK_EQ(state_, RUNNING); @@ -182,7 +177,7 @@ intptr_t Fiber::preempt(State state) { recordStackPosition(); - ret = fiberManager_.deactivateFiber(this); + fiberManager_.deactivateFiber(this); DCHECK_EQ(fiberManager_.activeFiber_, this); DCHECK_EQ(state_, READY_TO_RUN); @@ -194,8 +189,6 @@ intptr_t Fiber::preempt(State state) { } else { preemptImpl(); } - - return ret; } Fiber::LocalData::LocalData(const LocalData& other) : data_(nullptr) { diff --git a/folly/fibers/Fiber.h b/folly/fibers/Fiber.h index 062be3b5..2e50ff6c 100644 --- a/folly/fibers/Fiber.h +++ b/folly/fibers/Fiber.h @@ -45,11 +45,9 @@ class FiberManager; class Fiber { public: /** - * Sets data for the blocked task - * - * @param data this data will be returned by await() when task is resumed. + * Resume the blocked task */ - void setData(intptr_t data); + void resume(); Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; @@ -105,10 +103,8 @@ class Fiber { * performing necessary housekeeping for the new state. * * @param state New state, must not be RUNNING. - * - * @return The value passed back from the main context. */ - intptr_t preempt(State state); + void preempt(State state); /** * Examines how much of the stack we used at this moment and @@ -118,7 +114,6 @@ class Fiber { FiberManager& fiberManager_; /**< Associated FiberManager */ FContext fcontext_; /**< current task execution context */ - intptr_t data_; /**< Used to keep some data with the Fiber */ std::shared_ptr rcontext_; /**< current RequestContext */ folly::Function func_; /**< task function */ bool recordStackUsed_{false}; diff --git a/folly/fibers/FiberManagerInternal-inl.h b/folly/fibers/FiberManagerInternal-inl.h index abb7d70f..6cdb5f84 100644 --- a/folly/fibers/FiberManagerInternal-inl.h +++ b/folly/fibers/FiberManagerInternal-inl.h @@ -58,7 +58,7 @@ inline void FiberManager::ensureLoopScheduled() { loopController_->schedule(); } -inline intptr_t FiberManager::activateFiber(Fiber* fiber) { +inline void FiberManager::activateFiber(Fiber* fiber) { DCHECK_EQ(activeFiber_, (Fiber*)nullptr); #ifdef FOLLY_SANITIZE_ADDRESS @@ -75,10 +75,11 @@ inline intptr_t FiberManager::activateFiber(Fiber* fiber) { #endif activeFiber_ = fiber; - return jumpContext(&mainContext_, &fiber->fcontext_, fiber->data_); + jumpContext( + &mainContext_, &fiber->fcontext_, reinterpret_cast(fiber)); } -inline intptr_t FiberManager::deactivateFiber(Fiber* fiber) { +inline void FiberManager::deactivateFiber(Fiber* fiber) { DCHECK_EQ(activeFiber_, fiber); #ifdef FOLLY_SANITIZE_ADDRESS @@ -100,7 +101,8 @@ inline intptr_t FiberManager::deactivateFiber(Fiber* fiber) { #endif activeFiber_ = nullptr; - return jumpContext(&fiber->fcontext_, &mainContext_, 0); + auto context = jumpContext(&fiber->fcontext_, &mainContext_, 0); + DCHECK_EQ(fiber, reinterpret_cast(context)); } inline void FiberManager::runReadyFiber(Fiber* fiber) { @@ -229,7 +231,6 @@ inline bool FiberManager::loopUntilNoReady() { fiber->rcontext_ = std::move(task->rcontext); fiber->setFunction(std::move(task->func)); - fiber->data_ = reinterpret_cast(fiber); if (observer_) { observer_->runnable(reinterpret_cast(fiber)); } @@ -299,7 +300,6 @@ void FiberManager::addTask(F&& func) { fiber->setFunction(std::ref(*funcLoc)); } - fiber->data_ = reinterpret_cast(fiber); readyFibers_.push_back(*fiber); if (observer_) { observer_->runnable(reinterpret_cast(fiber)); @@ -434,7 +434,6 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { fiber->setFunctionFinally(std::ref(*funcLoc), std::ref(*finallyLoc)); } - fiber->data_ = reinterpret_cast(fiber); readyFibers_.push_back(*fiber); if (observer_) { observer_->runnable(reinterpret_cast(fiber)); diff --git a/folly/fibers/FiberManagerInternal.h b/folly/fibers/FiberManagerInternal.h index 12669c7c..1b8a1ed4 100644 --- a/folly/fibers/FiberManagerInternal.h +++ b/folly/fibers/FiberManagerInternal.h @@ -341,8 +341,8 @@ class FiberManager : public ::folly::Executor { AtomicIntrusiveLinkedListHook nextRemoteTask; }; - intptr_t activateFiber(Fiber* fiber); - intptr_t deactivateFiber(Fiber* fiber); + void activateFiber(Fiber* fiber); + void deactivateFiber(Fiber* fiber); typedef folly::IntrusiveList FiberTailQueue; typedef folly::IntrusiveList -- 2.34.1