From 177753d98f88313a80c8a02e5c3e96eeb7745b33 Mon Sep 17 00:00:00 2001 From: Sven Over Date: Wed, 6 Apr 2016 03:55:14 -0700 Subject: [PATCH] folly::fibers::Fiber: use folly::Function instead of std::function Summary:We are jumping through some ugly hoops in the implementation of fibers to allow for non-copyable functors/lambdas. We can get rid of those by using folly::Function instead of std::function to store functions in folly::fibers::Fiber. This involves one observable interface change: the virtual function folly::fibers::InlineFunctionRunner::run must take a folly::Function argument instead of a std::function. Reviewed By: andriigrynenko Differential Revision: D3102711 fb-gh-sync-id: 56705b972dc24cc0da551109ed44732b97cb6e13 fbshipit-source-id: 56705b972dc24cc0da551109ed44732b97cb6e13 --- folly/experimental/fibers/Fiber.h | 7 ++++--- folly/experimental/fibers/FiberManager-inl.h | 12 ++---------- folly/experimental/fibers/FiberManager.h | 14 +++++++------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/folly/experimental/fibers/Fiber.h b/folly/experimental/fibers/Fiber.h index 39a05fca..a0e397f5 100644 --- a/folly/experimental/fibers/Fiber.h +++ b/folly/experimental/fibers/Fiber.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -119,7 +120,7 @@ class Fiber { FContext fcontext_; /**< current task execution context */ intptr_t data_; /**< Used to keep some data with the Fiber */ std::shared_ptr rcontext_; /**< current RequestContext */ - std::function func_; /**< task function */ + folly::Function func_; /**< task function */ bool recordStackUsed_{false}; bool stackFilledWithMagic_{false}; @@ -133,8 +134,8 @@ class Fiber { void* getUserBuffer(); - std::function resultFunc_; - std::function finallyFunc_; + folly::Function resultFunc_; + folly::Function finallyFunc_; class LocalData { public: diff --git a/folly/experimental/fibers/FiberManager-inl.h b/folly/experimental/fibers/FiberManager-inl.h index c7990c44..ea588a68 100644 --- a/folly/experimental/fibers/FiberManager-inl.h +++ b/folly/experimental/fibers/FiberManager-inl.h @@ -299,23 +299,15 @@ auto FiberManager::addTaskFuture(F&& func) template void FiberManager::addTaskRemote(F&& func) { - // addTaskRemote indirectly requires wrapping the function in a - // std::function, which must be copyable. As move-only lambdas may be - // passed in we wrap it first in a move wrapper and then capture the wrapped - // version. - auto functionWrapper = [f = folly::makeMoveWrapper( - std::forward(func))]() mutable { - return (*f)(); - }; auto task = [&]() { auto currentFm = getFiberManagerUnsafe(); if (currentFm && currentFm->currentFiber_ && currentFm->localType_ == localType_) { return folly::make_unique( - std::move(functionWrapper), currentFm->currentFiber_->localData_); + std::forward(func), currentFm->currentFiber_->localData_); } - return folly::make_unique(std::move(functionWrapper)); + return folly::make_unique(std::forward(func)); }(); auto insertHead = [&]() { return remoteTaskQueue_.insertHead(task.release()); }; diff --git a/folly/experimental/fibers/FiberManager.h b/folly/experimental/fibers/FiberManager.h index d9dfef2b..5c893e85 100644 --- a/folly/experimental/fibers/FiberManager.h +++ b/folly/experimental/fibers/FiberManager.h @@ -61,7 +61,7 @@ class InlineFunctionRunner { /** * func must be executed inline and only once. */ - virtual void run(std::function func) = 0; + virtual void run(folly::Function func) = 0; }; /** @@ -116,8 +116,8 @@ class FiberManager : public ::folly::Executor { constexpr Options() {} }; - typedef std::function - ExceptionCallback; + using ExceptionCallback = + folly::Function; FiberManager(const FiberManager&) = delete; FiberManager& operator=(const FiberManager&) = delete; @@ -213,7 +213,7 @@ class FiberManager : public ::folly::Executor { -> folly::Future::type>; // Executor interface calls addTaskRemote - void add(std::function f) { + void add(folly::Func f) override { addTaskRemote(std::move(f)); } @@ -331,7 +331,7 @@ class FiberManager : public ::folly::Executor { func(std::forward(f)), localData(folly::make_unique(localData_)), rcontext(RequestContext::saveContext()) {} - std::function func; + folly::Function func; std::unique_ptr localData; std::shared_ptr rcontext; AtomicLinkedListHook nextRemoteTask; @@ -411,12 +411,12 @@ class FiberManager : public ::folly::Executor { /** * Function passed to the await call. */ - std::function awaitFunc_; + folly::Function awaitFunc_; /** * Function passed to the runInMainContext call. */ - std::function immediateFunc_; + folly::Function immediateFunc_; /** * Preempt runner. -- 2.34.1