From: Martin Martin Date: Fri, 29 Apr 2016 17:50:23 +0000 (-0700) Subject: Clang-format in preparation for other change X-Git-Tag: 2016.07.26~300 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=0a2b61fdf6ece03076c086091f1a42c82f4ecfd5;p=folly.git Clang-format in preparation for other change Summary: Clang-format in preparation for other change Reviewed By: andriigrynenko Differential Revision: D3241297 fb-gh-sync-id: b7b26812e9e61c291d5c7bdb523df8f28f2d9b4f fbshipit-source-id: b7b26812e9e61c291d5c7bdb523df8f28f2d9b4f --- diff --git a/folly/experimental/fibers/AddTasks-inl.h b/folly/experimental/fibers/AddTasks-inl.h index 8bc14d23..6a5c1da4 100644 --- a/folly/experimental/fibers/AddTasks-inl.h +++ b/folly/experimental/fibers/AddTasks-inl.h @@ -18,18 +18,16 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { template TaskIterator::TaskIterator(TaskIterator&& other) noexcept - : context_(std::move(other.context_)), - id_(other.id_) { -} + : context_(std::move(other.context_)), id_(other.id_) {} template TaskIterator::TaskIterator(std::shared_ptr context) - : context_(std::move(context)), - id_(-1) { + : context_(std::move(context)), id_(-1) { assert(context_); } @@ -82,11 +80,10 @@ inline void TaskIterator::reserve(size_t n) { size_t tasksLeft = context_->totalTasks - context_->results.size(); n = std::min(n, tasksLeft); - await( - [this, n](Promise promise) { - context_->tasksToFulfillPromise = n; - context_->promise.assign(std::move(promise)); - }); + await([this, n](Promise promise) { + context_->tasksToFulfillPromise = n; + context_->promise.assign(std::move(promise)); + }); } template @@ -97,10 +94,10 @@ inline size_t TaskIterator::getTaskID() const { template TaskIterator::value_type()>::type> + typename std::iterator_traits::value_type()>::type> addTasks(InputIterator first, InputIterator last) { typedef typename std::result_of< - typename std::iterator_traits::value_type()>::type + typename std::iterator_traits::value_type()>::type ResultType; typedef TaskIterator IteratorType; @@ -113,19 +110,17 @@ addTasks(InputIterator first, InputIterator last) { #pragma clang diagnostic push // ignore generalized lambda capture warning #pragma clang diagnostic ignored "-Wc++1y-extensions" #endif - addTask( - [i, context, f = std::move(*first)]() { - context->results.emplace_back(i, folly::makeTryWith(std::move(f))); - - // Check for awaiting iterator. - if (context->promise.hasValue()) { - if (--context->tasksToFulfillPromise == 0) { - context->promise->setValue(); - context->promise.clear(); - } + addTask([ i, context, f = std::move(*first) ]() { + context->results.emplace_back(i, folly::makeTryWith(std::move(f))); + + // Check for awaiting iterator. + if (context->promise.hasValue()) { + if (--context->tasksToFulfillPromise == 0) { + context->promise->setValue(); + context->promise.clear(); } } - ); + }); #ifdef __clang__ #pragma clang diagnostic pop #endif @@ -133,5 +128,5 @@ addTasks(InputIterator first, InputIterator last) { return IteratorType(std::move(context)); } - -}} +} +} diff --git a/folly/experimental/fibers/AddTasks.h b/folly/experimental/fibers/AddTasks.h index af8376eb..9e3019d3 100644 --- a/folly/experimental/fibers/AddTasks.h +++ b/folly/experimental/fibers/AddTasks.h @@ -22,7 +22,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { template class TaskIterator; @@ -39,10 +40,9 @@ class TaskIterator; * @return movable, non-copyable iterator */ template -TaskIterator< - typename std::result_of< - typename std::iterator_traits::value_type()>::type> -inline addTasks(InputIterator first, InputIterator last); +TaskIterator::value_type()>:: + type> inline addTasks(InputIterator first, InputIterator last); template class TaskIterator { @@ -99,9 +99,8 @@ class TaskIterator { private: template - friend TaskIterator< - typename std::result_of< - typename std::iterator_traits::value_type()>::type> + friend TaskIterator::value_type()>::type> addTasks(InputIterator first, InputIterator last); struct Context { @@ -119,7 +118,7 @@ class TaskIterator { folly::Try awaitNextResult(); }; - -}} +} +} #include diff --git a/folly/experimental/fibers/Baton-inl.h b/folly/experimental/fibers/Baton-inl.h index bde7d210..b4933ca1 100644 --- a/folly/experimental/fibers/Baton-inl.h +++ b/folly/experimental/fibers/Baton-inl.h @@ -16,14 +16,16 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { inline Baton::Baton() : Baton(NO_WAITER) { assert(Baton(NO_WAITER).futex_.futex == static_cast(NO_WAITER)); assert(Baton(POSTED).futex_.futex == static_cast(POSTED)); assert(Baton(TIMEOUT).futex_.futex == static_cast(TIMEOUT)); - assert(Baton(THREAD_WAITING).futex_.futex == - static_cast(THREAD_WAITING)); + assert( + Baton(THREAD_WAITING).futex_.futex == + static_cast(THREAD_WAITING)); assert(futex_.futex.is_lock_free()); assert(waitingFiber_.is_lock_free()); @@ -54,9 +56,8 @@ void Baton::waitFiber(FiberManager& fm, F&& mainContextFunc) { } else { throw std::logic_error("Some Fiber is already waiting on this Baton."); } - } while(!waitingFiber.compare_exchange_weak( - baton_fiber, - reinterpret_cast(&fiber))); + } while (!waitingFiber.compare_exchange_weak( + baton_fiber, reinterpret_cast(&fiber))); mainContextFunc(); }; @@ -66,8 +67,9 @@ void Baton::waitFiber(FiberManager& fm, F&& mainContextFunc) { } template -bool Baton::timed_wait(TimeoutController::Duration timeout, - F&& mainContextFunc) { +bool Baton::timed_wait( + TimeoutController::Duration timeout, + F&& mainContextFunc) { auto fm = FiberManager::getFiberManagerUnsafe(); if (!fm || !fm->activeFiber_) { @@ -82,8 +84,8 @@ bool Baton::timed_wait(TimeoutController::Duration timeout, canceled = true; }; - auto id = fm->timeoutManager_->registerTimeout( - std::ref(timeoutFunc), timeout); + auto id = + fm->timeoutManager_->registerTimeout(std::ref(timeoutFunc), timeout); waitFiber(*fm, std::move(mainContextFunc)); @@ -96,8 +98,8 @@ bool Baton::timed_wait(TimeoutController::Duration timeout, return posted; } -template -bool Baton::timed_wait(const std::chrono::time_point& timeout) { +template +bool Baton::timed_wait(const std::chrono::time_point& timeout) { auto now = C::now(); if (LIKELY(now <= timeout)) { @@ -107,6 +109,5 @@ bool Baton::timed_wait(const std::chrono::time_point& timeout) { return timed_wait(TimeoutController::Duration(0)); } } - - -}} +} +} diff --git a/folly/experimental/fibers/Baton.cpp b/folly/experimental/fibers/Baton.cpp index 9f6d4e41..a378483a 100644 --- a/folly/experimental/fibers/Baton.cpp +++ b/folly/experimental/fibers/Baton.cpp @@ -21,10 +21,11 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { void Baton::wait() { - wait([](){}); + wait([]() {}); } void Baton::wait(TimeoutHandler& timeoutHandler) { @@ -41,7 +42,7 @@ void Baton::wait(TimeoutHandler& timeoutHandler) { } bool Baton::timed_wait(TimeoutController::Duration timeout) { - return timed_wait(timeout, [](){}); + return timed_wait(timeout, []() {}); } void Baton::waitThread() { @@ -52,8 +53,9 @@ void Baton::waitThread() { auto fiber = waitingFiber_.load(); - if (LIKELY(fiber == NO_WAITER && - waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) { + if (LIKELY( + fiber == NO_WAITER && + waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) { do { folly::detail::MemoryIdler::futexWait(futex_.futex, THREAD_WAITING); fiber = waitingFiber_.load(std::memory_order_relaxed); @@ -75,7 +77,8 @@ void Baton::waitThread() { } bool Baton::spinWaitForEarlyPost() { - static_assert(PreBlockAttempts > 0, + static_assert( + PreBlockAttempts > 0, "isn't this assert clearer than an uninitialized variable warning?"); for (int i = 0; i < PreBlockAttempts; ++i) { if (try_wait()) { @@ -100,12 +103,13 @@ bool Baton::timedWaitThread(TimeoutController::Duration timeout) { auto fiber = waitingFiber_.load(); - if (LIKELY(fiber == NO_WAITER && - waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) { + if (LIKELY( + fiber == NO_WAITER && + waitingFiber_.compare_exchange_strong(fiber, THREAD_WAITING))) { auto deadline = TimeoutController::Clock::now() + timeout; do { const auto wait_rv = - futex_.futex.futexWaitUntil(THREAD_WAITING, deadline); + futex_.futex.futexWaitUntil(THREAD_WAITING, deadline); if (wait_rv == folly::detail::FutexResult::TIMEDOUT) { return false; } @@ -167,7 +171,8 @@ void Baton::postThread() { } void Baton::reset() { - waitingFiber_.store(NO_WAITER, std::memory_order_relaxed);; + waitingFiber_.store(NO_WAITER, std::memory_order_relaxed); + ; } void Baton::TimeoutHandler::scheduleTimeout( @@ -177,8 +182,8 @@ void Baton::TimeoutHandler::scheduleTimeout( assert(timeoutPtr_ == 0); if (timeout.count() > 0) { - timeoutPtr_ = fiberManager_->timeoutManager_->registerTimeout( - timeoutFunc_, timeout); + timeoutPtr_ = + fiberManager_->timeoutManager_->registerTimeout(timeoutFunc_, timeout); } } @@ -187,5 +192,5 @@ void Baton::TimeoutHandler::cancelTimeout() { fiberManager_->timeoutManager_->cancel(timeoutPtr_); } } - -}} +} +} diff --git a/folly/experimental/fibers/Baton.h b/folly/experimental/fibers/Baton.h index 874a8da3..5a9a6e01 100644 --- a/folly/experimental/fibers/Baton.h +++ b/folly/experimental/fibers/Baton.h @@ -20,7 +20,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class Fiber; class FiberManager; @@ -66,8 +67,8 @@ class Baton { * This is here only not break tao/locks. Please don't use it, because it is * inefficient when used on Fibers. */ - template - bool timed_wait(const std::chrono::time_point& timeout); + template + bool timed_wait(const std::chrono::time_point& timeout); /** * Puts active fiber to sleep. Returns when post is called. @@ -153,7 +154,7 @@ class Baton { PreBlockAttempts = 300, }; - explicit Baton(intptr_t state) : waitingFiber_(state) {}; + explicit Baton(intptr_t state) : waitingFiber_(state){}; void postHelper(intptr_t new_value); void postThread(); @@ -184,7 +185,7 @@ class Baton { } futex_; }; }; - -}} +} +} #include diff --git a/folly/experimental/fibers/BoostContextCompatibility.h b/folly/experimental/fibers/BoostContextCompatibility.h index 908b9542..e243767d 100644 --- a/folly/experimental/fibers/BoostContextCompatibility.h +++ b/folly/experimental/fibers/BoostContextCompatibility.h @@ -21,16 +21,19 @@ /** * Wrappers for different versions of boost::context library * API reference for different versions - * Boost 1.51: http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/context/context/boost_fcontext.html - * Boost 1.52: http://www.boost.org/doc/libs/1_52_0/libs/context/doc/html/context/context/boost_fcontext.html - * Boost 1.56: http://www.boost.org/doc/libs/1_56_0/libs/context/doc/html/context/context/boost_fcontext.html + * Boost 1.51: + * http://www.boost.org/doc/libs/1_51_0/libs/context/doc/html/context/context/boost_fcontext.html + * Boost 1.52: + * http://www.boost.org/doc/libs/1_52_0/libs/context/doc/html/context/context/boost_fcontext.html + * Boost 1.56: + * http://www.boost.org/doc/libs/1_56_0/libs/context/doc/html/context/context/boost_fcontext.html */ -namespace folly { namespace fibers { +namespace folly { +namespace fibers { struct FContext { public: - #if BOOST_VERSION >= 105200 using ContextStruct = boost::context::fcontext_t; #else @@ -57,17 +60,16 @@ struct FContext { ContextStruct context_; #endif - friend intptr_t jumpContext(FContext* oldC, FContext::ContextStruct* newC, - intptr_t p); - friend intptr_t jumpContext(FContext::ContextStruct* oldC, FContext* newC, - intptr_t p); - friend FContext makeContext(void* stackLimit, size_t stackSize, - void(*fn)(intptr_t)); + friend intptr_t + jumpContext(FContext* oldC, FContext::ContextStruct* newC, intptr_t p); + friend intptr_t + jumpContext(FContext::ContextStruct* oldC, FContext* newC, intptr_t p); + friend FContext + makeContext(void* stackLimit, size_t stackSize, void (*fn)(intptr_t)); }; -inline intptr_t jumpContext(FContext* oldC, FContext::ContextStruct* newC, - intptr_t p) { - +inline intptr_t +jumpContext(FContext* oldC, FContext::ContextStruct* newC, intptr_t p) { #if BOOST_VERSION >= 105600 return boost::context::jump_fcontext(&oldC->context_, *newC, p); #elif BOOST_VERSION >= 105200 @@ -75,22 +77,19 @@ inline intptr_t jumpContext(FContext* oldC, FContext::ContextStruct* newC, #else return jump_fcontext(&oldC->context_, newC, p); #endif - } -inline intptr_t jumpContext(FContext::ContextStruct* oldC, FContext* newC, - intptr_t p) { - +inline intptr_t +jumpContext(FContext::ContextStruct* oldC, FContext* newC, intptr_t p) { #if BOOST_VERSION >= 105200 return boost::context::jump_fcontext(oldC, newC->context_, p); #else return jump_fcontext(oldC, &newC->context_, p); #endif - } -inline FContext makeContext(void* stackLimit, size_t stackSize, - void(*fn)(intptr_t)) { +inline FContext +makeContext(void* stackLimit, size_t stackSize, void (*fn)(intptr_t)) { FContext res; res.stackLimit_ = stackLimit; res.stackBase_ = static_cast(stackLimit) + stackSize; @@ -105,5 +104,5 @@ inline FContext makeContext(void* stackLimit, size_t stackSize, return res; } - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/EventBaseLoopController-inl.h b/folly/experimental/fibers/EventBaseLoopController-inl.h index bb26484f..30848e2a 100644 --- a/folly/experimental/fibers/EventBaseLoopController-inl.h +++ b/folly/experimental/fibers/EventBaseLoopController-inl.h @@ -17,7 +17,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { inline EventBaseLoopController::EventBaseLoopController() : callback_(*this), aliveWeak_(destructionCallback_.getWeak()) {} @@ -27,8 +28,7 @@ inline EventBaseLoopController::~EventBaseLoopController() { } inline void EventBaseLoopController::attachEventBase( - folly::EventBase& eventBase) { - + folly::EventBase& eventBase) { if (eventBase_ != nullptr) { LOG(ERROR) << "Attempt to reattach EventBase to LoopController"; } @@ -87,16 +87,19 @@ inline void EventBaseLoopController::scheduleThreadSafe( } } -inline void EventBaseLoopController::timedSchedule(std::function func, - TimePoint time) { +inline void EventBaseLoopController::timedSchedule( + std::function func, + TimePoint time) { assert(eventBaseAttached_); // We want upper bound for the cast, thus we just add 1 - auto delay_ms = std::chrono::duration_cast< - std::chrono::milliseconds>(time - Clock::now()).count() + 1; + auto delay_ms = + std::chrono::duration_cast(time - Clock::now()) + .count() + + 1; // If clock is not monotonic delay_ms = std::max(delay_ms, 0L); eventBase_->tryRunAfterDelay(func, delay_ms); } - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/EventBaseLoopController.h b/folly/experimental/fibers/EventBaseLoopController.h index 96f588c4..eaedb45e 100644 --- a/folly/experimental/fibers/EventBaseLoopController.h +++ b/folly/experimental/fibers/EventBaseLoopController.h @@ -15,16 +15,17 @@ */ #pragma once -#include -#include #include #include +#include +#include namespace folly { class EventBase; } -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class FiberManager; @@ -48,7 +49,9 @@ class EventBaseLoopController : public LoopController { explicit ControllerCallback(EventBaseLoopController& controller) : controller_(controller) {} - void runLoopCallback() noexcept override { controller_.runLoop(); } + void runLoopCallback() noexcept override { + controller_.runLoop(); + } private: EventBaseLoopController& controller_; @@ -57,11 +60,17 @@ class EventBaseLoopController : public LoopController { class DestructionCallback : public folly::EventBase::LoopCallback { public: DestructionCallback() : alive_(new int(42)) {} - ~DestructionCallback() { reset(); } + ~DestructionCallback() { + reset(); + } - void runLoopCallback() noexcept override { reset(); } + void runLoopCallback() noexcept override { + reset(); + } - std::weak_ptr getWeak() { return {alive_}; } + std::weak_ptr getWeak() { + return {alive_}; + } private: void reset() { @@ -96,7 +105,7 @@ class EventBaseLoopController : public LoopController { friend class FiberManager; }; - -}} // folly::fibers +} +} // folly::fibers #include "EventBaseLoopController-inl.h" diff --git a/folly/experimental/fibers/Fiber-inl.h b/folly/experimental/fibers/Fiber-inl.h index 68ee253e..9c871692 100644 --- a/folly/experimental/fibers/Fiber-inl.h +++ b/folly/experimental/fibers/Fiber-inl.h @@ -17,7 +17,8 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { template void Fiber::setFunction(F&& func) { @@ -27,8 +28,7 @@ void Fiber::setFunction(F&& func) { } template -void Fiber::setFunctionFinally(F&& resultFunc, - G&& finallyFunc) { +void Fiber::setFunctionFinally(F&& resultFunc, G&& finallyFunc) { assert(state_ == INVALID); resultFunc_ = std::forward(resultFunc); finallyFunc_ = std::forward(finallyFunc); @@ -68,9 +68,9 @@ void Fiber::LocalData::dataBufferDestructor(void* ptr) { } template -void Fiber::LocalData::dataHeapDestructor(void *ptr) { +void Fiber::LocalData::dataHeapDestructor(void* ptr) { reinterpret_cast(ptr)->~T(); freeHeapBuffer(ptr); } - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/Fiber.cpp b/folly/experimental/fibers/Fiber.cpp index 56b0f3eb..5dc2ff22 100644 --- a/folly/experimental/fibers/Fiber.cpp +++ b/folly/experimental/fibers/Fiber.cpp @@ -18,10 +18,10 @@ #include #include +#include #include #include #include -#include #include #include @@ -29,7 +29,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { namespace { static const uint64_t kMagic8Bytes = 0xfaceb00cfaceb00c; @@ -50,16 +51,12 @@ static size_t nonMagicInBytes(const FContext& context) { uint64_t* end = static_cast(context.stackBase()); auto firstNonMagic = std::find_if( - begin, end, - [](uint64_t val) { - return val != kMagic8Bytes; - } - ); + begin, end, [](uint64_t val) { return val != kMagic8Bytes; }); return (end - firstNonMagic) * sizeof(uint64_t); } -} // anonymous namespace +} // anonymous namespace void Fiber::setData(intptr_t data) { DCHECK_EQ(state_, AWAITING); @@ -78,9 +75,7 @@ void Fiber::setData(intptr_t data) { } } -Fiber::Fiber(FiberManager& fiberManager) : - fiberManager_(fiberManager) { - +Fiber::Fiber(FiberManager& fiberManager) : fiberManager_(fiberManager) { auto size = fiberManager_.options_.stackSize; auto limit = fiberManager_.stackAllocator_.allocate(size); @@ -98,9 +93,10 @@ void Fiber::init(bool recordStackUsed) { auto limit = fcontext_.stackLimit(); auto base = fcontext_.stackBase(); - std::fill(static_cast(limit), - static_cast(base), - kMagic8Bytes); + std::fill( + static_cast(limit), + static_cast(base), + kMagic8Bytes); // newer versions of boost allocate context on fiber stack, // need to create a new one @@ -116,17 +112,17 @@ void Fiber::init(bool recordStackUsed) { Fiber::~Fiber() { fiberManager_.stackAllocator_.deallocate( - static_cast(fcontext_.stackLimit()), - fiberManager_.options_.stackSize); + static_cast(fcontext_.stackLimit()), + fiberManager_.options_.stackSize); } void Fiber::recordStackPosition() { int stackDummy; auto currentPosition = static_cast( - static_cast(fcontext_.stackBase()) - - static_cast(static_cast(&stackDummy))); + static_cast(fcontext_.stackBase()) - + static_cast(static_cast(&stackDummy))); fiberManager_.stackHighWatermark_ = - std::max(fiberManager_.stackHighWatermark_, currentPosition); + std::max(fiberManager_.stackHighWatermark_, currentPosition); VLOG(4) << "Stack usage: " << currentPosition; } @@ -152,17 +148,18 @@ void Fiber::fiberFunc() { func_(); } } catch (...) { - fiberManager_.exceptionCallback_(std::current_exception(), - "running Fiber func_/resultFunc_"); + fiberManager_.exceptionCallback_( + std::current_exception(), "running Fiber func_/resultFunc_"); } if (UNLIKELY(recordStackUsed_)) { - fiberManager_.stackHighWatermark_ = - std::max(fiberManager_.stackHighWatermark_, - nonMagicInBytes(fcontext_)); + fiberManager_.stackHighWatermark_ = std::max( + fiberManager_.stackHighWatermark_, nonMagicInBytes(fcontext_)); VLOG(3) << "Max stack usage: " << fiberManager_.stackHighWatermark_; - CHECK(fiberManager_.stackHighWatermark_ < - fiberManager_.options_.stackSize - 64) << "Fiber stack overflow"; + CHECK( + fiberManager_.stackHighWatermark_ < + fiberManager_.options_.stackSize - 64) + << "Fiber stack overflow"; } state_ = INVALID; @@ -243,5 +240,5 @@ void* Fiber::LocalData::allocateHeapBuffer(size_t size) { void Fiber::LocalData::freeHeapBuffer(void* buffer) { delete[] reinterpret_cast(buffer); } - -}} +} +} diff --git a/folly/experimental/fibers/Fiber.h b/folly/experimental/fibers/Fiber.h index 5025231b..efa43715 100644 --- a/folly/experimental/fibers/Fiber.h +++ b/folly/experimental/fibers/Fiber.h @@ -28,7 +28,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class Baton; class FiberManager; @@ -63,26 +64,26 @@ class Fiber { */ std::pair getStack() const { void* const stack = - std::min(fcontext_.stackLimit(), fcontext_.stackBase()); + std::min(fcontext_.stackLimit(), fcontext_.stackBase()); const size_t size = std::abs( reinterpret_cast(fcontext_.stackBase()) - reinterpret_cast(fcontext_.stackLimit())); - return { stack, size }; + return {stack, size}; } private: enum State { - INVALID, /**< Does't have task function */ - NOT_STARTED, /**< Has task function, not started */ - READY_TO_RUN, /**< Was started, blocked, then unblocked */ - RUNNING, /**< Is running right now */ - AWAITING, /**< Is currently blocked */ - AWAITING_IMMEDIATE, /**< Was preempted to run an immediate function, - and will be resumed right away */ - YIELDED, /**< The fiber yielded execution voluntarily */ + INVALID, /**< Does't have task function */ + NOT_STARTED, /**< Has task function, not started */ + READY_TO_RUN, /**< Was started, blocked, then unblocked */ + RUNNING, /**< Is running right now */ + AWAITING, /**< Is currently blocked */ + AWAITING_IMMEDIATE, /**< Was preempted to run an immediate function, + and will be resumed right away */ + YIELDED, /**< The fiber yielded execution voluntarily */ }; - State state_{INVALID}; /**< current Fiber state */ + State state_{INVALID}; /**< current Fiber state */ friend class Baton; friend class FiberManager; @@ -116,11 +117,11 @@ class Fiber { */ void recordStackPosition(); - FiberManager& fiberManager_; /**< Associated FiberManager */ - FContext fcontext_; /**< current task execution context */ - intptr_t data_; /**< Used to keep some data with the 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 */ + folly::Function func_; /**< task function */ bool recordStackUsed_{false}; bool stackFilledWithMagic_{false}; @@ -154,7 +155,7 @@ class Fiber { void reset(); - //private: + // private: template FOLLY_NOINLINE T& getSlow(); @@ -185,7 +186,7 @@ class Fiber { folly::IntrusiveListHook globalListHook_; /**< list hook for global list */ pid_t threadId_{0}; }; - -}} +} +} #include diff --git a/folly/experimental/fibers/FiberManager-inl.h b/folly/experimental/fibers/FiberManager-inl.h index 0d975b6b..6bad3618 100644 --- a/folly/experimental/fibers/FiberManager-inl.h +++ b/folly/experimental/fibers/FiberManager-inl.h @@ -32,7 +32,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { namespace { @@ -47,7 +48,7 @@ inline FiberManager::Options preprocessOptions(FiberManager::Options opts) { return opts; } -} // anonymous +} // anonymous inline void FiberManager::ensureLoopScheduled() { if (isLoopScheduled_) { @@ -86,8 +87,9 @@ inline void FiberManager::runReadyFiber(Fiber* fiber) { assert(activeFiber_ == nullptr); }; - assert(fiber->state_ == Fiber::NOT_STARTED || - fiber->state_ == Fiber::READY_TO_RUN); + assert( + fiber->state_ == Fiber::NOT_STARTED || + fiber->state_ == Fiber::READY_TO_RUN); currentFiber_ = fiber; fiber->rcontext_ = RequestContext::setContext(std::move(fiber->rcontext_)); if (observer_) { @@ -185,31 +187,27 @@ inline bool FiberManager::loopUntilNoReady() { runReadyFiber(&fiber); } - remoteReadyQueue_.sweep( - [this, &hadRemoteFiber] (Fiber* fiber) { - runReadyFiber(fiber); - hadRemoteFiber = true; + remoteReadyQueue_.sweep([this, &hadRemoteFiber](Fiber* fiber) { + runReadyFiber(fiber); + hadRemoteFiber = true; + }); + + remoteTaskQueue_.sweep([this, &hadRemoteFiber](RemoteTask* taskPtr) { + std::unique_ptr task(taskPtr); + auto fiber = getFiber(); + if (task->localData) { + fiber->localData_ = *task->localData; } - ); - - remoteTaskQueue_.sweep( - [this, &hadRemoteFiber] (RemoteTask* taskPtr) { - std::unique_ptr task(taskPtr); - auto fiber = getFiber(); - if (task->localData) { - fiber->localData_ = *task->localData; - } - fiber->rcontext_ = std::move(task->rcontext); + fiber->rcontext_ = std::move(task->rcontext); - fiber->setFunction(std::move(task->func)); - fiber->data_ = reinterpret_cast(fiber); - if (observer_) { - observer_->runnable(reinterpret_cast(fiber)); - } - runReadyFiber(fiber); - hadRemoteFiber = true; + fiber->setFunction(std::move(task->func)); + fiber->data_ = reinterpret_cast(fiber); + if (observer_) { + observer_->runnable(reinterpret_cast(fiber)); } - ); + runReadyFiber(fiber); + hadRemoteFiber = true; + }); } if (observer_) { @@ -229,19 +227,18 @@ struct FiberManager::AddTaskHelper { class Func; static constexpr bool allocateInBuffer = - sizeof(Func) <= Fiber::kUserBufferSize; + sizeof(Func) <= Fiber::kUserBufferSize; class Func { public: - Func(F&& func, FiberManager& fm) : - func_(std::forward(func)), fm_(fm) {} + Func(F&& func, FiberManager& fm) : func_(std::forward(func)), fm_(fm) {} void operator()() { try { func_(); } catch (...) { - fm_.exceptionCallback_(std::current_exception(), - "running Func functor"); + fm_.exceptionCallback_( + std::current_exception(), "running Func functor"); } if (allocateInBuffer) { this->~Func(); @@ -289,10 +286,11 @@ auto FiberManager::addTaskFuture(F&& func) using T = typename std::result_of::type; folly::Promise p; auto f = p.getFuture(); - addTaskFinally([func = std::forward(func)]() mutable { return func(); }, - [p = std::move(p)](folly::Try && t) mutable { - p.setTry(std::move(t)); - }); + addTaskFinally( + [func = std::forward(func)]() mutable { return func(); }, + [p = std::move(p)](folly::Try && t) mutable { + p.setTry(std::move(t)); + }); return f; } @@ -300,16 +298,16 @@ template void FiberManager::addTaskRemote(F&& func) { auto task = [&]() { auto currentFm = getFiberManagerUnsafe(); - if (currentFm && - currentFm->currentFiber_ && + if (currentFm && currentFm->currentFiber_ && currentFm->localType_ == localType_) { return folly::make_unique( std::forward(func), currentFm->currentFiber_->localData_); } return folly::make_unique(std::forward(func)); }(); - auto insertHead = - [&]() { return remoteTaskQueue_.insertHead(task.release()); }; + auto insertHead = [&]() { + return remoteTaskQueue_.insertHead(task.release()); + }; loopController_->scheduleThreadSafe(std::ref(insertHead)); } @@ -327,9 +325,13 @@ auto FiberManager::addTaskRemoteFuture(F&& func) } template -struct IsRvalueRefTry { static const bool value = false; }; +struct IsRvalueRefTry { + static const bool value = false; +}; template -struct IsRvalueRefTry&&> { static const bool value = true; }; +struct IsRvalueRefTry&&> { + static const bool value = true; +}; // We need this to be in a struct, not inlined in addTaskFinally, because clang // crashes otherwise. @@ -348,8 +350,8 @@ struct FiberManager::AddTaskFinallyHelper { try { finally_(std::move(*result_)); } catch (...) { - fm_.exceptionCallback_(std::current_exception(), - "running Finally functor"); + fm_.exceptionCallback_( + std::current_exception(), "running Finally functor"); } if (allocateInBuffer) { @@ -396,16 +398,14 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { typedef typename std::result_of::type Result; static_assert( - IsRvalueRefTry::type>::value, - "finally(arg): arg must be Try&&"); + IsRvalueRefTry::type>::value, + "finally(arg): arg must be Try&&"); static_assert( - std::is_convertible< - Result, - typename std::remove_reference< - typename FirstArgOf::type - >::type::element_type - >::value, - "finally(Try&&): T must be convertible from func()'s return type"); + std::is_convertible< + Result, + typename std::remove_reference< + typename FirstArgOf::type>::type::element_type>::value, + "finally(Try&&): T must be convertible from func()'s return type"); auto fiber = getFiber(); initLocalData(*fiber); @@ -416,10 +416,9 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { Helper; if (Helper::allocateInBuffer) { - auto funcLoc = static_cast( - fiber->getUserBuffer()); - auto finallyLoc = static_cast( - static_cast(funcLoc + 1)); + auto funcLoc = static_cast(fiber->getUserBuffer()); + auto finallyLoc = + static_cast(static_cast(funcLoc + 1)); new (finallyLoc) typename Helper::Finally(std::forward(finally), *this); new (funcLoc) typename Helper::Func(std::forward(func), *finallyLoc); @@ -444,8 +443,7 @@ void FiberManager::addTaskFinally(F&& func, G&& finally) { } template -typename std::result_of::type -FiberManager::runInMainContext(F&& func) { +typename std::result_of::type FiberManager::runInMainContext(F&& func) { if (UNLIKELY(activeFiber_ == nullptr)) { return func(); } @@ -512,18 +510,18 @@ inline void FiberManager::initLocalData(Fiber& fiber) { template FiberManager::FiberManager( - LocalType, - std::unique_ptr loopController__, - Options options) : - loopController_(std::move(loopController__)), - stackAllocator_(options.useGuardPages), - options_(preprocessOptions(std::move(options))), - exceptionCallback_([](std::exception_ptr eptr, std::string context) { + LocalType, + std::unique_ptr loopController__, + Options options) + : loopController_(std::move(loopController__)), + stackAllocator_(options.useGuardPages), + options_(preprocessOptions(std::move(options))), + exceptionCallback_([](std::exception_ptr eptr, std::string context) { try { std::rethrow_exception(eptr); } catch (const std::exception& e) { - LOG(DFATAL) << "Exception " << typeid(e).name() - << " with message '" << e.what() << "' was thrown in " + LOG(DFATAL) << "Exception " << typeid(e).name() << " with message '" + << e.what() << "' was thrown in " << "FiberManager with context '" << context << "'"; throw; } catch (...) { @@ -532,25 +530,24 @@ FiberManager::FiberManager( throw; } }), - timeoutManager_(std::make_shared(*loopController_)), - fibersPoolResizer_(*this), - localType_(typeid(LocalT)) { + timeoutManager_(std::make_shared(*loopController_)), + fibersPoolResizer_(*this), + localType_(typeid(LocalT)) { loopController_->setFiberManager(this); } template -typename FirstArgOf::type::value_type -inline await(F&& func) { +typename FirstArgOf::type::value_type inline await(F&& func) { typedef typename FirstArgOf::type::value_type Result; folly::Try result; Baton baton; baton.wait([&func, &result, &baton]() mutable { - func(Promise(result, baton)); - }); + func(Promise(result, baton)); + }); return folly::moveFromTry(result); } - -}} +} +} diff --git a/folly/experimental/fibers/FiberManager.cpp b/folly/experimental/fibers/FiberManager.cpp index d4dc6944..b23f4434 100644 --- a/folly/experimental/fibers/FiberManager.cpp +++ b/folly/experimental/fibers/FiberManager.cpp @@ -40,24 +40,28 @@ static void __asan_exit_fiber_weak() typedef void (*AsanEnterFiberFuncPtr)(void const*, size_t); typedef void (*AsanExitFiberFuncPtr)(); -namespace folly { namespace fibers { +namespace folly { +namespace fibers { static AsanEnterFiberFuncPtr getEnterFiberFunc(); static AsanExitFiberFuncPtr getExitFiberFunc(); - -}} +} +} #endif -namespace folly { namespace fibers { +namespace folly { +namespace fibers { FOLLY_TLS FiberManager* FiberManager::currentFiberManager_ = nullptr; -FiberManager::FiberManager(std::unique_ptr loopController, - Options options) : - FiberManager(LocalType(), - std::move(loopController), - std::move(options)) {} +FiberManager::FiberManager( + std::unique_ptr loopController, + Options options) + : FiberManager( + LocalType(), + std::move(loopController), + std::move(options)) {} FiberManager::~FiberManager() { if (isLoopScheduled_) { @@ -65,9 +69,7 @@ FiberManager::~FiberManager() { } while (!fibersPool_.empty()) { - fibersPool_.pop_front_and_dispose([] (Fiber* fiber) { - delete fiber; - }); + fibersPool_.pop_front_and_dispose([](Fiber* fiber) { delete fiber; }); } assert(readyFibers_.empty()); assert(fibersActive_ == 0); @@ -82,9 +84,8 @@ const LoopController& FiberManager::loopController() const { } bool FiberManager::hasTasks() const { - return fibersActive_ > 0 || - !remoteReadyQueue_.empty() || - !remoteTaskQueue_.empty(); + return fibersActive_ > 0 || !remoteReadyQueue_.empty() || + !remoteTaskQueue_.empty(); } Fiber* FiberManager::getFiber() { @@ -110,7 +111,7 @@ Fiber* FiberManager::getFiber() { } ++fiberId_; bool recordStack = (options_.recordStackEvery != 0) && - (fiberId_ % options_.recordStackEvery == 0); + (fiberId_ % options_.recordStackEvery == 0); return fiber; } @@ -166,7 +167,7 @@ void FiberManager::FibersPoolResizer::operator()() { fiberManager_.timeoutManager_->registerTimeout( *this, std::chrono::milliseconds( - fiberManager_.options_.fibersPoolResizePeriodMs)); + fiberManager_.options_.fibersPoolResizePeriodMs)); } #ifdef FOLLY_SANITIZE_ADDRESS @@ -235,4 +236,5 @@ static AsanExitFiberFuncPtr getExitFiberFunc() { } #endif // FOLLY_SANITIZE_ADDRESS -}} +} +} diff --git a/folly/experimental/fibers/FiberManager.h b/folly/experimental/fibers/FiberManager.h index 11a631b5..73a243ce 100644 --- a/folly/experimental/fibers/FiberManager.h +++ b/folly/experimental/fibers/FiberManager.h @@ -19,8 +19,8 @@ #include #include #include -#include #include +#include #include #include @@ -51,8 +51,7 @@ class LoopController; class TimeoutController; template -class LocalType { -}; +class LocalType {}; class InlineFunctionRunner { public: @@ -128,8 +127,9 @@ class FiberManager : public ::folly::Executor { * @param loopController * @param options FiberManager options */ - explicit FiberManager(std::unique_ptr loopController, - Options options = Options()); + explicit FiberManager( + std::unique_ptr loopController, + Options options = Options()); /** * Initializes, but doesn't start FiberManager loop @@ -140,10 +140,10 @@ class FiberManager : public ::folly::Executor { * Locals of other types will be considered thread-locals. */ template - FiberManager(LocalType, - std::unique_ptr loopController, - Options options = Options()); - + FiberManager( + LocalType, + std::unique_ptr loopController, + Options options = Options()); ~FiberManager(); @@ -237,8 +237,7 @@ class FiberManager : public ::folly::Executor { * @return value returned by func(). */ template - typename std::result_of::type - runInMainContext(F&& func); + typename std::result_of::type runInMainContext(F&& func); /** * Returns a refference to a fiber-local context for given Fiber. Should be @@ -323,14 +322,13 @@ class FiberManager : public ::folly::Executor { struct RemoteTask { template - explicit RemoteTask(F&& f) : - func(std::forward(f)), - rcontext(RequestContext::saveContext()) {} + explicit RemoteTask(F&& f) + : func(std::forward(f)), rcontext(RequestContext::saveContext()) {} template - RemoteTask(F&& f, const Fiber::LocalData& localData_) : - func(std::forward(f)), - localData(folly::make_unique(localData_)), - rcontext(RequestContext::saveContext()) {} + RemoteTask(F&& f, const Fiber::LocalData& localData_) + : func(std::forward(f)), + localData(folly::make_unique(localData_)), + rcontext(RequestContext::saveContext()) {} folly::Function func; std::unique_ptr localData; std::shared_ptr rcontext; @@ -351,17 +349,17 @@ class FiberManager : public ::folly::Executor { */ Fiber* currentFiber_{nullptr}; - FiberTailQueue readyFibers_; /**< queue of fibers ready to be executed */ - FiberTailQueue yieldedFibers_; /**< queue of fibers which have yielded - execution */ - FiberTailQueue fibersPool_; /**< pool of unitialized Fiber objects */ + FiberTailQueue readyFibers_; /**< queue of fibers ready to be executed */ + FiberTailQueue yieldedFibers_; /**< queue of fibers which have yielded + execution */ + FiberTailQueue fibersPool_; /**< pool of unitialized Fiber objects */ GlobalFiberTailQueue allFibers_; /**< list of all Fiber objects owned */ - size_t fibersAllocated_{0}; /**< total number of fibers allocated */ - size_t fibersPoolSize_{0}; /**< total number of fibers in the free pool */ - size_t fibersActive_{0}; /**< number of running or blocked fibers */ - size_t fiberId_{0}; /**< id of last fiber used */ + size_t fibersAllocated_{0}; /**< total number of fibers allocated */ + size_t fibersPoolSize_{0}; /**< total number of fibers in the free pool */ + size_t fibersActive_{0}; /**< number of running or blocked fibers */ + size_t fiberId_{0}; /**< id of last fiber used */ /** * Maximum number of active fibers in the last period lasting @@ -369,7 +367,7 @@ class FiberManager : public ::folly::Executor { */ size_t maxFibersActiveLastPeriod_{0}; - FContext::ContextStruct mainContext_; /**< stores loop function context */ + FContext::ContextStruct mainContext_; /**< stores loop function context */ std::unique_ptr loopController_; bool isLoopScheduled_{false}; /**< was the ready loop scheduled to run? */ @@ -386,7 +384,7 @@ class FiberManager : public ::folly::Executor { */ GuardPageAllocator stackAllocator_; - const Options options_; /**< FiberManager options */ + const Options options_; /**< FiberManager options */ /** * Largest observed individual Fiber stack usage in bytes. @@ -439,9 +437,9 @@ class FiberManager : public ::folly::Executor { std::shared_ptr timeoutManager_; struct FibersPoolResizer { - explicit FibersPoolResizer(FiberManager& fm) : - fiberManager_(fm) {} + explicit FibersPoolResizer(FiberManager& fm) : fiberManager_(fm) {} void operator()(); + private: FiberManager& fiberManager_; }; @@ -503,7 +501,7 @@ inline void addTask(F&& func) { template inline void addTaskFinally(F&& func, G&& finally) { return FiberManager::getFiberManager().addTaskFinally( - std::forward(func), std::forward(finally)); + std::forward(func), std::forward(finally)); } /** @@ -514,8 +512,7 @@ inline void addTaskFinally(F&& func, G&& finally) { * @return data which was used to fulfill the promise. */ template -typename FirstArgOf::type::value_type -inline await(F&& func); +typename FirstArgOf::type::value_type inline await(F&& func); /** * If called from a fiber, immediately switches to the FiberManager's context @@ -525,8 +522,7 @@ inline await(F&& func); * @return value returned by func(). */ template -typename std::result_of::type -inline runInMainContext(F&& func) { +typename std::result_of::type inline runInMainContext(F&& func) { auto fm = FiberManager::getFiberManagerUnsafe(); if (UNLIKELY(fm == nullptr)) { return func(); @@ -558,7 +554,7 @@ inline void yield() { std::this_thread::yield(); } } - -}} +} +} #include "FiberManager-inl.h" diff --git a/folly/experimental/fibers/FiberManagerMap.cpp b/folly/experimental/fibers/FiberManagerMap.cpp index 8f88cd22..45481371 100644 --- a/folly/experimental/fibers/FiberManagerMap.cpp +++ b/folly/experimental/fibers/FiberManagerMap.cpp @@ -18,10 +18,11 @@ #include #include -#include #include +#include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { namespace { @@ -109,12 +110,14 @@ class ThreadLocalCache { ThreadLocalCache() {} struct ThreadLocalCacheTag {}; - using ThreadThreadLocalCache = ThreadLocal; + using ThreadThreadLocalCache = + ThreadLocal; // Leak this intentionally. During shutdown, we may call getFiberManager, // and want access to the fiber managers during that time. static ThreadThreadLocalCache& instance() { - static auto ret = new ThreadThreadLocalCache([]() { return new ThreadLocalCache(); }); + static auto ret = + new ThreadThreadLocalCache([]() { return new ThreadLocalCache(); }); return *ret; } @@ -177,9 +180,10 @@ void EventBaseOnDestructionCallback::runLoopCallback() noexcept { } // namespace -FiberManager& getFiberManager(EventBase& evb, - const FiberManager::Options& opts) { +FiberManager& getFiberManager( + EventBase& evb, + const FiberManager::Options& opts) { return ThreadLocalCache::get(evb, opts); } - -}} +} +} diff --git a/folly/experimental/fibers/FiberManagerMap.h b/folly/experimental/fibers/FiberManagerMap.h index bb1988ae..8d233a8a 100644 --- a/folly/experimental/fibers/FiberManagerMap.h +++ b/folly/experimental/fibers/FiberManagerMap.h @@ -15,13 +15,14 @@ */ #pragma once -#include #include +#include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { FiberManager& getFiberManager( folly::EventBase& evb, const FiberManager::Options& opts = FiberManager::Options()); - -}} +} +} diff --git a/folly/experimental/fibers/ForEach-inl.h b/folly/experimental/fibers/ForEach-inl.h index 49a4db43..85333b39 100644 --- a/folly/experimental/fibers/ForEach-inl.h +++ b/folly/experimental/fibers/ForEach-inl.h @@ -15,26 +15,27 @@ */ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { namespace { template typename std::enable_if< - !std::is_same::type, void>::value, void>::type -inline callFuncs(F&& f, G&& g, size_t id) { + !std::is_same::type, void>::value, + void>::type inline callFuncs(F&& f, G&& g, size_t id) { g(id, f()); } template typename std::enable_if< - std::is_same::type, void>::value, void>::type -inline callFuncs(F&& f, G&& g, size_t id) { + std::is_same::type, void>::value, + void>::type inline callFuncs(F&& f, G&& g, size_t id) { f(); g(id); } -} // anonymous namespace +} // anonymous namespace template inline void forEach(InputIterator first, InputIterator last, F&& f) { @@ -52,20 +53,25 @@ inline void forEach(InputIterator first, InputIterator last, F&& f) { #pragma clang diagnostic push // ignore generalized lambda capture warning #pragma clang diagnostic ignored "-Wc++1y-extensions" #endif - auto taskFunc = - [&tasksTodo, &e, &f, &baton] (size_t id, FuncType&& func) { - return [id, &tasksTodo, &e, &f, &baton, - func_ = std::forward(func)]() mutable { - try { - callFuncs(std::forward(func_), f, id); - } catch (...) { - e = std::current_exception(); - } - if (--tasksTodo == 0) { - baton.post(); - } - }; + auto taskFunc = [&tasksTodo, &e, &f, &baton](size_t id, FuncType&& func) { + return [ + id, + &tasksTodo, + &e, + &f, + &baton, + func_ = std::forward(func) + ]() mutable { + try { + callFuncs(std::forward(func_), f, id); + } catch (...) { + e = std::current_exception(); + } + if (--tasksTodo == 0) { + baton.post(); + } }; + }; #ifdef __clang__ #pragma clang diagnostic pop #endif @@ -84,5 +90,5 @@ inline void forEach(InputIterator first, InputIterator last, F&& f) { std::rethrow_exception(e); } } - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/ForEach.h b/folly/experimental/fibers/ForEach.h index 5c71aa4f..ecbb63c0 100644 --- a/folly/experimental/fibers/ForEach.h +++ b/folly/experimental/fibers/ForEach.h @@ -15,7 +15,8 @@ */ #pragma once -namespace folly { namespace fibers { +namespace folly { +namespace fibers { /** * Schedules several tasks and blocks until all of them are completed. @@ -37,7 +38,7 @@ namespace folly { namespace fibers { */ template inline void forEach(InputIterator first, InputIterator last, F&& f); - -}} // folly::fibers +} +} // folly::fibers #include diff --git a/folly/experimental/fibers/GenericBaton.h b/folly/experimental/fibers/GenericBaton.h index de63e4bf..c87b5848 100644 --- a/folly/experimental/fibers/GenericBaton.h +++ b/folly/experimental/fibers/GenericBaton.h @@ -19,8 +19,9 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { typedef Baton GenericBaton; - -}} +} +} diff --git a/folly/experimental/fibers/GuardPageAllocator.cpp b/folly/experimental/fibers/GuardPageAllocator.cpp index f4ff2015..b424e6f7 100644 --- a/folly/experimental/fibers/GuardPageAllocator.cpp +++ b/folly/experimental/fibers/GuardPageAllocator.cpp @@ -25,7 +25,8 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { /** * Each stack with a guard page creates two memory mappings. @@ -52,12 +53,14 @@ constexpr size_t kMaxInUse = 100; */ class StackCache { public: - explicit StackCache(size_t stackSize) - : allocSize_(allocSize(stackSize)) { - auto p = ::mmap(nullptr, allocSize_ * kNumGuarded, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, - -1, 0); + explicit StackCache(size_t stackSize) : allocSize_(allocSize(stackSize)) { + auto p = ::mmap( + nullptr, + allocSize_ * kNumGuarded, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + -1, + 0); PCHECK(p != (void*)(-1)); storage_ = reinterpret_cast(p); @@ -140,7 +143,7 @@ class StackCache { /* Returns a multiple of pagesize() enough to store size + one guard page */ static size_t allocSize(size_t size) { - return pagesize() * ((size + pagesize() - 1)/pagesize() + 1); + return pagesize() * ((size + pagesize() - 1) / pagesize() + 1); } }; @@ -187,8 +190,7 @@ class CacheManager { class StackCacheEntry { public: explicit StackCacheEntry(size_t stackSize) - : stackCache_(folly::make_unique(stackSize)) { - } + : stackCache_(folly::make_unique(stackSize)) {} StackCache& cache() const noexcept { return *stackCache_; @@ -203,8 +205,7 @@ class StackCacheEntry { }; GuardPageAllocator::GuardPageAllocator(bool useGuardPages) - : useGuardPages_(useGuardPages) { -} + : useGuardPages_(useGuardPages) {} GuardPageAllocator::~GuardPageAllocator() = default; @@ -227,5 +228,5 @@ void GuardPageAllocator::deallocate(unsigned char* limit, size_t size) { fallbackAllocator_.deallocate(limit, size); } } - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/GuardPageAllocator.h b/folly/experimental/fibers/GuardPageAllocator.h index 60fc1a6b..21d684be 100644 --- a/folly/experimental/fibers/GuardPageAllocator.h +++ b/folly/experimental/fibers/GuardPageAllocator.h @@ -17,7 +17,8 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class StackCacheEntry; @@ -51,5 +52,5 @@ class GuardPageAllocator { std::allocator fallbackAllocator_; bool useGuardPages_{true}; }; - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/LoopController.h b/folly/experimental/fibers/LoopController.h index e44207bf..917a6557 100644 --- a/folly/experimental/fibers/LoopController.h +++ b/folly/experimental/fibers/LoopController.h @@ -18,7 +18,8 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class FiberManager; @@ -57,5 +58,5 @@ class LoopController { */ virtual void timedSchedule(std::function func, TimePoint time) = 0; }; - -}} // folly::fibers +} +} // folly::fibers diff --git a/folly/experimental/fibers/Promise-inl.h b/folly/experimental/fibers/Promise-inl.h index bc60f164..95565d72 100644 --- a/folly/experimental/fibers/Promise-inl.h +++ b/folly/experimental/fibers/Promise-inl.h @@ -15,16 +15,16 @@ */ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { template -Promise::Promise(folly::Try& value, Baton& baton) : - value_(&value), baton_(&baton) -{} +Promise::Promise(folly::Try& value, Baton& baton) + : value_(&value), baton_(&baton) {} template -Promise::Promise(Promise&& other) noexcept : -value_(other.value_), baton_(other.baton_) { +Promise::Promise(Promise&& other) noexcept + : value_(other.value_), baton_(other.baton_) { other.value_ = nullptr; other.baton_ = nullptr; } @@ -70,16 +70,14 @@ void Promise::setTry(folly::Try&& t) { template template void Promise::setValue(M&& v) { - static_assert(!std::is_same::value, - "Use setValue() instead"); + static_assert(!std::is_same::value, "Use setValue() instead"); setTry(folly::Try(std::forward(v))); } template void Promise::setValue() { - static_assert(std::is_same::value, - "Use setValue(value) instead"); + static_assert(std::is_same::value, "Use setValue(value) instead"); setTry(folly::Try()); } @@ -89,5 +87,5 @@ template void Promise::setWith(F&& func) { setTry(makeTryWith(std::forward(func))); } - -}} +} +} diff --git a/folly/experimental/fibers/Promise.h b/folly/experimental/fibers/Promise.h index c3b2c2ba..78388326 100644 --- a/folly/experimental/fibers/Promise.h +++ b/folly/experimental/fibers/Promise.h @@ -18,13 +18,13 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class Baton; template -typename FirstArgOf::type::value_type -inline await(F&& func); +typename FirstArgOf::type::value_type inline await(F&& func); template class Promise { @@ -84,17 +84,17 @@ class Promise { template typename std::enable_if< - std::is_convertible::type, T>::value && - !std::is_same::value>::type + std::is_convertible::type, T>::value && + !std::is_same::value>::type fulfilHelper(F&& func); template typename std::enable_if< - std::is_same::type, void>::value && - std::is_same::value>::type + std::is_same::type, void>::value && + std::is_same::value>::type fulfilHelper(F&& func); }; - -}} +} +} #include diff --git a/folly/experimental/fibers/SimpleLoopController.h b/folly/experimental/fibers/SimpleLoopController.h index bf7b73ac..94ae9a77 100644 --- a/folly/experimental/fibers/SimpleLoopController.h +++ b/folly/experimental/fibers/SimpleLoopController.h @@ -19,16 +19,14 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { class FiberManager; class SimpleLoopController : public LoopController { public: - SimpleLoopController() - : fm_(nullptr), - stopRequested_(false) { - } + SimpleLoopController() : fm_(nullptr), stopRequested_(false) {} /** * Run FiberManager loop; if no ready task are present, @@ -45,7 +43,7 @@ class SimpleLoopController : public LoopController { auto time = Clock::now(); - for (size_t i=0; i::read_lock() { assert(state_ == State::READ_LOCKED); return; } - assert((state_ == State::UNLOCKED && readers_ == 0) || - (state_ == State::READ_LOCKED && readers_ > 0)); + assert( + (state_ == State::UNLOCKED && readers_ == 0) || + (state_ == State::READ_LOCKED && readers_ > 0)); assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; @@ -147,8 +148,9 @@ bool TimedRWMutex::timed_read_lock( } return true; } - assert((state_ == State::UNLOCKED && readers_ == 0) || - (state_ == State::READ_LOCKED && readers_ > 0)); + assert( + (state_ == State::UNLOCKED && readers_ == 0) || + (state_ == State::READ_LOCKED && readers_ > 0)); assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; @@ -160,8 +162,9 @@ template bool TimedRWMutex::try_read_lock() { pthread_spin_lock(&lock_); if (state_ != State::WRITE_LOCKED) { - assert((state_ == State::UNLOCKED && readers_ == 0) || - (state_ == State::READ_LOCKED && readers_ > 0)); + assert( + (state_ == State::UNLOCKED && readers_ == 0) || + (state_ == State::READ_LOCKED && readers_ > 0)); assert(read_waiters_.empty()); state_ = State::READ_LOCKED; readers_ += 1; @@ -237,15 +240,17 @@ template void TimedRWMutex::unlock() { pthread_spin_lock(&lock_); assert(state_ != State::UNLOCKED); - assert((state_ == State::READ_LOCKED && readers_ > 0) || - (state_ == State::WRITE_LOCKED && readers_ == 0)); + assert( + (state_ == State::READ_LOCKED && readers_ > 0) || + (state_ == State::WRITE_LOCKED && readers_ == 0)); if (state_ == State::READ_LOCKED) { readers_ -= 1; } if (!read_waiters_.empty()) { - assert(state_ == State::WRITE_LOCKED && readers_ == 0 && - "read waiters can only accumulate while write locked"); + assert( + state_ == State::WRITE_LOCKED && readers_ == 0 && + "read waiters can only accumulate while write locked"); state_ = State::READ_LOCKED; readers_ = read_waiters_.size(); @@ -291,5 +296,5 @@ void TimedRWMutex::downgrade() { } pthread_spin_unlock(&lock_); } - -}} +} +} diff --git a/folly/experimental/fibers/TimedMutex.h b/folly/experimental/fibers/TimedMutex.h index 839f163c..a697b2f5 100644 --- a/folly/experimental/fibers/TimedMutex.h +++ b/folly/experimental/fibers/TimedMutex.h @@ -19,7 +19,8 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { /** * @class TimedMutex @@ -49,8 +50,7 @@ class TimedMutex { // // @return true if the mutex was locked, false otherwise template - bool timed_lock( - const std::chrono::duration& duration); + bool timed_lock(const std::chrono::duration& duration); // Try to obtain lock without blocking the thread or fiber bool try_lock(); @@ -68,18 +68,19 @@ class TimedMutex { MutexWaiterHookType hook; }; - typedef boost::intrusive::member_hook MutexWaiterHook; + typedef boost::intrusive:: + member_hook + MutexWaiterHook; - typedef boost::intrusive::list> - MutexWaiterList; + typedef boost::intrusive::list< + MutexWaiter, + MutexWaiterHook, + boost::intrusive::constant_time_size> + MutexWaiterList; - pthread_spinlock_t lock_; //< lock to protect waiter list - bool locked_ = false; //< is this locked by some thread? - MutexWaiterList waiters_; //< list of waiters + pthread_spinlock_t lock_; //< lock to protect waiter list + bool locked_ = false; //< is this locked by some thread? + MutexWaiterList waiters_; //< list of waiters }; /** @@ -136,7 +137,9 @@ class TimedRWMutex { bool try_write_lock(); // Wrapper for write_lock() for compatibility with Mutex - void lock() { write_lock(); } + void lock() { + write_lock(); + } // Realease the lock. The thread / fiber will wake up all readers if there are // any. If there are waiting writers then only one of them will be woken up. @@ -149,8 +152,7 @@ class TimedRWMutex { class ReadHolder { public: - explicit ReadHolder(TimedRWMutex& lock) - : lock_(&lock) { + explicit ReadHolder(TimedRWMutex& lock) : lock_(&lock) { lock_->read_lock(); } @@ -213,28 +215,29 @@ class TimedRWMutex { MutexWaiterHookType hook; }; - typedef boost::intrusive::member_hook MutexWaiterHook; + typedef boost::intrusive:: + member_hook + MutexWaiterHook; - typedef boost::intrusive::list> - MutexWaiterList; + typedef boost::intrusive::list< + MutexWaiter, + MutexWaiterHook, + boost::intrusive::constant_time_size> + MutexWaiterList; - pthread_spinlock_t lock_; //< lock protecting the internal state - // (state_, read_waiters_, etc.) + pthread_spinlock_t lock_; //< lock protecting the internal state + // (state_, read_waiters_, etc.) State state_ = State::UNLOCKED; - uint32_t readers_ = 0; //< Number of readers who have the lock + uint32_t readers_ = 0; //< Number of readers who have the lock - MutexWaiterList write_waiters_; //< List of thread / fibers waiting for - // exclusive access + MutexWaiterList write_waiters_; //< List of thread / fibers waiting for + // exclusive access - MutexWaiterList read_waiters_; //< List of thread / fibers waiting for - // shared access + MutexWaiterList read_waiters_; //< List of thread / fibers waiting for + // shared access }; - -}} +} +} #include "TimedMutex-inl.h" diff --git a/folly/experimental/fibers/TimeoutController.cpp b/folly/experimental/fibers/TimeoutController.cpp index 1ecfc1a5..60360e68 100644 --- a/folly/experimental/fibers/TimeoutController.cpp +++ b/folly/experimental/fibers/TimeoutController.cpp @@ -16,14 +16,15 @@ #include "TimeoutController.h" #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { -TimeoutController::TimeoutController(LoopController& loopController) : - nextTimeout_(TimePoint::max()), - loopController_(loopController) {} +TimeoutController::TimeoutController(LoopController& loopController) + : nextTimeout_(TimePoint::max()), loopController_(loopController) {} -intptr_t TimeoutController::registerTimeout(std::function f, - Duration duration) { +intptr_t TimeoutController::registerTimeout( + std::function f, + Duration duration) { auto& list = [&]() -> TimeoutHandleList& { for (auto& bucket : timeoutHandleBuckets_) { if (bucket.first == duration) { @@ -31,8 +32,8 @@ intptr_t TimeoutController::registerTimeout(std::function f, } } - timeoutHandleBuckets_.emplace_back(duration, - folly::make_unique()); + timeoutHandleBuckets_.emplace_back( + duration, folly::make_unique()); return *timeoutHandleBuckets_.back().second; }(); @@ -84,11 +85,13 @@ void TimeoutController::scheduleRun() { auto time = nextTimeout_; std::weak_ptr timeoutControllerWeak = shared_from_this(); - loopController_.timedSchedule([timeoutControllerWeak, time]() { - if (auto timeoutController = timeoutControllerWeak.lock()) { - timeoutController->runTimeouts(time); - } - }, time); + loopController_.timedSchedule( + [timeoutControllerWeak, time]() { + if (auto timeoutController = timeoutControllerWeak.lock()) { + timeoutController->runTimeouts(time); + } + }, + time); } void TimeoutController::cancel(intptr_t p) { @@ -101,5 +104,5 @@ void TimeoutController::cancel(intptr_t p) { list.pop(); } } - -}} +} +} diff --git a/folly/experimental/fibers/TimeoutController.h b/folly/experimental/fibers/TimeoutController.h index f0198cf8..0f2dced9 100644 --- a/folly/experimental/fibers/TimeoutController.h +++ b/folly/experimental/fibers/TimeoutController.h @@ -26,10 +26,11 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { -class TimeoutController : - public std::enable_shared_from_this { +class TimeoutController + : public std::enable_shared_from_this { public: typedef std::chrono::steady_clock Clock; typedef std::chrono::time_point TimePoint; @@ -50,10 +51,11 @@ class TimeoutController : typedef std::unique_ptr TimeoutHandleListPtr; struct TimeoutHandle { - TimeoutHandle(std::function func_, - TimePoint timeout_, - TimeoutHandleList& list_) : - func(std::move(func_)), timeout(timeout_), list(list_) {} + TimeoutHandle( + std::function func_, + TimePoint timeout_, + TimeoutHandleList& list_) + : func(std::move(func_)), timeout(timeout_), list(list_) {} std::function func; bool canceled{false}; @@ -65,5 +67,5 @@ class TimeoutController : TimePoint nextTimeout_; LoopController& loopController_; }; - -}} +} +} diff --git a/folly/experimental/fibers/WhenN-inl.h b/folly/experimental/fibers/WhenN-inl.h index bd930c95..6bb28048 100644 --- a/folly/experimental/fibers/WhenN-inl.h +++ b/folly/experimental/fibers/WhenN-inl.h @@ -18,24 +18,22 @@ #include #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { template -typename std::vector< - typename std::enable_if< +typename std::vector::value_type()>::type, void - >::value, + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, typename std::pair< - size_t, - typename std::result_of< - typename std::iterator_traits::value_type()>::type> - >::type - > + size_t, + typename std::result_of::value_type()>::type>>::type> collectN(InputIterator first, InputIterator last, size_t n) { typedef typename std::result_of< - typename std::iterator_traits::value_type()>::type Result; + typename std::iterator_traits::value_type()>::type Result; assert(n > 0); assert(std::distance(first, last) >= 0); assert(n <= static_cast(std::distance(first, last))); @@ -52,37 +50,35 @@ collectN(InputIterator first, InputIterator last, size_t n) { }; auto context = std::make_shared(n); - await( - [first, last, context](Promise promise) mutable { - context->promise = std::move(promise); - for (size_t i = 0; first != last; ++i, ++first) { + await([first, last, context](Promise promise) mutable { + context->promise = std::move(promise); + for (size_t i = 0; first != last; ++i, ++first) { #ifdef __clang__ #pragma clang diagnostic push // ignore generalized lambda capture warning #pragma clang diagnostic ignored "-Wc++1y-extensions" #endif - addTask( - [i, context, f = std::move(*first)]() { - try { - auto result = f(); - if (context->tasksTodo == 0) { - return; - } - context->results.emplace_back(i, std::move(result)); - } catch (...) { - if (context->tasksTodo == 0) { - return; - } - context->e = std::current_exception(); - } - if (--context->tasksTodo == 0) { - context->promise->setValue(); - } - }); + addTask([ i, context, f = std::move(*first) ]() { + try { + auto result = f(); + if (context->tasksTodo == 0) { + return; + } + context->results.emplace_back(i, std::move(result)); + } catch (...) { + if (context->tasksTodo == 0) { + return; + } + context->e = std::current_exception(); + } + if (--context->tasksTodo == 0) { + context->promise->setValue(); + } + }); #ifdef __clang__ #pragma clang diagnostic pop #endif - } - }); + } + }); if (context->e != std::exception_ptr()) { std::rethrow_exception(context->e); @@ -93,10 +89,11 @@ collectN(InputIterator first, InputIterator last, size_t n) { template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, std::vector>::type + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + std::vector>::type collectN(InputIterator first, InputIterator last, size_t n) { assert(n > 0); assert(std::distance(first, last) >= 0); @@ -114,37 +111,35 @@ collectN(InputIterator first, InputIterator last, size_t n) { }; auto context = std::make_shared(n); - await( - [first, last, context](Promise promise) mutable { - context->promise = std::move(promise); - for (size_t i = 0; first != last; ++i, ++first) { + await([first, last, context](Promise promise) mutable { + context->promise = std::move(promise); + for (size_t i = 0; first != last; ++i, ++first) { #ifdef __clang__ #pragma clang diagnostic push // ignore generalized lambda capture warning #pragma clang diagnostic ignored "-Wc++1y-extensions" #endif - addTask( - [i, context, f = std::move(*first)]() { - try { - f(); - if (context->tasksTodo == 0) { - return; - } - context->taskIndices.push_back(i); - } catch (...) { - if (context->tasksTodo == 0) { - return; - } - context->e = std::current_exception(); - } - if (--context->tasksTodo == 0) { - context->promise->setValue(); - } - }); + addTask([ i, context, f = std::move(*first) ]() { + try { + f(); + if (context->tasksTodo == 0) { + return; + } + context->taskIndices.push_back(i); + } catch (...) { + if (context->tasksTodo == 0) { + return; + } + context->e = std::current_exception(); + } + if (--context->tasksTodo == 0) { + context->promise->setValue(); + } + }); #ifdef __clang__ #pragma clang diagnostic pop #endif - } - }); + } + }); if (context->e != std::exception_ptr()) { std::rethrow_exception(context->e); @@ -155,26 +150,25 @@ collectN(InputIterator first, InputIterator last, size_t n) { template typename std::vector< - typename std::enable_if< - !std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, - typename std::result_of< - typename std::iterator_traits::value_type()>::type>::type> -inline collectAll(InputIterator first, InputIterator last) { + typename std::enable_if< + !std::is_same< + typename std::result_of::value_type()>::type, + void>::value, + typename std::result_of< + typename std::iterator_traits::value_type()>::type>:: + type> inline collectAll(InputIterator first, InputIterator last) { typedef typename std::result_of< - typename std::iterator_traits::value_type()>::type Result; + typename std::iterator_traits::value_type()>::type Result; size_t n = std::distance(first, last); std::vector results; std::vector order(n); results.reserve(n); - forEach(first, last, - [&results, &order] (size_t id, Result result) { - order[id] = results.size(); - results.emplace_back(std::move(result)); - }); + forEach(first, last, [&results, &order](size_t id, Result result) { + order[id] = results.size(); + results.emplace_back(std::move(result)); + }); assert(results.size() == n); std::vector orderedResults; @@ -189,26 +183,25 @@ inline collectAll(InputIterator first, InputIterator last) { template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, void>::type -inline collectAll(InputIterator first, InputIterator last) { + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + void>::type inline collectAll(InputIterator first, InputIterator last) { forEach(first, last, [](size_t /* id */) {}); } template typename std::enable_if< - !std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, - typename std::pair< - size_t, - typename std::result_of< - typename std::iterator_traits::value_type()>::type> - >::type -inline collectAny(InputIterator first, InputIterator last) { + !std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + typename std::pair< + size_t, + typename std::result_of::value_type()>::type>>:: + type inline collectAny(InputIterator first, InputIterator last) { auto result = collectN(first, last, 1); assert(result.size() == 1); return std::move(result[0]); @@ -216,14 +209,14 @@ inline collectAny(InputIterator first, InputIterator last) { template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, size_t>::type -inline collectAny(InputIterator first, InputIterator last) { + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + size_t>::type inline collectAny(InputIterator first, InputIterator last) { auto result = collectN(first, last, 1); assert(result.size() == 1); return std::move(result[0]); } - -}} +} +} diff --git a/folly/experimental/fibers/WhenN.h b/folly/experimental/fibers/WhenN.h index 2f12f5f8..a12e12f2 100644 --- a/folly/experimental/fibers/WhenN.h +++ b/folly/experimental/fibers/WhenN.h @@ -15,7 +15,8 @@ */ #pragma once -namespace folly { namespace fibers { +namespace folly { +namespace fibers { /** * Schedules several tasks and blocks until n of these tasks are completed. @@ -31,18 +32,16 @@ namespace folly { namespace fibers { */ template typename std::vector< - typename std::enable_if< - !std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, - void>::value, - typename std::pair< - size_t, - typename std::result_of< - typename std::iterator_traits::value_type()>::type> - >::type - > -inline collectN(InputIterator first, InputIterator last, size_t n); + typename std::enable_if< + !std::is_same< + typename std::result_of::value_type()>::type, + void>::value, + typename std::pair< + size_t, + typename std::result_of::value_type()>::type>>:: + type> inline collectN(InputIterator first, InputIterator last, size_t n); /** * collectN specialization for functions returning void @@ -55,11 +54,12 @@ inline collectN(InputIterator first, InputIterator last, size_t n); */ template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, std::vector>::type -inline collectN(InputIterator first, InputIterator last, size_t n); + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + std::vector>:: + type inline collectN(InputIterator first, InputIterator last, size_t n); /** * Schedules several tasks and blocks until all of these tasks are completed. @@ -73,16 +73,14 @@ inline collectN(InputIterator first, InputIterator last, size_t n); * @return vector of values returned by tasks */ template -typename std::vector< - typename std::enable_if< +typename std::vector::value_type()>::type, - void>::value, + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, typename std::result_of< - typename std::iterator_traits::value_type()>::type>::type - > -inline collectAll(InputIterator first, InputIterator last); + typename std::iterator_traits::value_type()>:: + type>::type> inline collectAll(InputIterator first, InputIterator last); /** * collectAll specialization for functions returning void @@ -92,11 +90,11 @@ inline collectAll(InputIterator first, InputIterator last); */ template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, void>::type -inline collectAll(InputIterator first, InputIterator last); + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + void>::type inline collectAll(InputIterator first, InputIterator last); /** * Schedules several tasks and blocks until one of them is completed. @@ -110,16 +108,15 @@ inline collectAll(InputIterator first, InputIterator last); */ template typename std::enable_if< - !std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, - typename std::pair< - size_t, - typename std::result_of< - typename std::iterator_traits::value_type()>::type> - >::type -inline collectAny(InputIterator first, InputIterator last); + !std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + typename std::pair< + size_t, + typename std::result_of::value_type()>::type>>:: + type inline collectAny(InputIterator first, InputIterator last); /** * WhenAny specialization for functions returning void. @@ -131,12 +128,12 @@ inline collectAny(InputIterator first, InputIterator last); */ template typename std::enable_if< - std::is_same< - typename std::result_of< - typename std::iterator_traits::value_type()>::type, void - >::value, size_t>::type -inline collectAny(InputIterator first, InputIterator last); - -}} + std::is_same< + typename std::result_of< + typename std::iterator_traits::value_type()>::type, + void>::value, + size_t>::type inline collectAny(InputIterator first, InputIterator last); +} +} #include diff --git a/folly/experimental/fibers/test/FibersTest.cpp b/folly/experimental/fibers/test/FibersTest.cpp index 4fe93645..ac66df6e 100644 --- a/folly/experimental/fibers/test/FibersTest.cpp +++ b/folly/experimental/fibers/test/FibersTest.cpp @@ -41,38 +41,34 @@ TEST(FiberManager, batonTimedWaitTimeout) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { - if (!taskAdded) { - manager.addTask( - [&]() { - Baton baton; + if (!taskAdded) { + manager.addTask([&]() { + Baton baton; - auto res = baton.timed_wait(std::chrono::milliseconds(230)); + auto res = baton.timed_wait(std::chrono::milliseconds(230)); - EXPECT_FALSE(res); - EXPECT_EQ(5, iterations); + EXPECT_FALSE(res); + EXPECT_EQ(5, iterations); - loopController.stop(); - } - ); - manager.addTask( - [&]() { - Baton baton; + loopController.stop(); + }); + manager.addTask([&]() { + Baton baton; - auto res = baton.timed_wait(std::chrono::milliseconds(130)); + auto res = baton.timed_wait(std::chrono::milliseconds(130)); - EXPECT_FALSE(res); - EXPECT_EQ(3, iterations); + EXPECT_FALSE(res); + EXPECT_EQ(3, iterations); - loopController.stop(); - } - ); + loopController.stop(); + }); taskAdded = true; } else { std::this_thread::sleep_for(std::chrono::milliseconds(50)); - iterations ++; + iterations++; } }; @@ -86,27 +82,25 @@ TEST(FiberManager, batonTimedWaitPost) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - Baton baton; - baton_ptr = &baton; + manager.addTask([&]() { + Baton baton; + baton_ptr = &baton; - auto res = baton.timed_wait(std::chrono::milliseconds(130)); + auto res = baton.timed_wait(std::chrono::milliseconds(130)); - EXPECT_TRUE(res); - EXPECT_EQ(2, iterations); + EXPECT_TRUE(res); + EXPECT_EQ(2, iterations); - loopController.stop(); - } - ); + loopController.stop(); + }); taskAdded = true; } else { std::this_thread::sleep_for(std::chrono::milliseconds(50)); - iterations ++; + iterations++; if (iterations == 2) { baton_ptr->post(); } @@ -122,8 +116,8 @@ TEST(FiberManager, batonTimedWaitTimeoutEvb) { folly::EventBase evb; FiberManager manager(folly::make_unique()); - dynamic_cast( - manager.loopController()).attachEventBase(evb); + dynamic_cast(manager.loopController()) + .attachEventBase(evb); auto task = [&](size_t timeout_ms) { Baton baton; @@ -135,7 +129,7 @@ TEST(FiberManager, batonTimedWaitTimeoutEvb) { EXPECT_FALSE(res); auto duration_ms = - std::chrono::duration_cast(finish - start); + std::chrono::duration_cast(finish - start); EXPECT_GT(duration_ms.count(), timeout_ms - 50); EXPECT_LT(duration_ms.count(), timeout_ms + 50); @@ -146,16 +140,8 @@ TEST(FiberManager, batonTimedWaitTimeoutEvb) { }; evb.runInEventBaseThread([&]() { - manager.addTask( - [&]() { - task(500); - } - ); - manager.addTask( - [&]() { - task(250); - } - ); + manager.addTask([&]() { task(500); }); + manager.addTask([&]() { task(250); }); }); evb.loopForever(); @@ -169,35 +155,31 @@ TEST(FiberManager, batonTimedWaitPostEvb) { folly::EventBase evb; FiberManager manager(folly::make_unique()); - dynamic_cast( - manager.loopController()).attachEventBase(evb); + dynamic_cast(manager.loopController()) + .attachEventBase(evb); evb.runInEventBaseThread([&]() { - manager.addTask([&]() { - Baton baton; + manager.addTask([&]() { + Baton baton; - evb.tryRunAfterDelay([&]() { - baton.post(); - }, - 100); + evb.tryRunAfterDelay([&]() { baton.post(); }, 100); - auto start = EventBaseLoopController::Clock::now(); - auto res = baton.timed_wait(std::chrono::milliseconds(130)); - auto finish = EventBaseLoopController::Clock::now(); + auto start = EventBaseLoopController::Clock::now(); + auto res = baton.timed_wait(std::chrono::milliseconds(130)); + auto finish = EventBaseLoopController::Clock::now(); - EXPECT_TRUE(res); + EXPECT_TRUE(res); - auto duration_ms = std::chrono::duration_cast< - std::chrono::milliseconds>(finish - start); + auto duration_ms = + std::chrono::duration_cast(finish - start); - EXPECT_TRUE(duration_ms.count() > 95 && - duration_ms.count() < 110); + EXPECT_TRUE(duration_ms.count() > 95 && duration_ms.count() < 110); - if (++tasksComplete == 1) { - evb.terminateLoopSoon(); - } - }); + if (++tasksComplete == 1) { + evb.terminateLoopSoon(); + } }); + }); evb.loopForever(); @@ -205,17 +187,16 @@ TEST(FiberManager, batonTimedWaitPostEvb) { } TEST(FiberManager, batonTryWait) { - FiberManager manager(folly::make_unique()); // Check if try_wait and post work as expected Baton b; - manager.addTask([&](){ + manager.addTask([&]() { while (!b.try_wait()) { } }); - auto thr = std::thread([&](){ + auto thr = std::thread([&]() { std::this_thread::sleep_for(std::chrono::milliseconds(300)); b.post(); }); @@ -226,7 +207,7 @@ TEST(FiberManager, batonTryWait) { Baton c; // Check try_wait without post - manager.addTask([&](){ + manager.addTask([&]() { int cnt = 100; while (cnt && !c.try_wait()) { cnt--; @@ -244,7 +225,7 @@ TEST(FiberManager, genericBatonFiberWait) { GenericBaton b; bool fiberRunning = false; - manager.addTask([&](){ + manager.addTask([&]() { EXPECT_EQ(manager.hasActiveFiber(), true); fiberRunning = true; b.wait(); @@ -255,7 +236,7 @@ TEST(FiberManager, genericBatonFiberWait) { manager.loopUntilNoReady(); EXPECT_TRUE(fiberRunning); // ensure fiber still active - auto thr = std::thread([&](){ + auto thr = std::thread([&]() { std::this_thread::sleep_for(std::chrono::milliseconds(300)); b.post(); }); @@ -272,20 +253,22 @@ TEST(FiberManager, genericBatonThreadWait) { GenericBaton b; std::atomic threadWaiting(false); - auto thr = std::thread([&](){ + auto thr = std::thread([&]() { threadWaiting = true; b.wait(); threadWaiting = false; }); - while (!threadWaiting) {} + while (!threadWaiting) { + } std::this_thread::sleep_for(std::chrono::milliseconds(300)); - manager.addTask([&](){ + manager.addTask([&]() { EXPECT_EQ(manager.hasActiveFiber(), true); EXPECT_TRUE(threadWaiting); b.post(); - while(threadWaiting) {} + while (threadWaiting) { + } }); manager.loopUntilNoReady(); @@ -298,36 +281,32 @@ TEST(FiberManager, addTasksNoncopyable) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector()>> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - return folly::make_unique(i*2 + 1); - } - ); - } + manager.addTask([&]() { + std::vector()>> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + return folly::make_unique(i * 2 + 1); + }); + } - auto iter = addTasks(funcs.begin(), funcs.end()); + auto iter = addTasks(funcs.begin(), funcs.end()); - size_t n = 0; - while (iter.hasNext()) { - auto result = iter.awaitNext(); - EXPECT_EQ(2 * iter.getTaskID() + 1, *result); - EXPECT_GE(2 - n, pendingFibers.size()); - ++n; - } - EXPECT_EQ(3, n); + size_t n = 0; + while (iter.hasNext()) { + auto result = iter.awaitNext(); + EXPECT_EQ(2 * iter.getTaskID() + 1, *result); + EXPECT_GE(2 - n, pendingFibers.size()); + ++n; } - ); + EXPECT_EQ(3, n); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -346,44 +325,40 @@ TEST(FiberManager, addTasksThrow) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - if (i % 2 == 0) { - throw std::runtime_error("Runtime"); - } - return i*2 + 1; - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + if (i % 2 == 0) { + throw std::runtime_error("Runtime"); + } + return i * 2 + 1; + }); + } - auto iter = addTasks(funcs.begin(), funcs.end()); + auto iter = addTasks(funcs.begin(), funcs.end()); - size_t n = 0; - while (iter.hasNext()) { - try { - int result = iter.awaitNext(); - EXPECT_EQ(1, iter.getTaskID() % 2); - EXPECT_EQ(2 * iter.getTaskID() + 1, result); - } catch (...) { - EXPECT_EQ(0, iter.getTaskID() % 2); - } - EXPECT_GE(2 - n, pendingFibers.size()); - ++n; + size_t n = 0; + while (iter.hasNext()) { + try { + int result = iter.awaitNext(); + EXPECT_EQ(1, iter.getTaskID() % 2); + EXPECT_EQ(2 * iter.getTaskID() + 1, result); + } catch (...) { + EXPECT_EQ(0, iter.getTaskID() % 2); } - EXPECT_EQ(3, n); + EXPECT_GE(2 - n, pendingFibers.size()); + ++n; } - ); + EXPECT_EQ(3, n); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -402,34 +377,30 @@ TEST(FiberManager, addTasksVoid) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + }); + } - auto iter = addTasks(funcs.begin(), funcs.end()); + auto iter = addTasks(funcs.begin(), funcs.end()); - size_t n = 0; - while (iter.hasNext()) { - iter.awaitNext(); - EXPECT_GE(2 - n, pendingFibers.size()); - ++n; - } - EXPECT_EQ(3, n); + size_t n = 0; + while (iter.hasNext()) { + iter.awaitNext(); + EXPECT_GE(2 - n, pendingFibers.size()); + ++n; } - ); + EXPECT_EQ(3, n); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -448,42 +419,38 @@ TEST(FiberManager, addTasksVoidThrow) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - if (i % 2 == 0) { - throw std::runtime_error(""); - } - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + if (i % 2 == 0) { + throw std::runtime_error(""); + } + }); + } - auto iter = addTasks(funcs.begin(), funcs.end()); + auto iter = addTasks(funcs.begin(), funcs.end()); - size_t n = 0; - while (iter.hasNext()) { - try { - iter.awaitNext(); - EXPECT_EQ(1, iter.getTaskID() % 2); - } catch (...) { - EXPECT_EQ(0, iter.getTaskID() % 2); - } - EXPECT_GE(2 - n, pendingFibers.size()); - ++n; + size_t n = 0; + while (iter.hasNext()) { + try { + iter.awaitNext(); + EXPECT_EQ(1, iter.getTaskID() % 2); + } catch (...) { + EXPECT_EQ(0, iter.getTaskID() % 2); } - EXPECT_EQ(3, n); + EXPECT_GE(2 - n, pendingFibers.size()); + ++n; } - ); + EXPECT_EQ(3, n); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -502,46 +469,42 @@ TEST(FiberManager, reserve) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [&pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([&pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + }); + } - auto iter = addTasks(funcs.begin(), funcs.end()); + auto iter = addTasks(funcs.begin(), funcs.end()); - iter.reserve(2); - EXPECT_TRUE(iter.hasCompleted()); - EXPECT_TRUE(iter.hasPending()); - EXPECT_TRUE(iter.hasNext()); + iter.reserve(2); + EXPECT_TRUE(iter.hasCompleted()); + EXPECT_TRUE(iter.hasPending()); + EXPECT_TRUE(iter.hasNext()); - iter.awaitNext(); - EXPECT_TRUE(iter.hasCompleted()); - EXPECT_TRUE(iter.hasPending()); - EXPECT_TRUE(iter.hasNext()); + iter.awaitNext(); + EXPECT_TRUE(iter.hasCompleted()); + EXPECT_TRUE(iter.hasPending()); + EXPECT_TRUE(iter.hasNext()); - iter.awaitNext(); - EXPECT_FALSE(iter.hasCompleted()); - EXPECT_TRUE(iter.hasPending()); - EXPECT_TRUE(iter.hasNext()); + iter.awaitNext(); + EXPECT_FALSE(iter.hasCompleted()); + EXPECT_TRUE(iter.hasPending()); + EXPECT_TRUE(iter.hasNext()); - iter.awaitNext(); - EXPECT_FALSE(iter.hasCompleted()); - EXPECT_FALSE(iter.hasPending()); - EXPECT_FALSE(iter.hasNext()); - } - ); + iter.awaitNext(); + EXPECT_FALSE(iter.hasCompleted()); + EXPECT_FALSE(iter.hasPending()); + EXPECT_FALSE(iter.hasNext()); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -560,36 +523,31 @@ TEST(FiberManager, forEach) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - return i * 2 + 1; - } - ); - } - - std::vector> results; - forEach(funcs.begin(), funcs.end(), - [&results](size_t id, int result) { - results.emplace_back(id, result); + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); }); - EXPECT_EQ(3, results.size()); - EXPECT_TRUE(pendingFibers.empty()); - for (size_t i = 0; i < 3; ++i) { - EXPECT_EQ(results[i].first * 2 + 1, results[i].second); - } + return i * 2 + 1; + }); } - ); + + std::vector> results; + forEach(funcs.begin(), funcs.end(), [&results](size_t id, int result) { + results.emplace_back(id, result); + }); + EXPECT_EQ(3, results.size()); + EXPECT_TRUE(pendingFibers.empty()); + for (size_t i = 0; i < 3; ++i) { + EXPECT_EQ(results[i].first * 2 + 1, results[i].second); + } + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -608,32 +566,28 @@ TEST(FiberManager, collectN) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - return i*2 + 1; - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + return i * 2 + 1; + }); + } - auto results = collectN(funcs.begin(), funcs.end(), 2); - EXPECT_EQ(2, results.size()); - EXPECT_EQ(1, pendingFibers.size()); - for (size_t i = 0; i < 2; ++i) { - EXPECT_EQ(results[i].first*2 + 1, results[i].second); - } + auto results = collectN(funcs.begin(), funcs.end(), 2); + EXPECT_EQ(2, results.size()); + EXPECT_EQ(1, pendingFibers.size()); + for (size_t i = 0; i < 2; ++i) { + EXPECT_EQ(results[i].first * 2 + 1, results[i].second); } - ); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -652,32 +606,28 @@ TEST(FiberManager, collectNThrow) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - throw std::runtime_error("Runtime"); - return i*2+1; - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + throw std::runtime_error("Runtime"); + return i * 2 + 1; + }); + } - try { - collectN(funcs.begin(), funcs.end(), 2); - } catch (...) { - EXPECT_EQ(1, pendingFibers.size()); - } + try { + collectN(funcs.begin(), funcs.end(), 2); + } catch (...) { + EXPECT_EQ(1, pendingFibers.size()); } - ); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -696,28 +646,24 @@ TEST(FiberManager, collectNVoid) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - } - ); - } - - auto results = collectN(funcs.begin(), funcs.end(), 2); - EXPECT_EQ(2, results.size()); - EXPECT_EQ(1, pendingFibers.size()); + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + }); } - ); + + auto results = collectN(funcs.begin(), funcs.end(), 2); + EXPECT_EQ(2, results.size()); + EXPECT_EQ(1, pendingFibers.size()); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -736,31 +682,27 @@ TEST(FiberManager, collectNVoidThrow) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - throw std::runtime_error("Runtime"); - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + throw std::runtime_error("Runtime"); + }); + } - try { - collectN(funcs.begin(), funcs.end(), 2); - } catch (...) { - EXPECT_EQ(1, pendingFibers.size()); - } + try { + collectN(funcs.begin(), funcs.end(), 2); + } catch (...) { + EXPECT_EQ(1, pendingFibers.size()); } - ); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -779,31 +721,27 @@ TEST(FiberManager, collectAll) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - return i*2+1; - } - ); - } + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + return i * 2 + 1; + }); + } - auto results = collectAll(funcs.begin(), funcs.end()); - EXPECT_TRUE(pendingFibers.empty()); - for (size_t i = 0; i < 3; ++i) { - EXPECT_EQ(i*2+1, results[i]); - } + auto results = collectAll(funcs.begin(), funcs.end()); + EXPECT_TRUE(pendingFibers.empty()); + for (size_t i = 0; i < 3; ++i) { + EXPECT_EQ(i * 2 + 1, results[i]); } - ); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -822,27 +760,23 @@ TEST(FiberManager, collectAllVoid) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector> funcs; - for (size_t i = 0; i < 3; ++ i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - } - ); - } - - collectAll(funcs.begin(), funcs.end()); - EXPECT_TRUE(pendingFibers.empty()); + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + }); } - ); + + collectAll(funcs.begin(), funcs.end()); + EXPECT_TRUE(pendingFibers.empty()); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -861,33 +795,29 @@ TEST(FiberManager, collectAny) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); auto loopFunc = [&]() { if (!taskAdded) { - manager.addTask( - [&]() { - std::vector > funcs; - for (size_t i = 0; i < 3; ++ i) { - funcs.push_back( - [i, &pendingFibers]() { - await([&pendingFibers](Promise promise) { - pendingFibers.push_back(std::move(promise)); - }); - if (i == 1) { - throw std::runtime_error("This exception will be ignored"); - } - return i*2+1; - } - ); - } - - auto result = collectAny(funcs.begin(), funcs.end()); - EXPECT_EQ(2, pendingFibers.size()); - EXPECT_EQ(2, result.first); - EXPECT_EQ(2*2+1, result.second); + manager.addTask([&]() { + std::vector> funcs; + for (size_t i = 0; i < 3; ++i) { + funcs.push_back([i, &pendingFibers]() { + await([&pendingFibers](Promise promise) { + pendingFibers.push_back(std::move(promise)); + }); + if (i == 1) { + throw std::runtime_error("This exception will be ignored"); + } + return i * 2 + 1; + }); } - ); + + auto result = collectAny(funcs.begin(), funcs.end()); + EXPECT_EQ(2, pendingFibers.size()); + EXPECT_EQ(2, result.first); + EXPECT_EQ(2 * 2 + 1, result.second); + }); taskAdded = true; } else if (pendingFibers.size()) { pendingFibers.back().setValue(0); @@ -925,15 +855,13 @@ void expectMainContext(bool& ran, int* mainLocation, int* fiberLocation) { TEST(FiberManager, runInMainContext) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); bool checkRan = false; int mainLocation; manager.runInMainContext( - [&]() { - expectMainContext(checkRan, &mainLocation, nullptr); - }); + [&]() { expectMainContext(checkRan, &mainLocation, nullptr); }); EXPECT_TRUE(checkRan); checkRan = false; @@ -955,11 +883,7 @@ TEST(FiberManager, runInMainContext) { EXPECT_EQ(42, ret.value); }); - loopController.loop( - [&]() { - loopController.stop(); - } - ); + loopController.loop([&]() { loopController.stop(); }); EXPECT_TRUE(checkRan); } @@ -967,30 +891,23 @@ TEST(FiberManager, runInMainContext) { TEST(FiberManager, addTaskFinally) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); bool checkRan = false; int mainLocation; manager.addTaskFinally( - [&]() { - return 1234; - }, - [&](Try&& result) { - EXPECT_EQ(result.value(), 1234); + [&]() { return 1234; }, + [&](Try&& result) { + EXPECT_EQ(result.value(), 1234); - expectMainContext(checkRan, &mainLocation, nullptr); - } - ); + expectMainContext(checkRan, &mainLocation, nullptr); + }); EXPECT_FALSE(checkRan); - loopController.loop( - [&]() { - loopController.stop(); - } - ); + loopController.loop([&]() { loopController.stop(); }); EXPECT_TRUE(checkRan); } @@ -1001,39 +918,23 @@ TEST(FiberManager, fibersPoolWithinLimit) { FiberManager manager(folly::make_unique(), opts); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); size_t fibersRun = 0; for (size_t i = 0; i < 5; ++i) { - manager.addTask( - [&]() { - ++fibersRun; - } - ); + manager.addTask([&]() { ++fibersRun; }); } - loopController.loop( - [&]() { - loopController.stop(); - } - ); + loopController.loop([&]() { loopController.stop(); }); EXPECT_EQ(5, fibersRun); EXPECT_EQ(5, manager.fibersAllocated()); EXPECT_EQ(5, manager.fibersPoolSize()); for (size_t i = 0; i < 5; ++i) { - manager.addTask( - [&]() { - ++fibersRun; - } - ); + manager.addTask([&]() { ++fibersRun; }); } - loopController.loop( - [&]() { - loopController.stop(); - } - ); + loopController.loop([&]() { loopController.stop(); }); EXPECT_EQ(10, fibersRun); EXPECT_EQ(5, manager.fibersAllocated()); @@ -1046,27 +947,19 @@ TEST(FiberManager, fibersPoolOverLimit) { FiberManager manager(folly::make_unique(), opts); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); size_t fibersRun = 0; for (size_t i = 0; i < 10; ++i) { - manager.addTask( - [&]() { - ++fibersRun; - } - ); + manager.addTask([&]() { ++fibersRun; }); } EXPECT_EQ(0, fibersRun); EXPECT_EQ(10, manager.fibersAllocated()); EXPECT_EQ(0, manager.fibersPoolSize()); - loopController.loop( - [&]() { - loopController.stop(); - } - ); + loopController.loop([&]() { loopController.stop(); }); EXPECT_EQ(10, fibersRun); EXPECT_EQ(5, manager.fibersAllocated()); @@ -1076,23 +969,19 @@ TEST(FiberManager, fibersPoolOverLimit) { TEST(FiberManager, remoteFiberBasic) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); int result[2]; result[0] = result[1] = 0; folly::Optional> savedPromise[2]; - manager.addTask( - [&] () { - result[0] = await([&] (Promise promise) { - savedPromise[0] = std::move(promise); - }); - }); - manager.addTask( - [&] () { - result[1] = await([&] (Promise promise) { - savedPromise[1] = std::move(promise); - }); - }); + manager.addTask([&]() { + result[0] = await( + [&](Promise promise) { savedPromise[0] = std::move(promise); }); + }); + manager.addTask([&]() { + result[1] = await( + [&](Promise promise) { savedPromise[1] = std::move(promise); }); + }); manager.loopUntilNoReady(); @@ -1101,16 +990,8 @@ TEST(FiberManager, remoteFiberBasic) { EXPECT_EQ(0, result[0]); EXPECT_EQ(0, result[1]); - std::thread remoteThread0{ - [&] () { - savedPromise[0]->setValue(42); - } - }; - std::thread remoteThread1{ - [&] () { - savedPromise[1]->setValue(43); - } - }; + std::thread remoteThread0{[&]() { savedPromise[0]->setValue(42); }}; + std::thread remoteThread1{[&]() { savedPromise[1]->setValue(43); }}; remoteThread0.join(); remoteThread1.join(); EXPECT_EQ(0, result[0]); @@ -1130,26 +1011,18 @@ TEST(FiberManager, addTaskRemoteBasic) { result[0] = result[1] = 0; folly::Optional> savedPromise[2]; - std::thread remoteThread0{ - [&] () { - manager.addTaskRemote( - [&] () { - result[0] = await([&] (Promise promise) { - savedPromise[0] = std::move(promise); - }); - }); - } - }; - std::thread remoteThread1{ - [&] () { - manager.addTaskRemote( - [&] () { - result[1] = await([&] (Promise promise) { - savedPromise[1] = std::move(promise); - }); - }); - } - }; + std::thread remoteThread0{[&]() { + manager.addTaskRemote([&]() { + result[0] = await( + [&](Promise promise) { savedPromise[0] = std::move(promise); }); + }); + }}; + std::thread remoteThread1{[&]() { + manager.addTaskRemote([&]() { + result[1] = await( + [&](Promise promise) { savedPromise[1] = std::move(promise); }); + }); + }}; remoteThread0.join(); remoteThread1.join(); @@ -1174,11 +1047,7 @@ TEST(FiberManager, addTaskRemoteBasic) { TEST(FiberManager, remoteHasTasks) { size_t counter = 0; FiberManager fm(folly::make_unique()); - std::thread remote([&]() { - fm.addTaskRemote([&]() { - ++counter; - }); - }); + std::thread remote([&]() { fm.addTaskRemote([&]() { ++counter; }); }); remote.join(); @@ -1196,9 +1065,8 @@ TEST(FiberManager, remoteHasReadyTasks) { FiberManager fm(folly::make_unique()); std::thread remote([&]() { fm.addTaskRemote([&]() { - result = await([&](Promise promise) { - savedPromise = std::move(promise); - }); + result = await( + [&](Promise promise) { savedPromise = std::move(promise); }); EXPECT_TRUE(fm.hasTasks()); }); }); @@ -1209,9 +1077,7 @@ TEST(FiberManager, remoteHasReadyTasks) { fm.loopUntilNoReady(); EXPECT_TRUE(fm.hasTasks()); - std::thread remote2([&](){ - savedPromise->setValue(47); - }); + std::thread remote2([&]() { savedPromise->setValue(47); }); remote2.join(); EXPECT_TRUE(fm.hasTasks()); @@ -1223,48 +1089,44 @@ TEST(FiberManager, remoteHasReadyTasks) { template void testFiberLocal() { - FiberManager fm(LocalType(), - folly::make_unique()); + FiberManager fm( + LocalType(), folly::make_unique()); fm.addTask([]() { - EXPECT_EQ(42, local().value); + EXPECT_EQ(42, local().value); - local().value = 43; + local().value = 43; - addTask([]() { - EXPECT_EQ(43, local().value); + addTask([]() { + EXPECT_EQ(43, local().value); - local().value = 44; + local().value = 44; - addTask([]() { - EXPECT_EQ(44, local().value); - }); - }); - }); + addTask([]() { EXPECT_EQ(44, local().value); }); + }); + }); fm.addTask([&]() { - EXPECT_EQ(42, local().value); + EXPECT_EQ(42, local().value); - local().value = 43; + local().value = 43; - fm.addTaskRemote([]() { - EXPECT_EQ(43, local().value); - }); - }); + fm.addTaskRemote([]() { EXPECT_EQ(43, local().value); }); + }); fm.addTask([]() { - EXPECT_EQ(42, local().value); - local().value = 43; - - auto task = []() { - EXPECT_EQ(43, local().value); - local().value = 44; - }; - std::vector> tasks{task}; - collectAny(tasks.begin(), tasks.end()); + EXPECT_EQ(42, local().value); + local().value = 43; + auto task = []() { EXPECT_EQ(43, local().value); - }); + local().value = 44; + }; + std::vector> tasks{task}; + collectAny(tasks.begin(), tasks.end()); + + EXPECT_EQ(43, local().value); + }); fm.loopUntilNoReady(); EXPECT_FALSE(fm.hasTasks()); @@ -1280,7 +1142,7 @@ TEST(FiberManager, fiberLocal) { TEST(FiberManager, fiberLocalHeap) { struct LargeData { - char _[1024*1024]; + char _[1024 * 1024]; int value{42}; }; @@ -1294,20 +1156,18 @@ TEST(FiberManager, fiberLocalDestructor) { ~CrazyData() { if (data == 41) { addTask([]() { - EXPECT_EQ(42, local().data); - // Make sure we don't have infinite loop - local().data = 0; - }); + EXPECT_EQ(42, local().data); + // Make sure we don't have infinite loop + local().data = 0; + }); } } }; - FiberManager fm(LocalType(), - folly::make_unique()); + FiberManager fm( + LocalType(), folly::make_unique()); - fm.addTask([]() { - local().data = 41; - }); + fm.addTask([]() { local().data = 41; }); fm.loopUntilNoReady(); EXPECT_FALSE(fm.hasTasks()); @@ -1316,24 +1176,20 @@ TEST(FiberManager, fiberLocalDestructor) { TEST(FiberManager, yieldTest) { FiberManager manager(folly::make_unique()); auto& loopController = - dynamic_cast(manager.loopController()); + dynamic_cast(manager.loopController()); bool checkRan = false; - manager.addTask( - [&]() { - manager.yield(); - checkRan = true; - } - ); + manager.addTask([&]() { + manager.yield(); + checkRan = true; + }); - loopController.loop( - [&]() { - if (checkRan) { - loopController.stop(); - } + loopController.loop([&]() { + if (checkRan) { + loopController.stop(); } - ); + }); EXPECT_TRUE(checkRan); } @@ -1352,25 +1208,22 @@ TEST(FiberManager, RequestContext) { folly::RequestContext::create(); auto rcontext1 = folly::RequestContext::get(); fm.addTask([&]() { - EXPECT_EQ(rcontext1, folly::RequestContext::get()); - baton1.wait([&]() { - EXPECT_EQ(rcontext1, folly::RequestContext::get()); - }); - EXPECT_EQ(rcontext1, folly::RequestContext::get()); - runInMainContext([&]() { - EXPECT_EQ(rcontext1, folly::RequestContext::get()); - }); - checkRun1 = true; - }); + EXPECT_EQ(rcontext1, folly::RequestContext::get()); + baton1.wait([&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); }); + EXPECT_EQ(rcontext1, folly::RequestContext::get()); + runInMainContext( + [&]() { EXPECT_EQ(rcontext1, folly::RequestContext::get()); }); + checkRun1 = true; + }); folly::RequestContext::create(); auto rcontext2 = folly::RequestContext::get(); fm.addTaskRemote([&]() { - EXPECT_EQ(rcontext2, folly::RequestContext::get()); - baton2.wait(); - EXPECT_EQ(rcontext2, folly::RequestContext::get()); - checkRun2 = true; - }); + EXPECT_EQ(rcontext2, folly::RequestContext::get()); + baton2.wait(); + EXPECT_EQ(rcontext2, folly::RequestContext::get()); + checkRun2 = true; + }); folly::RequestContext::create(); auto rcontext3 = folly::RequestContext::get(); @@ -1421,7 +1274,7 @@ TEST(FiberManager, resizePeriodically) { folly::EventBase evb; dynamic_cast(manager.loopController()) - .attachEventBase(evb); + .attachEventBase(evb); std::vector batons(10); @@ -1479,7 +1332,7 @@ TEST(FiberManager, batonWaitTimeoutHandler) { folly::EventBase evb; dynamic_cast(manager.loopController()) - .attachEventBase(evb); + .attachEventBase(evb); size_t fibersRun = 0; Baton baton; @@ -1511,7 +1364,7 @@ TEST(FiberManager, batonWaitTimeoutMany) { folly::EventBase evb; dynamic_cast(manager.loopController()) - .attachEventBase(evb); + .attachEventBase(evb); constexpr size_t kNumTimeoutTasks = 10000; size_t tasksCount = kNumTimeoutTasks; @@ -1558,13 +1411,13 @@ TEST(FiberManager, nestedFiberManagers) { folly::EventBase innerEvb; getFiberManager(outerEvb).addTask([&]() { - EXPECT_EQ(&getFiberManager(outerEvb), - FiberManager::getFiberManagerUnsafe()); + EXPECT_EQ( + &getFiberManager(outerEvb), FiberManager::getFiberManagerUnsafe()); runInMainContext([&]() { getFiberManager(innerEvb).addTask([&]() { - EXPECT_EQ(&getFiberManager(innerEvb), - FiberManager::getFiberManagerUnsafe()); + EXPECT_EQ( + &getFiberManager(innerEvb), FiberManager::getFiberManagerUnsafe()); innerEvb.terminateLoopSoon(); }); @@ -1572,8 +1425,8 @@ TEST(FiberManager, nestedFiberManagers) { innerEvb.loopForever(); }); - EXPECT_EQ(&getFiberManager(outerEvb), - FiberManager::getFiberManagerUnsafe()); + EXPECT_EQ( + &getFiberManager(outerEvb), FiberManager::getFiberManagerUnsafe()); outerEvb.terminateLoopSoon(); }); @@ -1588,7 +1441,7 @@ void runBenchmark(size_t numAwaits, size_t toSend) { FiberManager fiberManager(folly::make_unique()); auto& loopController = - dynamic_cast(fiberManager.loopController()); + dynamic_cast(fiberManager.loopController()); std::queue> pendingRequests; static const size_t maxOutstanding = 5; @@ -1602,14 +1455,13 @@ void runBenchmark(size_t numAwaits, size_t toSend) { pendingRequests.pop(); } else { fiberManager.addTask([&pendingRequests]() { - for (size_t i = 0; i < sNumAwaits; ++i) { - auto result = await( - [&pendingRequests](Promise promise) { - pendingRequests.push(std::move(promise)); - }); - DCHECK_EQ(result, 0); - } - }); + for (size_t i = 0; i < sNumAwaits; ++i) { + auto result = await([&pendingRequests](Promise promise) { + pendingRequests.push(std::move(promise)); + }); + DCHECK_EQ(result, 0); + } + }); if (--toSend == 0) { loopController.stop(); @@ -1651,11 +1503,7 @@ BENCHMARK(FiberManagerAllocateDeallocatePattern, iters) { size_t fibersRun = 0; for (size_t i = 0; i < kNumAllocations; ++i) { - fiberManager.addTask( - [&fibersRun] { - ++fibersRun; - } - ); + fiberManager.addTask([&fibersRun] { ++fibersRun; }); fiberManager.loopUntilNoReady(); } @@ -1678,11 +1526,7 @@ BENCHMARK(FiberManagerAllocateLargeChunk, iters) { size_t fibersRun = 0; for (size_t i = 0; i < kNumAllocations; ++i) { - fiberManager.addTask( - [&fibersRun] { - ++fibersRun; - } - ); + fiberManager.addTask([&fibersRun] { ++fibersRun; }); } fiberManager.loopUntilNoReady(); diff --git a/folly/experimental/fibers/test/FibersTestApp.cpp b/folly/experimental/fibers/test/FibersTestApp.cpp index 441831c9..44617ac7 100644 --- a/folly/experimental/fibers/test/FibersTestApp.cpp +++ b/folly/experimental/fibers/test/FibersTestApp.cpp @@ -25,18 +25,17 @@ using namespace folly::fibers; struct Application { public: - Application () + Application() : fiberManager(folly::make_unique()), toSend(20), - maxOutstanding(5) { - } + maxOutstanding(5) {} void loop() { if (pendingRequests.size() == maxOutstanding || toSend == 0) { if (pendingRequests.empty()) { return; } - intptr_t value = rand()%1000; + intptr_t value = rand() % 1000; std::cout << "Completing request with data = " << value << std::endl; pendingRequests.front().setValue(value); @@ -47,27 +46,25 @@ struct Application { std::cout << "Adding new request with id = " << id << std::endl; fiberManager.addTask([this, id]() { - std::cout << "Executing fiber with id = " << id << std::endl; + std::cout << "Executing fiber with id = " << id << std::endl; - auto result1 = await( - [this](Promise fiber) { - pendingRequests.push(std::move(fiber)); - }); + auto result1 = await([this](Promise fiber) { + pendingRequests.push(std::move(fiber)); + }); - std::cout << "Fiber id = " << id - << " got result1 = " << result1 << std::endl; + std::cout << "Fiber id = " << id << " got result1 = " << result1 + << std::endl; - auto result2 = await - ([this](Promise fiber) { - pendingRequests.push(std::move(fiber)); - }); - std::cout << "Fiber id = " << id - << " got result2 = " << result2 << std::endl; - }); + auto result2 = await([this](Promise fiber) { + pendingRequests.push(std::move(fiber)); + }); + std::cout << "Fiber id = " << id << " got result2 = " << result2 + << std::endl; + }); if (--toSend == 0) { auto& loopController = - dynamic_cast(fiberManager.loopController()); + dynamic_cast(fiberManager.loopController()); loopController.stop(); } } @@ -83,12 +80,10 @@ struct Application { int main() { Application app; - auto loop = [&app]() { - app.loop(); - }; + auto loop = [&app]() { app.loop(); }; auto& loopController = - dynamic_cast(app.fiberManager.loopController()); + dynamic_cast(app.fiberManager.loopController()); loopController.loop(std::move(loop)); diff --git a/folly/experimental/fibers/test/main.cpp b/folly/experimental/fibers/test/main.cpp index a2ee61ac..ac94a9ed 100644 --- a/folly/experimental/fibers/test/main.cpp +++ b/folly/experimental/fibers/test/main.cpp @@ -18,8 +18,10 @@ #include // for backward compatibility with gflags -namespace gflags { } -namespace google { using namespace gflags; } +namespace gflags {} +namespace google { +using namespace gflags; +} int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); diff --git a/folly/experimental/fibers/traits.h b/folly/experimental/fibers/traits.h index 0f16dd6c..3dc0e483 100644 --- a/folly/experimental/fibers/traits.h +++ b/folly/experimental/fibers/traits.h @@ -17,7 +17,8 @@ #include -namespace folly { namespace fibers { +namespace folly { +namespace fibers { /** * For any functor F taking >= 1 argument, @@ -38,7 +39,7 @@ namespace detail { * If F is a pointer-to-member, will contain a typedef type * with the type of F's first parameter */ -template +template struct ExtractFirstMemfn; template @@ -51,20 +52,20 @@ struct ExtractFirstMemfn { typedef First type; }; -} // detail +} // detail /** Default - use boost */ template struct FirstArgOf { typedef typename boost::function_traits< - typename std::remove_pointer::type>::arg1_type type; + typename std::remove_pointer::type>::arg1_type type; }; /** Specialization for function objects */ template struct FirstArgOf::value>::type> { - typedef typename detail::ExtractFirstMemfn< - decltype(&F::operator())>::type type; + typedef + typename detail::ExtractFirstMemfn::type type; }; - -}} // folly::fibers +} +} // folly::fibers