don't try to run the poly tests on gcc-4.9. they will fail.
[folly.git] / folly / experimental / flat_combining / FlatCombining.h
index 5f678596b6de41e1518877a21f6a0ec102c71e82..63fe1ce7bc2fe82af8fb5284cefd1860161a88a9 100644 (file)
@@ -20,7 +20,7 @@
 #include <folly/Function.h>
 #include <folly/IndexedMemPool.h>
 #include <folly/Portability.h>
-#include <folly/detail/CacheLocality.h>
+#include <folly/concurrency/CacheLocality.h>
 
 #include <atomic>
 #include <cassert>
@@ -35,7 +35,7 @@ namespace folly {
 ///
 /// FC is an alternative to coarse-grained locking for making
 /// sequential data structures thread-safe while minimizing the
-/// synchroniation overheads and cache coherence traffic associated
+/// synchronization overheads and cache coherence traffic associated
 /// with locking.
 ///
 /// Under FC, when a thread finds the lock contended, it can
@@ -50,7 +50,7 @@ namespace folly {
 ///   and acquiring the lock are eliminated from the critical path of
 ///   operating on the data structure.
 /// - Opportunities for smart combining, where executing multiple
-///   operations together may take less time than executng the
+///   operations together may take less time than executing the
 ///   operations separately, e.g., K delete_min operations on a
 ///   priority queue may be combined to take O(K + log N) time instead
 ///   of O(K * log N).
@@ -59,13 +59,13 @@ namespace folly {
 
 /// - A simple interface that requires minimal extra code by the
 ///   user. To use this interface efficiently the user-provided
-///   functions must be copyable to folly::Functio without dynamic
+///   functions must be copyable to folly::Function without dynamic
 ///   allocation. If this is impossible or inconvenient, the user is
 ///   encouraged to use the custom interface described below.
-/// - A custom interface that supports custom combinining and custom
+/// - A custom interface that supports custom combining and custom
 ///   request structure, either for the sake of smart combining or for
 ///   efficiently supporting operations that are not be copyable to
-///   folly::Function without synamic allocation.
+///   folly::Function without dynamic allocation.
 /// - Both synchronous and asynchronous operations.
 /// - Request records with and without thread-caching.
 /// - Combining with and without a dedicated combiner thread.
@@ -225,7 +225,8 @@ class FlatCombining {
     }
   };
 
-  using Pool = folly::IndexedMemPool<Rec, 32, 4, Atom, false, false>;
+  using Pool = folly::
+      IndexedMemPool<Rec, 32, 4, Atom, IndexedMemPoolTraitsLazyRecycle<Rec>>;
 
  public:
   /// The constructor takes three optional arguments:
@@ -236,7 +237,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 +278,6 @@ class FlatCombining {
     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();
@@ -294,6 +288,21 @@ class FlatCombining {
     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) {
@@ -556,6 +565,7 @@ class FlatCombining {
     if (!dedicated_) {
       while (isPending()) {
         clearPending();
+        ++sessions_;
         combined_ += combiningSession();
       }
     }
@@ -669,4 +679,4 @@ class FlatCombining {
   }
 };
 
-} // namespace folly {
+} // namespace folly