-//$$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
//@cond
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 ))
void onLockContention() { ++m_nLockContention; }
void onUnlockContention() { ++m_nUnlockContention;}
void onLockAllocation()
- {
- ++m_nLockAllocation;
+ {
+ ++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 ) )
+ 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;}
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.
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::yield
+ - \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>
typedef cds::sync::pool_monitor< pool_type > sync_monitor;
\endcode
*/
- template <class LockPool, typename BackOff = cds::backoff::yield, 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,
+ 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
//@cond
node_injection()
- : m_RefSpin( 0 )
- , m_pLock( nullptr )
- {}
+ : m_pLock( nullptr )
+ {
+ m_RefSpin.store( 0, atomics::memory_order_release );
+ }
~node_injection()
{
bool check_free() const
{
- return m_pLock == nullptr && m_RefSpin.load( atomics::memory_order_acquire ) == 0;
+ return m_pLock == nullptr && m_RefSpin.load( atomics::memory_order_relaxed ) == 0;
}
//@endcond
};
// 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 {
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 ( !pLock ) {
assert( cur == 0 );
pLock = p.m_SyncMonitorInjection.m_pLock = m_Pool.allocate( 1 );
+ assert( pLock != nullptr );
m_Stat.onLockAllocation();
}
// 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 ) )
+ atomics::memory_order_acquire, atomics::memory_order_acquire ))
{
back_off bkoff;
do {
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 ));
+ atomics::memory_order_acquire, atomics::memory_order_acquire ));
}
// spin locked now
/**
If class' template argument \p Stat is \p false,
the function returns \ref pool_monitor_traits::empty_stat "dummy statistics".
- Otherwise, it returns the reference to monitor's internal statistics
+ Otherwise, it returns the reference to monitor's internal statistics
of type \ref pool_monitor_traits::stat.
*/
internal_stat const& statistics() const