Commit dfed6248 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

Always enable fiber support in futures

Summary:
[Folly] Always enable fiber support in futures.

(Note: this ignores all push blocking failures!)

Reviewed By: andriigrynenko

Differential Revision: D19229519

fbshipit-source-id: f0a034e9387c5ccad676a911213fab74364ca340
parent bdd4cbe1
...@@ -49,13 +49,9 @@ coro::Task<void> BatchSemaphore::co_wait(int64_t tokens) { ...@@ -49,13 +49,9 @@ coro::Task<void> BatchSemaphore::co_wait(int64_t tokens) {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
SemiFuture<Unit> BatchSemaphore::future_wait(int64_t tokens) { SemiFuture<Unit> BatchSemaphore::future_wait(int64_t tokens) {
return future_wait_common(tokens); return future_wait_common(tokens);
} }
#endif
} // namespace fibers } // namespace fibers
} // namespace folly } // namespace folly
...@@ -75,14 +75,10 @@ class BatchSemaphore : public SemaphoreBase { ...@@ -75,14 +75,10 @@ class BatchSemaphore : public SemaphoreBase {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
/* /*
* Wait for requested tokens in the semaphore. * Wait for requested tokens in the semaphore.
*/ */
SemiFuture<Unit> future_wait(int64_t tokens); SemiFuture<Unit> future_wait(int64_t tokens);
#endif
}; };
} // namespace fibers } // namespace fibers
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <folly/ConstexprMath.h> #include <folly/ConstexprMath.h>
#include <folly/SingletonThreadLocal.h> #include <folly/SingletonThreadLocal.h>
#include <folly/portability/Config.h>
#include <folly/portability/SysSyscall.h> #include <folly/portability/SysSyscall.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
#include <folly/synchronization/SanitizeThread.h> #include <folly/synchronization/SanitizeThread.h>
...@@ -329,7 +330,9 @@ static AsanUnpoisonMemoryRegionFuncPtr getUnpoisonMemoryRegionFunc() { ...@@ -329,7 +330,9 @@ static AsanUnpoisonMemoryRegionFuncPtr getUnpoisonMemoryRegionFunc() {
#endif // FOLLY_SANITIZE_ADDRESS #endif // FOLLY_SANITIZE_ADDRESS
#ifndef _WIN32 // TVOS and WatchOS platforms have SIGSTKSZ but not sigaltstack
#if defined(SIGSTKSZ) && !FOLLY_APPLE_TVOS && !FOLLY_APPLE_WATCHOS
namespace { namespace {
// SIGSTKSZ (8 kB on our architectures) isn't always enough for // SIGSTKSZ (8 kB on our architectures) isn't always enough for
...@@ -383,11 +386,19 @@ class ScopedAlternateSignalStack { ...@@ -383,11 +386,19 @@ class ScopedAlternateSignalStack {
}; };
} // namespace } // namespace
void FiberManager::registerAlternateSignalStack() { void FiberManager::maybeRegisterAlternateSignalStack() {
SingletonThreadLocal<ScopedAlternateSignalStack>::get(); SingletonThreadLocal<ScopedAlternateSignalStack>::get();
alternateSignalStackRegistered_ = true; alternateSignalStackRegistered_ = true;
} }
#else
void FiberManager::maybeRegisterAlternateSignalStack() {
// no-op
}
#endif #endif
} // namespace fibers } // namespace fibers
} // namespace folly } // namespace folly
...@@ -195,11 +195,9 @@ inline void FiberManager::loopUntilNoReady() { ...@@ -195,11 +195,9 @@ inline void FiberManager::loopUntilNoReady() {
template <typename LoopFunc> template <typename LoopFunc>
void FiberManager::runFibersHelper(LoopFunc&& loopFunc) { void FiberManager::runFibersHelper(LoopFunc&& loopFunc) {
#ifndef _WIN32
if (UNLIKELY(!alternateSignalStackRegistered_)) { if (UNLIKELY(!alternateSignalStackRegistered_)) {
registerAlternateSignalStack(); maybeRegisterAlternateSignalStack();
} }
#endif
// Support nested FiberManagers // Support nested FiberManagers
auto originalFiberManager = std::exchange(getCurrentFiberManager(), this); auto originalFiberManager = std::exchange(getCurrentFiberManager(), this);
......
...@@ -618,11 +618,9 @@ class FiberManager : public ::folly::Executor { ...@@ -618,11 +618,9 @@ class FiberManager : public ::folly::Executor {
#endif // FOLLY_SANITIZE_ADDRESS #endif // FOLLY_SANITIZE_ADDRESS
#ifndef _WIN32
bool alternateSignalStackRegistered_{false}; bool alternateSignalStackRegistered_{false};
void registerAlternateSignalStack(); void maybeRegisterAlternateSignalStack();
#endif
}; };
/** /**
......
...@@ -187,8 +187,6 @@ coro::Task<void> Semaphore::co_wait() { ...@@ -187,8 +187,6 @@ coro::Task<void> Semaphore::co_wait() {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
namespace { namespace {
class FutureWaiter final : public fibers::Baton::Waiter { class FutureWaiter final : public fibers::Baton::Waiter {
...@@ -230,8 +228,6 @@ SemiFuture<Unit> Semaphore::future_wait() { ...@@ -230,8 +228,6 @@ SemiFuture<Unit> Semaphore::future_wait() {
return makeSemiFuture(); return makeSemiFuture();
} }
#endif
size_t Semaphore::getCapacity() const { size_t Semaphore::getCapacity() const {
return capacity_; return capacity_;
} }
......
...@@ -100,15 +100,11 @@ class Semaphore { ...@@ -100,15 +100,11 @@ class Semaphore {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
/* /*
* Wait for capacity in the semaphore. * Wait for capacity in the semaphore.
*/ */
SemiFuture<Unit> future_wait(); SemiFuture<Unit> future_wait();
#endif
size_t getCapacity() const; size_t getCapacity() const;
private: private:
......
...@@ -163,8 +163,6 @@ coro::Task<void> SemaphoreBase::co_wait_common(int64_t tokens) { ...@@ -163,8 +163,6 @@ coro::Task<void> SemaphoreBase::co_wait_common(int64_t tokens) {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
namespace { namespace {
class FutureWaiter final : public fibers::Baton::Waiter { class FutureWaiter final : public fibers::Baton::Waiter {
...@@ -207,8 +205,6 @@ SemiFuture<Unit> SemaphoreBase::future_wait_common(int64_t tokens) { ...@@ -207,8 +205,6 @@ SemiFuture<Unit> SemaphoreBase::future_wait_common(int64_t tokens) {
return makeSemiFuture(); return makeSemiFuture();
} }
#endif
size_t SemaphoreBase::getCapacity() const { size_t SemaphoreBase::getCapacity() const {
return capacity_; return capacity_;
} }
......
...@@ -93,15 +93,11 @@ class SemaphoreBase { ...@@ -93,15 +93,11 @@ class SemaphoreBase {
#endif #endif
#if FOLLY_FUTURE_USING_FIBER
/* /*
* Wait for request capacity in the semaphore. * Wait for request capacity in the semaphore.
*/ */
SemiFuture<Unit> future_wait_common(int64_t tokens); SemiFuture<Unit> future_wait_common(int64_t tokens);
#endif
bool waitSlow(Waiter& waiter, int64_t tokens); bool waitSlow(Waiter& waiter, int64_t tokens);
bool signalSlow(int64_t tokens, int64_t oldVal); bool signalSlow(int64_t tokens, int64_t oldVal);
......
...@@ -1737,11 +1737,7 @@ TEST(FiberManager, semaphore) { ...@@ -1737,11 +1737,7 @@ TEST(FiberManager, semaphore) {
sem.wait(); sem.wait();
break; break;
case 1: case 1:
#if FOLLY_FUTURE_USING_FIBER
sem.future_wait().get(); sem.future_wait().get();
#else
sem.wait();
#endif
break; break;
case 2: { case 2: {
Semaphore::Waiter waiter; Semaphore::Waiter waiter;
...@@ -1820,11 +1816,7 @@ TEST(FiberManager, batchSemaphore) { ...@@ -1820,11 +1816,7 @@ TEST(FiberManager, batchSemaphore) {
sem.wait(tokens); sem.wait(tokens);
break; break;
case 1: case 1:
#if FOLLY_FUTURE_USING_FIBER
sem.future_wait(tokens).get(); sem.future_wait(tokens).get();
#else
sem.wait(tokens);
#endif
break; break;
case 2: { case 2: {
BatchSemaphore::Waiter waiter{tokens}; BatchSemaphore::Waiter waiter{tokens};
......
...@@ -31,11 +31,6 @@ ...@@ -31,11 +31,6 @@
#include <folly/executors/InlineExecutor.h> #include <folly/executors/InlineExecutor.h>
#include <folly/executors/QueuedImmediateExecutor.h> #include <folly/executors/QueuedImmediateExecutor.h>
#include <folly/futures/detail/Core.h> #include <folly/futures/detail/Core.h>
#include <folly/synchronization/Baton.h>
#if FOLLY_FUTURE_USING_FIBER
#include <folly/fibers/Baton.h>
#endif
namespace folly { namespace folly {
...@@ -43,11 +38,7 @@ class Timekeeper; ...@@ -43,11 +38,7 @@ class Timekeeper;
namespace futures { namespace futures {
namespace detail { namespace detail {
#if FOLLY_FUTURE_USING_FIBER
typedef folly::fibers::Baton FutureBatonType; typedef folly::fibers::Baton FutureBatonType;
#else
typedef folly::Baton<> FutureBatonType;
#endif
} // namespace detail } // namespace detail
} // namespace futures } // namespace futures
...@@ -504,17 +495,13 @@ class WaitExecutor final : public folly::Executor { ...@@ -504,17 +495,13 @@ class WaitExecutor final : public folly::Executor {
void drive() { void drive() {
baton_.wait(); baton_.wait();
#if FOLLY_FUTURE_USING_FIBER
fibers::runInMainContext([&]() { fibers::runInMainContext([&]() {
#endif
baton_.reset(); baton_.reset();
auto funcs = std::move(queue_.wlock()->funcs); auto funcs = std::move(queue_.wlock()->funcs);
for (auto& func : funcs) { for (auto& func : funcs) {
std::exchange(func, nullptr)(); std::exchange(func, nullptr)();
} }
#if FOLLY_FUTURE_USING_FIBER
}); });
#endif
} }
using Clock = std::chrono::steady_clock; using Clock = std::chrono::steady_clock;
...@@ -523,18 +510,14 @@ class WaitExecutor final : public folly::Executor { ...@@ -523,18 +510,14 @@ class WaitExecutor final : public folly::Executor {
if (!baton_.try_wait_until(deadline)) { if (!baton_.try_wait_until(deadline)) {
return false; return false;
} }
#if FOLLY_FUTURE_USING_FIBER
return fibers::runInMainContext([&]() { return fibers::runInMainContext([&]() {
#endif
baton_.reset(); baton_.reset();
auto funcs = std::move(queue_.wlock()->funcs); auto funcs = std::move(queue_.wlock()->funcs);
for (auto& func : funcs) { for (auto& func : funcs) {
std::exchange(func, nullptr)(); std::exchange(func, nullptr)();
} }
return true; return true;
#if FOLLY_FUTURE_USING_FIBER
}); });
#endif
} }
void detach() { void detach() {
......
...@@ -39,8 +39,6 @@ Future<Unit> sleepUnsafe(HighResDuration dur, Timekeeper* tk) { ...@@ -39,8 +39,6 @@ Future<Unit> sleepUnsafe(HighResDuration dur, Timekeeper* tk) {
return sleep(dur, tk).toUnsafeFuture(); return sleep(dur, tk).toUnsafeFuture();
} }
#if FOLLY_FUTURE_USING_FIBER
namespace { namespace {
template <typename Ptr> template <typename Ptr>
class FutureWaiter : public fibers::Baton::Waiter { class FutureWaiter : public fibers::Baton::Waiter {
...@@ -76,8 +74,6 @@ SemiFuture<Unit> wait(std::shared_ptr<fibers::Baton> baton) { ...@@ -76,8 +74,6 @@ SemiFuture<Unit> wait(std::shared_ptr<fibers::Baton> baton) {
return sf; return sf;
} }
#endif
} // namespace futures } // namespace futures
#if FOLLY_USE_EXTERN_FUTURE_UNIT #if FOLLY_USE_EXTERN_FUTURE_UNIT
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <folly/Utility.h> #include <folly/Utility.h>
#include <folly/executors/DrivableExecutor.h> #include <folly/executors/DrivableExecutor.h>
#include <folly/executors/TimedDrivableExecutor.h> #include <folly/executors/TimedDrivableExecutor.h>
#include <folly/fibers/Baton.h>
#include <folly/functional/Invoke.h> #include <folly/functional/Invoke.h>
#include <folly/futures/Portability.h> #include <folly/futures/Portability.h>
#include <folly/futures/Promise.h> #include <folly/futures/Promise.h>
...@@ -108,14 +109,6 @@ class SemiFuture; ...@@ -108,14 +109,6 @@ class SemiFuture;
template <class T> template <class T>
class FutureSplitter; class FutureSplitter;
#if FOLLY_FUTURE_USING_FIBER
namespace fibers {
class Baton;
}
#endif
namespace futures { namespace futures {
namespace detail { namespace detail {
template <class T> template <class T>
...@@ -2171,13 +2164,9 @@ template <class F> ...@@ -2171,13 +2164,9 @@ template <class F>
auto when(bool p, F&& thunk) auto when(bool p, F&& thunk)
-> decltype(std::declval<invoke_result_t<F>>().unit()); -> decltype(std::declval<invoke_result_t<F>>().unit());
#if FOLLY_FUTURE_USING_FIBER
SemiFuture<Unit> wait(std::unique_ptr<fibers::Baton> baton); SemiFuture<Unit> wait(std::unique_ptr<fibers::Baton> baton);
SemiFuture<Unit> wait(std::shared_ptr<fibers::Baton> baton); SemiFuture<Unit> wait(std::shared_ptr<fibers::Baton> baton);
#endif
/** /**
* Returns a lazy SemiFuture constructed by f, which also ensures that ensure is * Returns a lazy SemiFuture constructed by f, which also ensures that ensure is
* called before completion. * called before completion.
......
...@@ -17,9 +17,3 @@ ...@@ -17,9 +17,3 @@
#pragma once #pragma once
#include <folly/Portability.h> #include <folly/Portability.h>
#if FOLLY_MOBILE || defined(__APPLE__)
#define FOLLY_FUTURE_USING_FIBER 0
#else
#define FOLLY_FUTURE_USING_FIBER 1
#endif
...@@ -1505,8 +1505,6 @@ TEST(Future, SimpleTimedGetTry) { ...@@ -1505,8 +1505,6 @@ TEST(Future, SimpleTimedGetTry) {
std::move(sf).getTry(std::chrono::milliseconds(100)), FutureTimeout); std::move(sf).getTry(std::chrono::milliseconds(100)), FutureTimeout);
} }
#if FOLLY_FUTURE_USING_FIBER
TEST(Future, BatonWait) { TEST(Future, BatonWait) {
auto baton = std::make_unique<fibers::Baton>(); auto baton = std::make_unique<fibers::Baton>();
bool posted{false}; bool posted{false};
...@@ -1524,5 +1522,3 @@ TEST(Future, BatonWait) { ...@@ -1524,5 +1522,3 @@ TEST(Future, BatonWait) {
.getVia(&executor); .getVia(&executor);
EXPECT_TRUE(postFuture.isReady()); EXPECT_TRUE(postFuture.isReady());
} }
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment