Uses different pass count for different parallel queue test cases
[libcds.git] / cds / sync / pool_monitor.h
index 404c35d270d16a6b9a911c3d1e24bd777f820f0a..43742382adcbb53e4be442d5fe78408923be3b94 100644 (file)
@@ -1,4 +1,32 @@
-//$$CDS-header$$
+/*
+    This file is a part of libcds - Concurrent Data Structures library
+
+    (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+    Source code repo: http://github.com/khizmax/libcds/
+    Download: http://sourceforge.net/projects/libcds/files/
+
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this
+      list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+
+    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
 
 #ifndef CDSLIB_SYNC_POOL_MONITOR_H
 #define CDSLIB_SYNC_POOL_MONITOR_H
@@ -33,18 +61,32 @@ namespace cds { namespace sync {
             typedef Counter event_counter; ///< measure type
 
             event_counter m_nLockCount;         ///< Number of monitor \p lock() call
-            event_counter m_nUnlockCount;       ///< Number of monitor \p unlock call
+            event_counter m_nUnlockCount;       ///< Number of monitor \p unlock() call
+            event_counter m_nMaxLocked;         ///< Max number of simuntaneously locked mutexes
             event_counter m_nLockContention;    ///< Number of \p lock() contenton
             event_counter m_nUnlockContention;  ///< Number of \p unlock() contention
             event_counter m_nLockAllocation;    ///< Number of the lock allocation from the pool
             event_counter m_nLockDeallocation;  ///< Number of the lock deallocation
+            event_counter m_nMaxAllocated;      ///< Max number of sumultaneously allocated mutexes
 
             //@cond
-            void onLock()               { ++m_nLockCount;       }
+            void onLock()
+            {
+                ++m_nLockCount;
+                int nDiff = static_cast<int>( m_nLockCount.get() - m_nUnlockCount.get());
+                if ( nDiff > 0 && m_nMaxLocked.get() < static_cast<typename event_counter::value_type>( nDiff ))
+                    m_nMaxLocked = static_cast<typename event_counter::value_type>( nDiff );
+            }
             void onUnlock()             { ++m_nUnlockCount;     }
             void onLockContention()     { ++m_nLockContention;  }
             void onUnlockContention()   { ++m_nUnlockContention;}
-            void onLockAllocation()     { ++m_nLockAllocation;  }
+            void onLockAllocation()
+            {
+                ++m_nLockAllocation;
+                int nDiff = static_cast<int>( m_nLockAllocation.get() - m_nLockDeallocation.get());
+                if ( nDiff > 0 && m_nMaxAllocated.get() < static_cast<typename event_counter::value_type>( nDiff ))
+                    m_nMaxAllocated = static_cast<typename event_counter::value_type>( nDiff );
+            }
             void onLockDeallocation()   { ++m_nLockDeallocation;}
             //@endcond
         };
@@ -56,7 +98,7 @@ namespace cds { namespace sync {
         The monitor is intended for reducing the number of system mutexes for
         huge containers like a tree. The monitor allocates the mutex from the pool \p LockPool
         only when container's node should be locked. Lifetime of node's mutex is managed by
-        reference counter. When the reference counter to node's mutex becomes zero, 
+        reference counter. When the reference counter to node's mutex becomes zero,
         the mutex is given back to the pool.
 
         The monitor is blocked: the access to node's mutex is performed under the spin-lock.
@@ -65,7 +107,7 @@ namespace cds { namespace sync {
         Template arguments:
         - \p LockPool - the @ref cds_memory_pool "pool type". The pool must maintain
             the objects of type \p std::mutex or similar. The access to the pool is not synchronized.
-        - \p BackOff - back-off strategy for spinning, default is \p cds::backoff::LockDefault
+        - \p BackOff - back-off strategy for spinning, default is \p cds::backoff::Default
         - \p Stat - enable (\p true) or disable (\p false, the default) monitor's internal statistics.
 
         <b>How to use</b>
@@ -74,26 +116,29 @@ namespace cds { namespace sync {
         typedef cds::sync::pool_monitor< pool_type > sync_monitor;
         \endcode
     */
