From: Yedidya Feldblum Date: Tue, 12 Dec 2017 06:10:59 +0000 (-0800) Subject: Switch to the try_wait_for and try_wait_until Baton APIs X-Git-Tag: v2017.12.18.00~26 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=a674aa6cee470969ddecd340154002cf1d3efc1c;p=folly.git 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 --- diff --git a/folly/Singleton-inl.h b/folly/Singleton-inl.h index 745147f0..762a9ec5 100644 --- a/folly/Singleton-inl.h +++ b/folly/Singleton-inl.h @@ -155,8 +155,8 @@ void SingletonHolder::destroyInstance() { instance_copy_.reset(); if (destroy_baton_) { constexpr std::chrono::seconds kDestroyWaitTime{5}; - auto last_reference_released = destroy_baton_->timed_wait( - std::chrono::steady_clock::now() + kDestroyWaitTime); + auto last_reference_released = + destroy_baton_->try_wait_for(kDestroyWaitTime); if (last_reference_released) { teardown_(instance_ptr_); } else { diff --git a/folly/Singleton.h b/folly/Singleton.h index 48251b66..921c443a 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -408,7 +408,7 @@ class SingletonVault { * folly::IOThreadPoolExecutor executor(max_concurrency_level); * folly::Baton<> 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); diff --git a/folly/experimental/observer/test/ObserverTest.cpp b/folly/experimental/observer/test/ObserverTest.cpp index 1c3bb28f..943f809f 100644 --- a/folly/experimental/observer/test/ObserverTest.cpp +++ b/folly/experimental/observer/test/ObserverTest.cpp @@ -38,7 +38,7 @@ TEST(Observer, Observable) { 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); } @@ -62,7 +62,7 @@ TEST(Observer, MakeObserver) { 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); } @@ -93,7 +93,7 @@ TEST(Observer, MakeObserverDiamond) { 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); } @@ -136,14 +136,14 @@ TEST(Observer, NullValue) { observable.setValue(2); // 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(); EXPECT_EQ(82, **oddObserver); 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); } @@ -199,7 +199,7 @@ TEST(Observer, Cycle) { for (size_t i = 1; i <= 3; ++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(); EXPECT_EQ(i, **collectObserver); diff --git a/folly/experimental/test/FunctionSchedulerTest.cpp b/folly/experimental/test/FunctionSchedulerTest.cpp index b869f2a8..f8dfc0bc 100644 --- a/folly/experimental/test/FunctionSchedulerTest.cpp +++ b/folly/experimental/test/FunctionSchedulerTest.cpp @@ -629,7 +629,7 @@ TEST(FunctionScheduler, CancelAndWaitOnRunningFunc) { baton.post(); }); - ASSERT_TRUE(baton.timed_wait(testInterval(15))); + ASSERT_TRUE(baton.try_wait_for(testInterval(15))); th.join(); } @@ -644,7 +644,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithRunningFunc) { baton.post(); }); - ASSERT_TRUE(baton.timed_wait(testInterval(15))); + ASSERT_TRUE(baton.try_wait_for(testInterval(15))); th.join(); } @@ -675,7 +675,7 @@ TEST(FunctionScheduler, CancelAllAndWaitWithOneRunningAndOneWaiting) { baton.post(); }); - ASSERT_TRUE(baton.timed_wait(testInterval(15))); + ASSERT_TRUE(baton.try_wait_for(testInterval(15))); th.join(); } diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index be53ff7c..70b0f892 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -1379,7 +1379,7 @@ void waitImpl(FutureType& f, Duration dur) { }); doBoost(f); f = std::move(ret); - if (baton->timed_wait(dur)) { + if (baton->try_wait_for(dur)) { assert(f.isReady()); } } diff --git a/folly/futures/test/InterruptTest.cpp b/folly/futures/test/InterruptTest.cpp index fc87606b..fe4b645d 100644 --- a/folly/futures/test/InterruptTest.cpp +++ b/folly/futures/test/InterruptTest.cpp @@ -79,6 +79,5 @@ TEST(Interrupt, withinTimedOut) { p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); }); p.getFuture().within(std::chrono::milliseconds(1)); // 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.timed_wait(t)); + EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100))); } diff --git a/folly/io/async/test/EventBaseThreadTest.cpp b/folly/io/async/test/EventBaseThreadTest.cpp index 88ed75b2..ff5c52b4 100644 --- a/folly/io/async/test/EventBaseThreadTest.cpp +++ b/folly/io/async/test/EventBaseThreadTest.cpp @@ -37,7 +37,7 @@ TEST_F(EventBaseThreadTest, example) { EXPECT_EQ(getCurrentThreadName().value(), "monkey"); done.post(); }); - ASSERT_TRUE(done.timed_wait(seconds(1))); + ASSERT_TRUE(done.try_wait_for(seconds(1))); } TEST_F(EventBaseThreadTest, start_stop) { @@ -50,7 +50,7 @@ TEST_F(EventBaseThreadTest, start_stop) { Baton<> done; 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()); ebt.stop(); @@ -69,7 +69,7 @@ TEST_F(EventBaseThreadTest, move) { Baton<> done; 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) { @@ -80,7 +80,7 @@ TEST_F(EventBaseThreadTest, self_move) { Baton<> done; 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) { diff --git a/folly/io/async/test/ScopedEventBaseThreadTest.cpp b/folly/io/async/test/ScopedEventBaseThreadTest.cpp index 321a0888..dfd10ae9 100644 --- a/folly/io/async/test/ScopedEventBaseThreadTest.cpp +++ b/folly/io/async/test/ScopedEventBaseThreadTest.cpp @@ -36,7 +36,7 @@ TEST_F(ScopedEventBaseThreadTest, example) { Baton<> done; 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) { @@ -51,7 +51,7 @@ TEST_F(ScopedEventBaseThreadTest, named_example) { done.post(); }); - ASSERT_TRUE(done.timed_wait(seconds(1))); + ASSERT_TRUE(done.try_wait_for(seconds(1))); if (createdThreadName) { ASSERT_EQ(kThreadName.toString(), createdThreadName.value()); } diff --git a/folly/synchronization/test/BatonTest.cpp b/folly/synchronization/test/BatonTest.cpp index 955f29bc..6bca0b15 100644 --- a/folly/synchronization/test/BatonTest.cpp +++ b/folly/synchronization/test/BatonTest.cpp @@ -54,7 +54,7 @@ TEST(Baton, pingpong_nonblocking) { run_pingpong_test(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 diff --git a/folly/synchronization/test/BatonTestHelpers.h b/folly/synchronization/test/BatonTestHelpers.h index 1f07a2af..f1f46703 100644 --- a/folly/synchronization/test/BatonTestHelpers.h +++ b/folly/synchronization/test/BatonTestHelpers.h @@ -58,7 +58,7 @@ void run_basic_timed_wait_tests() { Baton b; b.post(); // tests if early delivery works fine - EXPECT_TRUE(b.timed_wait(Clock::now())); + EXPECT_TRUE(b.try_wait_until(Clock::now())); } template