From f8ae709202cb0481b570d06415411a49e1b147d6 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Thu, 21 Mar 2019 14:43:59 -0700 Subject: [PATCH] Fix up fiber-related guards in futures code Summary: [Folly] Fix up fiber-related guards in futures code. Because of cyclic `#include`s the preprocessor symbol definition must be moved. Fixes #1061. Reviewed By: wez, andriigrynenko Differential Revision: D14555070 fbshipit-source-id: 5e4966fc0f98351924c00a459cc2d8893df4b368 --- folly/fibers/Semaphore.cpp | 4 ++++ folly/fibers/Semaphore.h | 4 ++++ folly/fibers/test/FibersTest.cpp | 4 ++++ folly/futures/Future-inl.h | 7 +------ folly/futures/Future.cpp | 2 ++ folly/futures/Future.h | 1 + folly/futures/Portability.h | 25 +++++++++++++++++++++++++ folly/futures/helpers.h | 5 +++++ folly/futures/test/FutureTest.cpp | 2 ++ 9 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 folly/futures/Portability.h diff --git a/folly/fibers/Semaphore.cpp b/folly/fibers/Semaphore.cpp index 40baa678e7c..63f66b7ba74 100644 --- a/folly/fibers/Semaphore.cpp +++ b/folly/fibers/Semaphore.cpp @@ -118,6 +118,8 @@ coro::Task Semaphore::co_wait() { #endif +#if FOLLY_FUTURE_USING_FIBER + SemiFuture Semaphore::future_wait() { auto oldVal = tokens_.load(std::memory_order_acquire); do { @@ -138,6 +140,8 @@ SemiFuture Semaphore::future_wait() { return makeSemiFuture(); } +#endif + size_t Semaphore::getCapacity() const { return capacity_; } diff --git a/folly/fibers/Semaphore.h b/folly/fibers/Semaphore.h index 6aada8b3779..670da2c251c 100644 --- a/folly/fibers/Semaphore.h +++ b/folly/fibers/Semaphore.h @@ -58,11 +58,15 @@ class Semaphore { #endif +#if FOLLY_FUTURE_USING_FIBER + /* * Wait for capacity in the semaphore. */ SemiFuture future_wait(); +#endif + size_t getCapacity() const; private: diff --git a/folly/fibers/test/FibersTest.cpp b/folly/fibers/test/FibersTest.cpp index 82382e16e79..a59dddee378 100644 --- a/folly/fibers/test/FibersTest.cpp +++ b/folly/fibers/test/FibersTest.cpp @@ -1597,7 +1597,11 @@ TEST(FiberManager, semaphore) { if (j % 2) { sem.wait(); } else { +#if FOLLY_FUTURE_USING_FIBER sem.future_wait().get(); +#else + sem.wait(); +#endif } ++counter; sem.signal(); diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 36e63f528f9..daed18f7a6e 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -28,14 +28,9 @@ #include #include -#ifndef FOLLY_FUTURE_USING_FIBER -#if FOLLY_MOBILE || defined(__APPLE__) -#define FOLLY_FUTURE_USING_FIBER 0 -#else -#define FOLLY_FUTURE_USING_FIBER 1 +#if FOLLY_FUTURE_USING_FIBER #include #endif -#endif namespace folly { diff --git a/folly/futures/Future.cpp b/folly/futures/Future.cpp index 96f93d5c89f..475c2dba40a 100644 --- a/folly/futures/Future.cpp +++ b/folly/futures/Future.cpp @@ -58,6 +58,7 @@ Future sleepUnsafe(Duration dur, Timekeeper* tk) { } #if FOLLY_FUTURE_USING_FIBER + namespace { class FutureWaiter : public fibers::Baton::Waiter { public: @@ -83,6 +84,7 @@ SemiFuture wait(std::unique_ptr baton) { new FutureWaiter(std::move(promise), std::move(baton)); return sf; } + #endif } // namespace futures diff --git a/folly/futures/Future.h b/folly/futures/Future.h index 4d4715026b8..d4c9eca68bc 100644 --- a/folly/futures/Future.h +++ b/folly/futures/Future.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/folly/futures/Portability.h b/folly/futures/Portability.h new file mode 100644 index 00000000000..dfaee624c86 --- /dev/null +++ b/folly/futures/Portability.h @@ -0,0 +1,25 @@ +/* + * Copyright 2019-present Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#if FOLLY_MOBILE || defined(__APPLE__) +#define FOLLY_FUTURE_USING_FIBER 0 +#else +#define FOLLY_FUTURE_USING_FIBER 1 +#endif diff --git a/folly/futures/helpers.h b/folly/futures/helpers.h index 5921424baa7..1adc29d3680 100644 --- a/folly/futures/helpers.h +++ b/folly/futures/helpers.h @@ -23,6 +23,7 @@ #include #include #include +#include #include namespace folly { @@ -145,8 +146,12 @@ auto mapTry(Executor& exec, Collection&& c, F&& func) return mapTry(exec, c.begin(), c.end(), std::forward(func)); } +#if FOLLY_FUTURE_USING_FIBER + SemiFuture wait(std::unique_ptr baton); +#endif + } // namespace futures /** diff --git a/folly/futures/test/FutureTest.cpp b/folly/futures/test/FutureTest.cpp index 3bbec4e3f2a..e2b1dfd408a 100644 --- a/folly/futures/test/FutureTest.cpp +++ b/folly/futures/test/FutureTest.cpp @@ -1627,6 +1627,7 @@ TEST(Future, ThenRecursion) { } #if FOLLY_FUTURE_USING_FIBER + TEST(Future, BatonWait) { auto baton = std::make_unique(); bool posted{false}; @@ -1644,4 +1645,5 @@ TEST(Future, BatonWait) { .getVia(&executor); EXPECT_TRUE(postFuture.isReady()); } + #endif