Switch the Baton template params
authorYedidya Feldblum <yfeldblum@fb.com>
Tue, 19 Dec 2017 19:24:17 +0000 (11:24 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 19 Dec 2017 19:35:30 +0000 (11:35 -0800)
Summary: [Folly] Switch the `Baton` template params for consistency with `SaturatingSemaphore`.

Reviewed By: davidtgoldblatt

Differential Revision: D6591060

fbshipit-source-id: 44d6243d3185d95364a27e497216cc02fb3bc2e8

folly/synchronization/Baton.h
folly/synchronization/LifoSem.h
folly/synchronization/test/BatonBenchmark.cpp
folly/synchronization/test/BatonTest.cpp
folly/synchronization/test/BatonTestHelpers.h

index 69135d00e163ca3d1bb18b909d851f9d283e17cb..5982c046ed3fdedba1f3a6e5ed7401d010e82621 100644 (file)
@@ -40,7 +40,7 @@ namespace folly {
 /// This is basically a stripped-down semaphore that supports only a
 /// single call to sem_post and a single call to sem_wait.
 ///
-/// The non-blocking version (Blocking == false) provides more speed
+/// The non-blocking version (MayBlock == false) provides more speed
 /// by using only load acquire and store release operations in the
 /// critical path, at the cost of disallowing blocking and timing out.
 ///
@@ -50,9 +50,7 @@ namespace folly {
 /// DeterministicSchedule.  By having a much more restrictive
 /// lifecycle we can also add a bunch of assertions that can help to
 /// catch race conditions ahead of time.
-template <
-    template <typename> class Atom = std::atomic,
-    bool Blocking = true> // blocking vs spinning
+template <bool MayBlock = true, template <typename> class Atom = std::atomic>
 struct Baton {
   constexpr Baton() noexcept : state_(INIT) {}
 
@@ -116,8 +114,8 @@ struct Baton {
   /// destruction or reset()) there can be at most one call to post(),
   /// in the single poster version.  Any thread may call post().
   void post() noexcept {
-    if (!Blocking) {
-      /// Non-blocking version
+    if (!MayBlock) {
+      /// Spin-only version
       ///
       assert([&] {
         auto state = state_.load(std::memory_order_relaxed);
@@ -127,7 +125,7 @@ struct Baton {
       return;
     }
 
-    /// Blocking versions
+    /// May-block versions
     ///
     uint32_t before = state_.load(std::memory_order_acquire);
 
@@ -200,7 +198,7 @@ struct Baton {
   FOLLY_ALWAYS_INLINE bool try_wait_for(
       const std::chrono::duration<Rep, Period>& timeout) noexcept {
     static_assert(
-        Blocking, "Non-blocking Baton does not support try_wait_for.");
+        MayBlock, "Non-blocking Baton does not support try_wait_for.");
 
     if (try_wait()) {
       return true;
@@ -225,7 +223,7 @@ struct Baton {
   FOLLY_ALWAYS_INLINE bool try_wait_until(
       const std::chrono::time_point<Clock, Duration>& deadline) noexcept {
     static_assert(
-        Blocking, "Non-blocking Baton does not support try_wait_until.");
+        MayBlock, "Non-blocking Baton does not support try_wait_until.");
 
     if (try_wait()) {
       return true;
@@ -305,7 +303,7 @@ struct Baton {
       return;
     }
 
-    if (!Blocking) {
+    if (!MayBlock) {
       while (!try_wait()) {
         std::this_thread::yield();
       }
index ef27df4ee1d43490a377c5ea12735a2190aa5947..b06ec4ca2501b9fdf6093302a7c175eda79cf2d9 100644 (file)
@@ -33,7 +33,7 @@ namespace folly {
 
 template <
     template <typename> class Atom = std::atomic,
-    class BatonType = Baton<Atom>>
+    class BatonType = Baton<true, Atom>>
 struct LifoSemImpl;
 
 /// LifoSem is a semaphore that wakes its waiters in a manner intended to
index 1c3c884646b14103ec46ae0365b6f6bf1cacad43..8e8ee03d5331f9b77e90fcc783a67118e9c9aa8a 100644 (file)
@@ -30,21 +30,21 @@ using folly::detail::EmulatedFutexAtomic;
 typedef DeterministicSchedule DSched;
 
 BENCHMARK(baton_pingpong_blocking, iters) {
-  run_pingpong_test<std::atomic, true>(iters);
+  run_pingpong_test<true, std::atomic>(iters);
 }
 
 BENCHMARK(baton_pingpong_nonblocking, iters) {
-  run_pingpong_test<std::atomic, false>(iters);
+  run_pingpong_test<false, std::atomic>(iters);
 }
 
 BENCHMARK_DRAW_LINE()
 
 BENCHMARK(baton_pingpong_emulated_futex_blocking, iters) {
-  run_pingpong_test<EmulatedFutexAtomic, true>(iters);
+  run_pingpong_test<true, EmulatedFutexAtomic>(iters);
 }
 
 BENCHMARK(baton_pingpong_emulated_futex_nonblocking, iters) {
-  run_pingpong_test<EmulatedFutexAtomic, false>(iters);
+  run_pingpong_test<false, EmulatedFutexAtomic>(iters);
 }
 
 BENCHMARK_DRAW_LINE()
index 6bca0b15cd0928fb1e033c423260a8e73bc5eb71..4ea0bb08dd69c89e7e9661f0ad81e94ec1a1c1ae 100644 (file)
@@ -29,15 +29,15 @@ using folly::detail::EmulatedFutexAtomic;
 /// Basic test
 
 TEST(Baton, basic_blocking) {
-  run_basic_test<std::atomic, true>();
-  run_basic_test<EmulatedFutexAtomic, true>();
-  run_basic_test<DeterministicAtomic, true>();
+  run_basic_test<true, std::atomic>();
+  run_basic_test<true, EmulatedFutexAtomic>();
+  run_basic_test<true, DeterministicAtomic>();
 }
 
 TEST(Baton, basic_nonblocking) {
-  run_basic_test<std::atomic, false>();
-  run_basic_test<EmulatedFutexAtomic, false>();
-  run_basic_test<DeterministicAtomic, false>();
+  run_basic_test<false, std::atomic>();
+  run_basic_test<false, EmulatedFutexAtomic>();
+  run_basic_test<false, DeterministicAtomic>();
 }
 
 /// Ping pong tests
@@ -45,13 +45,13 @@ TEST(Baton, basic_nonblocking) {
 TEST(Baton, pingpong_blocking) {
   DSched sched(DSched::uniform(0));
 
-  run_pingpong_test<DeterministicAtomic, true>(1000);
+  run_pingpong_test<true, DeterministicAtomic>(1000);
 }
 
 TEST(Baton, pingpong_nonblocking) {
   DSched sched(DSched::uniform(0));
 
-  run_pingpong_test<DeterministicAtomic, false>(1000);
+  run_pingpong_test<false, DeterministicAtomic>(1000);
 }
 
 /// Timed wait tests - Nonblocking Baton does not support try_wait_until()
@@ -107,13 +107,13 @@ TEST(Baton, timed_wait_steady_clock) {
 /// Try wait tests
 
 TEST(Baton, try_wait_blocking) {
-  run_try_wait_tests<std::atomic, true>();
-  run_try_wait_tests<EmulatedFutexAtomic, true>();
-  run_try_wait_tests<DeterministicAtomic, true>();
+  run_try_wait_tests<true, std::atomic>();
+  run_try_wait_tests<true, EmulatedFutexAtomic>();
+  run_try_wait_tests<true, DeterministicAtomic>();
 }
 
 TEST(Baton, try_wait_nonblocking) {
-  run_try_wait_tests<std::atomic, false>();
-  run_try_wait_tests<EmulatedFutexAtomic, false>();
-  run_try_wait_tests<DeterministicAtomic, false>();
+  run_try_wait_tests<false, std::atomic>();
+  run_try_wait_tests<false, EmulatedFutexAtomic>();
+  run_try_wait_tests<false, DeterministicAtomic>();
 }
index a0bba6e051166e7a7ab07e64b2c0ada8c193c4d4..016de875e9ad7b7ef1e5a4203d407b6f1a243eca 100644 (file)
@@ -25,16 +25,16 @@ namespace test {
 
 typedef DeterministicSchedule DSched;
 
-template <template <typename> class Atom, bool Blocking>
+template <bool MayBlock, template <typename> class Atom>
 void run_basic_test() {
-  Baton<Atom, Blocking> b;
+  Baton<MayBlock, Atom> b;
   b.post();
   b.wait();
 }
 
-template <template <typename> class Atom, bool Blocking>
+template <bool MayBlock, template <typename> class Atom>
 void run_pingpong_test(int numRounds) {
-  using B = Baton<Atom, Blocking>;
+  using B = Baton<MayBlock, Atom>;
   B batons[17];
   B& a = batons[0];
   B& b = batons[16]; // to get it on a different cache line
@@ -55,7 +55,7 @@ void run_pingpong_test(int numRounds) {
 
 template <template <typename> class Atom, typename Clock>
 void run_basic_timed_wait_tests() {
-  Baton<Atom> b;
+  Baton<true, Atom> b;
   b.post();
   // tests if early delivery works fine
   EXPECT_TRUE(b.try_wait_until(Clock::now()));
@@ -63,7 +63,7 @@ void run_basic_timed_wait_tests() {
 
 template <template <typename> class Atom, typename Clock>
 void run_timed_wait_tmo_tests() {
-  Baton<Atom> b;
+  Baton<true, Atom> b;
 
   auto thr = DSched::thread([&] {
     bool rv = b.try_wait_until(Clock::now() + std::chrono::milliseconds(1));
@@ -75,7 +75,7 @@ void run_timed_wait_tmo_tests() {
 
 template <template <typename> class Atom, typename Clock>
 void run_timed_wait_regular_test() {
-  Baton<Atom> b;
+  Baton<true, Atom> b;
 
   auto thr = DSched::thread([&] {
     // To wait forever we'd like to use time_point<Clock>::max, but
@@ -104,9 +104,9 @@ void run_timed_wait_regular_test() {
   DSched::join(thr);
 }
 
-template <template <typename> class Atom, bool Blocking>
+template <bool MayBlock, template <typename> class Atom>
 void run_try_wait_tests() {
-  Baton<Atom, Blocking> b;
+  Baton<MayBlock, Atom> b;
   EXPECT_FALSE(b.ready());
   EXPECT_FALSE(b.try_wait());
   b.post();