Commit a674aa6c authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Switch to the try_wait_for and try_wait_until Baton APIs

Summary: Switch to the `try_wait_for` and `try_wait_until` `Baton` APIs.

Reviewed By: davidtgoldblatt

Differential Revision: D6532103

fbshipit-source-id: aa3ce64152d167bb9c9cb1f266be0f9f8bd498f5
parent eb7bc45f
...@@ -155,8 +155,8 @@ void SingletonHolder<T>::destroyInstance() { ...@@ -155,8 +155,8 @@ void SingletonHolder<T>::destroyInstance() {
instance_copy_.reset(); instance_copy_.reset();
if (destroy_baton_) { if (destroy_baton_) {
constexpr std::chrono::seconds kDestroyWaitTime{5}; constexpr std::chrono::seconds kDestroyWaitTime{5};
auto last_reference_released = destroy_baton_->timed_wait( auto last_reference_released =
std::chrono::steady_clock::now() + kDestroyWaitTime); destroy_baton_->try_wait_for(kDestroyWaitTime);
if (last_reference_released) { if (last_reference_released) {
teardown_(instance_ptr_); teardown_(instance_ptr_);
} else { } else {
......
...@@ -408,7 +408,7 @@ class SingletonVault { ...@@ -408,7 +408,7 @@ class SingletonVault {
* folly::IOThreadPoolExecutor executor(max_concurrency_level); * folly::IOThreadPoolExecutor executor(max_concurrency_level);
* folly::Baton<> done; * folly::Baton<> done;
* doEagerInitVia(executor, &done); * doEagerInitVia(executor, &done);
* done.wait(); // or 'timed_wait', or spin with 'try_wait' * done.wait(); // or 'try_wait_for', etc.
* *
*/ */
void doEagerInitVia(Executor& exe, folly::Baton<>* done = nullptr); void doEagerInitVia(Executor& exe, folly::Baton<>* done = nullptr);
......
...@@ -38,7 +38,7 @@ TEST(Observer, Observable) { ...@@ -38,7 +38,7 @@ TEST(Observer, Observable) {
observable.setValue(24); observable.setValue(24);
EXPECT_TRUE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
EXPECT_EQ(24, **observer); EXPECT_EQ(24, **observer);
} }
...@@ -62,7 +62,7 @@ TEST(Observer, MakeObserver) { ...@@ -62,7 +62,7 @@ TEST(Observer, MakeObserver) {
observable.setValue(24); observable.setValue(24);
EXPECT_TRUE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
EXPECT_EQ(25, **observer); EXPECT_EQ(25, **observer);
} }
...@@ -93,7 +93,7 @@ TEST(Observer, MakeObserverDiamond) { ...@@ -93,7 +93,7 @@ TEST(Observer, MakeObserverDiamond) {
observable.setValue(24); observable.setValue(24);
EXPECT_TRUE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
EXPECT_EQ(25 * 26, **observer); EXPECT_EQ(25 * 26, **observer);
} }
...@@ -136,14 +136,14 @@ TEST(Observer, NullValue) { ...@@ -136,14 +136,14 @@ TEST(Observer, NullValue) {
observable.setValue(2); observable.setValue(2);
// Waiting observer shouldn't be updated // Waiting observer shouldn't be updated
EXPECT_FALSE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_FALSE(baton.try_wait_for(std::chrono::seconds{1}));
baton.reset(); baton.reset();
EXPECT_EQ(82, **oddObserver); EXPECT_EQ(82, **oddObserver);
observable.setValue(23); observable.setValue(23);
EXPECT_TRUE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
EXPECT_EQ(46, **oddObserver); EXPECT_EQ(46, **oddObserver);
} }
...@@ -199,7 +199,7 @@ TEST(Observer, Cycle) { ...@@ -199,7 +199,7 @@ TEST(Observer, Cycle) {
for (size_t i = 1; i <= 3; ++i) { for (size_t i = 1; i <= 3; ++i) {
observable.setValue(i); observable.setValue(i);
EXPECT_TRUE(baton.timed_wait(std::chrono::seconds{1})); EXPECT_TRUE(baton.try_wait_for(std::chrono::seconds{1}));
baton.reset(); baton.reset();
EXPECT_EQ(i, **collectObserver); EXPECT_EQ(i, **collectObserver);
......
...@@ -629,7 +629,7 @@ TEST(FunctionScheduler, CancelAndWaitOnRunningFunc) { ...@@ -629,7 +629,7 @@ TEST(FunctionScheduler, CancelAndWaitOnRunningFunc) {
baton.post(); baton.post();
}); });
ASSERT_TRUE(baton.timed_wait(testInterval(15))); ASSERT_TRUE(baton.try_wait_for(testInterval(15)));
th.join(); th.join();
} }
...@@ -644,7 +644,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithRunningFunc) { ...@@ -644,7 +644,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithRunningFunc) {
baton.post(); baton.post();
}); });
ASSERT_TRUE(baton.timed_wait(testInterval(15))); ASSERT_TRUE(baton.try_wait_for(testInterval(15)));
th.join(); th.join();
} }
...@@ -675,7 +675,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithOneRunningAndOneWaiting) { ...@@ -675,7 +675,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithOneRunningAndOneWaiting) {
baton.post(); baton.post();
}); });
ASSERT_TRUE(baton.timed_wait(testInterval(15))); ASSERT_TRUE(baton.try_wait_for(testInterval(15)));
th.join(); th.join();
} }
......
...@@ -1379,7 +1379,7 @@ void waitImpl(FutureType& f, Duration dur) { ...@@ -1379,7 +1379,7 @@ void waitImpl(FutureType& f, Duration dur) {
}); });
doBoost(f); doBoost(f);
f = std::move(ret); f = std::move(ret);
if (baton->timed_wait(dur)) { if (baton->try_wait_for(dur)) {
assert(f.isReady()); assert(f.isReady());
} }
} }
......
...@@ -79,6 +79,5 @@ TEST(Interrupt, withinTimedOut) { ...@@ -79,6 +79,5 @@ TEST(Interrupt, withinTimedOut) {
p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); }); p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); });
p.getFuture().within(std::chrono::milliseconds(1)); p.getFuture().within(std::chrono::milliseconds(1));
// Give it 100ms to time out and call the interrupt handler // Give it 100ms to time out and call the interrupt handler
auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(100); EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100)));
EXPECT_TRUE(done.timed_wait(t));
} }
...@@ -37,7 +37,7 @@ TEST_F(EventBaseThreadTest, example) { ...@@ -37,7 +37,7 @@ TEST_F(EventBaseThreadTest, example) {
EXPECT_EQ(getCurrentThreadName().value(), "monkey"); EXPECT_EQ(getCurrentThreadName().value(), "monkey");
done.post(); done.post();
}); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
} }
TEST_F(EventBaseThreadTest, start_stop) { TEST_F(EventBaseThreadTest, start_stop) {
...@@ -50,7 +50,7 @@ TEST_F(EventBaseThreadTest, start_stop) { ...@@ -50,7 +50,7 @@ TEST_F(EventBaseThreadTest, start_stop) {
Baton<> done; Baton<> done;
ebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
EXPECT_NE(nullptr, ebt.getEventBase()); EXPECT_NE(nullptr, ebt.getEventBase());
ebt.stop(); ebt.stop();
...@@ -69,7 +69,7 @@ TEST_F(EventBaseThreadTest, move) { ...@@ -69,7 +69,7 @@ TEST_F(EventBaseThreadTest, move) {
Baton<> done; Baton<> done;
ebt2.getEventBase()->runInEventBaseThread([&] { done.post(); }); ebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
} }
TEST_F(EventBaseThreadTest, self_move) { TEST_F(EventBaseThreadTest, self_move) {
...@@ -80,7 +80,7 @@ TEST_F(EventBaseThreadTest, self_move) { ...@@ -80,7 +80,7 @@ TEST_F(EventBaseThreadTest, self_move) {
Baton<> done; Baton<> done;
ebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
} }
TEST_F(EventBaseThreadTest, default_manager) { TEST_F(EventBaseThreadTest, default_manager) {
......
...@@ -36,7 +36,7 @@ TEST_F(ScopedEventBaseThreadTest, example) { ...@@ -36,7 +36,7 @@ TEST_F(ScopedEventBaseThreadTest, example) {
Baton<> done; Baton<> done;
sebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
} }
TEST_F(ScopedEventBaseThreadTest, named_example) { TEST_F(ScopedEventBaseThreadTest, named_example) {
...@@ -51,7 +51,7 @@ TEST_F(ScopedEventBaseThreadTest, named_example) { ...@@ -51,7 +51,7 @@ TEST_F(ScopedEventBaseThreadTest, named_example) {
done.post(); done.post();
}); });
ASSERT_TRUE(done.timed_wait(seconds(1))); ASSERT_TRUE(done.try_wait_for(seconds(1)));
if (createdThreadName) { if (createdThreadName) {
ASSERT_EQ(kThreadName.toString(), createdThreadName.value()); ASSERT_EQ(kThreadName.toString(), createdThreadName.value());
} }
......
...@@ -54,7 +54,7 @@ TEST(Baton, pingpong_nonblocking) { ...@@ -54,7 +54,7 @@ TEST(Baton, pingpong_nonblocking) {
run_pingpong_test<DeterministicAtomic, false>(1000); run_pingpong_test<DeterministicAtomic, false>(1000);
} }
/// Timed wait tests - Nonblocking Baton does not support timed_wait() /// Timed wait tests - Nonblocking Baton does not support try_wait_until()
// Timed wait basic system clock tests // Timed wait basic system clock tests
......
...@@ -58,7 +58,7 @@ void run_basic_timed_wait_tests() { ...@@ -58,7 +58,7 @@ void run_basic_timed_wait_tests() {
Baton<Atom> b; Baton<Atom> b;
b.post(); b.post();
// tests if early delivery works fine // tests if early delivery works fine
EXPECT_TRUE(b.timed_wait(Clock::now())); EXPECT_TRUE(b.try_wait_until(Clock::now()));
} }
template <template <typename> class Atom, typename Clock> template <template <typename> class Atom, typename Clock>
...@@ -66,7 +66,7 @@ void run_timed_wait_tmo_tests() { ...@@ -66,7 +66,7 @@ void run_timed_wait_tmo_tests() {
Baton<Atom> b; Baton<Atom> b;
auto thr = DSched::thread([&] { auto thr = DSched::thread([&] {
bool rv = b.timed_wait(Clock::now() + std::chrono::milliseconds(1)); bool rv = b.try_wait_until(Clock::now() + std::chrono::milliseconds(1));
// main thread is guaranteed to not post until timeout occurs // main thread is guaranteed to not post until timeout occurs
EXPECT_FALSE(rv); EXPECT_FALSE(rv);
}); });
...@@ -82,7 +82,7 @@ void run_timed_wait_regular_test() { ...@@ -82,7 +82,7 @@ void run_timed_wait_regular_test() {
// std::condition_variable does math to convert the timeout to // std::condition_variable does math to convert the timeout to
// system_clock without handling overflow. // system_clock without handling overflow.
auto farFuture = Clock::now() + std::chrono::hours(1000); auto farFuture = Clock::now() + std::chrono::hours(1000);
bool rv = b.timed_wait(farFuture); bool rv = b.try_wait_until(farFuture);
if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) { if (!std::is_same<Atom<int>, DeterministicAtomic<int>>::value) {
// DeterministicAtomic ignores actual times, so doesn't guarantee // DeterministicAtomic ignores actual times, so doesn't guarantee
// a lack of timeout // a lack of timeout
......
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