Skip to content

Commit

Permalink
Fix up fiber-related guards in futures code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Mar 21, 2019
1 parent 08f846d commit f8ae709
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions folly/fibers/Semaphore.cpp
Expand Up @@ -118,6 +118,8 @@ coro::Task<void> Semaphore::co_wait() {

#endif

#if FOLLY_FUTURE_USING_FIBER

SemiFuture<Unit> Semaphore::future_wait() {
auto oldVal = tokens_.load(std::memory_order_acquire);
do {
Expand All @@ -138,6 +140,8 @@ SemiFuture<Unit> Semaphore::future_wait() {
return makeSemiFuture();
}

#endif

size_t Semaphore::getCapacity() const {
return capacity_;
}
Expand Down
4 changes: 4 additions & 0 deletions folly/fibers/Semaphore.h
Expand Up @@ -58,11 +58,15 @@ class Semaphore {

#endif

#if FOLLY_FUTURE_USING_FIBER

/*
* Wait for capacity in the semaphore.
*/
SemiFuture<Unit> future_wait();

#endif

size_t getCapacity() const;

private:
Expand Down
4 changes: 4 additions & 0 deletions folly/fibers/test/FibersTest.cpp
Expand Up @@ -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();
Expand Down
7 changes: 1 addition & 6 deletions folly/futures/Future-inl.h
Expand Up @@ -28,14 +28,9 @@
#include <folly/futures/detail/Core.h>
#include <folly/synchronization/Baton.h>

#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 <folly/fibers/Baton.h>
#endif
#endif

namespace folly {

Expand Down
2 changes: 2 additions & 0 deletions folly/futures/Future.cpp
Expand Up @@ -58,6 +58,7 @@ Future<Unit> sleepUnsafe(Duration dur, Timekeeper* tk) {
}

#if FOLLY_FUTURE_USING_FIBER

namespace {
class FutureWaiter : public fibers::Baton::Waiter {
public:
Expand All @@ -83,6 +84,7 @@ SemiFuture<Unit> wait(std::unique_ptr<fibers::Baton> baton) {
new FutureWaiter(std::move(promise), std::move(baton));
return sf;
}

#endif

} // namespace futures
Expand Down
1 change: 1 addition & 0 deletions folly/futures/Future.h
Expand Up @@ -31,6 +31,7 @@
#include <folly/executors/DrivableExecutor.h>
#include <folly/executors/TimedDrivableExecutor.h>
#include <folly/functional/Invoke.h>
#include <folly/futures/Portability.h>
#include <folly/futures/Promise.h>
#include <folly/futures/detail/Types.h>
#include <folly/lang/Exception.h>
Expand Down
25 changes: 25 additions & 0 deletions 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 <folly/Portability.h>

#if FOLLY_MOBILE || defined(__APPLE__)
#define FOLLY_FUTURE_USING_FIBER 0
#else
#define FOLLY_FUTURE_USING_FIBER 1
#endif
5 changes: 5 additions & 0 deletions folly/futures/helpers.h
Expand Up @@ -23,6 +23,7 @@
#include <folly/Try.h>
#include <folly/functional/Invoke.h>
#include <folly/futures/Future.h>
#include <folly/futures/Portability.h>
#include <folly/futures/Promise.h>

namespace folly {
Expand Down Expand Up @@ -145,8 +146,12 @@ auto mapTry(Executor& exec, Collection&& c, F&& func)
return mapTry(exec, c.begin(), c.end(), std::forward<F>(func));
}

#if FOLLY_FUTURE_USING_FIBER

SemiFuture<Unit> wait(std::unique_ptr<fibers::Baton> baton);

#endif

} // namespace futures

/**
Expand Down
2 changes: 2 additions & 0 deletions folly/futures/test/FutureTest.cpp
Expand Up @@ -1627,6 +1627,7 @@ TEST(Future, ThenRecursion) {
}

#if FOLLY_FUTURE_USING_FIBER

TEST(Future, BatonWait) {
auto baton = std::make_unique<fibers::Baton>();
bool posted{false};
Expand All @@ -1644,4 +1645,5 @@ TEST(Future, BatonWait) {
.getVia(&executor);
EXPECT_TRUE(postFuture.isReady());
}

#endif

0 comments on commit f8ae709

Please sign in to comment.