-    template <class LockPool, typename BackOff = cds::backoff::LockDefault, bool Stat = false >
+    template <class LockPool, typename BackOff = cds::backoff::Default, bool Stat = false >
     class pool_monitor
     {
     public:
         typedef LockPool pool_type; ///< Pool type
         typedef typename pool_type::value_type lock_type; ///< node lock type
-        typedef typename std::conditional< 
-            std::is_same< BackOff, cds::opt::none >::value, 
-            cds::backoff::LockDefault,
+        typedef typename std::conditional<
+            std::is_same< BackOff, cds::opt::none >::value,
+            cds::backoff::yield,
             BackOff
         >::type  back_off;  ///< back-off strategy for spinning
         typedef uint32_t refspin_type;  ///< Reference counter + spin-lock bit
 
         /// Internal statistics
-        typedef typename std::conditional< 
-            Stat, 
-            typename pool_monitor_traits::stat<>, 
-            typename pool_monitor_traits::empty_stat 
+        typedef typename std::conditional<
+            Stat,
+            typename pool_monitor_traits::stat<>,
+            typename pool_monitor_traits::empty_stat
         >::type internal_stat;
 
+        /// Pool's default capacity
+        static CDS_CONSTEXPR size_t const c_nDefaultCapacity = 256;
+
     private:
         //@cond
         static CDS_CONSTEXPR refspin_type const c_nSpinBit = 1;
@@ -110,26 +155,34 @@ namespace cds { namespace sync {
             mutable atomics::atomic<refspin_type>   m_RefSpin;  ///< Spin-lock for \p m_pLock (bit 0) + reference counter
             mutable lock_type *                     m_pLock;    ///< Node-level lock
 
+            //@cond
             node_injection()
-                : m_RefSpin( 0 )
-                , m_pLock( nullptr )
-            {}
+                : m_pLock( nullptr )
+            {
+                m_RefSpin.store( 0, atomics::memory_order_release );
+            }
 
             ~node_injection()
             {
                 assert( m_pLock == nullptr );
                 assert( m_RefSpin.load( atomics::memory_order_relaxed ) == 0 );
             }
+
+            bool check_free() const
+            {
+                return m_pLock == nullptr && m_RefSpin.load( atomics::memory_order_relaxed ) == 0;
+            }
+            //@endcond
         };
 
         /// Initializes the pool of 256 preallocated mutexes
         pool_monitor()
-            : m_Pool( 256 )
+            : m_Pool( c_nDefaultCapacity )
         {}
 
         /// Initializes the pool of \p nPoolCapacity preallocated mutexes
         pool_monitor( size_t nPoolCapacity )
-            : m_Pool( nPoolCapacity)
+            : m_Pool( nPoolCapacity ? nPoolCapacity : c_nDefaultCapacity )
         {}
 
         /// Makes exclusive access to node \p p
@@ -143,7 +196,7 @@ namespace cds { namespace sync {
             // try lock spin and increment reference counter
             refspin_type cur = p.m_SyncMonitorInjection.m_RefSpin.load( atomics::memory_order_relaxed ) & ~c_nSpinBit;
             if ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur + c_nRefIncrement + c_nSpinBit,
-                atomics::memory_order_acquire, atomics::memory_order_relaxed ) ) 
+                atomics::memory_order_acq_rel, atomics::memory_order_acquire ))
             {
                 back_off bkoff;
                 do {
@@ -151,14 +204,16 @@ namespace cds { namespace sync {
                     bkoff();
                     cur &= ~c_nSpinBit;
                 } while ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur + c_nRefIncrement + c_nSpinBit,
-                    atomics::memory_order_acquire, atomics::memory_order_relaxed ));
+                    atomics::memory_order_acq_rel, atomics::memory_order_acquire ));
             }
 
             // spin locked
             // If the node has no lock, allocate it from pool
             pLock = p.m_SyncMonitorInjection.m_pLock;
             if ( !pLock ) {
+                assert( cur == 0 );
                 pLock = p.m_SyncMonitorInjection.m_pLock = m_Pool.allocate( 1 );
+                assert( pLock != nullptr );
                 m_Stat.onLockAllocation();
             }
 
@@ -182,16 +237,16 @@ namespace cds { namespace sync {
 
             // try lock spin
             refspin_type cur = p.m_SyncMonitorInjection.m_RefSpin.load( atomics::memory_order_relaxed ) & ~c_nSpinBit;
-            if ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur + c_nSpinBit,
-                atomics::memory_order_acquire, atomics::memory_order_relaxed ) ) 
+            if ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur | c_nSpinBit,
+                atomics::memory_order_acquire, atomics::memory_order_acquire ))
             {
                 back_off bkoff;
                 do {
                     m_Stat.onUnlockContention();
                     bkoff();
                     cur &= ~c_nSpinBit;
-                } while ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur + c_nSpinBit,
-                    atomics::memory_order_acquire, atomics::memory_order_relaxed ));
+                } while ( !p.m_SyncMonitorInjection.m_RefSpin.compare_exchange_weak( cur, cur | c_nSpinBit,
+                    atomics::memory_order_acquire, atomics::memory_order_acquire ));
             }
 
             // spin locked now
@@ -219,9 +274,9 @@ namespace cds { namespace sync {
         /// Returns the reference to internal statistics
         /**
             If class' template argument \p Stat is \p false,
-            the function returns \ref empty_stat "dummy statistics".
-            Otherwise, it returns the reference to monitor's internal statistics 
-            of type \ref stat.
+            the function returns \ref pool_monitor_traits::empty_stat "dummy statistics".
+            Otherwise, it returns the reference to monitor's internal statistics
+            of type \ref pool_monitor_traits::stat.
         */
         internal_stat const& statistics() const
         {