From 58399f25e774f06b3048eb7cde4302b917848d19 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 19 Dec 2017 11:24:17 -0800 Subject: [PATCH] Switch the Baton template params 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 | 18 ++++++------ folly/synchronization/LifoSem.h | 2 +- folly/synchronization/test/BatonBenchmark.cpp | 8 +++--- folly/synchronization/test/BatonTest.cpp | 28 +++++++++---------- folly/synchronization/test/BatonTestHelpers.h | 18 ++++++------ 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/folly/synchronization/Baton.h b/folly/synchronization/Baton.h index 69135d00..5982c046 100644 --- a/folly/synchronization/Baton.h +++ b/folly/synchronization/Baton.h @@ -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 class Atom = std::atomic, - bool Blocking = true> // blocking vs spinning +template 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& 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& 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(); } diff --git a/folly/synchronization/LifoSem.h b/folly/synchronization/LifoSem.h index ef27df4e..b06ec4ca 100644 --- a/folly/synchronization/LifoSem.h +++ b/folly/synchronization/LifoSem.h @@ -33,7 +33,7 @@ namespace folly { template < template class Atom = std::atomic, - class BatonType = Baton> + class BatonType = Baton> struct LifoSemImpl; /// LifoSem is a semaphore that wakes its waiters in a manner intended to diff --git a/folly/synchronization/test/BatonBenchmark.cpp b/folly/synchronization/test/BatonBenchmark.cpp index 1c3c8846..8e8ee03d 100644 --- a/folly/synchronization/test/BatonBenchmark.cpp +++ b/folly/synchronization/test/BatonBenchmark.cpp @@ -30,21 +30,21 @@ using folly::detail::EmulatedFutexAtomic; typedef DeterministicSchedule DSched; BENCHMARK(baton_pingpong_blocking, iters) { - run_pingpong_test(iters); + run_pingpong_test(iters); } BENCHMARK(baton_pingpong_nonblocking, iters) { - run_pingpong_test(iters); + run_pingpong_test(iters); } BENCHMARK_DRAW_LINE() BENCHMARK(baton_pingpong_emulated_futex_blocking, iters) { - run_pingpong_test(iters); + run_pingpong_test(iters); } BENCHMARK(baton_pingpong_emulated_futex_nonblocking, iters) { - run_pingpong_test(iters); + run_pingpong_test(iters); } BENCHMARK_DRAW_LINE() diff --git a/folly/synchronization/test/BatonTest.cpp b/folly/synchronization/test/BatonTest.cpp index 6bca0b15..4ea0bb08 100644 --- a/folly/synchronization/test/BatonTest.cpp +++ b/folly/synchronization/test/BatonTest.cpp @@ -29,15 +29,15 @@ using folly::detail::EmulatedFutexAtomic; /// Basic test TEST(Baton, basic_blocking) { - run_basic_test(); - run_basic_test(); - run_basic_test(); + run_basic_test(); + run_basic_test(); + run_basic_test(); } TEST(Baton, basic_nonblocking) { - run_basic_test(); - run_basic_test(); - run_basic_test(); + run_basic_test(); + run_basic_test(); + run_basic_test(); } /// Ping pong tests @@ -45,13 +45,13 @@ TEST(Baton, basic_nonblocking) { TEST(Baton, pingpong_blocking) { DSched sched(DSched::uniform(0)); - run_pingpong_test(1000); + run_pingpong_test(1000); } TEST(Baton, pingpong_nonblocking) { DSched sched(DSched::uniform(0)); - run_pingpong_test(1000); + run_pingpong_test(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(); - run_try_wait_tests(); - run_try_wait_tests(); + run_try_wait_tests(); + run_try_wait_tests(); + run_try_wait_tests(); } TEST(Baton, try_wait_nonblocking) { - run_try_wait_tests(); - run_try_wait_tests(); - run_try_wait_tests(); + run_try_wait_tests(); + run_try_wait_tests(); + run_try_wait_tests(); } diff --git a/folly/synchronization/test/BatonTestHelpers.h b/folly/synchronization/test/BatonTestHelpers.h index a0bba6e0..016de875 100644 --- a/folly/synchronization/test/BatonTestHelpers.h +++ b/folly/synchronization/test/BatonTestHelpers.h @@ -25,16 +25,16 @@ namespace test { typedef DeterministicSchedule DSched; -template