Switch to the try_wait_for and try_wait_until Baton APIs
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 12 Dec 2017 06:10:59 +0000 (22:10 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 12 Dec 2017 06:21:04 +0000 (22:21 -0800)
Summary: Switch to the `try_wait_for` and `try_wait_until` `Baton` APIs.

Reviewed By: davidtgoldblatt

Differential Revision: D6532103

fbshipit-source-id: aa3ce64152d167bb9c9cb1f266be0f9f8bd498f5

folly/Singleton-inl.h
folly/Singleton.h
folly/experimental/observer/test/ObserverTest.cpp
folly/experimental/test/FunctionSchedulerTest.cpp
folly/futures/Future-inl.h
folly/futures/test/InterruptTest.cpp
folly/io/async/test/EventBaseThreadTest.cpp
folly/io/async/test/ScopedEventBaseThreadTest.cpp
folly/synchronization/test/BatonTest.cpp
folly/synchronization/test/BatonTestHelpers.h

index 745147f0454934097e0d7332d6f6ce76c822500a..762a9ec57376089e6804e0f543f8e195f2a7f125 100644 (file)
@@ -155,8 +155,8 @@ void SingletonHolder<T>::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 {
index 48251b6674bb645a2d147dd2b0bedd3069e768d8..921c443ab3eeca0ff200894cedb82a8720aba1aa 100644 (file)
@@ -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);
index 1c3bb28f7bf3f45c43a51cea1a228157bde01fc1..943f809f5c2aad05108f4c01ccec6d9883a555e4 100644 (file)
@@ -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);
index b869f2a856eea59515768fa6086819f4061b5d2b..f8dfc0bca722c3e8f27f35dbb87438b0efac57be 100644 (file)
@@ -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();
 }
 
index be53ff7c9a0721d2ece56f899e65c2edda82147d..70b0f892de00320d9798cd1ff017ee938fc85c6d 100644 (file)
@@ -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());
   }
 }
index fc87606b9146c8c0080a345666d2044367e0df65..fe4b645d3604d72cc52ebfc678784b1714eba630 100644 (file)
@@ -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)));
 }
index 88ed75b26755a039a5e902824b921aa18c7f4fe1..ff5c52b4ae218ace3f58aa3afd6e08f96ca24c14 100644 (file)
@@ -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) {
index 321a088876133fa3c8c5e073a7af5e86b811a36b..dfd10ae92de67d13d113b05d9c6b62e63332c544 100644 (file)
@@ -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());
   }
index 955f29bc665ad551bb371a9c04782fd616e17991..6bca0b15cd0928fb1e033c423260a8e73bc5eb71 100644 (file)
@@ -54,7 +54,7 @@ TEST(Baton, pingpong_nonblocking) {
   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
 
index 1f07a2af4dacf1380b93ff461709dacd7c9a04a3..f1f4670362e35c90ca67ac6bdc2ea5d87ff77b57 100644 (file)
@@ -58,7 +58,7 @@ void run_basic_timed_wait_tests() {
   Baton<Atom> 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 <template <typename> class Atom, typename Clock>
@@ -66,7 +66,7 @@ void run_timed_wait_tmo_tests() {
   Baton<Atom> b;
 
   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
     EXPECT_FALSE(rv);
   });
@@ -82,7 +82,7 @@ void run_timed_wait_regular_test() {
     // std::condition_variable does math to convert the timeout to
     // system_clock without handling overflow.
     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) {
       // DeterministicAtomic ignores actual times, so doesn't guarantee
       // a lack of timeout