From b73507bb0f91c1593705f4ff0746c1ff93ed5762 Mon Sep 17 00:00:00 2001 From: Maged Michael Date: Thu, 27 Apr 2017 06:59:10 -0700 Subject: [PATCH] Flat combining: Add lock holder with deferred option. Minor fixes. Summary: Added a lock holder with deferred option for cases where the caller may want to call try_lock() later. Reviewed By: djwatson Differential Revision: D4949736 fbshipit-source-id: 31e0dc349dc3af9d04a33878e26cef1e48cce674 --- .../flat_combining/FlatCombining.h | 25 +++++++++++++------ .../flat_combining/test/FlatCombiningTest.cpp | 18 +++++++++++++ .../test/FlatCombiningTestHelpers.h | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/folly/experimental/flat_combining/FlatCombining.h b/folly/experimental/flat_combining/FlatCombining.h index 5f678596..3c951ecc 100644 --- a/folly/experimental/flat_combining/FlatCombining.h +++ b/folly/experimental/flat_combining/FlatCombining.h @@ -236,7 +236,7 @@ class FlatCombining { /// on the request records (if 0, then kDefaultMaxops) explicit FlatCombining( const bool dedicated = true, - uint32_t numRecs = 0, // number of combining records + const uint32_t numRecs = 0, // number of combining records const uint32_t maxOps = 0 // hint of max ops per combining session ) : numRecs_(numRecs == 0 ? kDefaultNumRecs : numRecs), @@ -277,13 +277,6 @@ class FlatCombining { m_.lock(); } - // Give the caller exclusive access through a lock holder. - // No need for explicit release. - template - void acquireExclusive(LockHolder& l) { - l = LockHolder(m_); - } - // Try to give the caller exclusive access. Returns true iff successful. bool tryExclusive() { return m_.try_lock(); @@ -294,6 +287,21 @@ class FlatCombining { m_.unlock(); } + // Give the lock holder ownership of the mutex and exclusive access. + // No need for explicit release. + template + void holdLock(LockHolder& l) { + l = LockHolder(m_); + } + + // Give the caller's lock holder ownership of the mutex but without + // exclusive access. The caller can later use the lock holder to try + // to acquire exclusive access. + template + void holdLock(LockHolder& l, std::defer_lock_t) { + l = LockHolder(m_, std::defer_lock); + } + // Execute an operation without combining template void requestNoFC(OpFunc& opFn) { @@ -556,6 +564,7 @@ class FlatCombining { if (!dedicated_) { while (isPending()) { clearPending(); + ++sessions_; combined_ += combiningSession(); } } diff --git a/folly/experimental/flat_combining/test/FlatCombiningTest.cpp b/folly/experimental/flat_combining/test/FlatCombiningTest.cpp index ff745b26..570a6ef8 100644 --- a/folly/experimental/flat_combining/test/FlatCombiningTest.cpp +++ b/folly/experimental/flat_combining/test/FlatCombiningTest.cpp @@ -19,6 +19,8 @@ #include #include +#include + using namespace folly::test; constexpr int LINES = 5; @@ -34,6 +36,22 @@ struct Params { class FlatCombiningTest : public ::testing::TestWithParam {}; +TEST(FlatCombiningTest, lock_holder) { + folly::FcSimpleExample<> ex(10); + { + std::unique_lock l; + ex.holdLock(l); + CHECK(l.owns_lock()); + } + { + std::unique_lock l; + ex.holdLock(l, std::defer_lock); + CHECK(l.try_lock()); + } + CHECK(ex.tryExclusive()); + ex.releaseExclusive(); +} + TEST_P(FlatCombiningTest, combining) { Params p = GetParam(); for (auto n : nthr) { diff --git a/folly/experimental/flat_combining/test/FlatCombiningTestHelpers.h b/folly/experimental/flat_combining/test/FlatCombiningTestHelpers.h index 07742e12..623563ef 100644 --- a/folly/experimental/flat_combining/test/FlatCombiningTestHelpers.h +++ b/folly/experimental/flat_combining/test/FlatCombiningTestHelpers.h @@ -107,7 +107,7 @@ uint64_t fc_test( // test of exclusive access through a lock holder { std::unique_lock l; - ex.acquireExclusive(l); + ex.holdLock(l); CHECK(!mutex); mutex = true; VLOG(2) << tid << " " << ex.getVal() << " ..........."; -- 2.34.1