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