/// 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),
m_.lock();
}
- // Give the caller exclusive access through a lock holder.
- // No need for explicit release.
- template <typename LockHolder>
- void acquireExclusive(LockHolder& l) {
- l = LockHolder(m_);
- }
-
// Try to give the caller exclusive access. Returns true iff successful.
bool tryExclusive() {
return m_.try_lock();
m_.unlock();
}
+ // Give the lock holder ownership of the mutex and exclusive access.
+ // No need for explicit release.
+ template <typename LockHolder>
+ 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 <typename LockHolder>
+ void holdLock(LockHolder& l, std::defer_lock_t) {
+ l = LockHolder(m_, std::defer_lock);
+ }
+
// Execute an operation without combining
template <typename OpFunc>
void requestNoFC(OpFunc& opFn) {
if (!dedicated_) {
while (isPending()) {
clearPending();
+ ++sessions_;
combined_ += combiningSession();
}
}
#include <folly/portability/GTest.h>
#include <glog/logging.h>
+#include <mutex>
+
using namespace folly::test;
constexpr int LINES = 5;
class FlatCombiningTest : public ::testing::TestWithParam<Params> {};
+TEST(FlatCombiningTest, lock_holder) {
+ folly::FcSimpleExample<> ex(10);
+ {
+ std::unique_lock<std::mutex> l;
+ ex.holdLock(l);
+ CHECK(l.owns_lock());
+ }
+ {
+ std::unique_lock<std::mutex> 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) {